CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
IceFPU.h
Go to the documentation of this file.
1 //----------------------------------------------------------------------
8 //----------------------------------------------------------------------
9 
10 //----------------------------------------------------------------------
11 // Include Guard
12 #ifndef RecoTracker_MkFitCore_src_Ice_IceFPU_h
13 #define RecoTracker_MkFitCore_src_Ice_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 
35 inline_ float FastFabs(float x) {
36  udword FloatBits = IR(x) & 0x7fffffff;
37  return FR(FloatBits);
38 }
39 
40 #ifdef WIN32
41 inline_ float FastSqrt(float square) {
43  float retval;
44 
45  __asm {
46  mov eax, square
47  sub eax, 0x3F800000
48  sar eax, 1
49  add eax, 0x3F800000
50  mov [retval], eax
51  }
52  return retval;
53 }
54 #endif
55 
57 inline_ float fsat(float f) {
58  udword y = (udword&)f & ~((sdword&)f >> 31);
59  return (float&)y;
60 }
61 
63 inline_ float frsqrt(float f) {
64  float x = f * 0.5f;
65  udword y = 0x5f3759df - ((udword&)f >> 1);
66  // Iteration...
67  (float&)y = (float&)y * (1.5f - (x * (float&)y * (float&)y));
68  // Result
69  return (float&)y;
70 }
71 
73 inline_ float InvSqrt(const float& x) {
74  udword tmp = (udword(IEEE_1_0 << 1) + IEEE_1_0 - *(udword*)&x) >> 1;
75  float y = *(float*)&tmp;
76  return y * (1.47f - 0.47f * x * y * y);
77 }
78 
81 inline_ float RSqrt(float number) {
82  long i;
83  float x2, y;
84  const float threehalfs = 1.5f;
85 
86  x2 = number * 0.5f;
87  y = number;
88  i = *(long*)&y;
89  i = 0x5f3759df - (i >> 1);
90  y = *(float*)&i;
91  y = y * (threehalfs - (x2 * y * y));
92 
93  return y;
94 }
95 
97 inline_ float fsqrt(float f) {
98  udword y = (((sdword&)f - 0x3f800000) >> 1) + 0x3f800000;
99  // Iteration...?
100  // (float&)y = (3.0f - ((float&)y * (float&)y) / f) * (float&)y * 0.5f;
101  // Result
102  return (float&)y;
103 }
104 
106 inline_ float fepsilon(float f) {
107  udword b = (udword&)f & 0xff800000;
108  udword a = b | 0x00000001;
109  (float&)a -= (float&)b;
110  // Result
111  return (float&)a;
112 }
113 
115 inline_ bool IsNAN(float value) { return (IR(value) & 0x7f800000) == 0x7f800000; }
116 inline_ bool IsIndeterminate(float value) { return IR(value) == 0xffc00000; }
117 inline_ bool IsPlusInf(float value) { return IR(value) == 0x7f800000; }
118 inline_ bool IsMinusInf(float value) { return IR(value) == 0xff800000; }
119 
121  if (IsNAN(value))
122  return false;
123  if (IsIndeterminate(value))
124  return false;
125  if (IsPlusInf(value))
126  return false;
127  if (IsMinusInf(value))
128  return false;
129  return true;
130 }
131 
132 #define CHECK_VALID_FLOAT(x) ASSERT(IsValidFloat(x));
133 
134 /*
136 inline_ void SetFPU()
137 {
138 // This function evaluates whether the floating-point
139 // control word is set to single precision/round to nearest/
140 // exceptions disabled. If these conditions don't hold, the
141 // function changes the control word to set them and returns
142 // true, putting the old control word value in the passback
143 // location pointed to by pwOldCW.
144 {
145 uword wTemp, wSave;
146 
147 __asm fstcw wSave
148 if (wSave & 0x300 || // Not single mode
149 0x3f != (wSave & 0x3f) || // Exceptions enabled
150 wSave & 0xC00) // Not round to nearest mode
151 {
152 __asm
153 {
154 mov ax, wSave
155 and ax, not 300h ;; single mode
156 or ax, 3fh ;; disable all exceptions
157 and ax, not 0xC00 ;; round to nearest mode
158 mov wTemp, ax
159 fldcw wTemp
160 }
161 }
162 }
163 }
164 */
167  float f = 1.0f;
168  ((udword&)f) ^= 1;
169  return f - 1.0f; // You can check it's the same as FLT_EPSILON
170 }
171 
172 inline_ bool IsFloatZero(float x, float epsilon = 1e-6f) { return x * x < epsilon; }
173 
174 #ifdef WIN32
175 #define FCOMI_ST0 _asm _emit 0xdb _asm _emit 0xf0
176 #define FCOMIP_ST0 _asm _emit 0xdf _asm _emit 0xf0
177 #define FCMOVB_ST0 _asm _emit 0xda _asm _emit 0xc0
178 #define FCMOVNB_ST0 _asm _emit 0xdb _asm _emit 0xc0
179 
180 #define FCOMI_ST1 _asm _emit 0xdb _asm _emit 0xf1
181 #define FCOMIP_ST1 _asm _emit 0xdf _asm _emit 0xf1
182 #define FCMOVB_ST1 _asm _emit 0xda _asm _emit 0xc1
183 #define FCMOVNB_ST1 _asm _emit 0xdb _asm _emit 0xc1
184 
185 #define FCOMI_ST2 _asm _emit 0xdb _asm _emit 0xf2
186 #define FCOMIP_ST2 _asm _emit 0xdf _asm _emit 0xf2
187 #define FCMOVB_ST2 _asm _emit 0xda _asm _emit 0xc2
188 #define FCMOVNB_ST2 _asm _emit 0xdb _asm _emit 0xc2
189 
190 #define FCOMI_ST3 _asm _emit 0xdb _asm _emit 0xf3
191 #define FCOMIP_ST3 _asm _emit 0xdf _asm _emit 0xf3
192 #define FCMOVB_ST3 _asm _emit 0xda _asm _emit 0xc3
193 #define FCMOVNB_ST3 _asm _emit 0xdb _asm _emit 0xc3
194 
195 #define FCOMI_ST4 _asm _emit 0xdb _asm _emit 0xf4
196 #define FCOMIP_ST4 _asm _emit 0xdf _asm _emit 0xf4
197 #define FCMOVB_ST4 _asm _emit 0xda _asm _emit 0xc4
198 #define FCMOVNB_ST4 _asm _emit 0xdb _asm _emit 0xc4
199 
200 #define FCOMI_ST5 _asm _emit 0xdb _asm _emit 0xf5
201 #define FCOMIP_ST5 _asm _emit 0xdf _asm _emit 0xf5
202 #define FCMOVB_ST5 _asm _emit 0xda _asm _emit 0xc5
203 #define FCMOVNB_ST5 _asm _emit 0xdb _asm _emit 0xc5
204 
205 #define FCOMI_ST6 _asm _emit 0xdb _asm _emit 0xf6
206 #define FCOMIP_ST6 _asm _emit 0xdf _asm _emit 0xf6
207 #define FCMOVB_ST6 _asm _emit 0xda _asm _emit 0xc6
208 #define FCMOVNB_ST6 _asm _emit 0xdb _asm _emit 0xc6
209 
210 #define FCOMI_ST7 _asm _emit 0xdb _asm _emit 0xf7
211 #define FCOMIP_ST7 _asm _emit 0xdf _asm _emit 0xf7
212 #define FCMOVB_ST7 _asm _emit 0xda _asm _emit 0xc7
213 #define FCMOVNB_ST7 _asm _emit 0xdb _asm _emit 0xc7
214 
216 inline_ float FCMax2(float a, float b) {
217  float Res;
218  _asm fld[a] _asm fld[b] FCOMI_ST1 FCMOVB_ST1 _asm fstp[Res] _asm fcomp return Res;
219 }
220 
222 inline_ float FCMin2(float a, float b) {
223  float Res;
224  _asm fld[a] _asm fld[b] FCOMI_ST1 FCMOVNB_ST1 _asm fstp[Res] _asm fcomp return Res;
225 }
226 
228 inline_ float FCMax3(float a, float b, float c) {
229  float Res;
230  _asm fld[a] _asm fld[b] _asm fld[c] FCOMI_ST1 FCMOVB_ST1 FCOMI_ST2 FCMOVB_ST2 _asm fstp[Res] _asm fcompp return Res;
231 }
232 
234 inline_ float FCMin3(float a, float b, float c) {
235  float Res;
236  _asm fld[a] _asm fld[b] _asm fld[c] FCOMI_ST1 FCMOVNB_ST1 FCOMI_ST2 FCMOVNB_ST2 _asm fstp[Res] _asm fcompp return Res;
237 }
238 #endif
239 
241  int& Fi = (int&)f;
242  int Fmask = (Fi >> 31);
243  Fi ^= Fmask;
244  Fmask &= ~(1 << 31);
245  Fi -= Fmask;
246  return Fi;
247 }
248 
249 enum FPUMode {
251  FPU_CEIL = 1,
252  FPU_BEST = 2,
253 
254  FPU_FORCE_DWORD = 0x7fffffff
255 };
256 
257 #ifdef WIN32
258 FUNCTION ICECORE_API FPUMode GetFPUMode();
259 FUNCTION ICECORE_API void SaveFPU();
260 FUNCTION ICECORE_API void RestoreFPU();
261 FUNCTION ICECORE_API void SetFPUFloorMode();
262 FUNCTION ICECORE_API void SetFPUCeilMode();
263 FUNCTION ICECORE_API void SetFPUBestMode();
264 
265 FUNCTION ICECORE_API void SetFPUPrecision24();
266 FUNCTION ICECORE_API void SetFPUPrecision53();
267 FUNCTION ICECORE_API void SetFPUPrecision64();
268 FUNCTION ICECORE_API void SetFPURoundingChop();
269 FUNCTION ICECORE_API void SetFPURoundingUp();
270 FUNCTION ICECORE_API void SetFPURoundingDown();
271 FUNCTION ICECORE_API void SetFPURoundingNear();
272 
273 FUNCTION ICECORE_API int intChop(const float& f);
274 FUNCTION ICECORE_API int intFloor(const float& f);
275 FUNCTION ICECORE_API int intCeil(const float& f);
276 #endif
277 
278 #endif // __ICEFPU_H__
#define IR(x)
Integer representation of a floating-point value.
Definition: IceFPU.h:18
const edm::EventSetup & c
inline_ float frsqrt(float f)
Computes 1.0f / sqrtf(x).
Definition: IceFPU.h:63
#define inline_
Definition: IceTypes.h:18
signed int sdword
sizeof(sdword) must be 4
Definition: IceTypes.h:51
#define FR(x)
Floating-point representation of an integer value.
Definition: IceFPU.h:27
const udword IEEE_1_0
integer representation of 1.0
Definition: IceTypes.h:81
inline_ float fepsilon(float f)
Returns the float ranged espilon value.
Definition: IceFPU.h:106
#define FUNCTION
inline_ bool IsMinusInf(float value)
Definition: IceFPU.h:118
inline_ float RSqrt(float number)
Definition: IceFPU.h:81
unsigned int udword
sizeof(udword) must be 4
Definition: IceTypes.h:52
inline_ bool IsNAN(float value)
Is the float valid ?
Definition: IceFPU.h:115
inline_ bool IsFloatZero(float x, float epsilon=1e-6f)
Definition: IceFPU.h:172
inline_ int ConvertToSortable(float f)
Definition: IceFPU.h:240
inline_ bool IsValidFloat(float value)
Definition: IceFPU.h:120
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
inline_ float fsqrt(float f)
TO BE DOCUMENTED.
Definition: IceFPU.h:97
inline_ float fsat(float f)
Saturates positive to zero.
Definition: IceFPU.h:57
double b
Definition: hdecay.h:118
static double square(double x)
inline_ float InvSqrt(const float &x)
Computes 1.0f / sqrtf(x). Comes from NVIDIA.
Definition: IceFPU.h:73
inline_ bool IsIndeterminate(float value)
Definition: IceFPU.h:116
inline_ bool IsPlusInf(float value)
Definition: IceFPU.h:117
double a
Definition: hdecay.h:119
inline_ float ComputeFloatEpsilon()
This function computes the slowest possible floating-point value (you can also directly use FLT_EPSIL...
Definition: IceFPU.h:166
float x
FPUMode
Definition: IceFPU.h:249
inline_ float FastFabs(float x)
Definition: IceFPU.h:35
tmp
align.sh
Definition: createJobs.py:716