Debian Codegraph @

build/sortedset/union16_asm.go

raw
1 // +build ignore
2
3 package main
4
5 import (
6 . "github.com/mmcloughlin/avo/build"
7 . "github.com/mmcloughlin/avo/operand"
8 )
9
10 func init() {
11 ConstraintExpr("!purego")
12 }
13
14 func main() {
15 TEXT("union16", NOSPLIT, "func(dst, a, b []byte) (i, j, k int)")
16
17 // Load all pointers.
18 dst := Load(Param("dst").Base(), GP64())
19 a := Load(Param("a").Base(), GP64())
20 b := Load(Param("b").Base(), GP64())
21
22 // Calculate the end of a/b so we know where to loop until.
23 aEnd := Load(Param("a").Len(), GP64())
24 ADDQ(a, aEnd)
25 bEnd := Load(Param("b").Len(), GP64())
26 ADDQ(b, bEnd)
27
28 ones := XMM()
29 VPCMPEQB(ones, ones, ones)
30
31 // Load the first item from a/b. We know that each has at least
32 // one item (this is enforced in the wrapper).
33 aItem := XMM()
34 bItem := XMM()
35 VMOVUPS(Mem{Base: a}, aItem)
36 VMOVUPS(Mem{Base: b}, bItem)
37
38 Label("loop")
39
40 // Compare bytes and extract two masks.
41 // ne = mask of bytes where a!=b
42 // lt = mask of bytes where a<b
43 ne := XMM()
44 lt := XMM()
45 VPCMPEQB(aItem, bItem, ne)
46 VPXOR(ne, ones, ne)
47 VPMINUB(aItem, bItem, lt)
48 VPCMPEQB(aItem, lt, lt)
49 VPAND(lt, ne, lt)
50 unequalMask := GP32()
51 lessMask := GP32()
52 VPMOVMSKB(ne, unequalMask)
53 VPMOVMSKB(lt, lessMask)
54
55 // Branch based on whether a==b, or a<b.
56 TESTL(unequalMask, unequalMask)
57 JZ(LabelRef("equal"))
58 unequalByteIndex := GP32()
59 BSFL(unequalMask, unequalByteIndex)
60 BTSL(unequalByteIndex, lessMask)
61 JCS(LabelRef("less"))
62
63 // If b>a, copy and advance a.
64 Label("greater")
65 VMOVUPS(bItem, Mem{Base: dst})
66 ADDQ(Imm(16), dst)
67 ADDQ(Imm(16), b)
68 CMPQ(b, bEnd)
69 JE(LabelRef("done"))
70 VMOVUPS(Mem{Base: b}, bItem)
71 JMP(LabelRef("loop"))
72
73 // If a<b, copy and advance a.
74 Label("less")
75 VMOVUPS(aItem, Mem{Base: dst})
76 ADDQ(Imm(16), dst)
77 ADDQ(Imm(16), a)
78 CMPQ(a, aEnd)
79 JE(LabelRef("done"))
80 VMOVUPS(Mem{Base: a}, aItem)
81 JMP(LabelRef("loop"))
82
83 // If a==b, copy either and advance both.
84 Label("equal")
85 VMOVUPS(aItem, Mem{Base: dst})
86 ADDQ(Imm(16), dst)
87 ADDQ(Imm(16), a)
88 ADDQ(Imm(16), b)
89 CMPQ(a, aEnd)
90 JE(LabelRef("done"))
91 CMPQ(b, bEnd)
92 JE(LabelRef("done"))
93 VMOVUPS(Mem{Base: a}, aItem)
94 VMOVUPS(Mem{Base: b}, bItem)
95 JMP(LabelRef("loop"))
96
97 // Calculate and return byte offsets of the each pointer.
98 Label("done")
99 SUBQ(Load(Param("a").Base(), GP64()), a)
100 Store(a, Return("i"))
101 SUBQ(Load(Param("b").Base(), GP64()), b)
102 Store(b, Return("j"))
103 SUBQ(Load(Param("dst").Base(), GP64()), dst)
104 Store(dst, Return("k"))
105 RET()
106 Generate()
107 }