| 1 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 |
/** |
| 3 |
* Contains FPU related code. |
| 4 |
* \file IceFPU.h |
| 5 |
* \author Pierre Terdiman |
| 6 |
* \date April, 4, 2000 |
| 7 |
*/ |
| 8 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 9 |
|
| 10 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 11 |
// Include Guard |
| 12 |
#ifndef __ICEFPU_H__ |
| 13 |
#define __ICEFPU_H__ |
| 14 |
|
| 15 |
#define SIGN_BITMASK 0x80000000 |
| 16 |
|
| 17 |
namespace { |
| 18 |
union float_udword { float f; udword u; }; |
| 19 |
union float_sdword { float f; sdword s; }; |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
//! Integer representation of a floating-point value. |
| 24 |
//#define IR(x) ((udword&)(x)) |
| 25 |
static inline udword IR(float x) { float_udword fu; fu.f = x; return fu.u; } |
| 26 |
|
| 27 |
//! Signed integer representation of a floating-point value. |
| 28 |
//#define SIR(x) ((sdword&)(x)) |
| 29 |
static inline sdword SIR(float x) { float_sdword fs; fs.f = x; return fs.s; } |
| 30 |
|
| 31 |
//! Absolute integer representation of a floating-point value |
| 32 |
#define AIR(x) (IR(x)&0x7fffffff) |
| 33 |
|
| 34 |
//! Floating-point representation of an integer value. |
| 35 |
//#define FR(x) ((float&)(x)) |
| 36 |
static inline float FR(unsigned x) { float_udword fu; fu.u = x; return fu.f; } |
| 37 |
|
| 38 |
//! Integer-based comparison of a floating point value. |
| 39 |
//! Don't use it blindly, it can be faster or slower than the FPU comparison, depends on the context. |
| 40 |
#define IS_NEGATIVE_FLOAT(x) (IR(x)&0x80000000) |
| 41 |
|
| 42 |
//! Fast fabs for floating-point values. It just clears the sign bit. |
| 43 |
//! Don't use it blindy, it can be faster or slower than the FPU comparison, depends on the context. |
| 44 |
inline_ float FastFabs(float x) |
| 45 |
{ |
| 46 |
udword FloatBits = IR(x)&0x7fffffff; |
| 47 |
return FR(FloatBits); |
| 48 |
} |
| 49 |
|
| 50 |
//! Fast square root for floating-point values. |
| 51 |
inline_ float FastSqrt(float square) |
| 52 |
{ |
| 53 |
return sqrtf(square); |
| 54 |
} |
| 55 |
|
| 56 |
//! Saturates positive to zero. |
| 57 |
inline_ float fsat(float f) |
| 58 |
{ |
| 59 |
udword y = IR(f) & ~(SIR(f) >>31); |
| 60 |
return FR(y); |
| 61 |
} |
| 62 |
|
| 63 |
//! Computes 1.0f / sqrtf(x). |
| 64 |
inline_ float frsqrt(float f) |
| 65 |
{ |
| 66 |
float x = f * 0.5f; |
| 67 |
udword y = 0x5f3759df - (IR(f) >> 1); |
| 68 |
// Iteration... |
| 69 |
const float fy = FR(y); |
| 70 |
const float result = fy * ( 1.5f - ( x * fy * fy ) ); |
| 71 |
// Result |
| 72 |
return result; |
| 73 |
} |
| 74 |
|
| 75 |
//! Computes 1.0f / sqrtf(x). Comes from NVIDIA. |
| 76 |
inline_ float InvSqrt(const float& x) |
| 77 |
{ |
| 78 |
const udword tmp = (udword(IEEE_1_0 << 1) + IEEE_1_0 - IR(x)) >> 1; |
| 79 |
const float y = FR(tmp); |
| 80 |
return y * (1.47f - 0.47f * x * y * y); |
| 81 |
} |
| 82 |
|
| 83 |
//! Computes 1.0f / sqrtf(x). Comes from Quake3. Looks like the first one I had above. |
| 84 |
//! See http://www.magic-software.com/3DGEDInvSqrt.html |
| 85 |
inline_ float RSqrt(float number) |
| 86 |
{ |
| 87 |
int i; |
| 88 |
float x2, y; |
| 89 |
const float threehalfs = 1.5f; |
| 90 |
|
| 91 |
x2 = number * 0.5f; |
| 92 |
y = number; |
| 93 |
i = IR(y); |
| 94 |
i = 0x5f3759df - (i >> 1); |
| 95 |
y = FR(i); |
| 96 |
y = y * (threehalfs - (x2 * y * y)); |
| 97 |
|
| 98 |
return y; |
| 99 |
} |
| 100 |
|
| 101 |
//! TO BE DOCUMENTED |
| 102 |
inline_ float fsqrt(float f) |
| 103 |
{ |
| 104 |
udword y = ( ( SIR(f) - 0x3f800000 ) >> 1 ) + 0x3f800000; |
| 105 |
// Iteration...? |
| 106 |
// (float&)y = (3.0f - ((float&)y * (float&)y) / f) * (float&)y * 0.5f; |
| 107 |
// Result |
| 108 |
return FR(y); |
| 109 |
} |
| 110 |
|
| 111 |
//! Returns the float ranged espilon value. |
| 112 |
inline_ float fepsilon(float f) |
| 113 |
{ |
| 114 |
udword b = IR(f) & 0xff800000; |
| 115 |
udword a = b | 0x00000001; |
| 116 |
// Result |
| 117 |
return FR(a) - FR(b); |
| 118 |
} |
| 119 |
|
| 120 |
//! Is the float valid ? |
| 121 |
inline_ bool IsNAN(float value) { return (IR(value)&0x7f800000) == 0x7f800000; } |
| 122 |
inline_ bool IsIndeterminate(float value) { return IR(value) == 0xffc00000; } |
| 123 |
inline_ bool IsPlusInf(float value) { return IR(value) == 0x7f800000; } |
| 124 |
inline_ bool IsMinusInf(float value) { return IR(value) == 0xff800000; } |
| 125 |
|
| 126 |
inline_ bool IsValidFloat(float value) |
| 127 |
{ |
| 128 |
if(IsNAN(value)) return false; |
| 129 |
if(IsIndeterminate(value)) return false; |
| 130 |
if(IsPlusInf(value)) return false; |
| 131 |
if(IsMinusInf(value)) return false; |
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
#define CHECK_VALID_FLOAT(x) ASSERT(IsValidFloat(x)); |
| 136 |
|
| 137 |
/* |
| 138 |
//! FPU precision setting function. |
| 139 |
inline_ void SetFPU() |
| 140 |
{ |
| 141 |
// This function evaluates whether the floating-point |
| 142 |
// control word is set to single precision/round to nearest/ |
| 143 |
// exceptions disabled. If these conditions don't hold, the |
| 144 |
// function changes the control word to set them and returns |
| 145 |
// TRUE, putting the old control word value in the passback |
| 146 |
// location pointed to by pwOldCW. |
| 147 |
{ |
| 148 |
uword wTemp, wSave; |
| 149 |
|
| 150 |
__asm fstcw wSave |
| 151 |
if (wSave & 0x300 || // Not single mode |
| 152 |
0x3f != (wSave & 0x3f) || // Exceptions enabled |
| 153 |
wSave & 0xC00) // Not round to nearest mode |
| 154 |
{ |
| 155 |
__asm |
| 156 |
{ |
| 157 |
mov ax, wSave |
| 158 |
and ax, not 300h ;; single mode |
| 159 |
or ax, 3fh ;; disable all exceptions |
| 160 |
and ax, not 0xC00 ;; round to nearest mode |
| 161 |
mov wTemp, ax |
| 162 |
fldcw wTemp |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
*/ |
| 168 |
//! This function computes the slowest possible floating-point value (you can also directly use FLT_EPSILON) |
| 169 |
inline_ float ComputeFloatEpsilon() |
| 170 |
{ |
| 171 |
const float f = FR( IR(1.0f) ^ 1 ); |
| 172 |
return f - 1.0f; // You can check it's the same as FLT_EPSILON |
| 173 |
} |
| 174 |
|
| 175 |
inline_ bool IsFloatZero(float x, float epsilon=1e-6f) |
| 176 |
{ |
| 177 |
return x*x < epsilon; |
| 178 |
} |
| 179 |
|
| 180 |
#define FCOMI_ST0 _asm _emit 0xdb _asm _emit 0xf0 |
| 181 |
#define FCOMIP_ST0 _asm _emit 0xdf _asm _emit 0xf0 |
| 182 |
#define FCMOVB_ST0 _asm _emit 0xda _asm _emit 0xc0 |
| 183 |
#define FCMOVNB_ST0 _asm _emit 0xdb _asm _emit 0xc0 |
| 184 |
|
| 185 |
#define FCOMI_ST1 _asm _emit 0xdb _asm _emit 0xf1 |
| 186 |
#define FCOMIP_ST1 _asm _emit 0xdf _asm _emit 0xf1 |
| 187 |
#define FCMOVB_ST1 _asm _emit 0xda _asm _emit 0xc1 |
| 188 |
#define FCMOVNB_ST1 _asm _emit 0xdb _asm _emit 0xc1 |
| 189 |
|
| 190 |
#define FCOMI_ST2 _asm _emit 0xdb _asm _emit 0xf2 |
| 191 |
#define FCOMIP_ST2 _asm _emit 0xdf _asm _emit 0xf2 |
| 192 |
#define FCMOVB_ST2 _asm _emit 0xda _asm _emit 0xc2 |
| 193 |
#define FCMOVNB_ST2 _asm _emit 0xdb _asm _emit 0xc2 |
| 194 |
|
| 195 |
#define FCOMI_ST3 _asm _emit 0xdb _asm _emit 0xf3 |
| 196 |
#define FCOMIP_ST3 _asm _emit 0xdf _asm _emit 0xf3 |
| 197 |
#define FCMOVB_ST3 _asm _emit 0xda _asm _emit 0xc3 |
| 198 |
#define FCMOVNB_ST3 _asm _emit 0xdb _asm _emit 0xc3 |
| 199 |
|
| 200 |
#define FCOMI_ST4 _asm _emit 0xdb _asm _emit 0xf4 |
| 201 |
#define FCOMIP_ST4 _asm _emit 0xdf _asm _emit 0xf4 |
| 202 |
#define FCMOVB_ST4 _asm _emit 0xda _asm _emit 0xc4 |
| 203 |
#define FCMOVNB_ST4 _asm _emit 0xdb _asm _emit 0xc4 |
| 204 |
|
| 205 |
#define FCOMI_ST5 _asm _emit 0xdb _asm _emit 0xf5 |
| 206 |
#define FCOMIP_ST5 _asm _emit 0xdf _asm _emit 0xf5 |
| 207 |
#define FCMOVB_ST5 _asm _emit 0xda _asm _emit 0xc5 |
| 208 |
#define FCMOVNB_ST5 _asm _emit 0xdb _asm _emit 0xc5 |
| 209 |
|
| 210 |
#define FCOMI_ST6 _asm _emit 0xdb _asm _emit 0xf6 |
| 211 |
#define FCOMIP_ST6 _asm _emit 0xdf _asm _emit 0xf6 |
| 212 |
#define FCMOVB_ST6 _asm _emit 0xda _asm _emit 0xc6 |
| 213 |
#define FCMOVNB_ST6 _asm _emit 0xdb _asm _emit 0xc6 |
| 214 |
|
| 215 |
#define FCOMI_ST7 _asm _emit 0xdb _asm _emit 0xf7 |
| 216 |
#define FCOMIP_ST7 _asm _emit 0xdf _asm _emit 0xf7 |
| 217 |
#define FCMOVB_ST7 _asm _emit 0xda _asm _emit 0xc7 |
| 218 |
#define FCMOVNB_ST7 _asm _emit 0xdb _asm _emit 0xc7 |
| 219 |
|
| 220 |
//! A global function to find MAX(a,b) using FCOMI/FCMOV |
| 221 |
inline_ float FCMax2(float a, float b) |
| 222 |
{ |
| 223 |
return (a > b) ? a : b; |
| 224 |
} |
| 225 |
|
| 226 |
//! A global function to find MIN(a,b) using FCOMI/FCMOV |
| 227 |
inline_ float FCMin2(float a, float b) |
| 228 |
{ |
| 229 |
return (a < b) ? a : b; |
| 230 |
} |
| 231 |
|
| 232 |
//! A global function to find MAX(a,b,c) using FCOMI/FCMOV |
| 233 |
inline_ float FCMax3(float a, float b, float c) |
| 234 |
{ |
| 235 |
return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c); |
| 236 |
} |
| 237 |
|
| 238 |
//! A global function to find MIN(a,b,c) using FCOMI/FCMOV |
| 239 |
inline_ float FCMin3(float a, float b, float c) |
| 240 |
{ |
| 241 |
return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c); |
| 242 |
} |
| 243 |
|
| 244 |
inline_ int ConvertToSortable(float f) |
| 245 |
{ |
| 246 |
int Fi = SIR(f); |
| 247 |
int Fmask = (Fi>>31); |
| 248 |
Fi ^= Fmask; |
| 249 |
Fmask &= ~(1<<31); |
| 250 |
Fi -= Fmask; |
| 251 |
return Fi; |
| 252 |
} |
| 253 |
|
| 254 |
enum FPUMode |
| 255 |
{ |
| 256 |
FPU_FLOOR = 0, |
| 257 |
FPU_CEIL = 1, |
| 258 |
FPU_BEST = 2, |
| 259 |
|
| 260 |
FPU_FORCE_DWORD = 0x7fffffff |
| 261 |
}; |
| 262 |
|
| 263 |
FUNCTION ICECORE_API FPUMode GetFPUMode(); |
| 264 |
FUNCTION ICECORE_API void SaveFPU(); |
| 265 |
FUNCTION ICECORE_API void RestoreFPU(); |
| 266 |
FUNCTION ICECORE_API void SetFPUFloorMode(); |
| 267 |
FUNCTION ICECORE_API void SetFPUCeilMode(); |
| 268 |
FUNCTION ICECORE_API void SetFPUBestMode(); |
| 269 |
|
| 270 |
FUNCTION ICECORE_API void SetFPUPrecision24(); |
| 271 |
FUNCTION ICECORE_API void SetFPUPrecision53(); |
| 272 |
FUNCTION ICECORE_API void SetFPUPrecision64(); |
| 273 |
FUNCTION ICECORE_API void SetFPURoundingChop(); |
| 274 |
FUNCTION ICECORE_API void SetFPURoundingUp(); |
| 275 |
FUNCTION ICECORE_API void SetFPURoundingDown(); |
| 276 |
FUNCTION ICECORE_API void SetFPURoundingNear(); |
| 277 |
|
| 278 |
FUNCTION ICECORE_API int intChop(const float& f); |
| 279 |
FUNCTION ICECORE_API int intFloor(const float& f); |
| 280 |
FUNCTION ICECORE_API int intCeil(const float& f); |
| 281 |
|
| 282 |
#endif // __ICEFPU_H__ |