STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
fenv.h
Go to the documentation of this file.
1 //
2 // fenv.h
3 //
4 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //
6 // Floating point environment library.
7 //
8 #pragma once
9 #define _FENV
10 
11 #include <corecrt.h>
12 #include <float.h>
13 
15 
16 
17 
18 #define FE_TONEAREST _RC_NEAR
19 #define FE_UPWARD _RC_UP
20 #define FE_DOWNWARD _RC_DOWN
21 #define FE_TOWARDZERO _RC_CHOP
22 
23 #define FE_ROUND_MASK _MCW_RC
24 
25 _ACRTIMP int __cdecl fegetround(void);
26 _ACRTIMP int __cdecl fesetround(_In_ int _Round);
27 
28 
29 
30 #if !defined _M_CEE
31 
32  typedef unsigned long fexcept_t;
33 
34  typedef struct fenv_t
35  {
36  unsigned long _Fe_ctl, _Fe_stat;
37  } fenv_t;
38 
39 
40 
41  #define FE_INEXACT _SW_INEXACT // _EM_INEXACT 0x00000001 inexact (precision)
42  #define FE_UNDERFLOW _SW_UNDERFLOW // _EM_UNDERFLOW 0x00000002 underflow
43  #define FE_OVERFLOW _SW_OVERFLOW // _EM_OVERFLOW 0x00000004 overflow
44  #define FE_DIVBYZERO _SW_ZERODIVIDE // _EM_ZERODIVIDE 0x00000008 zero divide
45  #define FE_INVALID _SW_INVALID // _EM_INVALID 0x00000010 invalid
46 
47  #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
48 
49  _ACRTIMP int __cdecl fegetenv(_Out_ fenv_t* _Env);
50  _ACRTIMP int __cdecl fesetenv(_In_ fenv_t const* _Env);
51  _ACRTIMP int __cdecl feclearexcept(_In_ int _Flags);
52  _ACRTIMP _Success_(return == 0) int __cdecl feholdexcept(_Out_ fenv_t* _Env);
53  _ACRTIMP int __cdecl fetestexcept(_In_ int _Flags);
54  _ACRTIMP int __cdecl fegetexceptflag(_Out_ fexcept_t* _Except, _In_ int _TestFlags);
55  _ACRTIMP int __cdecl fesetexceptflag(_In_ fexcept_t const* _Except, _In_ int _SetFlags);
56 
57  #if !defined __midl // MIDL does not support compound initializers
58  // In the original implementation (_Fenv0), the global variable was zero
59  // initialized, indicating no exceptions are masked. In the current
60  // implementation (_Fenv1), the global variable is initialized with all
61  // exceptions masked, which is the actual initial environment.
62  #if defined _M_IX86
63  __declspec(selectany) extern const fenv_t _Fenv1 = { 0x3f3f103f, 0 };
64  #elif defined _M_X64
65  __declspec(selectany) extern const fenv_t _Fenv1 = { 0x3f00003f, 0 };
66  #else
67  __declspec(selectany) extern const fenv_t _Fenv1 = { 0x0000003f, 0 };
68  #endif
69  #endif
70 
71  #define FE_DFL_ENV (&_Fenv1)
72 
73 
74 
75  // feraiseexcept is defined inline in this header so that it is compiled
76  // with the same /arch setting as is specified in the consuming application,
77  // rather than the /arch:IA32 setting with which the CRT sources are built.
78  // optimizer has to be turned off to avoid optimizing out since the function
79  // doesn't have side effects.
80  //
81  // feupdateenv is inline because it calls feraiseexcept.
82  #if _CRT_FUNCTIONS_REQUIRED
83  #if !defined(_BEGIN_PRAGMA_OPTIMIZE_DISABLE)
84  #define _BEGIN_PRAGMA_OPTIMIZE_DISABLE(flags, bug, reason) \
85  __pragma(optimize(flags, off))
86  #define _BEGIN_PRAGMA_OPTIMIZE_ENABLE(flags, bug, reason) \
87  __pragma(optimize(flags, on))
88  #define _END_PRAGMA_OPTIMIZE() \
89  __pragma(optimize("", on))
90  #endif
91  _BEGIN_PRAGMA_OPTIMIZE_DISABLE("", MSFT:4499495, "If optimizations are on, the floating-point exception might not get triggered (because the compiler optimizes it out), breaking the function.")
92  __inline int __CRTDECL feraiseexcept(_In_ int _Except)
93  {
94  static struct
95  {
96  int _Except_Val;
97  double _Num;
98  double _Denom;
99  } const _Table[] =
100  { // Raise exception by evaluating num / denom:
101  {FE_INVALID, 0.0, 0.0 },
102  {FE_DIVBYZERO, 1.0, 0.0 },
103  {FE_OVERFLOW, 1e+300, 1e-300 },
104  {FE_UNDERFLOW, 1e-300, 1e+300 },
105  {FE_INEXACT, 2.0, 3.0 }
106  };
107 
108  double _Ans = 0.0;
109  int _N;
110 
111  if ((_Except &= FE_ALL_EXCEPT) == 0)
112  {
113  return 0;
114  }
115 
116  // Raise the exceptions not masked:
117  for (_N = 0; _N < sizeof(_Table) / sizeof(_Table[0]); ++_N)
118  {
119  if ((_Except & _Table[_N]._Except_Val) != 0)
120  {
121  _Ans = _Table[_N]._Num / _Table[_N]._Denom;
122 
123  // x87 exceptions are raised immediately before execution of the
124  // next floating point instruction. If we're using /arch:IA32,
125  // force the exception to be raised immediately:
126  #if defined _M_IX86 && _M_IX86_FP == 0
127  __asm fwait;
128  #endif
129  }
130  }
131 
132  return 0;
133  }
135 
136  __inline int __CRTDECL feupdateenv(_In_ const fenv_t *_Penv)
137  {
138  int _Except = fetestexcept(FE_ALL_EXCEPT);
139 
140  if (fesetenv(_Penv) != 0 || feraiseexcept(_Except) != 0)
141  {
142  return 1;
143  }
144 
145  return 0;
146  }
147  #endif // _CRT_FUNCTIONS_REQUIRED
148 
149 #endif // !defined _M_CEE && !defined _CORECRT_BUILD
150 
#define _Out_
Definition: sal.h:342
#define _ACRTIMP
Definition: corecrt.h:27
_ACRTIMP int __cdecl fegetenv(_Out_ fenv_t *_Env)
__inline int __CRTDECL feupdateenv(_In_ const fenv_t *_Penv)
Definition: fenv.h:136
#define FE_OVERFLOW
Definition: fenv.h:43
#define FE_DIVBYZERO
Definition: fenv.h:44
_ACRTIMP int __cdecl fegetround(void)
unsigned long _Fe_stat
Definition: fenv.h:36
_In_ size_t _Deref_pre_opt_z_ char const _In_ size_t _N
Definition: wchar.h:78
_ACRTIMP int __cdecl fesetexceptflag(_In_ fexcept_t const *_Except, _In_ int _SetFlags)
#define FE_UNDERFLOW
Definition: fenv.h:42
#define _CRT_BEGIN_C_HEADER
Definition: vcruntime.h:73
Definition: fenv.h:34
__inline int __CRTDECL feraiseexcept(_In_ int _Except)
Definition: fenv.h:92
#define __CRTDECL
Definition: vcruntime.h:156
unsigned long _Fe_ctl
Definition: fenv.h:36
#define _In_
Definition: sal.h:305
__declspec(selectany) extern const fenv_t _Fenv1
#define _END_PRAGMA_OPTIMIZE()
Definition: fenv.h:88
_ACRTIMP int __cdecl fesetround(_In_ int _Round)
_ACRTIMP int __cdecl feclearexcept(_In_ int _Flags)
struct fenv_t fenv_t
_ACRTIMP int __cdecl fetestexcept(_In_ int _Flags)
#define FE_INVALID
Definition: fenv.h:45
char int *typedef int(__CRTDECL *_CRT_REPORT_HOOKW)(int
Definition: crtdbg.h:45
#define FE_ALL_EXCEPT
Definition: fenv.h:47
#define FE_INEXACT
Definition: fenv.h:41
#define _BEGIN_PRAGMA_OPTIMIZE_DISABLE(flags, bug, reason)
Definition: fenv.h:84
_ACRTIMP _Success_(return==0) int __cdecl feholdexcept(_Out_ fenv_t *_Env)
_ACRTIMP int __cdecl fegetexceptflag(_Out_ fexcept_t *_Except, _In_ int _TestFlags)
_ACRTIMP int __cdecl fesetenv(_In_ fenv_t const *_Env)
#define _CRT_END_C_HEADER
Definition: vcruntime.h:76
constexpr _To _Round(const duration< _Rep, _Period > &_Dur, false_type, false_type)
Definition: chrono:721
unsigned long fexcept_t
Definition: fenv.h:32