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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Joel Sing <joel@sing.id.au>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Meng Zhuo
2026-01-08 16:17:50 +08:00
parent d51d4d75a6
commit f6be78b539

View File

@@ -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++ {