Skip to content

Commit 7ffbea9

Browse files
committed
reflect: when Converting between float32s, don't lose signal NaNs
Trying this CL again, with a test that skips 387. When converting from float32->float64->float32, any signal NaNs get converted to quiet NaNs. Avoid that so using reflect.Value.Convert between two float32 types keeps the signal bit of NaNs. Skip the test on 387. I don't see any sane way of ensuring that a float load + float store is faithful on that platform. Fixes #36400 Change-Id: Ic316c74ddc155632e40424e207375b5d50dcd853 Reviewed-on: https://go-review.googlesource.com/c/go/+/221792 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Josh Bleecher Snyder <[email protected]>
1 parent 8e6a8d9 commit 7ffbea9

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/reflect/all_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4163,6 +4163,37 @@ func TestConvert(t *testing.T) {
41634163
}
41644164
}
41654165

4166+
var gFloat32 float32
4167+
4168+
func TestConvertNaNs(t *testing.T) {
4169+
const snan uint32 = 0x7f800001
4170+
4171+
// Test to see if a store followed by a load of a signaling NaN
4172+
// maintains the signaling bit. The only platform known to fail
4173+
// this test is 386,GO386=387. The real test below will always fail
4174+
// if the platform can't even store+load a float without mucking
4175+
// with the bits.
4176+
gFloat32 = math.Float32frombits(snan)
4177+
runtime.Gosched() // make sure we don't optimize the store/load away
4178+
r := math.Float32bits(gFloat32)
4179+
if r != snan {
4180+
// This should only happen on 386,GO386=387. We have no way to
4181+
// test for 387, so we just make sure we're at least on 386.
4182+
if runtime.GOARCH != "386" {
4183+
t.Errorf("store/load of sNaN not faithful")
4184+
}
4185+
t.Skip("skipping test, float store+load not faithful")
4186+
}
4187+
4188+
type myFloat32 float32
4189+
x := V(myFloat32(math.Float32frombits(snan)))
4190+
y := x.Convert(TypeOf(float32(0)))
4191+
z := y.Interface().(float32)
4192+
if got := math.Float32bits(z); got != snan {
4193+
t.Errorf("signaling nan conversion got %x, want %x", got, snan)
4194+
}
4195+
}
4196+
41664197
type ComparableStruct struct {
41674198
X int
41684199
}

src/reflect/value.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,6 +2541,14 @@ func makeFloat(f flag, v float64, t Type) Value {
25412541
return Value{typ, ptr, f | flagIndir | flag(typ.Kind())}
25422542
}
25432543

2544+
// makeFloat returns a Value of type t equal to v, where t is a float32 type.
2545+
func makeFloat32(f flag, v float32, t Type) Value {
2546+
typ := t.common()
2547+
ptr := unsafe_New(typ)
2548+
*(*float32)(ptr) = v
2549+
return Value{typ, ptr, f | flagIndir | flag(typ.Kind())}
2550+
}
2551+
25442552
// makeComplex returns a Value of type t equal to v (possibly truncated to complex64),
25452553
// where t is a complex64 or complex128 type.
25462554
func makeComplex(f flag, v complex128, t Type) Value {
@@ -2613,6 +2621,12 @@ func cvtUintFloat(v Value, t Type) Value {
26132621

26142622
// convertOp: floatXX -> floatXX
26152623
func cvtFloat(v Value, t Type) Value {
2624+
if v.Type().Kind() == Float32 && t.Kind() == Float32 {
2625+
// Don't do any conversion if both types have underlying type float32.
2626+
// This avoids converting to float64 and back, which will
2627+
// convert a signaling NaN to a quiet NaN. See issue 36400.
2628+
return makeFloat32(v.flag.ro(), *(*float32)(v.ptr), t)
2629+
}
26162630
return makeFloat(v.flag.ro(), v.Float(), t)
26172631
}
26182632

0 commit comments

Comments
 (0)