From: Michael Munday Date: Sun, 17 Sep 2017 16:20:35 +0000 (+0100) Subject: math: optimize dim and remove s390x assembly implementation X-Git-Tag: go1.10beta1~532 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=b97688d112216c94791b60a846384561974399b4;p=gostls13.git 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 TryBot-Result: Gobot Gobot Reviewed-by: Robert Griesemer --- diff --git a/src/math/dim.go b/src/math/dim.go index 1c634d415f..ac0aa869d7 100644 --- a/src/math/dim.go +++ b/src/math/dim.go @@ -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. diff --git a/src/math/dim_s390x.s b/src/math/dim_s390x.s index 503d2611f8..74fdd75b9c 100644 --- a/src/math/dim_s390x.s +++ b/src/math/dim_s390x.s @@ -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 diff --git a/src/math/stubs_s390x.s b/src/math/stubs_s390x.s index 889e248db9..4dceddac63 100644 --- a/src/math/stubs_s390x.s +++ b/src/math/stubs_s390x.s @@ -4,6 +4,9 @@ #include "textflag.h" +TEXT ·Dim(SB),NOSPLIT,$0 + BR ·dim(SB) + TEXT ·Exp2(SB),NOSPLIT,$0 BR ·exp2(SB)