diff options
author | fanzha02 <fannie.zhang@arm.com> | 2020-12-07 19:15:15 +0800 |
---|---|---|
committer | fannie zhang <Fannie.Zhang@arm.com> | 2021-03-02 06:38:07 +0000 |
commit | 2b50ab2aee75d3c361fcd1eb39e830e2e73056b6 (patch) | |
tree | 86c8adabbab0b962827eebb995e054fc5ac6098d /src/math | |
parent | ebb92dfed96fadb3c563ff11cead85bbb7536793 (diff) | |
download | go-2b50ab2aee75d3c361fcd1eb39e830e2e73056b6.tar.gz go-2b50ab2aee75d3c361fcd1eb39e830e2e73056b6.zip |
cmd/compile: optimize single-precision floating point square root
Add generic rule to rewrite the single-precision square root expression
with one single-precision instruction. The optimization will reduce two
times of precision converting between double-precision and single-precision.
On arm64 flatform.
previous:
FCVTSD F0, F0
FSQRTD F0, F0
FCVTDS F0, F0
optimized:
FSQRTS S0, S0
And this patch adds the test case to check the correctness.
This patch refers to CL 241877, contributed by Alice Xu
(dianhong.xu@arm.com)
Change-Id: I6de5d02281c693017ac4bd4c10963dd55989bd7e
Reviewed-on: https://go-review.googlesource.com/c/go/+/276873
Trust: fannie zhang <Fannie.Zhang@arm.com>
Run-TryBot: fannie zhang <Fannie.Zhang@arm.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/all_test.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/math/all_test.go b/src/math/all_test.go index 3aae0373c7..d154457999 100644 --- a/src/math/all_test.go +++ b/src/math/all_test.go @@ -2067,6 +2067,21 @@ var fmaC = []struct{ x, y, z, want float64 }{ {-7.751454006381804e-05, 5.588653777189071e-308, -2.2207280111272877e-308, -2.2211612130544025e-308}, } +var sqrt32 = []float32{ + 0, + float32(Copysign(0, -1)), + float32(NaN()), + float32(Inf(1)), + float32(Inf(-1)), + 1, + 2, + -2, + 4.9790119248836735e+00, + 7.7388724745781045e+00, + -2.7688005719200159e-01, + -5.0106036182710749e+00, +} + func tolerance(a, b, e float64) bool { // Multiplying by e here can underflow denormal values to zero. // Check a==b so that at least if a and b are small and identical @@ -3181,6 +3196,25 @@ func TestFloatMinMax(t *testing.T) { } } +var indirectSqrt = Sqrt + +// TestFloat32Sqrt checks the correctness of the float32 square root optimization result. +func TestFloat32Sqrt(t *testing.T) { + for _, v := range sqrt32 { + want := float32(indirectSqrt(float64(v))) + got := float32(Sqrt(float64(v))) + if IsNaN(float64(want)) { + if !IsNaN(float64(got)) { + t.Errorf("got=%#v want=NaN, v=%#v", got, v) + } + continue + } + if got != want { + t.Errorf("got=%#v want=%#v, v=%#v", got, want, v) + } + } +} + // Benchmarks // Global exported variables are used to store the |