| 1 |
// ==================================================================================================================== |
| 2 |
// ==================================================================================================================== |
| 3 |
// xs_Float.h |
| 4 |
// |
| 5 |
// Source: "Know Your FPU: Fixing Floating Fast" |
| 6 |
// http://www.stereopsis.com/sree/fpu2006.html |
| 7 |
// |
| 8 |
// xs_CRoundToInt: Round toward nearest, but ties round toward even (just like FISTP) |
| 9 |
// xs_ToInt: Round toward zero, just like the C (int) cast |
| 10 |
// xs_FloorToInt: Round down |
| 11 |
// xs_CeilToInt: Round up |
| 12 |
// xs_RoundToInt: Round toward nearest, but ties round up |
| 13 |
// ==================================================================================================================== |
| 14 |
// ==================================================================================================================== |
| 15 |
#ifndef _xs_FLOAT_H_ |
| 16 |
#define _xs_FLOAT_H_ |
| 17 |
|
| 18 |
#include "config.h" |
| 19 |
|
| 20 |
// ==================================================================================================================== |
| 21 |
// Defines |
| 22 |
// ==================================================================================================================== |
| 23 |
#ifndef _xs_DEFAULT_CONVERSION |
| 24 |
#define _xs_DEFAULT_CONVERSION 0 |
| 25 |
#endif //_xs_DEFAULT_CONVERSION |
| 26 |
|
| 27 |
|
| 28 |
#ifdef WORDS_BIGENDIAN |
| 29 |
#define _xs_iexp_ 0 |
| 30 |
#define _xs_iman_ 1 |
| 31 |
#else |
| 32 |
#define _xs_iexp_ 1 //intel is little endian |
| 33 |
#define _xs_iman_ 0 |
| 34 |
#endif //BigEndian_ |
| 35 |
|
| 36 |
#ifdef __GNUC__ |
| 37 |
#define finline inline __attribute__ ((__always_inline__)) |
| 38 |
#else |
| 39 |
#define finline __forceinline |
| 40 |
#endif |
| 41 |
|
| 42 |
typedef double real64; |
| 43 |
|
| 44 |
|
| 45 |
typedef union _xs_doubleints |
| 46 |
{ |
| 47 |
real64 val; |
| 48 |
unsigned ival[2]; |
| 49 |
} _xs_doubleints; |
| 50 |
|
| 51 |
|
| 52 |
// ==================================================================================================================== |
| 53 |
// ==================================================================================================================== |
| 54 |
// Inline implementation |
| 55 |
// ==================================================================================================================== |
| 56 |
// ==================================================================================================================== |
| 57 |
finline int xs_CRoundToInt(real64 val) |
| 58 |
{ |
| 59 |
#if _xs_DEFAULT_CONVERSION==0 |
| 60 |
_xs_doubleints uval; |
| 61 |
uval.val = val + 6755399441055744.0; |
| 62 |
return uval.ival[_xs_iman_]; |
| 63 |
#else |
| 64 |
return int(floor(val+.5)); |
| 65 |
#endif |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
// ==================================================================================================================== |
| 70 |
// ==================================================================================================================== |
| 71 |
// Unsigned variants |
| 72 |
// ==================================================================================================================== |
| 73 |
// ==================================================================================================================== |
| 74 |
finline unsigned xs_CRoundToUInt(real64 val) |
| 75 |
{ |
| 76 |
return (unsigned)xs_CRoundToInt(val); |
| 77 |
} |
| 78 |
|
| 79 |
// ==================================================================================================================== |
| 80 |
// ==================================================================================================================== |
| 81 |
#endif // _xs_FLOAT_H_ |