Skip to content

Commit 83e288f

Browse files
committed
cmd/compile: prevent constant folding of +/- when result is NaN
Missed as part of CL 221790. It isn't just * and / that can make NaNs. Update #36400 Fixes #38359 Change-Id: I3fa562f772fe03b510793a6dc0cf6189c0c3e652 Reviewed-on: https://go-review.googlesource.com/c/go/+/227860 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Alberto Donizetti <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]>
1 parent 37470c0 commit 83e288f

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/cmd/compile/internal/ssa/gen/generic.rules

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,17 @@
103103
(Add16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c+d))])
104104
(Add32 (Const32 [c]) (Const32 [d])) -> (Const32 [int64(int32(c+d))])
105105
(Add64 (Const64 [c]) (Const64 [d])) -> (Const64 [c+d])
106-
(Add32F (Const32F [c]) (Const32F [d])) -> (Const32F [auxFrom32F(auxTo32F(c) + auxTo32F(d))])
107-
(Add64F (Const64F [c]) (Const64F [d])) -> (Const64F [auxFrom64F(auxTo64F(c) + auxTo64F(d))])
106+
(Add32F (Const32F [c]) (Const32F [d])) && !math.IsNaN(float64(auxTo32F(c) + auxTo32F(d))) -> (Const32F [auxFrom32F(auxTo32F(c) + auxTo32F(d))])
107+
(Add64F (Const64F [c]) (Const64F [d])) && !math.IsNaN(auxTo64F(c) + auxTo64F(d)) -> (Const64F [auxFrom64F(auxTo64F(c) + auxTo64F(d))])
108108
(AddPtr <t> x (Const64 [c])) -> (OffPtr <t> x [c])
109109
(AddPtr <t> x (Const32 [c])) -> (OffPtr <t> x [c])
110110

111111
(Sub8 (Const8 [c]) (Const8 [d])) -> (Const8 [int64(int8(c-d))])
112112
(Sub16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c-d))])
113113
(Sub32 (Const32 [c]) (Const32 [d])) -> (Const32 [int64(int32(c-d))])
114114
(Sub64 (Const64 [c]) (Const64 [d])) -> (Const64 [c-d])
115-
(Sub32F (Const32F [c]) (Const32F [d])) -> (Const32F [auxFrom32F(auxTo32F(c) - auxTo32F(d))])
116-
(Sub64F (Const64F [c]) (Const64F [d])) -> (Const64F [auxFrom64F(auxTo64F(c) - auxTo64F(d))])
115+
(Sub32F (Const32F [c]) (Const32F [d])) && !math.IsNaN(float64(auxTo32F(c) - auxTo32F(d))) -> (Const32F [auxFrom32F(auxTo32F(c) - auxTo32F(d))])
116+
(Sub64F (Const64F [c]) (Const64F [d])) && !math.IsNaN(auxTo64F(c) - auxTo64F(d)) -> (Const64F [auxFrom64F(auxTo64F(c) - auxTo64F(d))])
117117

118118
(Mul8 (Const8 [c]) (Const8 [d])) -> (Const8 [int64(int8(c*d))])
119119
(Mul16 (Const16 [c]) (Const16 [d])) -> (Const16 [int64(int16(c*d))])

src/cmd/compile/internal/ssa/rewritegeneric.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixedbugs/issue38359.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// compile
2+
3+
// Copyright 2020 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Make sure NaN-NaN compiles correctly.
8+
9+
package p
10+
11+
func f() {
12+
var st struct {
13+
f float64
14+
_, _ string
15+
}
16+
17+
f := 1e308
18+
st.f = 2*f - 2*f
19+
}

0 commit comments

Comments
 (0)