Debian Codegraph @

build/ascii/valid_print_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 "github.com/segmentio/asm/cpu"
12 )
13
14 func init() {
15 ConstraintExpr("!purego")
16 }
17
18 func main() {
19 TEXT("ValidPrintString", NOSPLIT, "func(s string) bool")
20 Doc("ValidPrintString returns true if s contains only printable ASCII characters.")
21
22 p := Mem{Base: Load(Param("s").Base(), GP64())}
23 n := Load(Param("s").Len(), GP64())
24 ret, _ := ReturnIndex(0).Resolve()
25
26 m1 := GP64()
27 m2 := GP64()
28 m3 := GP64()
29 val := GP32()
30 tmp := GP32()
31
32 CMPQ(n, U8(16)) // if n < 16:
33 JB(LabelRef("init_x86")) // goto init_x86
34 JumpIfFeature("init_avx", cpu.AVX2) // goto init_avx if supported
35
36 Label("init_x86")
37 CMPQ(n, U8(8)) // if n < 8:
38 JB(LabelRef("cmp4")) // goto cmp4
39 MOVQ(U64(0xDFDFDFDFDFDFDFE0), m1)
40 MOVQ(U64(0x0101010101010101), m2)
41 MOVQ(U64(0x8080808080808080), m3)
42
43 Label("cmp8")
44 valid8(p, n, m1, m2, m3) // ZF = [compare 8 bytes]
45 JNE(LabelRef("done")) // return ZF if ZF == 0
46 CMPQ(n, U8(8)) // if n < 8:
47 JB(LabelRef("cmp4")) // goto cmp4
48 JMP(LabelRef("cmp8")) // loop cmp8
49
50 Label("cmp4")
51 CMPQ(n, U8(4)) // if n < 4:
52 JB(LabelRef("cmp3")) // goto cmp3
53 valid4(p, n) // ZF = [compare 4 bytes]
54 JNE(LabelRef("done")) // return ZF if ZF == 0
55
56 Label("cmp3")
57 CMPQ(n, U8(3)) // if n < 3:
58 JB(LabelRef("cmp2")) // goto cmp2
59 MOVWLZX(p, tmp) // tmp = p[0:2]
60 MOVBLZX(p.Offset(2), val) // val = p[2:3]
61 SHLL(U8(16), val) // val <<= 16
62 ORL(tmp, val) // val = tmp | val
63 ORL(U32(0x20000000), val) // val = 0x20000000 | val
64 JMP(LabelRef("final"))
65
66 Label("cmp2")
67 CMPQ(n, U8(2)) // if n < 2:
68 JB(LabelRef("cmp1")) // goto cmp1
69 MOVWLZX(p, val) // val = p[0:2]
70 ORL(U32(0x20200000), val) // val = 0x20200000 | val
71 JMP(LabelRef("final"))
72
73 Label("cmp1")
74 CMPQ(n, U8(0)) // if n == 0:
75 JE(LabelRef("done")) // return true
76 MOVBLZX(p, val) // val = p[0:1]
77 ORL(U32(0x20202000), val) // val = 0x20202000 | val
78
79 Label("final")
80 setup4(val) // [update val register]
81 TESTL(U32(0x80808080), val) // ZF = (0x80808080 & val) == 0
82
83 Label("done")
84 SETEQ(ret.Addr) // return ZF
85 RET() // ...
86
87 Label("init_avx")
88
89 min := VecBroadcast(U8(0x1F), YMM())
90 max := VecBroadcast(U8(0x7E), YMM())
91
92 vec := NewVectorizer(14, func(l VectorLane) Register {
93 v0 := l.Read(p)
94 v1 := l.Alloc()
95
96 VPCMPGTB(min, v0, v1) // v1 = bytes that are greater than the min-1 (i.e. valid at lower end)
97 VPCMPGTB(max, v0, v0) // v0 = bytes that are greater than the max (i.e. invalid at upper end)
98 VPANDN(v1, v0, v0) // y2 & ~y3 mask should be full unless there's an invalid byte
99
100 return v0
101 }).Reduce(ReduceAnd) // merge all comparisons together
102
103 cmpAVX := func(spec Spec, lanes int, incr bool) {
104 sz := int(spec.Size())
105 out := vec.Compile(spec, lanes)[0] // [compare sz*lanes bytes]
106 if incr {
107 ADDQ(U8(sz*lanes), p.Base) // p += sz*lanes
108 SUBQ(U8(sz*lanes), n) // n -= sz*lanes
109 }
110 VPMOVMSKB(out, tmp) // tmp[0,1,2,...] = y0[0,8,16,...]
111 XORL(U32(^uint32(0)>>(32-sz)), tmp) // ZF = (tmp == 0xFFFFFFFF)
112 }
113
114 Label("cmp128")
115 CMPQ(n, U8(128)) // if n < 128:
116 JB(LabelRef("cmp64")) // goto cmp64
117 cmpAVX(S256, 4, true) // ZF = [compare 128 bytes]
118 JNE(LabelRef("done")) // return if ZF == 0
119 JMP(LabelRef("cmp128")) // loop cmp128
120
121 Label("cmp64")
122 CMPQ(n, U8(64)) // if n < 64:
123 JB(LabelRef("cmp32")) // goto cmp32
124 cmpAVX(S256, 2, true) // ZF = [compare 64 bytes]
125 JNE(LabelRef("done")) // return if ZF == 0
126
127 Label("cmp32")
128 CMPQ(n, U8(32)) // if n < 32:
129 JB(LabelRef("cmp16")) // goto cmp16
130 cmpAVX(S256, 1, true) // ZF = [compare 32 bytes]
131 JNE(LabelRef("done")) // return if ZF == 0
132
133 Label("cmp16")
134
135 // Convert YMM masks to XMM
136 min = min.(Vec).AsX()
137 max = max.(Vec).AsX()
138
139 CMPQ(n, U8(16)) // if n <= 16:
140 JLE(LabelRef("cmp_tail")) // goto cmp_tail
141 cmpAVX(S128, 1, true) // ZF = [compare 16 bytes]
142 JNE(LabelRef("done")) // return if ZF == 0
143
144 Label("cmp_tail")
145 // At this point, we have <= 16 bytes to compare, but we know the total input
146 // is >= 16 bytes. Move the pointer to the *last* 16 bytes of the input so we
147 // can skip the fallback.
148 SUBQ(Imm(16), n) // n -= 16
149 ADDQ(n, p.Base) // p += n
150 cmpAVX(S128, 1, false) // ZF = [compare 16 bytes]
151 JMP(LabelRef("done")) // return ZF
152
153 Generate()
154 }
155
156 func valid4(p Mem, n Register) {
157 val := GP32()
158
159 MOVL(p, val) // val = p[0:4]
160 setup4(val) // [update val register]
161 ADDQ(U8(4), p.Base) // p += 4
162 SUBQ(U8(4), n) // n -= 4
163 TESTL(U32(0x80808080), val) // ZF = (0x80808080 & val) == 0
164 }
165
166 func setup4(val Register) {
167 nval := GP32()
168 tmp1 := GP32()
169 tmp2 := GP32()
170
171 MOVL(val, nval) // nval = val
172 LEAL(Mem{Disp: 0xDFDFDFE0, Base: val}, tmp1) // tmp1 = val + 0xDFDFDFE0
173 NOTL(nval) // nval = ^nval
174 ANDL(nval, tmp1) // tmp1 = nval & tmp1
175 LEAL(Mem{Disp: 0x01010101, Base: val}, tmp2) // tmp2 = val + 0x01010101
176 ORL(tmp2, val) // val = val | tmp2
177 ORL(tmp1, val) // val = val | tmp1
178 }
179
180 func valid8(p Mem, n, m1, m2, m3 Register) {
181 val := GP64()
182 nval := GP64()
183 tmp1 := GP64()
184 tmp2 := GP64()
185
186 MOVQ(p, val) // val = p[0:8]
187 MOVQ(val, nval) // nval = val
188 LEAQ(Mem{Base: val, Index: m1, Scale: 1}, tmp1) // tmp1 = val + m1
189 NOTQ(nval) // nval = ^nval
190 ANDQ(nval, tmp1) // tmp1 = nval & tmp1
191 LEAQ(Mem{Base: val, Index: m2, Scale: 1}, tmp2) // tmp2 = val + m2
192 ORQ(tmp2, val) // val = val | tmp2
193 ORQ(tmp1, val) // val = val | tmp1
194 ADDQ(U8(8), p.Base) // p += 8
195 SUBQ(U8(8), n) // n -= 8
196 TESTQ(m3, val) // ZF = (m3 & val) == 0
197 }