From f6be78b539d0a5b81fdb68ddf02eb48eee140287 Mon Sep 17 00:00:00 2001 From: Meng Zhuo Date: Thu, 8 Jan 2026 16:17:50 +0800 Subject: [PATCH] math: add benchmark for float32/float64 conversions This CL adds benchmarks for float32/float64 conversions for following functions: - Abs - Sqrt - Round - RoundToEven - Floor - Ceil - Trunc Update #75463 Change-Id: Idb57e2e28a0fb59321e480cd676115c6e540cf4f Reviewed-on: https://go-review.googlesource.com/c/go/+/734680 LUCI-TryBot-Result: Go LUCI Reviewed-by: Junyang Shao Reviewed-by: Joel Sing Reviewed-by: David Chase --- src/math/all_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/src/math/all_test.go b/src/math/all_test.go index 4e5f451762..afd97da956 100644 --- a/src/math/all_test.go +++ b/src/math/all_test.go @@ -3336,9 +3336,10 @@ func TestFloat32Sqrt(t *testing.T) { // Storing the results in these variables prevents the compiler // from completely optimizing the benchmarked functions away. var ( - GlobalI int - GlobalB bool - GlobalF float64 + GlobalI int + GlobalB bool + GlobalF float64 + GlobalF32 float32 ) func BenchmarkAcos(b *testing.B) { @@ -3413,6 +3414,14 @@ func BenchmarkCeil(b *testing.B) { GlobalF = x } +func BenchmarkCeil32(b *testing.B) { + var x, src float32 = 0.0, 0.5 + for i := 0; i < b.N; i++ { + x = float32(Ceil(float64(src))) + } + GlobalF32 = x +} + var copysignNeg = -1.0 func BenchmarkCopysign(b *testing.B) { @@ -3512,6 +3521,7 @@ func BenchmarkExp2Go(b *testing.B) { } var absPos = .5 +var absPos32 float32 = .5 func BenchmarkAbs(b *testing.B) { x := 0.0 @@ -3522,6 +3532,15 @@ func BenchmarkAbs(b *testing.B) { } +func BenchmarkAbs32(b *testing.B) { + var x float32 = 0.0 + for i := 0; i < b.N; i++ { + x = float32(Abs(float64(absPos32))) + } + GlobalF32 = x + +} + func BenchmarkDim(b *testing.B) { x := 0.0 for i := 0; i < b.N; i++ { @@ -3538,6 +3557,14 @@ func BenchmarkFloor(b *testing.B) { GlobalF = x } +func BenchmarkFloor32(b *testing.B) { + var x, src float32 = 0.0, .5 + for i := 0; i < b.N; i++ { + x = float32(Floor(float64(src))) + } + GlobalF32 = x +} + func BenchmarkMax(b *testing.B) { x := 0.0 for i := 0; i < b.N; i++ { @@ -3749,6 +3776,7 @@ func BenchmarkPow10Neg(b *testing.B) { } var roundNeg = float64(-2.5) +var roundNeg32 float32 = -2.5 func BenchmarkRound(b *testing.B) { x := 0.0 @@ -3758,6 +3786,14 @@ func BenchmarkRound(b *testing.B) { GlobalF = x } +func BenchmarkRound32(b *testing.B) { + var x float32 = 0.0 + for i := 0; i < b.N; i++ { + x = float32(Round(float64(roundNeg32))) + } + GlobalF32 = x +} + func BenchmarkRoundToEven(b *testing.B) { x := 0.0 for i := 0; i < b.N; i++ { @@ -3766,6 +3802,14 @@ func BenchmarkRoundToEven(b *testing.B) { GlobalF = x } +func BenchmarkRoundToEven32(b *testing.B) { + var x float32 = 0.0 + for i := 0; i < b.N; i++ { + x = float32(RoundToEven(float64(roundNeg32))) + } + GlobalF32 = x +} + func BenchmarkRemainder(b *testing.B) { x := 0.0 for i := 0; i < b.N; i++ { @@ -3827,6 +3871,14 @@ func BenchmarkSqrtLatency(b *testing.B) { GlobalF = x } +func BenchmarkSqrt32Latency(b *testing.B) { + var x float32 = 10.0 + for i := 0; i < b.N; i++ { + x = float32(Sqrt(float64(x))) + } + GlobalF32 = x +} + func BenchmarkSqrtIndirectLatency(b *testing.B) { x := 10.0 f := Sqrt @@ -3889,6 +3941,14 @@ func BenchmarkTrunc(b *testing.B) { GlobalF = x } +func BenchmarkTrunc32(b *testing.B) { + var x, src float32 = 0.0, .5 + for i := 0; i < b.N; i++ { + x = float32(Trunc(float64(src))) + } + GlobalF32 = x +} + func BenchmarkY0(b *testing.B) { x := 0.0 for i := 0; i < b.N; i++ {