]> Cypherpunks repositories - gostls13.git/commitdiff
strconv: simplify (*extFloat).Multiply using math/bits.Mul64
authorRémy Oudompheng <remyoudompheng@gmail.com>
Sun, 13 Jan 2019 17:10:19 +0000 (18:10 +0100)
committerRobert Griesemer <gri@golang.org>
Mon, 4 Mar 2019 22:25:21 +0000 (22:25 +0000)
This method was using a handwritten long multiplication of uint64s.
Since implementation of #24813 we can remove it and replace it
by Mul64 from math/bits.

This brings a small speedup for 64-bit platforms.

Benchmarks on Haswell Celeron 2955U.

benchmark                                  old ns/op     new ns/op     delta
BenchmarkAppendFloat/Decimal-2             127           127           +0.00%
BenchmarkAppendFloat/Float-2               340           317           -6.76%
BenchmarkAppendFloat/Exp-2                 258           233           -9.69%
BenchmarkAppendFloat/NegExp-2              256           231           -9.77%
BenchmarkAppendFloat/Big-2                 402           375           -6.72%
BenchmarkAppendFloat/BinaryExp-2           113           114           +0.88%
BenchmarkAppendFloat/32Integer-2           125           125           +0.00%
BenchmarkAppendFloat/32ExactFraction-2     274           249           -9.12%
BenchmarkAppendFloat/32Point-2             339           317           -6.49%
BenchmarkAppendFloat/32Exp-2               255           229           -10.20%
BenchmarkAppendFloat/32NegExp-2            254           229           -9.84%
BenchmarkAppendFloat/64Fixed1-2            165           154           -6.67%
BenchmarkAppendFloat/64Fixed2-2            184           176           -4.35%
BenchmarkAppendFloat/64Fixed3-2            168           158           -5.95%
BenchmarkAppendFloat/64Fixed4-2            187           177           -5.35%
BenchmarkAppendFloat/Slowpath64-2          84977         84883         -0.11%

Change-Id: If05784e856289b3b7bf136567882e7ee10234756
Reviewed-on: https://go-review.googlesource.com/c/go/+/157717
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/strconv/extfloat.go

index 32d3340f5f8c33e44f700fa65be6b057e5c9cb4b..2a2dd7a4084457f18bdf4cf5e3cf6a8e78b990dd 100644 (file)
@@ -214,20 +214,9 @@ func (f *extFloat) Normalize() uint {
 // Multiply sets f to the product f*g: the result is correctly rounded,
 // but not normalized.
 func (f *extFloat) Multiply(g extFloat) {
-       fhi, flo := f.mant>>32, uint64(uint32(f.mant))
-       ghi, glo := g.mant>>32, uint64(uint32(g.mant))
-
-       // Cross products.
-       cross1 := fhi * glo
-       cross2 := flo * ghi
-
-       // f.mant*g.mant is fhi*ghi << 64 + (cross1+cross2) << 32 + flo*glo
-       f.mant = fhi*ghi + (cross1 >> 32) + (cross2 >> 32)
-       rem := uint64(uint32(cross1)) + uint64(uint32(cross2)) + ((flo * glo) >> 32)
+       hi, lo := bits.Mul64(f.mant, g.mant)
        // Round up.
-       rem += (1 << 31)
-
-       f.mant += (rem >> 32)
+       f.mant = hi + (lo >> 63)
        f.exp = f.exp + g.exp + 64
 }