aboutsummaryrefslogtreecommitdiff
path: root/test/codegen
diff options
context:
space:
mode:
authorKeith Randall <khr@golang.org>2020-01-06 11:23:08 -0800
committerKeith Randall <khr@golang.org>2020-02-25 02:21:53 +0000
commit2aa7c6c5488d3ef5d3eeb53488f74d39facd9301 (patch)
tree8d5a86b0d5996d4f5fa00bb772e00165642b951e /test/codegen
parent0fb1a49c1ae0e9e587c74947d6d6c3fe92c161df (diff)
downloadgo-2aa7c6c5488d3ef5d3eeb53488f74d39facd9301.tar.xz
cmd/compile: don't allow NaNs in floating-point constant ops
We store 32-bit floating point constants in a 64-bit field, by converting that 32-bit float to 64-bit float to store it, and convert it back to use it. That works for *almost* all floating-point constants. The exception is signaling NaNs. The round trip described above means we can't represent a 32-bit signaling NaN, because conversions strip the signaling bit. To fix this issue, just forbid NaNs as floating-point constants in SSA form. This shouldn't affect any real-world code, as people seldom constant-propagate NaNs (except in test code). Additionally, NaNs are somewhat underspecified (which of the many NaNs do you get when dividing 0/0?), so when cross-compiling there's a danger of using the compiler machine's NaN regime for some math, and the target machine's NaN regime for other math. Better to use the target machine's NaN regime always. This has been a bug since 1.10, and there's an easy workaround (declare a global varaible containing the signaling NaN pattern, and use that as the argument to math.Float32frombits) so we'll fix it in 1.15. Fixes #36400 Update #36399 Change-Id: Icf155e743281560eda2eed953d19a829552ccfda Reviewed-on: https://go-review.googlesource.com/c/go/+/213477 Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
Diffstat (limited to 'test/codegen')
-rw-r--r--test/codegen/math.go33
1 files changed, 31 insertions, 2 deletions
diff --git a/test/codegen/math.go b/test/codegen/math.go
index 80e5d60d96..1ebfda0405 100644
--- a/test/codegen/math.go
+++ b/test/codegen/math.go
@@ -151,13 +151,13 @@ func toFloat32(u32 uint32) float32 {
func constantCheck64() bool {
// amd64:"MOVB\t[$]0",-"FCMP",-"MOVB\t[$]1"
// s390x:"MOV(B|BZ|D)\t[$]0,",-"FCMPU",-"MOV(B|BZ|D)\t[$]1,"
- return 0.5 == float64(uint32(1)) || 1.5 > float64(uint64(1<<63)) || math.NaN() == math.NaN()
+ return 0.5 == float64(uint32(1)) || 1.5 > float64(uint64(1<<63))
}
func constantCheck32() bool {
// amd64:"MOVB\t[$]1",-"FCMP",-"MOVB\t[$]0"
// s390x:"MOV(B|BZ|D)\t[$]1,",-"FCMPU",-"MOV(B|BZ|D)\t[$]0,"
- return float32(0.5) <= float32(int64(1)) && float32(1.5) >= float32(int32(-1<<31)) && float32(math.NaN()) != float32(math.NaN())
+ return float32(0.5) <= float32(int64(1)) && float32(1.5) >= float32(int32(-1<<31))
}
// Test that integer constants are converted to floating point constants
@@ -186,3 +186,32 @@ func constantConvertInt32(x uint32) uint32 {
}
return x
}
+
+func nanGenerate64() float64 {
+ // Test to make sure we don't generate a NaN while constant propagating.
+ // See issue 36400.
+ zero := 0.0
+ // amd64:-"DIVSD"
+ inf := 1 / zero // +inf. We can constant propagate this one.
+ negone := -1.0
+
+ // amd64:"DIVSD"
+ z0 := zero / zero
+ // amd64:"MULSD"
+ z1 := zero * inf
+ // amd64:"SQRTSD"
+ z2 := math.Sqrt(negone)
+ return z0 + z1 + z2
+}
+
+func nanGenerate32() float32 {
+ zero := float32(0.0)
+ // amd64:-"DIVSS"
+ inf := 1 / zero // +inf. We can constant propagate this one.
+
+ // amd64:"DIVSS"
+ z0 := zero / zero
+ // amd64:"MULSS"
+ z1 := zero * inf
+ return z0 + z1
+}