raz / commit 99e7ccbf

commit
99e7ccbf8f11e14267b6c5ff937e3b58a481fffe
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
d0aa106b
Bytes: the zero-byte test written out in the loops

A call in a loop pushed its values into the frame. Portable body,
best of 5: Lines 772 -> 306 ms, find over 16 MB 592 -> 199 ms.

patch · log

1 file changed, +41 -44

lib/Bytes.raz (history) modified +41 -44

@@ -12,36 +12,25 @@
1212 Lows = 7F7F7F7F7F7F7F7FH; (* the low seven bits of each byte *)
1313 Tops = 8080808080808080H; (* the top bit of each byte *)
1414
15-(* eight bytes at s[i]: i + 8 must not pass LEN(s). The address needn't
16- be a multiple of 8; x86-64 reads such a word as it does any. *)
17-proc word(s: []U8; i: INT): U64;
18-var p: ^U64;
19-begin
20- p := ^U64(ADR(s[i]));
21- return p^;
22-end;
15+(* Every loop below tests eight bytes w for b in one expression, written
16+ out where it is used, since a call in a loop would push the loop's
17+ values out of their registers into the frame. With v = w xor (b in
18+ each byte), a byte of w is b when the byte of v is zero. A byte of v
19+ is zero when its low seven bits are, which adding 7FH to them shows,
20+ as no sum overflows into the next byte, and its top bit is. So
2321
24-(* The top bit of each byte of v that is zero, and no other bit. A byte
25- of v is zero when its low seven bits are, which adding 7FH to them
26- shows, as no sum overflows into the next byte, and its top bit is. *)
27-proc zeros(v: U64): U64;
28-begin
29- return ~(((v & Lows) + Lows) | v | Lows) & Tops;
30-end;
22+ ~(((v & Lows) + Lows) | v | Lows) & Tops
3123
32-(* The eight bytes of w that are b: their top bits. *)
33-proc matches(w: U64; b: U8): U64;
34-begin
35- return zeros(w xor (U64(b) * Ones));
36-end;
24+ is the top bit of each byte of w that is b, and no other bit; call it
25+ m. The place, 0 to 7, of the lowest bit of m (m is not 0) is the
26+ ones below that bit, one in each byte up to its own, summed into the
27+ top byte by a multiply, less one; the highest is found by a loop.
28+ The bits m sets are counted by moving each down to bit 0 of its
29+ byte and summing them into the top byte by a multiply.
3730
38-(* The place, 0 to 7, of the lowest top bit that m sets (m is not 0):
39- the ones below that bit, one in each byte up to its own, summed
40- into the top byte by the multiply, less one. *)
41-proc lowest(m: U64): INT;
42-begin
43- return INT((((m & (0 - m)) - 1) & Ones) * Ones shr 56) - 1;
44-end;
31+ A word is read through a U64 pointer to s[i], with i + 8 not past
32+ LEN(s); the address needn't be a multiple of 8, and x86-64 reads
33+ such a word as it does any. *)
4534
4635 (* The place, 0 to 7, of the highest top bit that m sets (m is not 0). *)
4736 proc highest(m: U64): INT;
@@ -52,21 +41,17 @@
5241 return k;
5342 end;
5443
55-(* How many top bits m sets: each moved down to bit 0 of its byte, and
56- summed into the top byte by the multiply. *)
57-proc ones(m: U64): INT;
58-begin
59- return INT(((m shr 7) * Ones) shr 56);
60-end;
61-
6244 (* The first place of b in s, or -1. *)
6345 proc index*(s: []U8; b: U8): INT;
64-var i: INT; m: U64;
46+var i: INT; m, v, bs: U64; p: ^U64;
6547 begin
48+ bs := U64(b) * Ones;
6649 i := 0;
6750 while i + 8 <= LEN(s) do
68- m := matches(word(s, i), b);
69- if m # 0 then return i + lowest(m); end;
51+ p := ^U64(ADR(s[i]));
52+ v := p^ xor bs;
53+ m := ~(((v & Lows) + Lows) | v | Lows) & Tops;
54+ if m # 0 then return i + INT((((m & (0 - m)) - 1) & Ones) * Ones shr 56) - 1; end;
7055 i := i + 8;
7156 end;
7257 while i < LEN(s) do
@@ -78,11 +63,14 @@
7863
7964 (* The last place of b in s, or -1. *)
8065 proc last*(s: []U8; b: U8): INT;
81-var i: INT; m: U64;
66+var i: INT; m, v, bs: U64; p: ^U64;
8267 begin
68+ bs := U64(b) * Ones;
8369 i := LEN(s);
8470 while i >= 8 do
85- m := matches(word(s, i - 8), b);
71+ p := ^U64(ADR(s[i - 8]));
72+ v := p^ xor bs;
73+ m := ~(((v & Lows) + Lows) | v | Lows) & Tops;
8674 if m # 0 then return i - 8 + highest(m); end;
8775 i := i - 8;
8876 end;
@@ -95,12 +83,15 @@
9583
9684 (* How many bytes of s are b. *)
9785 proc count*(s: []U8; b: U8): INT;
98-var i, n: INT;
86+var i, n: INT; v, bs: U64; p: ^U64;
9987 begin
88+ bs := U64(b) * Ones;
10089 i := 0;
10190 n := 0;
10291 while i + 8 <= LEN(s) do
103- n := n + ones(matches(word(s, i), b));
92+ p := ^U64(ADR(s[i]));
93+ v := p^ xor bs;
94+ n := n + INT(((~(((v & Lows) + Lows) | v | Lows) & Tops) shr 7) * Ones shr 56);
10495 i := i + 8;
10596 end;
10697 while i < LEN(s) do
@@ -117,18 +108,24 @@
117108 being the same test as two can be, and need no table of which bytes
118109 are rare. *)
119110 proc find*(s, sub: []U8): INT;
120-var i, j, n, lastStart: INT; m: U64;
111+var i, j, n, lastStart: INT; m, v, w, b0, b1: U64; p: ^U64;
121112 begin
122113 n := LEN(sub);
123114 if n = 0 then return 0; end;
124115 if n > LEN(s) then return -1; end;
125116 if n = 1 then return index(s, sub[0]); end;
126117 lastStart := LEN(s) - n;
118+ b0 := U64(sub[0]) * Ones;
119+ b1 := U64(sub[n - 1]) * Ones;
127120 i := 0;
128121 while i + 7 <= lastStart do
129- m := matches(word(s, i), sub[0]) & matches(word(s, i + n - 1), sub[n - 1]);
122+ p := ^U64(ADR(s[i]));
123+ v := p^ xor b0;
124+ p := ^U64(ADR(s[i + n - 1]));
125+ w := p^ xor b1;
126+ m := ~(((v & Lows) + Lows) | v | Lows) & ~(((w & Lows) + Lows) | w | Lows) & Tops;
130127 while m # 0 do
131- j := i + lowest(m);
128+ j := i + INT((((m & (0 - m)) - 1) & Ones) * Ones shr 56) - 1;
132129 if s[j..j + n] = sub then return j; end;
133130 m := m & (m - 1);
134131 end;