|
1 | 1 | --- |
2 | 2 | title: "fp_contract | 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.fp_contract", "fp_contract_CPP"] |
11 | 8 | dev_langs: ["C++"] |
12 | 9 | helpviewer_keywords: ["pragmas, fp_contract", "fp_contract pragma"] |
13 | 10 | ms.assetid: 15b97338-6680-4287-ba2a-2dccc5b2ccf5 |
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 | # fp_contract |
21 | | -Determines whether floating-point contraction will take place. |
22 | | - |
23 | | -## Syntax |
24 | | - |
25 | | -``` |
26 | | -#pragma fp_contract [ON | OFF] |
27 | | -``` |
28 | | - |
| 17 | + |
| 18 | +Determines whether floating-point contraction takes place. A floating-point contraction is an instruction such as FMA (Fused-Multiply-Add) that combines two separate floating point operations into a single instruction. Use of these instructions can affect floating-point precision, because instead of rounding after each operation, the processor may round only once after both operations. |
| 19 | + |
| 20 | +## Syntax |
| 21 | + |
| 22 | +> **#pragma fp_contract** [ **on** | **off** ] |
| 23 | +
|
29 | 24 | ## Remarks |
30 | | - By default, `fp_contract` is ON. |
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 | | - Other floating-point pragmas include: |
35 | | - |
36 | | -- [fenv_access](../preprocessor/fenv-access.md) |
37 | | - |
38 | | -- [float_control](../preprocessor/float-control.md) |
39 | | - |
40 | | -## Example |
41 | | - The code generated from this sample does not use the Fused Multiply Add (**fma**) instruction on Itanium processors. If you comment out `#pragma fp_contract (off)`, the generated code will use the **fma** instruction. |
42 | | - |
43 | | -``` |
44 | | -// pragma_directive_fp_contract.cpp |
45 | | -// compile with: /O2 |
46 | | -#include <stdio.h> |
47 | | -#include <float.h> |
48 | | - |
49 | | -#pragma fp_contract (off) |
50 | | - |
51 | | -int main() { |
52 | | - double z, b, t; |
53 | | - |
54 | | - for (int i = 0; i < 10; i++) { |
55 | | - b = i * 5.5; |
56 | | - t = i * 56.025; |
57 | | - _set_controlfp(_PC_24, _MCW_PC); |
58 | | - |
59 | | - z = t * i + b; |
60 | | - printf_s ("out=%.15e\n", z); |
61 | | - } |
62 | | -} |
63 | | -``` |
64 | | - |
65 | | -```Output |
66 | | -out=0.000000000000000e+000 |
67 | | -out=6.152500152587891e+001 |
68 | | -out=2.351000061035156e+002 |
69 | | -out=5.207249755859375e+002 |
70 | | -out=9.184000244140625e+002 |
71 | | -out=1.428125000000000e+003 |
72 | | -out=2.049899902343750e+003 |
73 | | -out=2.783724853515625e+003 |
74 | | -out=3.629600097656250e+003 |
75 | | -out=4.587524902343750e+003 |
76 | | -``` |
| 25 | + |
| 26 | +By default, **fp_contract** is **on**. This tells the compiler to use floating-point contraction instructions where possible. Set **fp_contract** to **off** to preserve individual floating-point instructions. |
| 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 | +Other floating-point pragmas include: |
| 31 | + |
| 32 | +- [fenv_access](../preprocessor/fenv-access.md) |
| 33 | + |
| 34 | +- [float_control](../preprocessor/float-control.md) |
| 35 | + |
| 36 | +## Example |
| 37 | + |
| 38 | +The code generated from this sample does not use a fused-multiply-add instruction even when it is available on the target processor. If you comment out `#pragma fp_contract (off)`, the generated code may use a fused-multiply-add instruction if it is available. |
77 | 39 |
|
78 | | -## See Also |
79 | | - [Pragma Directives and the __Pragma Keyword](../preprocessor/pragma-directives-and-the-pragma-keyword.md) |
| 40 | +```cpp |
| 41 | +// pragma_directive_fp_contract.cpp |
| 42 | +// on x86 and x64 compile with: /O2 /fp:fast /arch:AVX2 |
| 43 | +// other platforms compile with: /O2 |
| 44 | + |
| 45 | +#include <stdio.h> |
| 46 | + |
| 47 | +// remove the following line to enable FP contractions |
| 48 | +#pragma fp_contract (off) |
| 49 | + |
| 50 | +int main() { |
| 51 | + double z, b, t; |
| 52 | + |
| 53 | + for (int i = 0; i < 10; i++) { |
| 54 | + b = i * 5.5; |
| 55 | + t = i * 56.025; |
| 56 | + |
| 57 | + z = t * i + b; |
| 58 | + printf("out = %.15e\n", z); |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +```Output |
| 64 | +out = 0.000000000000000e+00 |
| 65 | +out = 6.152500000000000e+01 |
| 66 | +out = 2.351000000000000e+02 |
| 67 | +out = 5.207249999999999e+02 |
| 68 | +out = 9.184000000000000e+02 |
| 69 | +out = 1.428125000000000e+03 |
| 70 | +out = 2.049900000000000e+03 |
| 71 | +out = 2.783725000000000e+03 |
| 72 | +out = 3.629600000000000e+03 |
| 73 | +out = 4.587525000000000e+03 |
| 74 | +``` |
| 75 | + |
| 76 | +## See also |
| 77 | + |
| 78 | +[Pragma Directives and the __Pragma Keyword](../preprocessor/pragma-directives-and-the-pragma-keyword.md) |
0 commit comments