diff options
Diffstat (limited to 'src/cmd/compile/internal/test/float_test.go')
| -rw-r--r-- | src/cmd/compile/internal/test/float_test.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/test/float_test.go b/src/cmd/compile/internal/test/float_test.go index 7a5e27870f..00735e3cb1 100644 --- a/src/cmd/compile/internal/test/float_test.go +++ b/src/cmd/compile/internal/test/float_test.go @@ -727,6 +727,65 @@ func TestFusedNaNChecks32(t *testing.T) { } } +// minNormal64 is the smallest float64 value that is not subnormal. +const minNormal64 = 2.2250738585072014e-308 + +//go:noinline +func isAbsLessThanMinNormal64(x float64) bool { + return math.Abs(x) < minNormal64 +} + +//go:noinline +func isLessThanMinNormal64(x float64) bool { + return x < minNormal64 +} + +//go:noinline +func isGreaterThanNegMinNormal64(x float64) bool { + return x > -minNormal64 +} + +//go:noinline +func isGreaterThanOrEqualToMinNormal64(x float64) bool { + return math.Abs(x) >= minNormal64 +} + +func TestSubnormalComparisons(t *testing.T) { + tests := []struct { + value float64 + isAbsLessThanMinNormal bool + isPositive bool + isNegative bool + isNaN bool + }{ + {value: math.Inf(1), isPositive: true}, + {value: math.MaxFloat64, isPositive: true}, + {value: math.Inf(-1), isNegative: true}, + {value: -math.MaxFloat64, isNegative: true}, + {value: math.NaN(), isNaN: true}, + {value: minNormal64, isPositive: true}, + {value: minNormal64 / 2, isAbsLessThanMinNormal: true, isPositive: true}, + {value: -minNormal64, isNegative: true}, + {value: -minNormal64 / 2, isAbsLessThanMinNormal: true, isNegative: true}, + {value: 0, isAbsLessThanMinNormal: true, isPositive: true}, + {value: math.Copysign(0, -1), isAbsLessThanMinNormal: true, isNegative: true}, + } + + check := func(name string, f func(x float64) bool, value float64, want bool) { + got := f(value) + if got != want { + t.Errorf("%v(%g): want %v, got %v", name, value, want, got) + } + } + + for _, test := range tests { + check("isAbsLessThanMinNormal64", isAbsLessThanMinNormal64, test.value, test.isAbsLessThanMinNormal) + check("isLessThanMinNormal64", isLessThanMinNormal64, test.value, test.isAbsLessThanMinNormal || test.isNegative) + check("isGreaterThanNegMinNormal64", isGreaterThanNegMinNormal64, test.value, test.isAbsLessThanMinNormal || test.isPositive) + check("isGreaterThanOrEqualToMinNormal64", isGreaterThanOrEqualToMinNormal64, test.value, !test.isAbsLessThanMinNormal && !test.isNaN) + } +} + var sinkFloat float64 func BenchmarkMul2(b *testing.B) { |
