Skip to content

Commit

Permalink
math: Add Round function (ties away from zero)
Browse files Browse the repository at this point in the history
This function avoids subtle faults found in many ad-hoc implementations,
and is simple enough to be inlined by the compiler.

Fixes #20100

Change-Id: Ib320254e9b1f1f798c6ef906b116f63bc29e8d08
Reviewed-on: https://go-review.googlesource.com/43652
Reviewed-by: Robert Griesemer <[email protected]>
  • Loading branch information
mpx authored and griesemer committed Sep 2, 2017
1 parent dbe3522 commit 03c3bb5
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/math/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,18 @@ var remainder = []float64{
8.734595415957246977711748e-01,
1.314075231424398637614104e+00,
}
var round = []float64{
5,
8,
Copysign(0, -1),
-5,
10,
3,
5,
3,
2,
-9,
}
var signbit = []bool{
false,
false,
Expand Down Expand Up @@ -1755,6 +1767,20 @@ var pow10SC = []float64{
Inf(1), // pow10(MaxInt32)
}

var vfroundSC = [][2]float64{
{0, 0},
{1.390671161567e-309, 0}, // denormal
{0.49999999999999994, 0}, // 0.5-epsilon
{0.5, 1},
{0.5000000000000001, 1}, // 0.5+epsilon
{-1.5, -2},
{NaN(), NaN()},
{Inf(1), Inf(1)},
{2251799813685249.5, 2251799813685250}, // 1 bit fraction
{4503599627370495.5, 4503599627370496}, // 1 bit fraction, rounding to 0 bit fraction
{4503599627370497, 4503599627370497}, // large integer
}

var vfsignbitSC = []float64{
Inf(-1),
Copysign(0, -1),
Expand Down Expand Up @@ -2713,6 +2739,19 @@ func TestRemainder(t *testing.T) {
}
}

func TestRound(t *testing.T) {
for i := 0; i < len(vf); i++ {
if f := Round(vf[i]); !alike(round[i], f) {
t.Errorf("Round(%g) = %g, want %g", vf[i], f, round[i])
}
}
for i := 0; i < len(vfroundSC); i++ {
if f := Round(vfroundSC[i][0]); !alike(vfroundSC[i][1], f) {
t.Errorf("Round(%g) = %g, want %g", vfroundSC[i][0], f, vfroundSC[i][1])
}
}
}

func TestSignbit(t *testing.T) {
for i := 0; i < len(vf); i++ {
if f := Signbit(vf[i]); signbit[i] != f {
Expand Down Expand Up @@ -3360,6 +3399,16 @@ func BenchmarkPow10Neg(b *testing.B) {
GlobalF = x
}

var roundNeg = float64(-2.5)

func BenchmarkRound(b *testing.B) {
x := 0.0
for i := 0; i < b.N; i++ {
x = Round(roundNeg)
}
GlobalF = x
}

func BenchmarkRemainder(b *testing.B) {
x := 0.0
for i := 0; i < b.N; i++ {
Expand Down
45 changes: 44 additions & 1 deletion src/math/floor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2009-2010 The Go Authors. All rights reserved.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -54,3 +54,46 @@ func trunc(x float64) float64 {
d, _ := Modf(x)
return d
}

// Round returns the nearest integer, rounding half away from zero.
//
// Special cases are:
// Round(±0) = ±0
// Round(±Inf) = ±Inf
// Round(NaN) = NaN
func Round(x float64) float64 {
// Round is a faster implementation of:
//
// func Round(x float64) float64 {
// t := Trunc(x)
// if Abs(x-t) >= 0.5 {
// return t + Copysign(1, x)
// }
// return t
// }
const (
signMask = 1 << 63
fracMask = 1<<shift - 1
half = 1 << (shift - 1)
one = bias << shift
)

bits := Float64bits(x)
e := uint(bits>>shift) & mask
if e < bias {
// Round abs(x) < 1 including denormals.
bits &= signMask // +-0
if e == bias-1 {
bits |= one // +-1
}
} else if e < bias+shift {
// Round any abs(x) >= 1 containing a fractional component [0,1).
//
// Numbers with larger exponents are returned unchanged since they
// must be either an integer, infinity, or NaN.
e -= bias
bits += half >> e
bits &^= fracMask >> e
}
return Float64frombits(bits)
}

5 comments on commit 03c3bb5

@dansouza
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can someone ELI5 why can't we just use native processor instructions for some of these?

@randall77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dansouza We already use a native instruction for the S390X backend. Other backends are welcome to do so also.

We can't naively use amd64's ROUND instructions because we assume only SSE2. ROUND[SP][SD] are SSE4.1. So to use them we'd need to condition its use on a CPUID check and have a fallback path.

@dansouza
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @randall77, thank you for taking the time to explain it. Are there already mechanisms in the compiler to, based on CPUID, change the pointers to certain functions to be used just once at bootstrap, instead of calling and branching on CPUID everytime Math.Round is called?

For example, when you start a Go program, a bootstrap routine that runs before main() rewrites some function table based on CPUID parameters - if the current CPU has SSE4.1, we change the address of the 'Math.Round' function in the ELF symbol table to an optimized version that uses ROUND(SP|SD), otherwise keep the fallback. Is that a thing already?

@randall77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we do that. See alginit in src/runtime/alg.go.

You can also just generate the conditional code and fallback. We do it for POPCOUNT, which is also not SSE2. See src/cmd/compile/internal/gc/ssa.go, search for popcnt.

@dansouza
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@randall77 thank you for the pointers, I'll check it out and add this optimization.

Please sign in to comment.