Fix #10261 ABS(int64)

Added inline bit-shift absolute value for int64 type as per:
http://cavaliercoder.com/blog/optimized-abs-for-int64-in-go.html
Updated implementations in Iterator and the continuous query service
pull/10276/head
Tim Brooks 2018-09-11 21:21:36 +01:00
parent a940ebd45a
commit 343ce42238
4 changed files with 9 additions and 10 deletions

View File

@ -1307,10 +1307,8 @@ func (p reverseStringSlice) Less(i, j int) bool { return p[i] > p[j] }
func (p reverseStringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func abs(v int64) int64 {
if v < 0 {
return -v
}
return v
sign := v >> 63
return (v ^ sign) - sign
}
// IteratorEncoder is an encoder for encoding an iterator's points to w.

View File

@ -98,7 +98,10 @@ func (v MathValuer) Call(name string, args []interface{}) (interface{}, bool) {
switch arg0 := arg0.(type) {
case float64:
return math.Abs(arg0), true
case int64, uint64:
case int64:
sign := arg0 >> 63
return (arg0 ^ sign) - sign, true
case uint64:
return arg0, true
default:
return nil, true

View File

@ -144,7 +144,7 @@ func TestMathValuer_Call(t *testing.T) {
{s: `abs(f)`, values: values{"f": float64(2)}, exp: float64(2)},
{s: `abs(f)`, values: values{"f": float64(-2)}, exp: float64(2)},
{s: `abs(i)`, values: values{"i": int64(2)}, exp: int64(2)},
{s: `abs(i)`, values: values{"i": int64(-2)}, exp: int64(-2)},
{s: `abs(i)`, values: values{"i": int64(-2)}, exp: int64(2)},
{s: `abs(u)`, values: values{"u": uint64(2)}, exp: uint64(2)},
{s: `sin(f)`, values: values{"f": math.Pi / 2}, exp: math.Sin(math.Pi / 2)},
{s: `sin(i)`, values: values{"i": int64(2)}, exp: math.Sin(2)},

View File

@ -572,8 +572,6 @@ func zone(ts time.Time) int64 {
}
func abs(v int64) int64 {
if v < 0 {
return -v
}
return v
sign := v >> 63
return (v ^ sign) - sign
}