xref: /reactos/sdk/include/ucrt/float.h (revision 53d808d2)
1 //
2 // float.h
3 //
4 //      Copyright (c) Microsoft Corporation. All rights reserved.
5 //
6 // Implementation-defined values commonly used by sophisticated numerical
7 // (floating point) programs.
8 //
9 #pragma once
10 #ifndef _INC_FLOAT // include guard for 3rd party interop
11 #define _INC_FLOAT
12 
13 #include <corecrt.h>
14 
15 #pragma warning(push)
16 #pragma warning(disable: _UCRT_DISABLED_WARNINGS)
17 _UCRT_DISABLE_CLANG_WARNINGS
18 
19 _CRT_BEGIN_C_HEADER
20 
21 
22 
23 #ifndef _CRT_MANAGED_FP_DEPRECATE
24     #ifdef _CRT_MANAGED_FP_NO_DEPRECATE
25         #define _CRT_MANAGED_FP_DEPRECATE
26     #else
27         #ifdef _M_CEE
28             #define _CRT_MANAGED_FP_DEPRECATE _CRT_DEPRECATE_TEXT("Direct floating point control is not supported or reliable from within managed code. ")
29         #else
30             #define _CRT_MANAGED_FP_DEPRECATE
31         #endif
32     #endif
33 #endif
34 
35 
36 // Define the floating point precision used.
37 //
38 // For x86, results are in double precision (unless /arch:sse2 is used, in which
39 // case results are in source precision.
40 //
41 // For x64 and ARM, results are in source precision.
42 //
43 // If the compiler is invoked with /fp:fast, the compiler is allowed to use the
44 // fastest precision and even mix within a single function, so precision is
45 // indeterminable.
46 //
47 // Note that manipulating the floating point behavior using the float_control/
48 // fenv_access/fp_contract #pragmas may alter the actual floating point evaluation
49 // method, which may in turn invalidate the value of FLT_EVAL_METHOD.
50 #ifdef _M_FP_FAST
51     #define FLT_EVAL_METHOD -1
52 #else
53     #ifdef _M_IX86
54         #if _M_IX86_FP >= 2
55             #define FLT_EVAL_METHOD 0
56         #else
57             #define FLT_EVAL_METHOD 2
58         #endif
59     #else
60         #define FLT_EVAL_METHOD 0
61     #endif
62 #endif
63 
64 
65 
66 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 //
68 // Constants
69 //
70 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 #define DBL_DECIMAL_DIG  17                      // # of decimal digits of rounding precision
72 #define DBL_DIG          15                      // # of decimal digits of precision
73 #define DBL_EPSILON      2.2204460492503131e-016 // smallest such that 1.0+DBL_EPSILON != 1.0
74 #define DBL_HAS_SUBNORM  1                       // type does support subnormal numbers
75 #define DBL_MANT_DIG     53                      // # of bits in mantissa
76 #define DBL_MAX          1.7976931348623158e+308 // max value
77 #define DBL_MAX_10_EXP   308                     // max decimal exponent
78 #define DBL_MAX_EXP      1024                    // max binary exponent
79 #define DBL_MIN          2.2250738585072014e-308 // min positive value
80 #define DBL_MIN_10_EXP   (-307)                  // min decimal exponent
81 #define DBL_MIN_EXP      (-1021)                 // min binary exponent
82 #define _DBL_RADIX       2                       // exponent radix
83 #define DBL_TRUE_MIN     4.9406564584124654e-324 // min positive value
84 
85 #define FLT_DECIMAL_DIG  9                       // # of decimal digits of rounding precision
86 #define FLT_DIG          6                       // # of decimal digits of precision
87 #define FLT_EPSILON      1.192092896e-07F        // smallest such that 1.0+FLT_EPSILON != 1.0
88 #define FLT_HAS_SUBNORM  1                       // type does support subnormal numbers
89 #define FLT_GUARD        0
90 #define FLT_MANT_DIG     24                      // # of bits in mantissa
91 #define FLT_MAX          3.402823466e+38F        // max value
92 #define FLT_MAX_10_EXP   38                      // max decimal exponent
93 #define FLT_MAX_EXP      128                     // max binary exponent
94 #define FLT_MIN          1.175494351e-38F        // min normalized positive value
95 #define FLT_MIN_10_EXP   (-37)                   // min decimal exponent
96 #define FLT_MIN_EXP      (-125)                  // min binary exponent
97 #define FLT_NORMALIZE    0
98 #define FLT_RADIX        2                       // exponent radix
99 #define FLT_TRUE_MIN     1.401298464e-45F        // min positive value
100 
101 #define LDBL_DIG         DBL_DIG                 // # of decimal digits of precision
102 #define LDBL_EPSILON     DBL_EPSILON             // smallest such that 1.0+LDBL_EPSILON != 1.0
103 #define LDBL_HAS_SUBNORM DBL_HAS_SUBNORM         // type does support subnormal numbers
104 #define LDBL_MANT_DIG    DBL_MANT_DIG            // # of bits in mantissa
105 #define LDBL_MAX         DBL_MAX                 // max value
106 #define LDBL_MAX_10_EXP  DBL_MAX_10_EXP          // max decimal exponent
107 #define LDBL_MAX_EXP     DBL_MAX_EXP             // max binary exponent
108 #define LDBL_MIN         DBL_MIN                 // min normalized positive value
109 #define LDBL_MIN_10_EXP  DBL_MIN_10_EXP          // min decimal exponent
110 #define LDBL_MIN_EXP     DBL_MIN_EXP             // min binary exponent
111 #define _LDBL_RADIX      _DBL_RADIX              // exponent radix
112 #define LDBL_TRUE_MIN    DBL_TRUE_MIN            // min positive value
113 
114 #define DECIMAL_DIG      DBL_DECIMAL_DIG
115 
116 
117 
118 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119 //
120 // Flags
121 //
122 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123 #define _SW_INEXACT     0x00000001              // Inexact (precision)
124 #define _SW_UNDERFLOW   0x00000002              // Underflow
125 #define _SW_OVERFLOW    0x00000004              // Overflow
126 #define _SW_ZERODIVIDE  0x00000008              // Divide by zero
127 #define _SW_INVALID     0x00000010              // Invalid
128 #define _SW_DENORMAL    0x00080000              // Denormal status bit
129 
130 // New Control Bit that specifies the ambiguity in control word.
131 #define _EM_AMBIGUIOUS  0x80000000 // For backwards compatibility
132 #define _EM_AMBIGUOUS   0x80000000
133 
134 // Abstract User Control Word Mask and bit definitions
135 #define _MCW_EM         0x0008001f              // Interrupt Exception Masks
136 #define _EM_INEXACT     0x00000001              //     inexact (precision)
137 #define _EM_UNDERFLOW   0x00000002              //     underflow
138 #define _EM_OVERFLOW    0x00000004              //     overflow
139 #define _EM_ZERODIVIDE  0x00000008              //     zero divide
140 #define _EM_INVALID     0x00000010              //     invalid
141 #define _EM_DENORMAL    0x00080000              // Denormal exception mask (_control87 only)
142 
143 #define _MCW_RC         0x00000300              // Rounding Control
144 #define _RC_NEAR        0x00000000              //     near
145 #define _RC_DOWN        0x00000100              //     down
146 #define _RC_UP          0x00000200              //     up
147 #define _RC_CHOP        0x00000300              //     chop
148 
149 // i386 specific definitions
150 #define _MCW_PC         0x00030000              // Precision Control
151 #define _PC_64          0x00000000              //     64 bits
152 #define _PC_53          0x00010000              //     53 bits
153 #define _PC_24          0x00020000              //     24 bits
154 
155 #define _MCW_IC         0x00040000              // Infinity Control
156 #define _IC_AFFINE      0x00040000              //     affine
157 #define _IC_PROJECTIVE  0x00000000              //     projective
158 
159 // RISC specific definitions
160 #define _MCW_DN         0x03000000              // Denormal Control
161 #define _DN_SAVE        0x00000000              //   save denormal results and operands
162 #define _DN_FLUSH       0x01000000              //   flush denormal results and operands to zero
163 #define _DN_FLUSH_OPERANDS_SAVE_RESULTS 0x02000000  // flush operands to zero and save results
164 #define _DN_SAVE_OPERANDS_FLUSH_RESULTS 0x03000000  // save operands and flush results to zero
165 
166 
167 
168 // Invalid subconditions (_SW_INVALID also set)
169 #define _SW_UNEMULATED          0x0040  // Unemulated instruction
170 #define _SW_SQRTNEG             0x0080  // Square root of a negative number
171 #define _SW_STACKOVERFLOW       0x0200  // FP stack overflow
172 #define _SW_STACKUNDERFLOW      0x0400  // FP stack underflow
173 
174 
175 
176 // Floating point error signals and return codes
177 #define _FPE_INVALID            0x81
178 #define _FPE_DENORMAL           0x82
179 #define _FPE_ZERODIVIDE         0x83
180 #define _FPE_OVERFLOW           0x84
181 #define _FPE_UNDERFLOW          0x85
182 #define _FPE_INEXACT            0x86
183 
184 #define _FPE_UNEMULATED         0x87
185 #define _FPE_SQRTNEG            0x88
186 #define _FPE_STACKOVERFLOW      0x8a
187 #define _FPE_STACKUNDERFLOW     0x8b
188 
189 #define _FPE_EXPLICITGEN        0x8c // raise(SIGFPE);
190 
191 // On x86 with arch:SSE2, the OS returns these exceptions
192 #define _FPE_MULTIPLE_TRAPS     0x8d
193 #define _FPE_MULTIPLE_FAULTS    0x8e
194 
195 
196 
197 #define _FPCLASS_SNAN  0x0001  // signaling NaN
198 #define _FPCLASS_QNAN  0x0002  // quiet NaN
199 #define _FPCLASS_NINF  0x0004  // negative infinity
200 #define _FPCLASS_NN    0x0008  // negative normal
201 #define _FPCLASS_ND    0x0010  // negative denormal
202 #define _FPCLASS_NZ    0x0020  // -0
203 #define _FPCLASS_PZ    0x0040  // +0
204 #define _FPCLASS_PD    0x0080  // positive denormal
205 #define _FPCLASS_PN    0x0100  // positive normal
206 #define _FPCLASS_PINF  0x0200  // positive infinity
207 
208 
209 
210 // Initial Control Word value
211 #if defined _M_IX86
212 
213     #define _CW_DEFAULT (_RC_NEAR + _PC_53 + _EM_INVALID + _EM_ZERODIVIDE + _EM_OVERFLOW + _EM_UNDERFLOW + _EM_INEXACT + _EM_DENORMAL)
214 
215 #elif defined _M_X64 || defined _M_ARM || defined _M_ARM64
216 
217     #define _CW_DEFAULT (_RC_NEAR + _EM_INVALID + _EM_ZERODIVIDE + _EM_OVERFLOW + _EM_UNDERFLOW + _EM_INEXACT + _EM_DENORMAL)
218 
219 #endif
220 
221 
222 
223 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
224 //
225 // State Manipulation
226 //
227 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
228 // Note that reading or writing the floating point control or status words is
229 // not supported in managed code.
230 _CRT_MANAGED_FP_DEPRECATE
231 _ACRTIMP unsigned int __cdecl _clearfp(void);
232 
233 #pragma warning(push)
234 #pragma warning(disable: 4141) // Double deprecation
235 
236 _CRT_MANAGED_FP_DEPRECATE _CRT_INSECURE_DEPRECATE(_controlfp_s)
237 _ACRTIMP unsigned int __cdecl _controlfp(
238     _In_ unsigned int _NewValue,
239     _In_ unsigned int _Mask
240     );
241 
242 #pragma warning(pop)
243 
244 _CRT_MANAGED_FP_DEPRECATE
245 _ACRTIMP void __cdecl _set_controlfp(
246     _In_ unsigned int _NewValue,
247     _In_ unsigned int _Mask
248     );
249 
250 _CRT_MANAGED_FP_DEPRECATE
251 _ACRTIMP errno_t __cdecl _controlfp_s(
252     _Out_opt_ unsigned int* _CurrentState,
253     _In_      unsigned int  _NewValue,
254     _In_      unsigned int  _Mask
255     );
256 
257 _CRT_MANAGED_FP_DEPRECATE
258 _ACRTIMP unsigned int __cdecl _statusfp(void);
259 
260 _CRT_MANAGED_FP_DEPRECATE
261 _ACRTIMP void __cdecl _fpreset(void);
262 
263 #ifdef _M_IX86
264 
265     _CRT_MANAGED_FP_DEPRECATE
266     _ACRTIMP void __cdecl _statusfp2(
267         _Out_opt_ unsigned int* _X86Status,
268         _Out_opt_ unsigned int* _SSE2Status
269         );
270 
271 #endif
272 
273 #define _clear87        _clearfp
274 #define _status87       _statusfp
275 
276 _CRT_MANAGED_FP_DEPRECATE
277 _ACRTIMP unsigned int __cdecl _control87(
278     _In_ unsigned int _NewValue,
279     _In_ unsigned int _Mask
280     );
281 
282 #ifdef _M_IX86
283     _CRT_MANAGED_FP_DEPRECATE
284     _ACRTIMP int __cdecl __control87_2(
285         _In_      unsigned int  _NewValue,
286         _In_      unsigned int  _Mask,
287         _Out_opt_ unsigned int* _X86ControlWord,
288         _Out_opt_ unsigned int* _Sse2ControlWord
289         );
290 #endif
291 
292 // Global variable holding floating point error code
293 _Check_return_
294 _ACRTIMP int* __cdecl __fpecode(void);
295 
296 #define _fpecode (*__fpecode())
297 
298 _Check_return_
299 _ACRTIMP int __cdecl __fpe_flt_rounds(void);
300 
301 #define FLT_ROUNDS   (__fpe_flt_rounds())
302 #define _DBL_ROUNDS  FLT_ROUNDS
303 #define _LDBL_ROUNDS _DBL_ROUNDS
304 
305 
306 
307 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
308 //
309 // IEEE Recommended Functions
310 //
311 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
312 _Check_return_ _ACRTIMP double __cdecl _copysign(_In_ double _Number, _In_ double _Sign);
313 _Check_return_ _ACRTIMP double __cdecl _chgsign(_In_ double _X);
314 _Check_return_ _ACRTIMP double __cdecl _scalb(_In_ double _X, _In_ long _Y);
315 _Check_return_ _ACRTIMP double __cdecl _logb(_In_ double _X);
316 _Check_return_ _ACRTIMP double __cdecl _nextafter(_In_ double _X, _In_ double _Y);
317 _Check_return_ _ACRTIMP int    __cdecl _finite(_In_ double _X);
318 _Check_return_ _ACRTIMP int    __cdecl _isnan(_In_ double _X);
319 _Check_return_ _ACRTIMP int    __cdecl _fpclass(_In_ double _X);
320 
321 #ifdef _M_X64
322     _Check_return_ _ACRTIMP float __cdecl _scalbf(_In_ float _X, _In_ long _Y);
323 #endif
324 
325 
326 
327 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
328 //
329 // Nonstandard Names for Compatibility
330 //
331 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
332 #if defined(_CRT_INTERNAL_NONSTDC_NAMES) && _CRT_INTERNAL_NONSTDC_NAMES
333 
334     #define clear87   _clear87
335     #define status87  _status87
336     #define control87 _control87
337 
338     _CRT_MANAGED_FP_DEPRECATE
339     _ACRTIMP void __cdecl fpreset(void);
340 
341     #define DBL_RADIX          _DBL_RADIX
342     #define DBL_ROUNDS         _DBL_ROUNDS
343 
344     #define LDBL_RADIX         _LDBL_RADIX
345     #define LDBL_ROUNDS        _LDBL_ROUNDS
346 
347     // For backwards compatibility with the old spelling
348     #define EM_AMBIGUIOUS      _EM_AMBIGUOUS
349     #define EM_AMBIGUOUS       _EM_AMBIGUOUS
350 
351     #define MCW_EM             _MCW_EM
352     #define EM_INVALID         _EM_INVALID
353     #define EM_DENORMAL        _EM_DENORMAL
354     #define EM_ZERODIVIDE      _EM_ZERODIVIDE
355     #define EM_OVERFLOW        _EM_OVERFLOW
356     #define EM_UNDERFLOW       _EM_UNDERFLOW
357     #define EM_INEXACT         _EM_INEXACT
358 
359     #define MCW_IC             _MCW_IC
360     #define IC_AFFINE          _IC_AFFINE
361     #define IC_PROJECTIVE      _IC_PROJECTIVE
362 
363     #define MCW_RC             _MCW_RC
364     #define RC_CHOP            _RC_CHOP
365     #define RC_UP              _RC_UP
366     #define RC_DOWN            _RC_DOWN
367     #define RC_NEAR            _RC_NEAR
368 
369     #define MCW_PC             _MCW_PC
370     #define PC_24              _PC_24
371     #define PC_53              _PC_53
372     #define PC_64              _PC_64
373 
374     #define CW_DEFAULT         _CW_DEFAULT
375 
376     #define SW_INVALID         _SW_INVALID
377     #define SW_DENORMAL        _SW_DENORMAL
378     #define SW_ZERODIVIDE      _SW_ZERODIVIDE
379     #define SW_OVERFLOW        _SW_OVERFLOW
380     #define SW_UNDERFLOW       _SW_UNDERFLOW
381     #define SW_INEXACT         _SW_INEXACT
382 
383     #define SW_UNEMULATED      _SW_UNEMULATED
384     #define SW_SQRTNEG         _SW_SQRTNEG
385     #define SW_STACKOVERFLOW   _SW_STACKOVERFLOW
386     #define SW_STACKUNDERFLOW  _SW_STACKUNDERFLOW
387 
388     #define FPE_INVALID        _FPE_INVALID
389     #define FPE_DENORMAL       _FPE_DENORMAL
390     #define FPE_ZERODIVIDE     _FPE_ZERODIVIDE
391     #define FPE_OVERFLOW       _FPE_OVERFLOW
392     #define FPE_UNDERFLOW      _FPE_UNDERFLOW
393     #define FPE_INEXACT        _FPE_INEXACT
394 
395     #define FPE_UNEMULATED     _FPE_UNEMULATED
396     #define FPE_SQRTNEG        _FPE_SQRTNEG
397     #define FPE_STACKOVERFLOW  _FPE_STACKOVERFLOW
398     #define FPE_STACKUNDERFLOW _FPE_STACKUNDERFLOW
399 
400     #define FPE_EXPLICITGEN    _FPE_EXPLICITGEN
401 
402 #endif // _CRT_INTERNAL_NONSTDC_NAMES
403 
404 
405 
406 _CRT_END_C_HEADER
407 _UCRT_RESTORE_CLANG_WARNINGS
408 #pragma warning(pop) // _UCRT_DISABLED_WARNINGS
409 #endif // _INC_FLOAT
410