Debian Codegraph @

build/sortedset/intersect16_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("intersect16", NOSPLIT, "func(dst, a, b []byte) 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, advance b.
64 Label("greater")
65 ADDQ(Imm(16), b)
66 CMPQ(b, bEnd)
67 JE(LabelRef("done"))
68 VMOVUPS(Mem{Base: b}, bItem)
69 JMP(LabelRef("loop"))
70
71 // If a<b, advance a.
72 Label("less")
73 ADDQ(Imm(16), a)
74 CMPQ(a, aEnd)
75 JE(LabelRef("done"))
76 VMOVUPS(Mem{Base: a}, aItem)
77 JMP(LabelRef("loop"))
78
79 // If a==b, copy either and advance both.
80 Label("equal")
81 VMOVUPS(aItem, Mem{Base: dst})
82 ADDQ(Imm(16), dst)
83 ADDQ(Imm(16), a)
84 ADDQ(Imm(16), b)
85 CMPQ(a, aEnd)
86 JE(LabelRef("done"))
87 CMPQ(b, bEnd)
88 JE(LabelRef("done"))
89 VMOVUPS(Mem{Base: a}, aItem)
90 VMOVUPS(Mem{Base: b}, bItem)
91 JMP(LabelRef("loop"))
92
93 // Calculate and return byte offset of the dst pointer.
94 Label("done")
95 SUBQ(Load(Param("dst").Base(), GP64()), dst)
96 Store(dst, ReturnIndex(0))
97 RET()
98 Generate()
99 }