Skip to content

Commit 94b6011

Browse files
committed
spec: clarify use of fused-floating point operations
Added a paragraph and examples explaining when an implementation may use fused floating-point operations (such as FMA) and how to prevent operation fusion. For #17895. Change-Id: I64c9559fc1097e597525caca420cfa7032d67014 Reviewed-on: https://go-review.googlesource.com/40391 Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-by: Rob Pike <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 7d4cca0 commit 94b6011

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

doc/go_spec.html

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Version of April 12, 2017",
3+
"Subtitle": "Version of April 17, 2017",
44
"Path": "/ref/spec"
55
}-->
66

@@ -3582,6 +3582,33 @@ <h4 id="Floating_point_operators">Floating-point operators</h4>
35823582
occurs is implementation-specific.
35833583
</p>
35843584

3585+
<p>
3586+
An implementation may combine multiple floating-point operations into a single
3587+
fused operation, possibly across statements, and produce a result that differs
3588+
from the value obtained by executing and rounding the instructions individually.
3589+
A floating-point type <a href="#Conversions">conversion</a> explicitly rounds to
3590+
the precision of the target type, preventing fusion that would discard that rounding.
3591+
</p>
3592+
3593+
<p>
3594+
For instance, some architectures provide a "fused multiply and add" (FMA) instruction
3595+
that computes <code>x*y + z</code> without rounding the intermediate result <code>x*y</code>.
3596+
These examples show when a Go implementation can use that instruction:
3597+
</p>
3598+
3599+
<pre>
3600+
// FMA allowed for computing r, because x*y is not explicitly rounded:
3601+
r = x*y + z
3602+
r = z; r += x*y
3603+
t = x*y; r = t + z
3604+
*p = x*y; r = *p + z
3605+
r = x*y + float64(z)
3606+
3607+
// FMA disallowed for computing r, because it would omit rounding of x*y:
3608+
r = float64(x*y) + z
3609+
r = z; r += float64(x*y)
3610+
t = float64(x*y); r = t + z
3611+
</pre>
35853612

35863613
<h4 id="String_concatenation">String concatenation</h4>
35873614

@@ -3640,7 +3667,7 @@ <h3 id="Comparison_operators">Comparison operators</h3>
36403667
</li>
36413668

36423669
<li>
3643-
Floating point values are comparable and ordered,
3670+
Floating-point values are comparable and ordered,
36443671
as defined by the IEEE-754 standard.
36453672
</li>
36463673

0 commit comments

Comments
 (0)