summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fmath%2Ffma.html
blob: ce600c3f330e8f0c0c4ed86448a717f76a09a482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    <h1 id="firstHeading" class="firstHeading">fma, fmaf, fmal</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;math.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">float       fmaf( float x, float y, float z );</pre>
</td> <td> (1) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">double      fma( double x, double y, double z );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">long double fmal( long double x, long double y, long double z );</pre>
</td> <td> (3) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define FP_FAST_FMA  /* implementation-defined */</pre>
</td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define FP_FAST_FMAF /* implementation-defined */</pre>
</td> <td> (5) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define FP_FAST_FMAL /* implementation-defined */</pre>
</td> <td> (6) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;tgmath.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define fma( x, y, z )</pre>
</td> <td> (7) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr>  </table> <div class="t-li1">
<span class="t-li">1-3)</span> Computes <code>(x*y) + z</code> as if to infinite precision and rounded only once to fit the result type.</div> <div class="t-li1">
<span class="t-li">4-6)</span> If the macro constants <code>FP_FAST_FMA</code>, <code>FP_FAST_FMAF</code>, or <code>FP_FAST_FMAL</code> are defined, the corresponding function <code>fmaf</code>, <code>fma</code>, or <code>fmal</code> evaluates faster (in addition to being more precise) than the expression <code>x*y+z</code> for <code>float</code>, <code>double</code>, and <code>long double</code> arguments, respectively. If defined, these macros evaluate to integer <code>1</code>.</div> <div class="t-li1">
<span class="t-li">7)</span> Type-generic macro: If any argument has type <code>long double</code>, <code>fmal</code> is called. Otherwise, if any argument has integer type or has type <code>double</code>, <code>fma</code> is called. Otherwise, <code>fmaf</code> is called.</div>  <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> x, y, z </td> <td> - </td> <td> floating point values </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>If successful, returns the value of <code>(x*y) + z</code> as if calculated to infinite precision and rounded once to fit the result type (or, alternatively, calculated as a single ternary floating-point operation).</p>
<p>If a range error due to overflow occurs, <code><a href="huge_val" title="c/numeric/math/HUGE VAL">±HUGE_VAL</a></code>, <code>±HUGE_VALF</code>, or <code>±HUGE_VALL</code> is returned.</p>
<p>If a range error due to underflow occurs, the correct value (after rounding) is returned.</p>
<h3 id="Error_handling"> Error handling</h3> <p>Errors are reported as specified in <a href="math_errhandling" title="c/numeric/math/math errhandling"><code>math_errhandling</code></a>.</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p>
<ul>
<li> If x is zero and y is infinite or if x is infinite and y is zero, and z is not a NaN, then NaN is returned and <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> is raised </li>
<li> If x is zero and y is infinite or if x is infinite and y is zero, and z is a NaN, then NaN is returned and <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> may be raised </li>
<li> If <code>x*y</code> is an exact infinity and z is an infinity with the opposite sign, NaN is returned and <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> is raised </li>
<li> If x or y are NaN, NaN is returned </li>
<li> If z is NaN, and <code>x*y</code> aren't 0*Inf or Inf*0, then NaN is returned (without <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code>). </li>
</ul> <h3 id="Notes"> Notes</h3> <p>This operation is commonly implemented in hardware as <a href="https://en.wikipedia.org/wiki/Multiply%E2%80%93accumulate_operation" class="extiw" title="enwiki:Multiply–accumulate operation">fused multiply-add</a> CPU instruction. If supported by hardware, the appropriate <code>FP_FAST_FMA*</code> macros are expected to be defined, but many implementations make use of the CPU instruction even when the macros are not defined.</p>
<p><a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/fma.html">POSIX specifies</a> that the situation where the value <code>x*y</code> is invalid and z is a NaN is a domain error.</p>
<p>Due to its infinite intermediate precision, <code>fma</code> is a common building block of other correctly-rounded mathematical operations, such as <code><a href="sqrt" title="c/numeric/math/sqrt">sqrt</a></code> or even the division (where not provided by the CPU, e.g. Itanium).</p>
<p>As with all floating-point expressions, the expression <code>(x*y) + z</code> may be compiled as a fused mutiply-add unless the <a href="../../preprocessor/impl" title="c/preprocessor/impl"><code> #pragma</code></a> <code>STDC FP_CONTRACT</code> is off.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;float.h&gt;
#include &lt;fenv.h&gt;
#pragma STDC FENV_ACCESS ON
int main(void)
{
    // demo the difference between fma and built-in operators
    double in = 0.1;
    printf("0.1 double is %.23f (%a)\n", in, in);
    printf("0.1*10 is 1.0000000000000000555112 (0x8.0000000000002p-3),"
           " or 1.0 if rounded to double\n");
    double expr_result = 0.1 * 10 - 1;
    printf("0.1 * 10 - 1 = %g : 1 subtracted after "
           "intermediate rounding to 1.0\n", expr_result);
    double fma_result = fma(0.1, 10, -1);
    printf("fma(0.1, 10, -1) = %g (%a)\n", fma_result, fma_result);
 
    // fma use in double-double arithmetic
    printf("\nin double-double arithmetic, 0.1 * 10 is representable as ");
    double high = 0.1 * 10;
    double low = fma(0.1, 10, -high);
    printf("%g + %g\n\n", high, low);
 
    //error handling
    feclearexcept(FE_ALL_EXCEPT);
    printf("fma(+Inf, 10, -Inf) = %f\n", fma(INFINITY, 10, -INFINITY));
    if(fetestexcept(FE_INVALID)) puts("    FE_INVALID raised");
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">0.1 double is 0.10000000000000000555112 (0x1.999999999999ap-4)
0.1*10 is 1.0000000000000000555112 (0x8.0000000000002p-3), or 1.0 if rounded to double
0.1 * 10 - 1 = 0 : 1 subtracted after intermediate rounding to 1.0
fma(0.1, 10, -1) = 5.55112e-17 (0x1p-54)
 
in double-double arithmetic, 0.1 * 10 is representable as 1 + 5.55112e-17
 
fma(+Inf, 10, -Inf) = -nan
    FE_INVALID raised</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.12.13.1 The fma functions (p: 188-189) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 272-273) </li>
<li> F.10.10.1 The fma functions (p: 386) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.13.1 The fma functions (p: 258) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 373-375) </li>
<li> F.10.10.1 The fma functions (p: 530) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.13.1 The fma functions (p: 239) </li>
<li> 7.22 Type-generic math &lt;tgmath.h&gt; (p: 335-337) </li>
<li> F.9.10.1 The fma functions (p: 466) </li>
</ul>
</ul>                 <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="remainder" title="c/numeric/math/remainder"> <span class="t-lines"><span>remainder</span><span>remainderf</span><span>remainderl</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> computes signed remainder of the floating-point division operation <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="remquo" title="c/numeric/math/remquo"> <span class="t-lines"><span>remquo</span><span>remquof</span><span>remquol</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> computes signed remainder as well as the three last bits of the division operation <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/numeric/math/fma" title="cpp/numeric/math/fma">C++ documentation</a></span> for <code>fma</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/numeric/math/fma" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/fma</a>
  </p>
</div>