Skip to content

Commit 78132bd

Browse files
authored
Merge pull request #2493 from corob-msft/cr-float-control-contract
float_control update clarifies scope
2 parents 11c42f5 + 38aee5a commit 78132bd

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

docs/preprocessor/fenv-access.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: "fenv_access pragma"
3-
ms.date: "08/29/2019"
3+
description: "Describes the usage and effects of the fenv_access pragma directive. The fenv_access directive controls access to the floating-point environment at runtime."
4+
ms.date: "11/19/2019"
45
f1_keywords: ["vc-pragma.fenv_access", "fenv_access_CPP"]
56
helpviewer_keywords: ["pragmas, fenv_access", "fenv_access pragma"]
67
ms.assetid: 2ccea292-0ae4-42ce-9c67-cc189299857b
@@ -15,9 +16,17 @@ Disables (**on**) or enables (**off**) optimizations that could change floating-
1516
1617
## Remarks
1718

18-
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.
19+
By default, **fenv_access** is **off**. The compiler assumes your code doesn't access or manipulate the floating-point environment. If environment access isn't required, the compiler can do more to optimize your floating-point code.
1920

20-
For more information on floating-point behavior, see [/fp (Specify Floating-Point Behavior)](../build/reference/fp-specify-floating-point-behavior.md).
21+
Enable **fenv_access** if your code tests floating-point status flags, exceptions, or sets control mode flags. The compiler disables floating-point optimizations, so your code can access the floating-point environment consistently.
22+
23+
The [/fp:strict] command-line option automatically enables **fenv_access**. For more information on this and other floating-point behavior, see [/fp (Specify Floating-Point Behavior)](../build/reference/fp-specify-floating-point-behavior.md).
24+
25+
There are restrictions on the ways you can use the **fenv_access** pragma in combination with other floating-point settings:
26+
27+
- You can't enable **fenv_access** unless precise semantics are enabled. Precise semantics can be enabled either by the [float_control](float-control.md) pragma, or by using the [/fp:precise](../build/reference/fp-specify-floating-point-behavior.md) or [/fp:strict](../build/reference/fp-specify-floating-point-behavior.md) compiler options. The compiler defaults to **/fp:precise** if no other floating-point command-line option is specified.
28+
29+
- You can't use **float_control** to disable precise semantics when **fenv_access(on)** is set.
2130

2231
The kinds of optimizations that are subject to **fenv_access** are:
2332

