IceFPU.h
Go to the documentation of this file.
1 
8 
11 // Include Guard
12 #ifndef __ICEFPU_H__
13 #define __ICEFPU_H__
14 
15  #define SIGN_BITMASK 0x80000000
16 
18  #define IR(x) ((udword&)(x))
19 
21  #define SIR(x) ((sdword&)(x))
22 
24  #define AIR(x) (IR(x)&0x7fffffff)
25 
27  #define FR(x) ((float&)(x))
28 
31  //#define IS_NEGATIVE_FLOAT(x) (IR(x)&0x80000000)
32  #define IS_NEGATIVE_FLOAT(x) ((x)<0)
33 
36  inline_ float FastFabs(float x)
37  {
38  udword FloatBits = IR(x)&0x7fffffff;
39  return FR(FloatBits);
40  }
41 
43  inline_ float FastSqrt(float square)
44  {
45 #if defined(_MSC_VER) && not defined(_WIN64)
46  float retval;
47 
48  __asm {
49  mov eax, square
50  sub eax, 0x3F800000
51  sar eax, 1
52  add eax, 0x3F800000
53  mov [retval], eax
54  }
55  return retval;
56 #else
57  return sqrt(square);
58 #endif
59  }
60 
62  inline_ float fsat(float f)
63  {
64  udword y = (udword&)f & ~((sdword&)f >>31);
65  return (float&)y;
66  }
67 
69  inline_ float frsqrt(float f)
70  {
71  float x = f * 0.5f;
72  udword y = 0x5f3759df - ((udword&)f >> 1);
73  // Iteration...
74  (float&)y = (float&)y * ( 1.5f - ( x * (float&)y * (float&)y ) );
75  // Result
76  return (float&)y;
77  }
78 
80  inline_ float InvSqrt(const float& x)
81  {
82  udword tmp = (udword(IEEE_1_0 << 1) + IEEE_1_0 - *(udword*)&x) >> 1;
83  float y = *(float*)&tmp;
84  return y * (1.47f - 0.47f * x * y * y);
85  }
86 
89  inline_ float RSqrt(float number)
90  {
91  long i;
92  float x2, y;
93  const float threehalfs = 1.5f;
94 
95  x2 = number * 0.5f;
96  y = number;
97  i = * (long *) &y;
98  i = 0x5f3759df - (i >> 1);
99  y = * (float *) &i;
100  y = y * (threehalfs - (x2 * y * y));
101 
102  return y;
103  }
104 
106  inline_ float fsqrt(float f)
107  {
108  udword y = ( ( (sdword&)f - 0x3f800000 ) >> 1 ) + 0x3f800000;
109  // Iteration...?
110  // (float&)y = (3.0f - ((float&)y * (float&)y) / f) * (float&)y * 0.5f;
111  // Result
112  return (float&)y;
113  }
114 
116  inline_ float fepsilon(float f)
117  {
118  udword b = (udword&)f & 0xff800000;
119  udword a = b | 0x00000001;
120  (float&)a -= (float&)b;
121  // Result
122  return (float&)a;
123  }
124 
126  inline_ bool IsNAN(float value) { return (IR(value)&0x7f800000) == 0x7f800000; }
127  inline_ bool IsIndeterminate(float value) { return IR(value) == 0xffc00000; }
128  inline_ bool IsPlusInf(float value) { return IR(value) == 0x7f800000; }
129  inline_ bool IsMinusInf(float value) { return IR(value) == 0xff800000; }
130 
132  {
133  if(IsNAN(value)) return false;
134  if(IsIndeterminate(value)) return false;
135  if(IsPlusInf(value)) return false;
136  if(IsMinusInf(value)) return false;
137  return true;
138  }
139 
140  #define CHECK_VALID_FLOAT(x) ASSERT(IsValidFloat(x));
141 
142 /*
144  inline_ void SetFPU()
145  {
146  // This function evaluates whether the floating-point
147  // control word is set to single precision/round to nearest/
148  // exceptions disabled. If these conditions don't hold, the
149  // function changes the control word to set them and returns
150  // TRUE, putting the old control word value in the passback
151  // location pointed to by pwOldCW.
152  {
153  uword wTemp, wSave;
154 
155  __asm fstcw wSave
156  if (wSave & 0x300 || // Not single mode
157  0x3f != (wSave & 0x3f) || // Exceptions enabled
158  wSave & 0xC00) // Not round to nearest mode
159  {
160  __asm
161  {
162  mov ax, wSave
163  and ax, not 300h ;; single mode
164  or ax, 3fh ;; disable all exceptions
165  and ax, not 0xC00 ;; round to nearest mode
166  mov wTemp, ax
167  fldcw wTemp
168  }
169  }
170  }
171  }
172 */
175  {
176  float f = 1.0f;
177  ((udword&)f)^=1;
178  return f - 1.0f; // You can check it's the same as FLT_EPSILON
179  }
180 
181  inline_ bool IsFloatZero(float x, float epsilon=1e-6f)
182  {
183  return x*x < epsilon;
184  }
185 
186  #define FCOMI_ST0 _asm _emit 0xdb _asm _emit 0xf0
187  #define FCOMIP_ST0 _asm _emit 0xdf _asm _emit 0xf0
188  #define FCMOVB_ST0 _asm _emit 0xda _asm _emit 0xc0
189  #define FCMOVNB_ST0 _asm _emit 0xdb _asm _emit 0xc0
190 
191  #define FCOMI_ST1 _asm _emit 0xdb _asm _emit 0xf1
192  #define FCOMIP_ST1 _asm _emit 0xdf _asm _emit 0xf1
193  #define FCMOVB_ST1 _asm _emit 0xda _asm _emit 0xc1
194  #define FCMOVNB_ST1 _asm _emit 0xdb _asm _emit 0xc1
195 
196  #define FCOMI_ST2 _asm _emit 0xdb _asm _emit 0xf2
197  #define FCOMIP_ST2 _asm _emit 0xdf _asm _emit 0xf2
198  #define FCMOVB_ST2 _asm _emit 0xda _asm _emit 0xc2
199  #define FCMOVNB_ST2 _asm _emit 0xdb _asm _emit 0xc2
200 
201  #define FCOMI_ST3 _asm _emit 0xdb _asm _emit 0xf3
202  #define FCOMIP_ST3 _asm _emit 0xdf _asm _emit 0xf3
203  #define FCMOVB_ST3 _asm _emit 0xda _asm _emit 0xc3
204  #define FCMOVNB_ST3 _asm _emit 0xdb _asm _emit 0xc3
205 
206  #define FCOMI_ST4 _asm _emit 0xdb _asm _emit 0xf4
207  #define FCOMIP_ST4 _asm _emit 0xdf _asm _emit 0xf4
208  #define FCMOVB_ST4 _asm _emit 0xda _asm _emit 0xc4
209  #define FCMOVNB_ST4 _asm _emit 0xdb _asm _emit 0xc4
210 
211  #define FCOMI_ST5 _asm _emit 0xdb _asm _emit 0xf5
212  #define FCOMIP_ST5 _asm _emit 0xdf _asm _emit 0xf5
213  #define FCMOVB_ST5 _asm _emit 0xda _asm _emit 0xc5
214  #define FCMOVNB_ST5 _asm _emit 0xdb _asm _emit 0xc5
215 
216  #define FCOMI_ST6 _asm _emit 0xdb _asm _emit 0xf6
217  #define FCOMIP_ST6 _asm _emit 0xdf _asm _emit 0xf6
218  #define FCMOVB_ST6 _asm _emit 0xda _asm _emit 0xc6
219  #define FCMOVNB_ST6 _asm _emit 0xdb _asm _emit 0xc6
220 
221  #define FCOMI_ST7 _asm _emit 0xdb _asm _emit 0xf7
222  #define FCOMIP_ST7 _asm _emit 0xdf _asm _emit 0xf7
223  #define FCMOVB_ST7 _asm _emit 0xda _asm _emit 0xc7
224  #define FCMOVNB_ST7 _asm _emit 0xdb _asm _emit 0xc7
225 
227  inline_ float FCMax2(float a, float b)
228  {
229 #if defined(_MSC_VER) && not defined(_WIN64)
230  float Res;
231  _asm fld [a]
232  _asm fld [b]
233  FCOMI_ST1
234  FCMOVB_ST1
235  _asm fstp [Res]
236  _asm fcomp
237  return Res;
238 #else
239  return (a > b) ? a : b;
240 #endif
241  }
242 
244  inline_ float FCMin2(float a, float b)
245  {
246 #if defined(_MSC_VER) && not defined(_WIN64)
247  float Res;
248  _asm fld [a]
249  _asm fld [b]
250  FCOMI_ST1
252  _asm fstp [Res]
253  _asm fcomp
254  return Res;
255 #else
256  return (a < b) ? a : b;
257 #endif
258  }
259 
261  inline_ float FCMax3(float a, float b, float c)
262  {
263 #if defined(_MSC_VER) && not defined(_WIN64)
264  float Res;
265  _asm fld [a]
266  _asm fld [b]
267  _asm fld [c]
268  FCOMI_ST1
269  FCMOVB_ST1
270  FCOMI_ST2
271  FCMOVB_ST2
272  _asm fstp [Res]
273  _asm fcompp
274  return Res;
275 #else
276  return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
277 #endif
278  }
279 
281  inline_ float FCMin3(float a, float b, float c)
282  {
283 #if defined(_MSC_VER) && not defined(_WIN64)
284  float Res;
285  _asm fld [a]
286  _asm fld [b]
287  _asm fld [c]
288  FCOMI_ST1
290  FCOMI_ST2
292  _asm fstp [Res]
293  _asm fcompp
294  return Res;
295 #else
296  return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);
297 #endif
298  }
299 
301  {
302  int& Fi = (int&)f;
303  int Fmask = (Fi>>31);
304  Fi ^= Fmask;
305  Fmask &= ~(1<<31);
306  Fi -= Fmask;
307  return Fi;
308  }
309 
310  enum FPUMode
311  {
313  FPU_CEIL = 1,
314  FPU_BEST = 2,
315 
316  FPU_FORCE_DWORD = 0x7fffffff
317  };
318 
325 
333 
334  FUNCTION ICECORE_API int intChop(const float& f);
335  FUNCTION ICECORE_API int intFloor(const float& f);
336  FUNCTION ICECORE_API int intCeil(const float& f);
337 
338 #endif // __ICEFPU_H__
FUNCTION ICECORE_API void SetFPURoundingDown()
#define IR(x)
Integer representation of a floating-point value.
Definition: IceFPU.h:18
FUNCTION ICECORE_API void SetFPURoundingNear()
int c
Definition: autoplay.py:16
FUNCTION ICECORE_API void RestoreFPU()
inline_ float FCMin2(float a, float b)
A global function to find MIN(a,b) using FCOMI/FCMOV.
Definition: IceFPU.h:244
inline_ float frsqrt(float f)
Computes 1.0f / sqrtf(x).
Definition: IceFPU.h:69
signed int sdword
sizeof(sdword) must be 4
Definition: IceTypes.h:64
FUNCTION ICECORE_API int intChop(const float &f)
* x
Definition: IceUtils.h:98
inline_ float FastSqrt(float square)
Fast square root for floating-point values.
Definition: IceFPU.h:43
FUNCTION ICECORE_API void SetFPUPrecision64()
#define FR(x)
Floating-point representation of an integer value.
Definition: IceFPU.h:27
png_voidp int value
Definition: png.h:2113
inline_ float fepsilon(float f)
Returns the float ranged espilon value.
Definition: IceFPU.h:116
#define FUNCTION
#define inline_
FUNCTION ICECORE_API void SetFPUFloorMode()
FUNCTION ICECORE_API void SetFPUCeilMode()
png_uint_32 i
Definition: png.h:2735
inline_ bool IsMinusInf(float value)
Definition: IceFPU.h:129
long b
Definition: jpegint.h:371
inline_ float RSqrt(float number)
Definition: IceFPU.h:89
#define ICECORE_API
FUNCTION ICECORE_API void SetFPUPrecision24()
unsigned int udword
sizeof(udword) must be 4
Definition: IceTypes.h:65
FUNCTION ICECORE_API int intCeil(const float &f)
FUNCTION ICECORE_API int intFloor(const float &f)
inline_ bool IsNAN(float value)
Is the float valid ?
Definition: IceFPU.h:126
#define FCMOVB_ST2
Definition: IceFPU.h:198
FUNCTION ICECORE_API FPUMode GetFPUMode()
FUNCTION ICECORE_API void SetFPURoundingChop()
string a
inline_ bool IsFloatZero(float x, float epsilon=1e-6f)
Definition: IceFPU.h:181
inline_ int ConvertToSortable(float f)
Definition: IceFPU.h:300
inline_ bool IsValidFloat(float value)
Definition: IceFPU.h:131
#define FCOMI_ST1
Definition: IceFPU.h:191
#define FCMOVNB_ST1
Definition: IceFPU.h:194
inline_ float fsqrt(float f)
TO BE DOCUMENTED.
Definition: IceFPU.h:106
inline_ float fsat(float f)
Saturates positive to zero.
Definition: IceFPU.h:62
FUNCTION ICECORE_API void SaveFPU()
inline_ float InvSqrt(const float &x)
Computes 1.0f / sqrtf(x). Comes from NVIDIA.
Definition: IceFPU.h:80
inline_ float FCMin3(float a, float b, float c)
A global function to find MIN(a,b,c) using FCOMI/FCMOV.
Definition: IceFPU.h:281
inline_ bool IsIndeterminate(float value)
Definition: IceFPU.h:127
inline_ float FCMax3(float a, float b, float c)
A global function to find MAX(a,b,c) using FCOMI/FCMOV.
Definition: IceFPU.h:261
#define FCMOVB_ST1
Definition: IceFPU.h:193
#define IEEE_1_0
integer representation of 1.0
Definition: IceTypes.h:132
FUNCTION ICECORE_API void SetFPUBestMode()
inline_ bool IsPlusInf(float value)
Definition: IceFPU.h:128
inline_ float ComputeFloatEpsilon()
This function computes the slowest possible floating-point value (you can also directly use FLT_EPSIL...
Definition: IceFPU.h:174
FPUMode
Definition: IceFPU.h:310
* y
Definition: IceUtils.h:97
inline_ float FastFabs(float x)
Definition: IceFPU.h:36
#define FCOMI_ST2
Definition: IceFPU.h:196
FUNCTION ICECORE_API void SetFPURoundingUp()
inline_ float FCMax2(float a, float b)
A global function to find MAX(a,b) using FCOMI/FCMOV.
Definition: IceFPU.h:227
FUNCTION ICECORE_API void SetFPUPrecision53()
#define FCMOVNB_ST2
Definition: IceFPU.h:199


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Sep 8 2022 02:24:03