raz / commit 2eba0e44

commit
2eba0e44f101ff359980f34b7c94a842675ee3d2
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
1107e80e
A fourth kernel: Grep, lupa's search eight bytes at a time

The lines of a 16 MB text, and those holding "error", found as lupa
finds them with SSE2 but in U64 words: the zero-byte test on the word
xor the byte's copies, the word sought by two of its bytes. Its twin
is the same code over PByte, with no inline, since raz has none.
raz 250 ms, FPC -O2 242, ulam 262.

patch · log

2 files changed, +262 -0

test/bench/Grep.raz (history) added +141 -0

@@ -0,0 +1,141 @@
1+(* A grep: the lines of a text, and those that hold a word, found
2+ eight bytes at a time, as lupa (~/lib/utb) finds them sixteen at a
3+ time with SSE2. A word of eight bytes holds a byte b exactly where
4+ w xor b's eight copies has a zero byte; (v - 0101...H) & ~v &
5+ 8080...H sets the top bit of the lowest such byte, and maybe of
6+ ones above it, which each place's own check rules out. The word is
7+ sought by two of its bytes, each at its place in it, and checked
8+ whole only where both are. *)
9+module Grep;
10+import Out;
11+check off;
12+
13+const
14+ N = 16777216;
15+ Rounds = 16;
16+ Letters = "etaoinshrdlucmfw";
17+ Ones = 0101010101010101H;
18+ Tops = 8080808080808080H;
19+ Word = "error";
20+ A = 1; (* the two bytes sought: the r at 1 and the o at 3 *)
21+ B = 3;
22+
23+var text: [N]U8;
24+
25+(* the top bit of each byte of v that is zero, the lowest one exactly *)
26+proc zeros(v: U64): U64;
27+begin
28+ return (v - Ones) & ~v & Tops;
29+end;
30+
31+(* the place of the lowest byte whose top bit m sets: the ones below
32+ that bit, a one in each byte below it and one in its own, summed
33+ into the top byte by the multiply *)
34+proc lowest(m: U64): INT;
35+begin
36+ return INT((((m & (0 - m)) - 1) & Ones) * Ones shr 56) - 1;
37+end;
38+
39+(* where the first line feed in s is, or LEN(s) *)
40+proc lineEnd(s: []U8): INT;
41+var i: INT; p: ^U64; m: U64;
42+begin
43+ i := 0;
44+ while i + 8 <= LEN(s) do
45+ p := ^U64(ADR(s[i]));
46+ m := zeros(p^ xor 0A0A0A0A0A0A0A0AH);
47+ if m # 0 then return i + lowest(m); end;
48+ i := i + 8;
49+ end;
50+ while (i < LEN(s)) and (s[i] # 10) do INC(i); end;
51+ return i;
52+end;
53+
54+(* where Word first starts in s, or -1 *)
55+proc find(s: []U8): INT;
56+var i, j, last: INT; p, q: ^U64; m, wa, wb: U64;
57+begin
58+ last := LEN(s) - LEN(Word);
59+ wa := U64(Word[A]) * Ones;
60+ wb := U64(Word[B]) * Ones;
61+ i := 0;
62+ while i + 7 <= last do
63+ p := ^U64(ADR(s[i + A]));
64+ q := ^U64(ADR(s[i + B]));
65+ m := zeros(p^ xor wa) & zeros(q^ xor wb);
66+ while m # 0 do
67+ j := i + lowest(m);
68+ if s[j..j + LEN(Word)] = Word then return j; end;
69+ m := m & (m - 1);
70+ end;
71+ i := i + 8;
72+ end;
73+ while i <= last do
74+ if s[i..i + LEN(Word)] = Word then return i; end;
75+ INC(i);
76+ end;
77+ return -1;
78+end;
79+
80+(* how many lines s has, a line feed ending each *)
81+proc lines(s: []U8): INT;
82+var i, n: INT;
83+begin
84+ i := 0;
85+ n := 0;
86+ while i < LEN(s) do
87+ i := i + lineEnd(s[i..LEN(s)]) + 1;
88+ INC(n);
89+ end;
90+ return n;
91+end;
92+
93+(* how many lines of s hold Word: from each match on to its line's end *)
94+proc matching(s: []U8): INT;
95+var i, k, n: INT;
96+begin
97+ i := 0;
98+ n := 0;
99+ k := find(s);
100+ while k >= 0 do
101+ INC(n);
102+ i := i + k;
103+ i := i + lineEnd(s[i..LEN(s)]) + 1;
104+ if i >= LEN(s) then return n; end;
105+ k := find(s[i..LEN(s)]);
106+ end;
107+ return n;
108+end;
109+
110+proc main*;
111+var i, r, x, v, total, hits: INT;
112+begin
113+ x := 1;
114+ for i := 0 to N - 1 do
115+ x := (x * 1103515245 + 12345) & 7FFFFFFFH;
116+ v := (x shr 16) & 63;
117+ if v = 0 then text[i] := 10;
118+ elsif v < 10 then text[i] := U8(' ');
119+ else text[i] := Letters[v & 15];
120+ end;
121+ end;
122+ text[N - 1] := 10;
123+ (* by chance the word is about four times in the text: some more *)
124+ i := 0;
125+ while i < N - 1000 do
126+ COPY(text[i..i + LEN(Word)], Word);
127+ i := i + 997;
128+ end;
129+ total := 0;
130+ hits := 0;
131+ for r := 1 to Rounds do
132+ total := total + lines(text);
133+ hits := hits + matching(text);
134+ end;
135+ Out.int(total);
136+ Out.str(" ");
137+ Out.int(hits);
138+ Out.ln;
139+end;
140+
141+end Grep.

test/bench/grep.pas (history) added +121 -0

@@ -0,0 +1,121 @@
1+{ Grep.raz's twin. }
2+program grep;
3+{$mode objfpc}
4+const
5+ N = 16777216;
6+ Rounds = 16;
7+ Letters: string[16] = 'etaoinshrdlucmfw';
8+ Ones = qword($0101010101010101);
9+ Tops = qword($8080808080808080);
10+ Word: string[5] = 'error';
11+ A = 1;
12+ B = 3;
13+var
14+ text: array[0..N - 1] of byte;
15+ i, r, x, v, total, hits: int64;
16+
17+function Zeros(v: qword): qword;
18+begin
19+ Zeros := (v - Ones) and not v and Tops;
20+end;
21+
22+function Lowest(m: qword): int64;
23+begin
24+ Lowest := int64((((m and (0 - m)) - 1) and Ones) * Ones shr 56) - 1;
25+end;
26+
27+function Holds(s: PByte): boolean;
28+var k: int64;
29+begin
30+ for k := 0 to Length(Word) - 1 do
31+ if s[k] <> ord(Word[k + 1]) then exit(false);
32+ Holds := true;
33+end;
34+
35+function LineEnd(s: PByte; n: int64): int64;
36+var i: int64; m: qword;
37+begin
38+ i := 0;
39+ while i + 8 <= n do begin
40+ m := Zeros(PQWord(@s[i])^ xor qword($0A0A0A0A0A0A0A0A));
41+ if m <> 0 then exit(i + Lowest(m));
42+ i := i + 8;
43+ end;
44+ while (i < n) and (s[i] <> 10) do inc(i);
45+ LineEnd := i;
46+end;
47+
48+function Find(s: PByte; n: int64): int64;
49+var i, j, last: int64; m, wa, wb: qword;
50+begin
51+ last := n - Length(Word);
52+ wa := qword(ord(Word[A + 1])) * Ones;
53+ wb := qword(ord(Word[B + 1])) * Ones;
54+ i := 0;
55+ while i + 7 <= last do begin
56+ m := Zeros(PQWord(@s[i + A])^ xor wa) and Zeros(PQWord(@s[i + B])^ xor wb);
57+ while m <> 0 do begin
58+ j := i + Lowest(m);
59+ if Holds(@s[j]) then exit(j);
60+ m := m and (m - 1);
61+ end;
62+ i := i + 8;
63+ end;
64+ while i <= last do begin
65+ if Holds(@s[i]) then exit(i);
66+ inc(i);
67+ end;
68+ Find := -1;
69+end;
70+
71+function Lines(s: PByte; len: int64): int64;
72+var i, n: int64;
73+begin
74+ i := 0;
75+ n := 0;
76+ while i < len do begin
77+ i := i + LineEnd(@s[i], len - i) + 1;
78+ inc(n);
79+ end;
80+ Lines := n;
81+end;
82+
83+function Matching(s: PByte; len: int64): int64;
84+var i, k, n: int64;
85+begin
86+ i := 0;
87+ n := 0;
88+ k := Find(s, len);
89+ while k >= 0 do begin
90+ inc(n);
91+ i := i + k;
92+ i := i + LineEnd(@s[i], len - i) + 1;
93+ if i >= len then exit(n);
94+ k := Find(@s[i], len - i);
95+ end;
96+ Matching := n;
97+end;
98+
99+begin
100+ x := 1;
101+ for i := 0 to N - 1 do begin
102+ x := (x * 1103515245 + 12345) and $7FFFFFFF;
103+ v := (x shr 16) and 63;
104+ if v = 0 then text[i] := 10
105+ else if v < 10 then text[i] := ord(' ')
106+ else text[i] := ord(Letters[(v and 15) + 1]);
107+ end;
108+ text[N - 1] := 10;
109+ i := 0;
110+ while i < N - 1000 do begin
111+ Move(Word[1], text[i], Length(Word));
112+ i := i + 997;
113+ end;
114+ total := 0;
115+ hits := 0;
116+ for r := 1 to Rounds do begin
117+ total := total + Lines(@text[0], N);
118+ hits := hits + Matching(@text[0], N);
119+ end;
120+ writeln(total, ' ', hits);
121+end.