raz / lib / Bytes.x86_64.raz

349 lines · 9.3 KiB · raw · history · last change e0d008cd

1(* Searches of bytes, many at a time: the first and the last place of a
2 byte in a []U8, how many places it has, and where one []U8 first
3 starts in another. This body is for x86-64, where SSE2 compares
4 sixteen bytes at a time; Bytes.raz is the same interface for any
5 64-bit target, eight at a time in a U64. *)
6module Bytes;
7import Rt;
8import SYSTEM;
9
10(* The first place of b in s, or -1. Sixteen bytes at a time are
11 compared with b's sixteen copies (pcmpeqb), and pmovmskb makes of the
12 result a number with a bit set for each byte that matched. The bytes
13 left over, fewer than sixteen, are compared one by one. Where the
14 machine finds them: s's address in rdi, its length in rsi, b in dl. *)
15proc index*(s: []const U8; b: U8): INT;
16code
17 movd xmm1, edx;
18 punpcklbw xmm1, xmm1; (* b in the low two bytes... *)
19 punpcklwd xmm1, xmm1; (* ...in the low four... *)
20 pshufd xmm1, xmm1, 0; (* ...and in all sixteen *)
21 xor ecx, ecx;
22 blocks:
23 lea rax, [rcx + 16];
24 cmp rax, rsi;
25 ja tail;
26 movdqu xmm0, [rdi + rcx];
27 pcmpeqb xmm0, xmm1;
28 pmovmskb eax, xmm0;
29 test eax, eax;
30 jnz found;
31 add rcx, 16;
32 jmp blocks;
33 found:
34 bsf eax, eax; (* the lowest bit set: the first match *)
35 add rax, rcx;
36 ret;
37 tail:
38 cmp rcx, rsi;
39 jae missing;
40 cmp [rdi + rcx], dl;
41 je here;
42 inc rcx;
43 jmp tail;
44 here:
45 mov rax, rcx;
46 ret;
47 missing:
48 mov rax, -1;
49 ret;
50end index;
51
52(* The last place of b in s, or -1: as index, from the end. *)
53proc last*(s: []const U8; b: U8): INT;
54code
55 movd xmm1, edx;
56 punpcklbw xmm1, xmm1;
57 punpcklwd xmm1, xmm1;
58 pshufd xmm1, xmm1, 0;
59 mov rcx, rsi; (* the bytes not yet seen are the first rcx *)
60 blocks:
61 cmp rcx, 16;
62 jb tail;
63 sub rcx, 16;
64 movdqu xmm0, [rdi + rcx];
65 pcmpeqb xmm0, xmm1;
66 pmovmskb eax, xmm0;
67 test eax, eax;
68 jz blocks;
69 bsr eax, eax; (* the highest bit set: the last match *)
70 add rax, rcx;
71 ret;
72 tail:
73 test rcx, rcx;
74 jz missing;
75 dec rcx;
76 cmp [rdi + rcx], dl;
77 jne tail;
78 mov rax, rcx;
79 ret;
80 missing:
81 mov rax, -1;
82 ret;
83end last;
84
85(* How many bytes of s are b: the bits of each block's number, counted. *)
86proc count*(s: []const U8; b: U8): INT;
87code
88 movd xmm1, edx;
89 punpcklbw xmm1, xmm1;
90 punpcklwd xmm1, xmm1;
91 pshufd xmm1, xmm1, 0;
92 xor eax, eax; (* the count *)
93 xor ecx, ecx;
94 blocks:
95 lea r8, [rcx + 16];
96 cmp r8, rsi;
97 ja tail;
98 movdqu xmm0, [rdi + rcx];
99 pcmpeqb xmm0, xmm1;
100 pmovmskb r8d, xmm0;
101 popcnt r8d, r8d;
102 add rax, r8;
103 add rcx, 16;
104 jmp blocks;
105 tail:
106 cmp rcx, rsi;
107 jae done;
108 cmp [rdi + rcx], dl;
109 jne next;
110 inc rax;
111 next:
112 inc rcx;
113 jmp tail;
114 done:
115 ret;
116end count;
117
118(* The first place i, from from on, with s[i] = a and s[i + gap] = b,
119 or -1; LEN(s) must be more than gap. Sixteen places are tried at
120 once: sixteen bytes are compared with a, sixteen from gap places on
121 with b, and the two results anded. The places in rdi and rsi (s),
122 rdx (gap), cl (a), r8b (b) and r9 (from). *)
123proc pair(s: []const U8; gap: INT; a, b: U8; from: INT): INT;
124code
125 mov r11, rcx; (* a, for the places one by one *)
126 movd xmm1, ecx;
127 punpcklbw xmm1, xmm1;
128 punpcklwd xmm1, xmm1;
129 pshufd xmm1, xmm1, 0;
130 movd xmm2, r8d;
131 punpcklbw xmm2, xmm2;
132 punpcklwd xmm2, xmm2;
133 pshufd xmm2, xmm2, 0;
134 mov r10, rsi;
135 sub r10, rdx; (* the places are those below r10 *)
136 mov rax, r9;
137 blocks:
138 lea rcx, [rax + 16];
139 cmp rcx, r10;
140 ja tail;
141 movdqu xmm0, [rdi + rax];
142 pcmpeqb xmm0, xmm1;
143 lea rcx, [rax + rdx];
144 movdqu xmm3, [rdi + rcx];
145 pcmpeqb xmm3, xmm2;
146 pand xmm0, xmm3;
147 pmovmskb ecx, xmm0;
148 test ecx, ecx;
149 jnz found;
150 add rax, 16;
151 jmp blocks;
152 found:
153 bsf ecx, ecx;
154 add rax, rcx;
155 ret;
156 tail:
157 cmp rax, r10;
158 jae missing;
159 cmp [rdi + rax], r11b;
160 jne next;
161 lea rcx, [rax + rdx];
162 cmp [rdi + rcx], r8b;
163 je here;
164 next:
165 inc rax;
166 jmp tail;
167 here:
168 ret;
169 missing:
170 mov rax, -1;
171 ret;
172end pair;
173
174(* Where sub first starts in s, or -1; 0 for an empty sub. Places are
175 found by two bytes of sub, its first and its last, each compared at
176 its place (pair), and checked whole only where both agree. They are
177 the two bytes farthest apart, so as far from being the same test as
178 two can be, and need no table of which bytes are rare. *)
179proc find*(s, sub: []const U8): INT;
180var i, n: INT;
181begin
182 n := LEN(sub);
183 if n = 0 then return 0; end;
184 if n > LEN(s) then return -1; end;
185 i := pair(s, n - 1, sub[0], sub[n - 1], 0);
186 while i >= 0 do
187 if s[i..i + n] = sub then return i; end;
188 i := pair(s, n - 1, sub[0], sub[n - 1], i + 1);
189 end;
190 return -1;
191end;
192
193(* The tests: each result against a plain loop over the same bytes. *)
194
195var
196 seed: U64;
197 fills: [6]U8; (* bytes that tempt the zero-byte test *)
198 buf: [160]U8;
199 subs: [24]U8;
200
201(* a pseudo-random number below n *)
202proc random(n: INT): INT;
203begin
204 seed := seed * 6364136223846793005 + 1442695040888963407;
205 return INT((seed shr 33) mod U64(n));
206end;
207
208proc refIndex(s: []const U8; b: U8): INT;
209var i: INT;
210begin
211 for i := 0 to LEN(s) - 1 do
212 if s[i] = b then return i; end;
213 end;
214 return -1;
215end;
216
217proc refLast(s: []const U8; b: U8): INT;
218var i: INT;
219begin
220 for i := LEN(s) - 1 to 0 by -1 do
221 if s[i] = b then return i; end;
222 end;
223 return -1;
224end;
225
226proc refCount(s: []const U8; b: U8): INT;
227var i, n: INT;
228begin
229 n := 0;
230 for i := 0 to LEN(s) - 1 do
231 if s[i] = b then INC(n); end;
232 end;
233 return n;
234end;
235
236proc refFind(s, sub: []const U8): INT;
237var i: INT;
238begin
239 for i := 0 to LEN(s) - LEN(sub) do
240 if s[i..i + LEN(sub)] = sub then return i; end;
241 end;
242 return -1;
243end;
244
245(* index, last and count of b in s agree with the plain loops *)
246proc checkByte(s: []const U8; b: U8);
247begin
248 if index(s, b) # refIndex(s, b) then Rt.stop("Bytes.index"); end;
249 if last(s, b) # refLast(s, b) then Rt.stop("Bytes.last"); end;
250 if count(s, b) # refCount(s, b) then Rt.stop("Bytes.count"); end;
251end;
252
253proc checkFind(s, sub: []const U8);
254begin
255 if find(s, sub) # refFind(s, sub) then Rt.stop("Bytes.find"); end;
256end;
257
258(* every length to 40, from every start 0 to 8, with one byte at every
259 place, among each kind of filler: the first place, the last, and
260 the places either side of a block of 8 or 16 *)
261proc testEdges;
262var n, o, p, f, g, i: INT;
263begin
264 for o := 0 to 8 do
265 for n := 0 to 40 do
266 for f := 0 to 5 do
267 for g := 0 to 5 do
268 for i := 0 to 159 do buf[i] := fills[f]; end;
269 checkByte(buf[o..o + n], fills[g]);
270 for p := 0 to n - 1 do
271 buf[o + p] := fills[g];
272 checkByte(buf[o..o + n], fills[g]);
273 buf[o + p] := fills[f];
274 end;
275 end;
276 end;
277 end;
278 end;
279end;
280
281(* random bytes from a few kinds, of every length to 150 *)
282proc testRandom;
283var n, i, k, o: INT;
284begin
285 for n := 0 to 150 do
286 for k := 1 to 20 do
287 o := random(8);
288 for i := 0 to 159 do buf[i] := fills[random(2 + k mod 5)]; end;
289 for i := 0 to 5 do checkByte(buf[o..o + n], fills[i]); end;
290 checkByte(buf[o..o + n], 41H);
291 end;
292 end;
293end;
294
295(* a sub of every length at every place of a long slice, among fillers
296 that are almost its bytes; the slice cut short just before and just
297 after it; also subs longer than the slice, and empty ones *)
298proc testPlanted;
299var n, p, i, f: INT; s: []const U8;
300begin
301 for f := 0 to 2 do
302 for n := 1 to 24 do
303 for i := 0 to 23 do subs[i] := U8(0F0H + random(2) + i mod 3); end;
304 for p := 0 to 159 - n do
305 for i := 0 to 159 do buf[i] := U8(0F0H + f); end;
306 COPY(buf[p..p + n], subs[0..n]);
307 checkFind(buf[0..160], subs[0..n]);
308 checkFind(buf[1..160], subs[0..n]);
309 checkFind(buf[0..p + n], subs[0..n]);
310 checkFind(buf[0..p + n - 1], subs[0..n]);
311 end;
312 end;
313 end;
314 if find(buf[0..5], "") # 0 then Rt.stop("Bytes.find, empty sub"); end;
315 if find(buf[0..0], "") # 0 then Rt.stop("Bytes.find, both empty"); end;
316 if find(buf[0..3], buf[0..4]) # -1 then Rt.stop("Bytes.find, sub longer"); end;
317 s := "abcabd";
318 if (find(s, "abd") # 3) or (find(s, "bc") # 1) or (find(s, "d") # 5) or (find(s, s) # 0)
319 or (find(s, "abcabde") # -1) then Rt.stop("Bytes.find, text"); end;
320end;
321
322(* random text and subs from two or three kinds of byte, so that one
323 end of the sub is often where the other isn't *)
324proc testFind;
325var n, m, i, k, o: INT;
326begin
327 for n := 0 to 100 do
328 for k := 1 to 12 do
329 o := random(8);
330 for i := 0 to 159 do buf[i] := fills[3 + random(2 + k mod 2)]; end;
331 for m := 0 to 24 do
332 for i := 0 to 23 do subs[i] := fills[3 + random(2 + k mod 2)]; end;
333 checkFind(buf[o..o + n], subs[0..m]);
334 end;
335 end;
336 end;
337end;
338
339proc main*;
340begin
341 fills := [0, 1, 7FH, 80H, 81H, 0FFH];
342 seed := 12345;
343 testEdges;
344 testRandom;
345 testPlanted;
346 testFind;
347end;
348
349end Bytes.