|
1 | 1 | --- |
2 | 2 | title: "fenv_access | Microsoft Docs" |
3 | 3 | ms.custom: "" |
4 | | -ms.date: "11/04/2016" |
5 | | -ms.reviewer: "" |
6 | | -ms.suite: "" |
| 4 | +ms.date: "03/12/2018" |
7 | 5 | ms.technology: ["cpp-tools"] |
8 | | -ms.tgt_pltfrm: "" |
9 | 6 | ms.topic: "reference" |
10 | 7 | f1_keywords: ["vc-pragma.fenv_access", "fenv_access_CPP"] |
11 | 8 | dev_langs: ["C++"] |
12 | 9 | helpviewer_keywords: ["pragmas, fenv_access", "fenv_access pragma"] |
13 | 10 | ms.assetid: 2ccea292-0ae4-42ce-9c67-cc189299857b |
14 | | -caps.latest.revision: 12 |
15 | 11 | author: "corob-msft" |
16 | 12 | ms.author: "corob" |
17 | 13 | manager: "ghogen" |
18 | 14 | ms.workload: ["cplusplus"] |
19 | 15 | --- |
20 | 16 | # fenv_access |
21 | | -Disables (ON) or enables (OFF) optimizations that could change flag tests and mode changes. |
22 | | - |
23 | | -## Syntax |
24 | | - |
25 | | -``` |
26 | | -#pragma fenv_access [ON | OFF] |
27 | | -``` |
28 | | - |
29 | | -## Remarks |
30 | | - By default, `fenv_access` is OFF. |
31 | | - |
32 | | - For more information on floating-point behavior, see [/fp (Specify Floating-Point Behavior)](../build/reference/fp-specify-floating-point-behavior.md). |
33 | | - |
34 | | - The kinds of optimizations that are subject to `fenv_access` are: |
35 | | - |
36 | | -- Global common subexpression elimination |
37 | | - |
38 | | -- Code motion |
39 | | - |
40 | | -- Constant folding |
41 | | - |
42 | | - Other floating-point pragmas include: |
43 | | - |
44 | | -- [float_control](../preprocessor/float-control.md) |
45 | | - |
46 | | -- [fp_contract](../preprocessor/fp-contract.md) |
47 | | - |
48 | | -## Example |
49 | | - |
50 | | -``` |
51 | | -// pragma_directive_fenv_access_x86.cpp |
52 | | -// compile with: /O2 |
53 | | -// processor: x86 |
54 | | -#include <stdio.h> |
55 | | -#include <float.h> |
56 | | -#include <errno.h> |
57 | | -#pragma fenv_access (on) |
58 | | - |
59 | | -int main() { |
60 | | - double z, b = 0.1, t = 0.1; |
61 | | - unsigned int currentControl; |
62 | | - errno_t err; |
63 | | - |
64 | | - err = _controlfp_s(¤tControl, _PC_24, _MCW_PC); |
65 | | - if (err != 0) { |
66 | | - printf_s("The function _controlfp_s failed!\n"); |
67 | | - return -1; |
68 | | - } |
69 | | - z = b * t; |
70 | | - printf_s ("out=%.15e\n",z); |
71 | | -} |
72 | | -``` |
73 | | - |
74 | | -```Output |
75 | | -out=9.999999776482582e-003 |
76 | | -``` |
77 | | - |
78 | | -## Example |
79 | | - The following sample is for compiler producing output files for Itanium processors. **/fp:precise** keeps the intermediate results in extended precision where values greater than FLT_MAX (3.402823466e+38F) can be calculated and as a result of that sum will have 1.0 result, as it should if manually calculated. **/fp:strict** keeps intermediate results in their source precision (float) so the first addition produces infinity, which is kept throughout the expression. |
80 | | - |
81 | | -``` |
82 | | -// pragma_directive_fenv_access_IPF.cpp |
83 | | -// compile with: /O2 /fp:precise |
84 | | -// processor: IPF |
85 | | -// compiling with /fp:precise prints 1.0F |
86 | | -// compile with /fp:strict to print infinity |
87 | | - |
88 | | -#include <stdio.h> |
89 | | -float arr[5] = {3.402823465e+38F, |
90 | | - 3.402823462e+38F, |
91 | | - 3.402823464e+38F, |
92 | | - 3.402823463e+38F, |
93 | | - 1.0F}; |
94 | | - |
95 | | -int main() { |
96 | | - float sum = 0; |
97 | | - sum = arr[0] + arr[1] - arr[2] - arr[3] + arr[4]; |
98 | | - printf_s("%f\n", sum); |
99 | | -} |
100 | | -``` |
101 | | - |
102 | | -```Output |
103 | | -1.000000 |
104 | | -``` |
105 | | - |
106 | | -## Example |
107 | | - When commenting out `#pragma fenv_access (on)` from the previous sample, note that the output is different because the compiler does compile-time evaluation, which does not use the control mode. |
108 | | - |
109 | | -``` |
110 | | -// pragma_directive_fenv_access_2.cpp |
111 | | -// compile with: /O2 |
112 | | -#include <stdio.h> |
113 | | -#include <float.h> |
114 | | - |
115 | | -int main() { |
116 | | - double z, b = 0.1, t = 0.1; |
117 | | - unsigned int currentControl; |
118 | | - errno_t err; |
119 | | - |
120 | | - err = _controlfp_s(¤tControl, _PC_24, _MCW_PC); |
121 | | - if (err != 0) { |
122 | | - printf_s("The function _controlfp_s failed!\n"); |
123 | | - return -1; |
124 | | - } |
125 | | - z = b * t; |
126 | | - printf_s ("out=%.15e\n",z); |
127 | | -} |
128 | | -``` |
129 | | - |
130 | | -```Output |
131 | | -out=1.000000000000000e-002 |
132 | | -``` |
133 | | - |
134 | | -## See Also |
135 | | - [Pragma Directives and the __Pragma Keyword](../preprocessor/pragma-directives-and-the-pragma-keyword.md) |
| 17 | + |
| 18 | +Disables (**on**) or enables (**off*) optimizations that could change floating-point environment flag tests and mode changes. |
| 19 | + |
| 20 | +## Syntax |
| 21 | + |
| 22 | +> **#pragma fenv_access** [ **on** | **off** ] |
| 23 | +
|
| 24 | +## Remarks |
| 25 | + |
| 26 | +By default, **fenv_access** is **off**. If the compiler can assume that your code does not access or manipulate the floating-point environment, then it can perform many floating-point code optimizations. Set **fenv_access** to **on** to inform the compiler that your code accesses the floating-point environment to test status flags, exceptions, or to set control mode flags. The compiler disables these optimizations so that your code can access the floating-point environment consistently. |
| 27 | + |
| 28 | +For more information on floating-point behavior, see [/fp (Specify Floating-Point Behavior)](../build/reference/fp-specify-floating-point-behavior.md). |
| 29 | + |
| 30 | +The kinds of optimizations that are subject to **fenv_access** are: |
| 31 | + |
| 32 | +- Global common subexpression elimination |
| 33 | + |
| 34 | +- Code motion |
| 35 | + |
| 36 | +- Constant folding |
| 37 | + |
| 38 | +Other floating-point pragmas include: |
| 39 | + |
| 40 | +- [float_control](../preprocessor/float-control.md) |
| 41 | + |
| 42 | +- [fp_contract](../preprocessor/fp-contract.md) |
| 43 | + |
| 44 | +## Examples |
| 45 | + |
| 46 | +This example sets **fenv_access** to **on** to set the floating-point control register for 24-bit precision: |
| 47 | + |
| 48 | +```cpp |
| 49 | +// pragma_directive_fenv_access_x86.cpp |
| 50 | +// compile with: /O2 |
| 51 | +// processor: x86 |
| 52 | +#include <stdio.h> |
| 53 | +#include <float.h> |
| 54 | +#include <errno.h> |
| 55 | +#pragma fenv_access (on) |
| 56 | + |
| 57 | +int main() { |
| 58 | + double z, b = 0.1, t = 0.1; |
| 59 | + unsigned int currentControl; |
| 60 | + errno_t err; |
| 61 | + |
| 62 | + err = _controlfp_s(¤tControl, _PC_24, _MCW_PC); |
| 63 | + if (err != 0) { |
| 64 | + printf_s("The function _controlfp_s failed!\n"); |
| 65 | + return -1; |
| 66 | + } |
| 67 | + z = b * t; |
| 68 | + printf_s ("out=%.15e\n",z); |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +```Output |
| 73 | +out=9.999999776482582e-003 |
| 74 | +``` |
| 75 | + |
| 76 | +If you comment out `#pragma fenv_access (on)` from the previous sample, note that the output is different because the compiler does compile-time evaluation, which does not use the control mode. |
| 77 | + |
| 78 | +```cpp |
| 79 | +// pragma_directive_fenv_access_2.cpp |
| 80 | +// compile with: /O2 |
| 81 | +#include <stdio.h> |
| 82 | +#include <float.h> |
| 83 | + |
| 84 | +int main() { |
| 85 | + double z, b = 0.1, t = 0.1; |
| 86 | + unsigned int currentControl; |
| 87 | + errno_t err; |
| 88 | + |
| 89 | + err = _controlfp_s(¤tControl, _PC_24, _MCW_PC); |
| 90 | + if (err != 0) { |
| 91 | + printf_s("The function _controlfp_s failed!\n"); |
| 92 | + return -1; |
| 93 | + } |
| 94 | + z = b * t; |
| 95 | + printf_s ("out=%.15e\n",z); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +```Output |
| 100 | +out=1.000000000000000e-002 |
| 101 | +``` |
| 102 | + |
| 103 | +## See also |
| 104 | + |
| 105 | +[Pragma Directives and the __Pragma Keyword](../preprocessor/pragma-directives-and-the-pragma-keyword.md) |
0 commit comments