1
0
mirror of https://github.com/golang/go synced 2024-11-19 10:04:56 -07:00

math: optimize dim and remove s390x assembly implementation

By calculating dim directly, rather than calling max, we can simplify
the generated code significantly. The compiler now reports that dim
is easily inlineable, but it can't be inlined because there is still
an assembly stub for Dim.

Since dim is now very simple I no longer think it is worth having
assembly implementations of it. I have therefore removed the s390x
assembly. Removing the other assembly for Dim is #21913.

name  old time/op  new time/op  delta
Dim   4.29ns ± 0%  3.53ns ± 0%  -17.62%  (p=0.000 n=9+8)

Change-Id: Ic38a6b51603cbc661dcdb868ecf2b1947e9f399e
Reviewed-on: https://go-review.googlesource.com/64194
Run-TryBot: Michael Munday <mike.munday@ibm.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Michael Munday 2017-09-17 17:20:35 +01:00 committed by Robert Griesemer
parent be08ddbfcd
commit b97688d112
3 changed files with 15 additions and 37 deletions

View File

@ -13,7 +13,18 @@ package math
func Dim(x, y float64) float64
func dim(x, y float64) float64 {
return max(x-y, 0)
// The special cases result in NaN after the subtraction:
// +Inf - +Inf = NaN
// -Inf - -Inf = NaN
// NaN - y = NaN
// x - NaN = NaN
v := x - y
if v <= 0 {
// v is negative or 0
return 0
}
// v is positive or NaN
return v
}
// Max returns the larger of x or y.

View File

@ -10,42 +10,6 @@
#define NaN 0x7FF8000000000001
#define NegInf 0xFFF0000000000000
// func Dim(x, y float64) float64
TEXT ·Dim(SB),NOSPLIT,$0
// (+Inf, +Inf) special case
MOVD x+0(FP), R2
MOVD y+8(FP), R3
MOVD $PosInf, R4
CMPUBNE R4, R2, dim2
CMPUBEQ R4, R3, bothInf
dim2: // (-Inf, -Inf) special case
MOVD $NegInf, R4
CMPUBNE R4, R2, dim3
CMPUBEQ R4, R3, bothInf
dim3: // (NaN, x) or (x, NaN)
MOVD $~(1<<63), R5
MOVD $PosInf, R4
AND R5, R2 // x = |x|
CMPUBLT R4, R2, isDimNaN
AND R5, R3 // y = |y|
CMPUBLT R4, R3, isDimNaN
FMOVD x+0(FP), F1
FMOVD y+8(FP), F2
FSUB F2, F1
FMOVD $(0.0), F2
FCMPU F2, F1
BGE +3(PC)
FMOVD F1, ret+16(FP)
RET
FMOVD F2, ret+16(FP)
RET
bothInf: // Dim(-Inf, -Inf) or Dim(+Inf, +Inf)
isDimNaN:
MOVD $NaN, R4
MOVD R4, ret+16(FP)
RET
// func ·Max(x, y float64) float64
TEXT ·Max(SB),NOSPLIT,$0
// +Inf special cases

View File

@ -4,6 +4,9 @@
#include "textflag.h"
TEXT ·Dim(SB),NOSPLIT,$0
BR ·dim(SB)
TEXT ·Exp2(SB),NOSPLIT,$0
BR ·exp2(SB)