From 2eba0e44f101ff359980f34b7c94a842675ee3d2 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Tue, 13 Oct 2026 10:50:33 +0100 Subject: [PATCH] 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. --- diff --git a/test/bench/Grep.raz b/test/bench/Grep.raz new file mode 100644 index 0000000..eb0f7f2 --- /dev/null +++ b/test/bench/Grep.raz @@ -0,0 +1,141 @@ +(* A grep: the lines of a text, and those that hold a word, found + eight bytes at a time, as lupa (~/lib/utb) finds them sixteen at a + time with SSE2. A word of eight bytes holds a byte b exactly where + w xor b's eight copies has a zero byte; (v - 0101...H) & ~v & + 8080...H sets the top bit of the lowest such byte, and maybe of + ones above it, which each place's own check rules out. The word is + sought by two of its bytes, each at its place in it, and checked + whole only where both are. *) +module Grep; +import Out; +check off; + +const + N = 16777216; + Rounds = 16; + Letters = "etaoinshrdlucmfw"; + Ones = 0101010101010101H; + Tops = 8080808080808080H; + Word = "error"; + A = 1; (* the two bytes sought: the r at 1 and the o at 3 *) + B = 3; + +var text: [N]U8; + +(* the top bit of each byte of v that is zero, the lowest one exactly *) +proc zeros(v: U64): U64; +begin + return (v - Ones) & ~v & Tops; +end; + +(* the place of the lowest byte whose top bit m sets: the ones below + that bit, a one in each byte below it and one in its own, summed + into the top byte by the multiply *) +proc lowest(m: U64): INT; +begin + return INT((((m & (0 - m)) - 1) & Ones) * Ones shr 56) - 1; +end; + +(* where the first line feed in s is, or LEN(s) *) +proc lineEnd(s: []U8): INT; +var i: INT; p: ^U64; m: U64; +begin + i := 0; + while i + 8 <= LEN(s) do + p := ^U64(ADR(s[i])); + m := zeros(p^ xor 0A0A0A0A0A0A0A0AH); + if m # 0 then return i + lowest(m); end; + i := i + 8; + end; + while (i < LEN(s)) and (s[i] # 10) do INC(i); end; + return i; +end; + +(* where Word first starts in s, or -1 *) +proc find(s: []U8): INT; +var i, j, last: INT; p, q: ^U64; m, wa, wb: U64; +begin + last := LEN(s) - LEN(Word); + wa := U64(Word[A]) * Ones; + wb := U64(Word[B]) * Ones; + i := 0; + while i + 7 <= last do + p := ^U64(ADR(s[i + A])); + q := ^U64(ADR(s[i + B])); + m := zeros(p^ xor wa) & zeros(q^ xor wb); + while m # 0 do + j := i + lowest(m); + if s[j..j + LEN(Word)] = Word then return j; end; + m := m & (m - 1); + end; + i := i + 8; + end; + while i <= last do + if s[i..i + LEN(Word)] = Word then return i; end; + INC(i); + end; + return -1; +end; + +(* how many lines s has, a line feed ending each *) +proc lines(s: []U8): INT; +var i, n: INT; +begin + i := 0; + n := 0; + while i < LEN(s) do + i := i + lineEnd(s[i..LEN(s)]) + 1; + INC(n); + end; + return n; +end; + +(* how many lines of s hold Word: from each match on to its line's end *) +proc matching(s: []U8): INT; +var i, k, n: INT; +begin + i := 0; + n := 0; + k := find(s); + while k >= 0 do + INC(n); + i := i + k; + i := i + lineEnd(s[i..LEN(s)]) + 1; + if i >= LEN(s) then return n; end; + k := find(s[i..LEN(s)]); + end; + return n; +end; + +proc main*; +var i, r, x, v, total, hits: INT; +begin + x := 1; + for i := 0 to N - 1 do + x := (x * 1103515245 + 12345) & 7FFFFFFFH; + v := (x shr 16) & 63; + if v = 0 then text[i] := 10; + elsif v < 10 then text[i] := U8(' '); + else text[i] := Letters[v & 15]; + end; + end; + text[N - 1] := 10; + (* by chance the word is about four times in the text: some more *) + i := 0; + while i < N - 1000 do + COPY(text[i..i + LEN(Word)], Word); + i := i + 997; + end; + total := 0; + hits := 0; + for r := 1 to Rounds do + total := total + lines(text); + hits := hits + matching(text); + end; + Out.int(total); + Out.str(" "); + Out.int(hits); + Out.ln; +end; + +end Grep. diff --git a/test/bench/grep.pas b/test/bench/grep.pas new file mode 100644 index 0000000..5c8d894 --- /dev/null +++ b/test/bench/grep.pas @@ -0,0 +1,121 @@ +{ Grep.raz's twin. } +program grep; +{$mode objfpc} +const + N = 16777216; + Rounds = 16; + Letters: string[16] = 'etaoinshrdlucmfw'; + Ones = qword($0101010101010101); + Tops = qword($8080808080808080); + Word: string[5] = 'error'; + A = 1; + B = 3; +var + text: array[0..N - 1] of byte; + i, r, x, v, total, hits: int64; + +function Zeros(v: qword): qword; +begin + Zeros := (v - Ones) and not v and Tops; +end; + +function Lowest(m: qword): int64; +begin + Lowest := int64((((m and (0 - m)) - 1) and Ones) * Ones shr 56) - 1; +end; + +function Holds(s: PByte): boolean; +var k: int64; +begin + for k := 0 to Length(Word) - 1 do + if s[k] <> ord(Word[k + 1]) then exit(false); + Holds := true; +end; + +function LineEnd(s: PByte; n: int64): int64; +var i: int64; m: qword; +begin + i := 0; + while i + 8 <= n do begin + m := Zeros(PQWord(@s[i])^ xor qword($0A0A0A0A0A0A0A0A)); + if m <> 0 then exit(i + Lowest(m)); + i := i + 8; + end; + while (i < n) and (s[i] <> 10) do inc(i); + LineEnd := i; +end; + +function Find(s: PByte; n: int64): int64; +var i, j, last: int64; m, wa, wb: qword; +begin + last := n - Length(Word); + wa := qword(ord(Word[A + 1])) * Ones; + wb := qword(ord(Word[B + 1])) * Ones; + i := 0; + while i + 7 <= last do begin + m := Zeros(PQWord(@s[i + A])^ xor wa) and Zeros(PQWord(@s[i + B])^ xor wb); + while m <> 0 do begin + j := i + Lowest(m); + if Holds(@s[j]) then exit(j); + m := m and (m - 1); + end; + i := i + 8; + end; + while i <= last do begin + if Holds(@s[i]) then exit(i); + inc(i); + end; + Find := -1; +end; + +function Lines(s: PByte; len: int64): int64; +var i, n: int64; +begin + i := 0; + n := 0; + while i < len do begin + i := i + LineEnd(@s[i], len - i) + 1; + inc(n); + end; + Lines := n; +end; + +function Matching(s: PByte; len: int64): int64; +var i, k, n: int64; +begin + i := 0; + n := 0; + k := Find(s, len); + while k >= 0 do begin + inc(n); + i := i + k; + i := i + LineEnd(@s[i], len - i) + 1; + if i >= len then exit(n); + k := Find(@s[i], len - i); + end; + Matching := n; +end; + +begin + x := 1; + for i := 0 to N - 1 do begin + x := (x * 1103515245 + 12345) and $7FFFFFFF; + v := (x shr 16) and 63; + if v = 0 then text[i] := 10 + else if v < 10 then text[i] := ord(' ') + else text[i] := ord(Letters[(v and 15) + 1]); + end; + text[N - 1] := 10; + i := 0; + while i < N - 1000 do begin + Move(Word[1], text[i], Length(Word)); + i := i + 997; + end; + total := 0; + hits := 0; + for r := 1 to Rounds do begin + total := total + Lines(@text[0], N); + hits := hits + Matching(@text[0], N); + end; + writeln(total, ' ', hits); +end.