]> Cypherpunks repositories - gostls13.git/commitdiff
math: optimize dim and remove s390x assembly implementation
authorMichael Munday <mike.munday@ibm.com>
Sun, 17 Sep 2017 16:20:35 +0000 (17:20 +0100)
committerRobert Griesemer <gri@golang.org>
Mon, 30 Oct 2017 19:05:51 +0000 (19:05 +0000)
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>
src/math/dim.go
src/math/dim_s390x.s
src/math/stubs_s390x.s

index 1c634d415f17d27b2deb852423ff8e30e5e4c283..ac0aa869d731e3086dabc37082ac861852d20f5d 100644 (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.
index 503d2611f80b58818951447309d89b0d8dbcbfa2..74fdd75b9c9d27f94e1ce4cde6f911dd3beea4ec 100644 (file)
 #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
index 889e248db9a552aa1198184aa704d1399e1c3379..4dceddac6323f52001de9023c2d8a9f24841c931 100644 (file)
@@ -4,6 +4,9 @@
 
 #include "textflag.h"
 
+TEXT ·Dim(SB),NOSPLIT,$0
+       BR ·dim(SB)
+
 TEXT ·Exp2(SB),NOSPLIT,$0
        BR ·exp2(SB)