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 - definitions and declarations for floating point environment library
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 * This file contains constant definitions and external subroutine
8 * declarations for the floating point environment subroutine library.
9 *
10 * [Public]
11 *
12 ****/
13 
14 #pragma once
15 
16 #ifndef _FENV
17 #define _FENV
18 
19 #include <float.h>
20 
21 /*
22  * Currently, all MS C compilers for Win32 platforms default to 8 byte
23  * alignment.
24  */
25 #pragma pack(push,_CRT_PACKING)
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif /* __cplusplus */
30 
31 #if !defined(_M_CEE) || defined(_CRTBLD)
32 
33 typedef unsigned long fexcept_t;
34 typedef struct fenv_t
35 {
36  unsigned long _Fe_ctl, _Fe_stat;
37 } fenv_t;
38 
39 #define FE_INEXACT _SW_INEXACT /* _EM_INEXACT 0x00000001 inexact (precision) */
40 #define FE_UNDERFLOW _SW_UNDERFLOW /* _EM_UNDERFLOW 0x00000002 underflow */
41 #define FE_OVERFLOW _SW_OVERFLOW /* _EM_OVERFLOW 0x00000004 overflow */
42 #define FE_DIVBYZERO _SW_ZERODIVIDE /* _EM_ZERODIVIDE 0x00000008 zero divide */
43 #define FE_INVALID _SW_INVALID /* _EM_INVALID 0x00000010 invalid */
44 
45 #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
46 
47 _CRTIMP int __cdecl fegetenv(_Inout_ fenv_t *);
48 _CRTIMP int __cdecl fesetenv(_In_ const fenv_t *);
49 _CRTIMP int __cdecl feclearexcept(_In_ int);
50 _CRTIMP int __cdecl feholdexcept(_Inout_ fenv_t *);
51 _CRTIMP int __cdecl fetestexcept(_In_ int);
52 _CRTIMP int __cdecl fegetexceptflag(_Inout_ fexcept_t *, _In_ int);
53 _CRTIMP int __cdecl fesetexceptflag(_In_ const fexcept_t *, _In_ int);
54 
55 __declspec(selectany) extern const fenv_t _Fenv0 = {0, 0};
56 
57 #define FE_DFL_ENV (&_Fenv0)
58 
59 #endif /* !defined(_M_CEE) || defined(_CRTBLD) */
60 
61 #define FE_TONEAREST 0x0000
62 #define FE_UPWARD 0x0100
63 #define FE_DOWNWARD 0x0200
64 #define FE_TOWARDZERO 0x0300
65 
66 #define FE_ROUND_MASK 0x0300
67 
68 _CRTIMP int __cdecl fegetround(void);
69 _CRTIMP int __cdecl fesetround(_In_ int);
70 
71 #if !defined(_M_CEE) && !defined(_CRTBLD)
72 
73 /*
74 * feraiseexcept is inline in this header so that it will be compiled with
75 * the /arch setting specified in the consuming application, rather than the
76 * /arch:IA32 setting which is the default for the CRT library itself.
77 *
78 * feupdateenv is inline because it calls feraiseexcept.
79 */
80 __inline int __CRTDECL feraiseexcept(_In_ int _Except)
81 {
82  #define __DBL_BIG 1e+300 /* may raise inexact too */
83 
84  static struct
85  {
86  int _Except_Val;
87  double _Num, _Denom;
88  } const _Table[] =
89  { /* raise exception by evaluating num / denom */
90  {FE_INVALID, 0.0, 0.0},
91  {FE_DIVBYZERO, 1.0, 0.0},
92  {FE_OVERFLOW, __DBL_BIG, 1.0 / __DBL_BIG},
94  {FE_INEXACT, 2.0, 3.0}
95  };
96 
97  fexcept_t _Flags = 0;
98  double _Ans = 0.0;
99  int _N;
100 
101  if ((_Except &= FE_ALL_EXCEPT) == 0)
102  {
103  return 0;
104  }
105 
106  /* get the current status flags */
107  fegetexceptflag(&_Flags, FE_ALL_EXCEPT);
108 
109  _Except &= ~_Flags;
110  _Flags |= _Except;
111 
112  /* set the new status flags */
113  fesetexceptflag(&_Flags, FE_ALL_EXCEPT);
114 
115  /* raise the exceptions not masked */
116  for (_N = 0; _N < sizeof(_Table) / sizeof(_Table[0]); ++_N)
117  {
118  if ((_Except & _Table[_N]._Except_Val) != 0)
119  {
120  _Ans = _Table[_N]._Num / _Table[_N]._Denom;
121  }
122  }
123 
124  return 0;
125 }
126 
127 __inline int __CRTDECL feupdateenv(_In_ const fenv_t *_Penv)
128 {
129  int _Except = fetestexcept(FE_ALL_EXCEPT);
130 
131  if (fesetenv(_Penv) != 0 || feraiseexcept(_Except) != 0)
132  {
133  return 1;
134  }
135 
136  return 0;
137 }
138 
139 #endif /* !defined(_M_CEE) && !defined(_CRTBLD) */
140 
141 #ifdef __cplusplus
142 }
143 #endif /* __cplusplus */
144 
145 #pragma pack(pop)
146 
147 #endif /* _FENV */
_CRTIMP int __cdecl fegetexceptflag(_Inout_ fexcept_t *, _In_ int)
__declspec(selectany) extern const fenv_t _Fenv0
__inline int __CRTDECL feupdateenv(_In_ const fenv_t *_Penv)
Definition: fenv.h:127
#define FE_OVERFLOW
Definition: fenv.h:41
#define FE_DIVBYZERO
Definition: fenv.h:42
#define _CRTIMP
Definition: crtdefs.h:23
_CRTIMP int __cdecl fesetround(_In_ int)
unsigned long _Fe_stat
Definition: fenv.h:36
_N
Definition: wchar.h:1269
#define FE_UNDERFLOW
Definition: fenv.h:40
Definition: fenv.h:34
_CRTIMP int __cdecl fesetenv(_In_ const fenv_t *)
__inline int __CRTDECL feraiseexcept(_In_ int _Except)
Definition: fenv.h:80
unsigned long _Fe_ctl
Definition: fenv.h:36
_CRTIMP int __cdecl fegetround(void)
#define _In_
Definition: sal.h:314
struct fenv_t fenv_t
#define FE_INVALID
Definition: fenv.h:43
_CRTIMP int __cdecl fesetexceptflag(_In_ const fexcept_t *, _In_ int)
#define FE_ALL_EXCEPT
Definition: fenv.h:45
#define FE_INEXACT
Definition: fenv.h:39
_CRTIMP int __cdecl feclearexcept(_In_ int)
_CRTIMP int __cdecl feholdexcept(_Inout_ fenv_t *)
#define __CRTDECL
Definition: crtdefs.h:622
#define __DBL_BIG
_CRTIMP int __cdecl fetestexcept(_In_ int)
#define _Inout_
Definition: sal.h:384
unsigned long fexcept_t
Definition: fenv.h:33
_CRTIMP int __cdecl fegetenv(_Inout_ fenv_t *)