Skip to content

Commit 07f0f09

Browse files
mrosier-qdtcherrymui
authored andcommitted
cmd/compile: make math.Ceil/Floor/Round/Trunc intrinsics on arm64
name old time/op new time/op delta Ceil 550ns ± 0% 486ns ± 7% -11.64% (p=0.000 n=13+18) Floor 495ns ±19% 512ns ±12% ~ (p=0.164 n=20+20) Round 550ns ± 0% 487ns ± 8% -11.49% (p=0.000 n=12+19) Trunc 563ns ± 7% 488ns ±13% -13.44% (p=0.000 n=15+2) Change-Id: I53f234b160b3c026a277506e2cf977d150379464 Reviewed-on: https://go-review.googlesource.com/88295 Run-TryBot: Cherry Zhang <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent ba99433 commit 07f0f09

File tree

7 files changed

+170
-6
lines changed

7 files changed

+170
-6
lines changed

src/cmd/compile/internal/arm64/ssa.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,11 @@ func ssaGenValue(s *gc.SSAGenState, v *ssa.Value) {
559559
ssa.OpARM64RBIT,
560560
ssa.OpARM64RBITW,
561561
ssa.OpARM64CLZ,
562-
ssa.OpARM64CLZW:
562+
ssa.OpARM64CLZW,
563+
ssa.OpARM64FRINTAD,
564+
ssa.OpARM64FRINTMD,
565+
ssa.OpARM64FRINTPD,
566+
ssa.OpARM64FRINTZD:
563567
p := s.Prog(v.Op.Asm())
564568
p.From.Type = obj.TYPE_REG
565569
p.From.Reg = v.Args[0].Reg()

src/cmd/compile/internal/gc/asm_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ var allAsmTests = []*asmTests{
248248
{
249249
arch: "arm64",
250250
os: "linux",
251-
imports: []string{"encoding/binary", "math/bits"},
251+
imports: []string{"encoding/binary", "math", "math/bits"},
252252
tests: linuxARM64Tests,
253253
},
254254
{
@@ -2849,6 +2849,47 @@ var linuxARM64Tests = []*asmTest{
28492849
pos: []string{"\tMOVHU\t\\(R[0-9]+\\)"},
28502850
neg: []string{"ORR\tR[0-9]+<<8\t"},
28512851
},
2852+
// Intrinsic tests for math.
2853+
{
2854+
fn: `
2855+
func sqrt(x float64) float64 {
2856+
return math.Sqrt(x)
2857+
}
2858+
`,
2859+
pos: []string{"FSQRTD"},
2860+
},
2861+
{
2862+
fn: `
2863+
func ceil(x float64) float64 {
2864+
return math.Ceil(x)
2865+
}
2866+
`,
2867+
pos: []string{"FRINTPD"},
2868+
},
2869+
{
2870+
fn: `
2871+
func floor(x float64) float64 {
2872+
return math.Floor(x)
2873+
}
2874+
`,
2875+
pos: []string{"FRINTMD"},
2876+
},
2877+
{
2878+
fn: `
2879+
func round(x float64) float64 {
2880+
return math.Round(x)
2881+
}
2882+
`,
2883+
pos: []string{"FRINTAD"},
2884+
},
2885+
{
2886+
fn: `
2887+
func trunc(x float64) float64 {
2888+
return math.Trunc(x)
2889+
}
2890+
`,
2891+
pos: []string{"FRINTZD"},
2892+
},
28522893
}
28532894

28542895
var linuxMIPSTests = []*asmTest{

src/cmd/compile/internal/gc/ssa.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,22 +2918,22 @@ func init() {
29182918
func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
29192919
return s.newValue1(ssa.OpTrunc, types.Types[TFLOAT64], args[0])
29202920
},
2921-
sys.PPC64, sys.S390X)
2921+
sys.ARM64, sys.PPC64, sys.S390X)
29222922
addF("math", "Ceil",
29232923
func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
29242924
return s.newValue1(ssa.OpCeil, types.Types[TFLOAT64], args[0])
29252925
},
2926-
sys.PPC64, sys.S390X)
2926+
sys.ARM64, sys.PPC64, sys.S390X)
29272927
addF("math", "Floor",
29282928
func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
29292929
return s.newValue1(ssa.OpFloor, types.Types[TFLOAT64], args[0])
29302930
},
2931-
sys.PPC64, sys.S390X)
2931+
sys.ARM64, sys.PPC64, sys.S390X)
29322932
addF("math", "Round",
29332933
func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
29342934
return s.newValue1(ssa.OpRound, types.Types[TFLOAT64], args[0])
29352935
},
2936-
sys.S390X)
2936+
sys.ARM64, sys.S390X)
29372937
addF("math", "RoundToEven",
29382938
func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
29392939
return s.newValue1(ssa.OpRoundToEven, types.Types[TFLOAT64], args[0])

src/cmd/compile/internal/ssa/gen/ARM64.rules

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@
8181
(Com16 x) -> (MVN x)
8282
(Com8 x) -> (MVN x)
8383

84+
// math package intrinsics
8485
(Sqrt x) -> (FSQRTD x)
86+
(Ceil x) -> (FRINTPD x)
87+
(Floor x) -> (FRINTMD x)
88+
(Round x) -> (FRINTAD x)
89+
(Trunc x) -> (FRINTZD x)
8590

8691
(Ctz64 <t> x) -> (CLZ (RBIT <t> x))
8792
(Ctz32 <t> x) -> (CLZW (RBITW <t> x))

src/cmd/compile/internal/ssa/gen/ARM64Ops.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ func init() {
323323
{name: "FCVTSD", argLength: 1, reg: fp11, asm: "FCVTSD"}, // float32 -> float64
324324
{name: "FCVTDS", argLength: 1, reg: fp11, asm: "FCVTDS"}, // float64 -> float32
325325

326+
// floating-point round to integral
327+
{name: "FRINTAD", argLength: 1, reg: fp11, asm: "FRINTAD"},
328+
{name: "FRINTMD", argLength: 1, reg: fp11, asm: "FRINTMD"},
329+
{name: "FRINTPD", argLength: 1, reg: fp11, asm: "FRINTPD"},
330+
{name: "FRINTZD", argLength: 1, reg: fp11, asm: "FRINTZD"},
331+
326332
// conditional instructions
327333
{name: "CSELULT", argLength: 3, reg: gp2flags1, asm: "CSEL"}, // returns arg0 if flags indicates unsigned LT, arg1 otherwise, arg2=flags
328334
{name: "CSELULT0", argLength: 2, reg: gp1flags1, asm: "CSEL"}, // returns arg0 if flags indicates unsigned LT, 0 otherwise, arg1=flags

src/cmd/compile/internal/ssa/opGen.go

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/compile/internal/ssa/rewriteARM64.go

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)