Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Commit

Permalink
Add 32-bit "exact int" optimization
Browse files Browse the repository at this point in the history
name                          old time/op  new time/op  delta
AppendFloat32/0e+00-12        2.97ns ± 1%  2.96ns ± 1%     ~     (p=0.738 n=5+5)
AppendFloat32/1e+00-12        36.5ns ± 1%  13.1ns ± 0%  -64.04%  (p=0.008 n=5+5)
AppendFloat32/3e-01-12        32.3ns ± 2%  32.6ns ± 2%     ~     (p=0.214 n=5+5)
AppendFloat32/1e+06-12        36.3ns ± 1%  17.7ns ± 1%  -51.40%  (p=0.008 n=5+5)
AppendFloat32/-1.2345e+02-12  33.6ns ± 1%  34.3ns ± 1%   +2.08%  (p=0.024 n=5+5)
  • Loading branch information
cespare committed Jan 12, 2019
1 parent 1f734ae commit bf6487c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
7 changes: 4 additions & 3 deletions ryu.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ func AppendFloat32(b []byte, f float32) []byte {
return appendSpecial(b, neg, exp == 0, mant == 0)
}

// FIXME: add "exact int" optimization.

d := float32ToDecimal(mant, exp)
d, ok := float32ToDecimalExactInt(mant, exp)
if !ok {
d = float32ToDecimal(mant, exp)
}
return d.append(b, neg)
}

Expand Down
18 changes: 18 additions & 0 deletions ryu32.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ func (d dec32) append(b []byte, neg bool) []byte {
return b
}

func float32ToDecimalExactInt(mant, exp uint32) (d dec32, ok bool) {
e := exp - bias32
if e > mantBits32 {
return d, false
}
shift := mantBits32 - e
mant |= 1 << mantBits32 // implicit 1
d.m = mant >> shift
if d.m<<shift != mant {
return d, false
}
for d.m%10 == 0 {
d.m /= 10
d.e++
}
return d, true
}

func float32ToDecimal(mant, exp uint32) dec32 {
var e2 int32
var m2 uint32
Expand Down

0 comments on commit bf6487c

Please sign in to comment.