2.9.16
Coherent GT
A modern user interface library for games
Floating-point exceptions

When setting explicitly the floating point (FP) exceptions mask in your code using a method such as _controlfp, you might run into FP exceptions within the Coherent GT SDK. In order to avoid them, you'll need to disable FP exceptions while executing some Coherent GT functions.

Typical places that you should guard are:

Following is a sample that demonstrates how to save and restore the state of the floating point control word. Note that resetting has some cost associated with it and you should use it sparingly.

Since the user has prior knowledge about their system, she can optimize the FP control word resets. That's the reason the exception handling is left up to the user, instead of integrating it in the SDK.

For example, if you're not explicitly setting the exception mask, you don't need to do anything at all. If you are, it's likely that you already know what's your desired FP control word and don't need to execute a function to save it before changing it. Another example would be iterating through all Coherent::UIGT::View instances and calling Coherent::UIGT::View::Layout - in that case you can add a single FP guard for all Layout calls, instead of one per View.

Sample code for MSVC

#include <float.h>
class FPExceptionsGuard
{
public:
FPExceptionsGuard()
{
_clearfp();
unsigned int cw = _controlfp(0, 0) & _MCW_EM;
m_ControlWord = cw;
cw |= (_EM_INVALID |
_EM_DENORMAL |
_EM_ZERODIVIDE |
_EM_OVERFLOW |
_EM_UNDERFLOW |
_EM_INEXACT);
// _EM_DENORMAL is _control87 only;
// if you need to mask that type of exception, use _control87 instead
_controlfp(cw, _MCW_EM);
}
~FPExceptionsGuard()
{
_clearfp();
_controlfp(m_ControlWord, _MCW_EM);
}
private:
unsigned int m_ControlWord;
};

Sample code for GCC/Clang

#include <fenv.h>
class FPExceptionsGuard
{
static const int s_FPGuardExMask = FE_ALL_EXCEPT;
public:
FPExceptionsGuard()
{
feclearexcept(s_FPGuardExMask);
int cw = fegetexcept();
m_ControlWord = cw;
cw |= s_FPGuardExMask;
fedisableexcept(cw);
}
~FPExceptionsGuard()
{
feclearexcept(s_FPGuardExMask);
feenableexcept(m_ControlWord);
}
private:
int m_ControlWord;
};

Example usage of the FPExceptionsGuard

...
{
FPExceptionsGuard fpguard;
m_UISystem->Advance();
}
...