Skip to content

Commit

Permalink
[perf/fastrand] tidy code
Browse files Browse the repository at this point in the history
Change-Id: Ifaadf47329268c5356567f6c90fd2d641240fc7d
  • Loading branch information
jxskiss committed Nov 21, 2023
1 parent f24f1a9 commit ce02afb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 55 deletions.
4 changes: 2 additions & 2 deletions perf/fastrand/fastrand.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func Int31n(n int32) (x int32) {
prod := uint64(u32) * uint64(n)
low := uint32(prod)
if low < uint32(n) {
thresh := uint32(-n) % uint32(n)
for low < thresh {
threshold := uint32(-n) % uint32(n)
for low < threshold {
u32 = Uint32()
prod = uint64(u32) * uint64(n)
low = uint32(prod)
Expand Down
4 changes: 2 additions & 2 deletions perf/fastrand/pcg64.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func (p *PCG64) Int31n(n int32) int32 {
prod := uint64(u32) * uint64(n)
low := uint32(prod)
if low < uint32(n) {
thresh := uint32(-n) % uint32(n)
for low < thresh {
threshold := uint32(-n) % uint32(n)
for low < threshold {
u32 = p.Uint32()
prod = uint64(u32) * uint64(n)
low = uint32(prod)
Expand Down
1 change: 0 additions & 1 deletion perf/fastrand/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func Fastrandn(n uint32) uint32 {
}

func makeSeed() (ret uint64) {
ret = linkname.Runtime_fastrand64()
for ret == 0 {
ret = linkname.Runtime_fastrand64()
}
Expand Down
26 changes: 8 additions & 18 deletions perf/fastrand/std_exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,7 @@ import "math"
//
// sample = ExpFloat64() / desiredRateParameter
func ExpFloat64() float64 {
for {
v := Uint64()
j := v >> 11
i := v & 0xFF
x := float64(j) * we[i]
if j < ke[i] {
return x
}
if i == 0 {
return re - math.Log(Float64())
}
if fe[i]+Float64()*(fe[i-1]-fe[i]) < math.Exp(-x) {
return x
}
}
return execExpFloat64(globalStdSource{})
}

// ExpFloat64 returns an exponentially distributed float64 in the range
Expand All @@ -46,18 +32,22 @@ func ExpFloat64() float64 {
//
// sample = ExpFloat64() / desiredRateParameter
func (p *PCG64) ExpFloat64() float64 {
return execExpFloat64(p)
}

func execExpFloat64(source stdSource) float64 {
for {
v := p.Uint64()
v := source.Uint64()
j := v >> 11
i := v & 0xFF
x := float64(j) * we[i]
if j < ke[i] {
return x
}
if i == 0 {
return re - math.Log(p.Float64())
return re - math.Log(source.Float64())
}
if fe[i]+p.Float64()*(fe[i-1]-fe[i]) < math.Exp(-x) {
if fe[i]+source.Float64()*(fe[i-1]-fe[i]) < math.Exp(-x) {
return x
}
}
Expand Down
41 changes: 9 additions & 32 deletions perf/fastrand/std_normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,7 @@ import "math"
//
// sample = NormFloat64() * desiredStdDev + desiredMean
func NormFloat64() float64 {
for {
v := Uint64()
j := int64(v) >> 11 // Possibly negative
i := v & 0xFF
x := float64(j) * wn[i]
if absInt64(j) < kn[i] {
// This case should be hit better than 99% of the time.
return x
}

if i == 0 {
// This extra work is only required for the base strip.
for {
x = -math.Log(Float64()) * (1.0 / rn)
y := -math.Log(Float64())
if y+y >= x*x {
break
}
}
if j > 0 {
return rn + x
}
return -rn - x
}
if fn[i]+Float64()*(fn[i-1]-fn[i]) < math.Exp(-.5*x*x) {
return x
}
}
return execNormFloat64(globalStdSource{})
}

// NormFloat64 returns a normally distributed float64 in
Expand All @@ -59,8 +32,12 @@ func NormFloat64() float64 {
//
// sample = NormFloat64() * desiredStdDev + desiredMean
func (p *PCG64) NormFloat64() float64 {
return execNormFloat64(p)
}

func execNormFloat64(source stdSource) float64 {
for {
v := p.Uint64()
v := source.Uint64()
j := int64(v) >> 11 // Possibly negative
i := v & 0xFF
x := float64(j) * wn[i]
Expand All @@ -72,8 +49,8 @@ func (p *PCG64) NormFloat64() float64 {
if i == 0 {
// This extra work is only required for the base strip.
for {
x = -math.Log(p.Float64()) * (1.0 / rn)
y := -math.Log(p.Float64())
x = -math.Log(source.Float64()) * (1.0 / rn)
y := -math.Log(source.Float64())
if y+y >= x*x {
break
}
Expand All @@ -83,7 +60,7 @@ func (p *PCG64) NormFloat64() float64 {
}
return -rn - x
}
if fn[i]+p.Float64()*(fn[i-1]-fn[i]) < math.Exp(-.5*x*x) {
if fn[i]+source.Float64()*(fn[i-1]-fn[i]) < math.Exp(-.5*x*x) {
return x
}
}
Expand Down
11 changes: 11 additions & 0 deletions perf/fastrand/std_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fastrand

type stdSource interface {
Uint64() uint64
Float64() float64
}

type globalStdSource struct{}

func (_ globalStdSource) Uint64() uint64 { return Uint64() }

Check warning on line 10 in perf/fastrand/std_utils.go

View workflow job for this annotation

GitHub Actions / Lint

receiver-naming: receiver name should not be an underscore, omit the name if it is unused (revive)
func (_ globalStdSource) Float64() float64 { return Float64() }

Check warning on line 11 in perf/fastrand/std_utils.go

View workflow job for this annotation

GitHub Actions / Lint

receiver-naming: receiver name should not be an underscore, omit the name if it is unused (revive)

0 comments on commit ce02afb

Please sign in to comment.