Debian Codegraph @

build/ascii/equal_fold_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 . "github.com/mmcloughlin/avo/reg"
9 . "github.com/segmentio/asm/build/internal/x86"
10
11 "fmt"
12
13 "github.com/segmentio/asm/cpu"
14 )
15
16 func init() {
17 ConstraintExpr("!purego")
18 }
19
20 func main() {
21 TEXT("EqualFoldString", NOSPLIT, "func(a, b string) bool")
22 Doc(
23 "EqualFoldString is a version of strings.EqualFold designed to work on ASCII",
24 "input instead of UTF-8.",
25 "",
26 "When the program has guarantees that the input is composed of ASCII",
27 "characters only, it allows for greater optimizations.",
28 )
29
30 // Use index for byte position. We have plenty of registers, and it saves an
31 // ADD operation as the memory index is the same for both a and b.
32 i := GP64()
33 a := Mem{Base: Load(Param("a").Base(), GP64()), Index: i, Scale: 1}
34 n := Load(Param("a").Len(), GP64())
35 b := Mem{Base: Load(Param("b").Base(), GP64()), Index: i, Scale: 1}
36 bn, _ := Param("b").Len().Resolve()
37 ret, _ := ReturnIndex(0).Resolve()
38
39 CMPQ(n, bn.Addr) // if len(a) != len(b):
40 JNE(LabelRef("done")) // return false
41 XORQ(i, i) // i = 0
42
43 CMPQ(n, U8(16)) // if n < 16:
44 JB(LabelRef("init_x86")) // goto init_x86
45 JumpIfFeature("init_avx", cpu.AVX2) // goto init_avx if supported
46
47 Label("init_x86")
48
49 cmp := GP32()
50 av := GP32()
51 bv := GP32()
52
53 // Map to convert ASCII upper characters to lower case.
54 lowerCase := Mem{Base: GP64(), Scale: 1}
55 LEAQ(NewDataAddr(Symbol{Name: "github·com∕segmentio∕asm∕ascii·lowerCase"}, 0), lowerCase.Base)
56 XORL(cmp, cmp)
57
58 Label("cmp8")
59 CMPQ(n, U8(8)) // if n < 0:
60 JB(LabelRef("cmp7")) // goto cmp7
61 for i := 0; i < 8; i++ {
62 MOVBLZX(a.Offset(i), av) // av = a[i]
63 MOVBLZX(b.Offset(i), bv) // bv = b[i]
64 MOVB(lowerCase.Idx(av, 1), av.As8()) // av = lowerCase[av]
65 XORB(lowerCase.Idx(bv, 1), av.As8()) // av = lowerCase[bv] ^ av
66 ORB(av.As8(), cmp.As8()) // cmp |= av
67 }
68 JNE(LabelRef("done")) // return false if ZF == 0
69 ADDQ(Imm(8), a.Index) // i += 8
70 SUBQ(Imm(8), n) // n -= 8
71 JMP(LabelRef("cmp8"))
72
73 for i := 6; i >= 0; i-- {
74 Label(fmt.Sprintf("cmp%d", i+1))
75 next := "success"
76 if i > 0 {
77 next = fmt.Sprintf("cmp%d", i)
78 }
79
80 CMPQ(n, U8(i+1)) // if n < i:
81 JB(LabelRef(next)) // goto cmp${i-1}
82 MOVBLZX(a.Offset(i), av) // av = a[i]
83 MOVBLZX(b.Offset(i), bv) // bv = b[i]
84 MOVB(lowerCase.Idx(av, 1), av.As8()) // av = lowerCase[av]
85 XORB(lowerCase.Idx(bv, 1), av.As8()) // av = lowerCase[bv] ^ av
86 ORB(av.As8(), cmp.As8()) // cmp |= av
87 }
88
89 Label("done")
90 SETEQ(ret.Addr) // return ZF
91 RET() // ...
92
93 Label("success")
94 MOVB(U8(1), ret.Addr) // return true
95 RET() // ...
96
97 Label("init_avx")
98
99 bit := VecBroadcast(U8(0x20), YMM()) // "case" bit
100 msk := VecBroadcast(U8(0x1F), YMM()) // 0b10000000 - 'a'
101 rng := VecBroadcast(U8(0x9A), YMM()) // 'z' - 'a' + 1 - 0x80 (overflowed 8-bits)
102 one := VecBroadcast(U8(0x01), YMM()) // 1-bit for ANDing with comparison
103
104 vec := NewVectorizer(12, func(l VectorLane) Register {
105 v0 := l.Read(a)
106 v1 := l.Read(b)
107 v2 := l.Alloc()
108
109 VXORPD(v0, v1, v1) // calculate difference between a and b
110 VPCMPEQB(bit, v1, v2) // check if above difference is the 6th bit
111 VORPD(bit, v0, v0) // set the 6th bit for a
112 VPADDB(msk, v0, v0) // add 0x1f to each byte to set top bit for letters
113 VPCMPGTB(v0, rng, v0) // compare if not letter: v - 'a' < 'z' - 'a' + 1
114 VPAND(v2, v0, v0) // combine 6th-bit difference with letter range
115 VPAND(one, v0, v0) // merge test mask
116 VPSLLW(Imm(5), v0, v0) // shift into case bit position
117 VPCMPEQB(v1, v0, v0) // compare original difference with case-only difference
118
119 return v0
120 }).Reduce(ReduceAnd) // merge all comparisons together
121
122 cmpAVX := func(spec Spec, lanes int, incr bool) {
123 sz := int(spec.Size())
124 tmp := GP32()
125 out := vec.Compile(spec, lanes)[0] // [compare sz*lanes bytes]
126 if incr {
127 ADDQ(U8(sz*lanes), a.Index) // i += sz*lanes
128 SUBQ(U8(sz*lanes), n) // n -= sz*lanes
129 }
130 VPMOVMSKB(out, tmp) // tmp[0,1,2,...] = y0[0,8,16,...]
131 XORL(U32(^uint32(0)>>(32-sz)), tmp) // ZF = (tmp == 0xFFFFFFFF)
132 }
133
134 Label("cmp128")
135 CMPQ(n, U8(128)) // if n < 128:
136 JB(LabelRef("cmp64")) // goto cmp64
137 cmpAVX(S256, 4, true) // ZF = [compare 128 bytes]
138 JNE(LabelRef("done")) // return if ZF == 0
139 JMP(LabelRef("cmp128")) // loop cmp128
140
141 Label("cmp64")
142 CMPQ(n, U8(64)) // if n < 64:
143 JB(LabelRef("cmp32")) // goto cmp32
144 cmpAVX(S256, 2, true) // ZF = [compare 64 bytes]
145 JNE(LabelRef("done")) // return if ZF == 0
146
147 Label("cmp32")
148 CMPQ(n, U8(32)) // if n < 32:
149 JB(LabelRef("cmp16")) // goto cmp16
150 cmpAVX(S256, 1, true) // ZF = [compare 32 bytes]
151 JNE(LabelRef("done")) // return if ZF == 0
152
153 Label("cmp16")
154
155 // Convert YMM masks to XMM
156 bit = bit.(Vec).AsX()
157 msk = msk.(Vec).AsX()
158 rng = rng.(Vec).AsX()
159 one = one.(Vec).AsX()
160
161 CMPQ(n, U8(16)) // if n <= 16:
162 JLE(LabelRef("cmp_tail")) // goto cmp_tail
163 cmpAVX(S128, 1, true) // ZF = [compare 16 bytes]
164 JNE(LabelRef("done")) // return if ZF == 0
165
166 Label("cmp_tail")
167 // At this point, we have <= 16 bytes to compare, but we know the total input
168 // is >= 16 bytes. Move the pointer to the *last* 16 bytes of the input so we
169 // can skip the fallback.
170 SUBQ(Imm(16), n) // n -= 16
171 ADDQ(n, a.Index) // i += n
172 cmpAVX(S128, 1, false) // ZF = [compare 16 bytes]
173 JMP(LabelRef("done")) // return ZF
174
175 Generate()
176 }