Skip to content

Commit b97688d

Browse files
mundaymgriesemer
authored andcommitted
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent be08ddb commit b97688d

File tree

3 files changed

+15
-37
lines changed

3 files changed

+15
-37
lines changed

src/math/dim.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,18 @@ package math
1313
func Dim(x, y float64) float64
1414

1515
func dim(x, y float64) float64 {
16-
return max(x-y, 0)
16+
// The special cases result in NaN after the subtraction:
17+
// +Inf - +Inf = NaN
18+
// -Inf - -Inf = NaN
19+
// NaN - y = NaN
20+
// x - NaN = NaN
21+
v := x - y
22+
if v <= 0 {
23+
// v is negative or 0
24+
return 0
25+
}
26+
// v is positive or NaN
27+
return v
1728
}
1829

1930
// Max returns the larger of x or y.

src/math/dim_s390x.s

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,6 @@
1010
#define NaN 0x7FF8000000000001
1111
#define NegInf 0xFFF0000000000000
1212

13-
// func Dim(x, y float64) float64
14-
TEXT ·Dim(SB),NOSPLIT,$0
15-
// (+Inf, +Inf) special case
16-
MOVD x+0(FP), R2
17-
MOVD y+8(FP), R3
18-
MOVD $PosInf, R4
19-
CMPUBNE R4, R2, dim2
20-
CMPUBEQ R4, R3, bothInf
21-
dim2: // (-Inf, -Inf) special case
22-
MOVD $NegInf, R4
23-
CMPUBNE R4, R2, dim3
24-
CMPUBEQ R4, R3, bothInf
25-
dim3: // (NaN, x) or (x, NaN)
26-
MOVD $~(1<<63), R5
27-
MOVD $PosInf, R4
28-
AND R5, R2 // x = |x|
29-
CMPUBLT R4, R2, isDimNaN
30-
AND R5, R3 // y = |y|
31-
CMPUBLT R4, R3, isDimNaN
32-
33-
FMOVD x+0(FP), F1
34-
FMOVD y+8(FP), F2
35-
FSUB F2, F1
36-
FMOVD $(0.0), F2
37-
FCMPU F2, F1
38-
BGE +3(PC)
39-
FMOVD F1, ret+16(FP)
40-
RET
41-
FMOVD F2, ret+16(FP)
42-
RET
43-
bothInf: // Dim(-Inf, -Inf) or Dim(+Inf, +Inf)
44-
isDimNaN:
45-
MOVD $NaN, R4
46-
MOVD R4, ret+16(FP)
47-
RET
48-
4913
// func ·Max(x, y float64) float64
5014
TEXT ·Max(SB),NOSPLIT,$0
5115
// +Inf special cases

src/math/stubs_s390x.s

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
#include "textflag.h"
66

7+
TEXT ·Dim(SB),NOSPLIT,$0
8+
BR ·dim(SB)
9+
710
TEXT ·Exp2(SB),NOSPLIT,$0
811
BR ·exp2(SB)
912

0 commit comments

Comments
 (0)