@@ -65,7 +74,7 @@ int main() {
6574
out=9.999999776482582e-03
6675
```
6776

68-
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+
If you comment out `#pragma fenv_access (on)` from the previous sample, the output is different. It's because the compiler does compile-time evaluation, which doesn't use the control mode.
6978

7079
```cpp
7180
// pragma_directive_fenv_access_2.cpp

docs/preprocessor/float-control.md

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: "float_control pragma"
3-
ms.date: "08/29/2019"
3+
description: "Describes the usage and effects of the float_control pragma directive. The float_control directive controls the state of floating-point precise semantics and exception semantics at runtime."
4+
ms.date: "11/18/2019"
45
f1_keywords: ["vc-pragma.float_control", "float_control_CPP"]
56
helpviewer_keywords: ["float_control pragma", "pragmas, float_control"]
67
ms.assetid: 4f4ba5cf-3707-413e-927d-5ecdbc0a9a43
@@ -12,50 +13,70 @@ Specifies floating-point behavior for a function.
1213
## Syntax
1314

1415
> **#pragma float_control**\
15-
> **#pragma float_control(** { **precise** | **strict** | **except** } **,** { **on** | **off** } [ **, push** ] **)**\
16+
> **#pragma float_control( precise,** { **on** | **off** } [ **, push** ] **)**\
17+
> **#pragma float_control( except,** { **on** | **off** } [ **, push** ] **)**\
1618
> **#pragma float_control(** { **push** | **pop** } **)**
1719
1820
## Options
1921

20-
**precise** | **strict** | **except**, **on** | **off**, **push**\
21-
Specifies floating-point behavior, which can be **precise**, **strict**, or **except**. For more information, see [/fp (Specify Floating-Point Behavior)](../build/reference/fp-specify-floating-point-behavior.md). The setting can either be **on** or **off**.
22+
**precise**, **on** | **off**, **push**\
23+
Specifies whether to enable (**on**) or disable (**off**) precise floating-point semantics. For information on how this option differs from the similarly named **/fp:precise** compiler option, see the Remarks section. The optional **push** token tells the compiler to push the current setting for **float_control** on the internal compiler stack.
2224

23-
When **strict**, the settings for both **strict** and **except** are specified by the **on** or **off** setting. **except** can only be set to **on** when **precise** or **strict** is also set to **on**.
25+
**except**, **on** | **off**, **push**\
26+
Specifies whether to enable (**on**) or disable (**off**) floating-point exception semantics. For information on how this option differs from the similarly named **/fp:except** compiler option, see the Remarks section. The optional **push** token tells the compiler to push the current setting for **float_control** on the internal compiler stack.
2427

25-
If the optional **push** token is added, the current setting for **float_control** is pushed on to the internal compiler stack.
28+
**except** can only be set to **on** when **precise** is also set to **on**.
2629

2730
**push**\
28-
Push the current **float_control** setting on to the internal compiler stack
31+
Pushes the current **float_control** setting on to the internal compiler stack.
2932

3033
**pop**\
31-
Removes the **float_control** setting from the top of the internal compiler stack and makes that the new **float_control** setting.
34+
Removes the **float_control** setting from the top of the internal compiler stack, and makes that the new **float_control** setting.
3235

3336
## Remarks
3437

35-
You cannot use **float_control** to turn **precise** off when **except** is on. Similarly, **precise** cannot be turned off when [fenv_access](../preprocessor/fenv-access.md) is on. To go from strict model to a fast model by using the **float_control** pragma, use the following code:
38+
The **precise** and **except** options don't have exactly the same behavior as the [/fp](../build/reference/fp-specify-floating-point-behavior.md) compiler options of the same names. The **float_control** pragma only governs part of the floating-point behavior. It must be combined with [fp_contract](../preprocessor/fp-contract.md) and [fenv_access](../preprocessor/fenv-access.md) pragmas to recreate the **/fp** compiler options. The following table shows the equivalent pragma settings for each compiler option:
39+
40+
| | float_control(precise, \*) | float_control(except, \*) | fp_contract(\*) | fenv_access(\*) |
41+
|-|-|-|-|-|
42+
| /fp:strict | on | on | off | on |
43+
| /fp:strict /fp:except- | on | off | off | on |
44+
| /fp:precise | on | off | on | off |
45+
| /fp:precise /fp:except | on | on | on | off |
46+
| /fp:fast | off | off | on | off |
47+
48+
In other words, you must use several pragmas in combination to emulate the **/fp:fast**, **/fp:precise**, **/fp:strict**, and **/fp:except** command-line options.
49+
50+
There are restrictions on the ways you can use the **float_control** and **fenv_access** floating-point pragmas in combination:
51+
52+
- You can only use **float_control** to set **except** to **on** if precise semantics are enabled. Precise semantics can be enabled either by the **float_control** pragma, or by using the **/fp:precise** or **/fp:strict** compiler options.
53+
54+
- You can't use **float_control** to turn **precise** off when exception semantics are enabled, whether by a **float_control** pragma or a **/fp:except** compiler option.
55+
56+
- You can't enable **fenv_access** unless precise semantics are enabled, whether by a **float_control** pragma or a compiler option.
57+
58+
- You can't use **float_control** to turn **precise** off when **fenv_access** is enabled.
59+
60+
These restrictions mean the order of some floating-point pragmas is significant. To go from a fast model to a strict model using the **float_control** and related pragmas, use the following code:
3661

3762
```cpp
38-
#pragma float_control(except, off)
39-
#pragma fenv_access(off)
40-
#pragma float_control(precise, off)
63+
#pragma float_control(precise, on) // enable precise semantics
64+
#pragma fenv_access(on) // enable environment sensitivity
65+
#pragma float_control(except, on) // enable exception semantics
66+
#pragma fp_contract(off) // disable contractions
4167
```
4268

43-
To go from fast model to a strict model with the **float_control** pragma, use the following code:
69+
To go from a strict model to a fast model by using the **float_control** pragma, use the following code:
4470

4571
```cpp
46-
#pragma float_control(precise, on)
47-
#pragma fenv_access(on)
48-
#pragma float_control(except, on)
72+
#pragma float_control(except, off) // disable exception semantics
73+
#pragma fenv_access(off) // disable environment sensitivity
74+
#pragma float_control(precise, off) // disable precise semantics
75+
#pragma fp_contract(on) // ensable contractions
4976
```
5077

5178
If no options are specified, **float_control** has no effect.
5279

53-
Other floating-point pragmas include:
54-
55-
- [fenv_access](../preprocessor/fenv-access.md)
56-
57-
- [fp_contract](../preprocessor/fp-contract.md)
58-
5980
## Example
6081

6182
The following sample shows how to catch an overflow floating-point exception by using pragma **float_control**.
@@ -100,4 +121,6 @@ Pass
100121

101122
## See also
102123

103-
[Pragma directives and the __pragma keyword](../preprocessor/pragma-directives-and-the-pragma-keyword.md)
124+
[Pragma directives and the __pragma keyword](../preprocessor/pragma-directives-and-the-pragma-keyword.md)\
125+
[fenv_access](../preprocessor/fenv-access.md)\
126+
[fp_contract](../preprocessor/fp-contract.md)

0 commit comments

Comments
 (0)