From c04ee33e530e8eff223103fde2019b5270ee71a4 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Tue, 13 Oct 2026 11:31:13 +0100 Subject: [PATCH] Bytes: index, last, count and find, eight bytes at a time, sixteen with SSE2 Bytes.raz is portable (SWAR in a U64); Bytes.x86_64.raz has the hot loops as SSE2 code procedures. Str.find calls Bytes.find. test/bench gains Lines, against FPC's IndexByte. The compiler follows 2026-10.19. --- diff --git a/CLAUDE.md b/CLAUDE.md index c87baea..eb3a8bf 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -99,14 +99,15 @@ defect, and the library grows only by what breaks no program written for it. A wish for more, the owner's or yours, goes on the list in `PLAN.md`, for a later thaw; it isn't built. v0.6.0 was such a thaw, -by the owner's word: report 2026-10.12; the compiler follows 2026-10.18, +by the owner's word: report 2026-10.12; the compiler follows 2026-10.19, whose `Sys` writes without waiting, for rat's pastes (10.13), and whose blank, `_ := f(x)`, drops a result that is otherwise warned of (10.14), and whose `Math` has `pow`, the logarithms to 2 and 10, rounding and the inverse functions, and `Sys` `space` (10.15), and whose constants may be records and tables of them, with `Str.copy` (10.16), and `COPY` (10.17), also by the owner's word, and whose code -procedures take and return slices (10.18). Its parser +procedures take and return slices (10.18), and whose library has +`Bytes` (10.19). Its parser reaches the machine only through `gen.pas`. Keep `doc/compiler.md`'s table of lines and its routine names true when the compiler changes. diff --git a/Makefile b/Makefile index ad258bc..de82658 100644 --- a/Makefile +++ b/Makefile @@ -82,7 +82,7 @@ # raz-lib(3), the library's manual page, made by tools/Doc.raz from # its sources, in the order of the report's section 11 LIBDOC = Rt Mem Heap Arena Lock Thread.linux-x86_64 Out Sys.linux-x86_64 \ - Str Buf Fmt List Map Sort Math Files Opt C + Str Bytes.x86_64 Buf Fmt List Map Sort Math Files Opt C man: build/raz-lib.3 diff --git a/README.md b/README.md index d0c2793..00f9235 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ `Rt` (ending the program, failed checks, signals), `Sys` (system calls), `Out` (buffered output), `C`, `Lock`, `Thread` (threads without C), `Mem`, -`Heap`, `Arena`, `Str`, `Buf`, `Fmt`, `Math`, `Files`, `Opt` (a command +`Heap`, `Arena`, `Str`, `Bytes`, `Buf`, `Fmt`, `Math`, `Files`, `Opt` (a command line read through one table), `List(T)`, `Map(K, V; hash, eq)` and `Sort(T; less)`. Section 11 of the report gives each one's interface, and `tools/Doc.raz` prints it from the module's file: diff --git a/lib/Bytes.raz b/lib/Bytes.raz new file mode 100644 index 0000000..672859b --- /dev/null +++ b/lib/Bytes.raz @@ -0,0 +1,300 @@ +(* Searches of bytes, many at a time: the first and the last place of a + byte in a []U8, how many places it has, and where one []U8 first + starts in another. This body is for any 64-bit target: eight bytes + at a time in a U64. Where the target has instructions for it, a + file of its own, as Bytes.x86_64.raz does, does more at a time; the + interface is the same. *) +module Bytes; +import Rt; + +const + Ones = 0101010101010101H; (* a one in each byte *) + Lows = 7F7F7F7F7F7F7F7FH; (* the low seven bits of each byte *) + Tops = 8080808080808080H; (* the top bit of each byte *) + +(* eight bytes at s[i]: i + 8 must not pass LEN(s). The address needn't + be a multiple of 8; x86-64 reads such a word as it does any. *) +proc word(s: []U8; i: INT): U64; +var p: ^U64; +begin + p := ^U64(ADR(s[i])); + return p^; +end; + +(* The top bit of each byte of v that is zero, and no other bit. A byte + of v is zero when its low seven bits are, which adding 7FH to them + shows, as no sum overflows into the next byte, and its top bit is. *) +proc zeros(v: U64): U64; +begin + return ~(((v & Lows) + Lows) | v | Lows) & Tops; +end; + +(* The eight bytes of w that are b: their top bits. *) +proc matches(w: U64; b: U8): U64; +begin + return zeros(w xor (U64(b) * Ones)); +end; + +(* The place, 0 to 7, of the lowest top bit that m sets (m is not 0): + the ones below that bit, one in each byte up to its own, summed + into the top byte by the multiply, less one. *) +proc lowest(m: U64): INT; +begin + return INT((((m & (0 - m)) - 1) & Ones) * Ones shr 56) - 1; +end; + +(* The place, 0 to 7, of the highest top bit that m sets (m is not 0). *) +proc highest(m: U64): INT; +var k: INT; +begin + k := 7; + while (m shr (8 * k)) & 80H = 0 do DEC(k); end; + return k; +end; + +(* How many top bits m sets: each moved down to bit 0 of its byte, and + summed into the top byte by the multiply. *) +proc ones(m: U64): INT; +begin + return INT(((m shr 7) * Ones) shr 56); +end; + +(* The first place of b in s, or -1. *) +proc index*(s: []U8; b: U8): INT; +var i: INT; m: U64; +begin + i := 0; + while i + 8 <= LEN(s) do + m := matches(word(s, i), b); + if m # 0 then return i + lowest(m); end; + i := i + 8; + end; + while i < LEN(s) do + if s[i] = b then return i; end; + INC(i); + end; + return -1; +end; + +(* The last place of b in s, or -1. *) +proc last*(s: []U8; b: U8): INT; +var i: INT; m: U64; +begin + i := LEN(s); + while i >= 8 do + m := matches(word(s, i - 8), b); + if m # 0 then return i - 8 + highest(m); end; + i := i - 8; + end; + while i > 0 do + DEC(i); + if s[i] = b then return i; end; + end; + return -1; +end; + +(* How many bytes of s are b. *) +proc count*(s: []U8; b: U8): INT; +var i, n: INT; +begin + i := 0; + n := 0; + while i + 8 <= LEN(s) do + n := n + ones(matches(word(s, i), b)); + i := i + 8; + end; + while i < LEN(s) do + if s[i] = b then INC(n); end; + INC(i); + end; + return n; +end; + +(* Where sub first starts in s, or -1; 0 for an empty sub. Eight places + are tried at once, by two bytes of sub, its first and its last, + each compared at its place: a place is checked whole only where + both agree. They are the two bytes farthest apart, so as far from + being the same test as two can be, and need no table of which bytes + are rare. *) +proc find*(s, sub: []U8): INT; +var i, j, n, lastStart: INT; m: U64; +begin + n := LEN(sub); + if n = 0 then return 0; end; + if n > LEN(s) then return -1; end; + if n = 1 then return index(s, sub[0]); end; + lastStart := LEN(s) - n; + i := 0; + while i + 7 <= lastStart do + m := matches(word(s, i), sub[0]) & matches(word(s, i + n - 1), sub[n - 1]); + while m # 0 do + j := i + lowest(m); + if s[j..j + n] = sub then return j; end; + m := m & (m - 1); + end; + i := i + 8; + end; + while i <= lastStart do + if s[i..i + n] = sub then return i; end; + INC(i); + end; + return -1; +end; + +(* The tests: each result against a plain loop over the same bytes. *) + +var + seed: U64; + fills: [6]U8; (* bytes that tempt the zero-byte test *) + buf: [160]U8; + subs: [24]U8; + +(* a pseudo-random number below n *) +proc random(n: INT): INT; +begin + seed := seed * 6364136223846793005 + 1442695040888963407; + return INT((seed shr 33) mod U64(n)); +end; + +proc refIndex(s: []U8; b: U8): INT; +var i: INT; +begin + for i := 0 to LEN(s) - 1 do + if s[i] = b then return i; end; + end; + return -1; +end; + +proc refLast(s: []U8; b: U8): INT; +var i: INT; +begin + for i := LEN(s) - 1 to 0 by -1 do + if s[i] = b then return i; end; + end; + return -1; +end; + +proc refCount(s: []U8; b: U8): INT; +var i, n: INT; +begin + n := 0; + for i := 0 to LEN(s) - 1 do + if s[i] = b then INC(n); end; + end; + return n; +end; + +proc refFind(s, sub: []U8): INT; +var i: INT; +begin + for i := 0 to LEN(s) - LEN(sub) do + if s[i..i + LEN(sub)] = sub then return i; end; + end; + return -1; +end; + +(* index, last and count of b in s agree with the plain loops *) +proc checkByte(s: []U8; b: U8); +begin + if index(s, b) # refIndex(s, b) then Rt.stop("Bytes.index"); end; + if last(s, b) # refLast(s, b) then Rt.stop("Bytes.last"); end; + if count(s, b) # refCount(s, b) then Rt.stop("Bytes.count"); end; +end; + +proc checkFind(s, sub: []U8); +begin + if find(s, sub) # refFind(s, sub) then Rt.stop("Bytes.find"); end; +end; + +(* every length to 40, from every start 0 to 8, with one byte at every + place, among each kind of filler: the first place, the last, and + the places either side of a block of 8 or 16 *) +proc testEdges; +var n, o, p, f, g, i: INT; +begin + for o := 0 to 8 do + for n := 0 to 40 do + for f := 0 to 5 do + for g := 0 to 5 do + for i := 0 to 159 do buf[i] := fills[f]; end; + checkByte(buf[o..o + n], fills[g]); + for p := 0 to n - 1 do + buf[o + p] := fills[g]; + checkByte(buf[o..o + n], fills[g]); + buf[o + p] := fills[f]; + end; + end; + end; + end; + end; +end; + +(* random bytes from a few kinds, of every length to 150 *) +proc testRandom; +var n, i, k, o: INT; +begin + for n := 0 to 150 do + for k := 1 to 20 do + o := random(8); + for i := 0 to 159 do buf[i] := fills[random(2 + k mod 5)]; end; + for i := 0 to 5 do checkByte(buf[o..o + n], fills[i]); end; + checkByte(buf[o..o + n], 41H); + end; + end; +end; + +(* a sub of every length at every place of a long slice, among fillers + that are almost its bytes; the slice cut short just before and just + after it; also subs longer than the slice, and empty ones *) +proc testPlanted; +var n, p, i, f: INT; s: []U8; +begin + for f := 0 to 2 do + for n := 1 to 24 do + for i := 0 to 23 do subs[i] := U8(0F0H + random(2) + i mod 3); end; + for p := 0 to 159 - n do + for i := 0 to 159 do buf[i] := U8(0F0H + f); end; + COPY(buf[p..p + n], subs[0..n]); + checkFind(buf[0..160], subs[0..n]); + checkFind(buf[1..160], subs[0..n]); + checkFind(buf[0..p + n], subs[0..n]); + checkFind(buf[0..p + n - 1], subs[0..n]); + end; + end; + end; + if find(buf[0..5], "") # 0 then Rt.stop("Bytes.find, empty sub"); end; + if find(buf[0..0], "") # 0 then Rt.stop("Bytes.find, both empty"); end; + if find(buf[0..3], buf[0..4]) # -1 then Rt.stop("Bytes.find, sub longer"); end; + s := "abcabd"; + if (find(s, "abd") # 3) or (find(s, "bc") # 1) or (find(s, "d") # 5) or (find(s, s) # 0) + or (find(s, "abcabde") # -1) then Rt.stop("Bytes.find, text"); end; +end; + +(* random text and subs from two or three kinds of byte, so that one + end of the sub is often where the other isn't *) +proc testFind; +var n, m, i, k, o: INT; +begin + for n := 0 to 100 do + for k := 1 to 12 do + o := random(8); + for i := 0 to 159 do buf[i] := fills[3 + random(2 + k mod 2)]; end; + for m := 0 to 24 do + for i := 0 to 23 do subs[i] := fills[3 + random(2 + k mod 2)]; end; + checkFind(buf[o..o + n], subs[0..m]); + end; + end; + end; +end; + +proc main*; +begin + fills := [0, 1, 7FH, 80H, 81H, 0FFH]; + seed := 12345; + testEdges; + testRandom; + testPlanted; + testFind; +end; + +end Bytes. diff --git a/lib/Bytes.x86_64.raz b/lib/Bytes.x86_64.raz new file mode 100644 index 0000000..401929f --- /dev/null +++ b/lib/Bytes.x86_64.raz @@ -0,0 +1,348 @@ +(* Searches of bytes, many at a time: the first and the last place of a + byte in a []U8, how many places it has, and where one []U8 first + starts in another. This body is for x86-64, where SSE2 compares + sixteen bytes at a time; Bytes.raz is the same interface for any + 64-bit target, eight at a time in a U64. *) +module Bytes; +import Rt; + +(* The first place of b in s, or -1. Sixteen bytes at a time are + compared with b's sixteen copies (pcmpeqb), and pmovmskb makes of the + result a number with a bit set for each byte that matched. The bytes + left over, fewer than sixteen, are compared one by one. Where the + machine finds them: s's address in rdi, its length in rsi, b in dl. *) +proc index*(s: []U8; b: U8): INT; +code + movd xmm1, edx; + punpcklbw xmm1, xmm1; (* b in the low two bytes... *) + punpcklwd xmm1, xmm1; (* ...in the low four... *) + pshufd xmm1, xmm1, 0; (* ...and in all sixteen *) + xor ecx, ecx; + blocks: + lea rax, [rcx + 16]; + cmp rax, rsi; + ja tail; + movdqu xmm0, [rdi + rcx]; + pcmpeqb xmm0, xmm1; + pmovmskb eax, xmm0; + test eax, eax; + jnz found; + add rcx, 16; + jmp blocks; + found: + bsf eax, eax; (* the lowest bit set: the first match *) + add rax, rcx; + ret; + tail: + cmp rcx, rsi; + jae missing; + cmp [rdi + rcx], dl; + je here; + inc rcx; + jmp tail; + here: + mov rax, rcx; + ret; + missing: + mov rax, -1; + ret; +end index; + +(* The last place of b in s, or -1: as index, from the end. *) +proc last*(s: []U8; b: U8): INT; +code + movd xmm1, edx; + punpcklbw xmm1, xmm1; + punpcklwd xmm1, xmm1; + pshufd xmm1, xmm1, 0; + mov rcx, rsi; (* the bytes not yet seen are the first rcx *) + blocks: + cmp rcx, 16; + jb tail; + sub rcx, 16; + movdqu xmm0, [rdi + rcx]; + pcmpeqb xmm0, xmm1; + pmovmskb eax, xmm0; + test eax, eax; + jz blocks; + bsr eax, eax; (* the highest bit set: the last match *) + add rax, rcx; + ret; + tail: + test rcx, rcx; + jz missing; + dec rcx; + cmp [rdi + rcx], dl; + jne tail; + mov rax, rcx; + ret; + missing: + mov rax, -1; + ret; +end last; + +(* How many bytes of s are b: the bits of each block's number, counted. *) +proc count*(s: []U8; b: U8): INT; +code + movd xmm1, edx; + punpcklbw xmm1, xmm1; + punpcklwd xmm1, xmm1; + pshufd xmm1, xmm1, 0; + xor eax, eax; (* the count *) + xor ecx, ecx; + blocks: + lea r8, [rcx + 16]; + cmp r8, rsi; + ja tail; + movdqu xmm0, [rdi + rcx]; + pcmpeqb xmm0, xmm1; + pmovmskb r8d, xmm0; + popcnt r8d, r8d; + add rax, r8; + add rcx, 16; + jmp blocks; + tail: + cmp rcx, rsi; + jae done; + cmp [rdi + rcx], dl; + jne next; + inc rax; + next: + inc rcx; + jmp tail; + done: + ret; +end count; + +(* The first place i, from from on, with s[i] = a and s[i + gap] = b, + or -1; LEN(s) must be more than gap. Sixteen places are tried at + once: sixteen bytes are compared with a, sixteen from gap places on + with b, and the two results anded. The places in rdi and rsi (s), + rdx (gap), cl (a), r8b (b) and r9 (from). *) +proc pair(s: []U8; gap: INT; a, b: U8; from: INT): INT; +code + mov r11, rcx; (* a, for the places one by one *) + movd xmm1, ecx; + punpcklbw xmm1, xmm1; + punpcklwd xmm1, xmm1; + pshufd xmm1, xmm1, 0; + movd xmm2, r8d; + punpcklbw xmm2, xmm2; + punpcklwd xmm2, xmm2; + pshufd xmm2, xmm2, 0; + mov r10, rsi; + sub r10, rdx; (* the places are those below r10 *) + mov rax, r9; + blocks: + lea rcx, [rax + 16]; + cmp rcx, r10; + ja tail; + movdqu xmm0, [rdi + rax]; + pcmpeqb xmm0, xmm1; + lea rcx, [rax + rdx]; + movdqu xmm3, [rdi + rcx]; + pcmpeqb xmm3, xmm2; + pand xmm0, xmm3; + pmovmskb ecx, xmm0; + test ecx, ecx; + jnz found; + add rax, 16; + jmp blocks; + found: + bsf ecx, ecx; + add rax, rcx; + ret; + tail: + cmp rax, r10; + jae missing; + cmp [rdi + rax], r11b; + jne next; + lea rcx, [rax + rdx]; + cmp [rdi + rcx], r8b; + je here; + next: + inc rax; + jmp tail; + here: + ret; + missing: + mov rax, -1; + ret; +end pair; + +(* Where sub first starts in s, or -1; 0 for an empty sub. Places are + found by two bytes of sub, its first and its last, each compared at + its place (pair), and checked whole only where both agree. They are + the two bytes farthest apart, so as far from being the same test as + two can be, and need no table of which bytes are rare. *) +proc find*(s, sub: []U8): INT; +var i, n: INT; +begin + n := LEN(sub); + if n = 0 then return 0; end; + if n > LEN(s) then return -1; end; + i := pair(s, n - 1, sub[0], sub[n - 1], 0); + while i >= 0 do + if s[i..i + n] = sub then return i; end; + i := pair(s, n - 1, sub[0], sub[n - 1], i + 1); + end; + return -1; +end; + +(* The tests: each result against a plain loop over the same bytes. *) + +var + seed: U64; + fills: [6]U8; (* bytes that tempt the zero-byte test *) + buf: [160]U8; + subs: [24]U8; + +(* a pseudo-random number below n *) +proc random(n: INT): INT; +begin + seed := seed * 6364136223846793005 + 1442695040888963407; + return INT((seed shr 33) mod U64(n)); +end; + +proc refIndex(s: []U8; b: U8): INT; +var i: INT; +begin + for i := 0 to LEN(s) - 1 do + if s[i] = b then return i; end; + end; + return -1; +end; + +proc refLast(s: []U8; b: U8): INT; +var i: INT; +begin + for i := LEN(s) - 1 to 0 by -1 do + if s[i] = b then return i; end; + end; + return -1; +end; + +proc refCount(s: []U8; b: U8): INT; +var i, n: INT; +begin + n := 0; + for i := 0 to LEN(s) - 1 do + if s[i] = b then INC(n); end; + end; + return n; +end; + +proc refFind(s, sub: []U8): INT; +var i: INT; +begin + for i := 0 to LEN(s) - LEN(sub) do + if s[i..i + LEN(sub)] = sub then return i; end; + end; + return -1; +end; + +(* index, last and count of b in s agree with the plain loops *) +proc checkByte(s: []U8; b: U8); +begin + if index(s, b) # refIndex(s, b) then Rt.stop("Bytes.index"); end; + if last(s, b) # refLast(s, b) then Rt.stop("Bytes.last"); end; + if count(s, b) # refCount(s, b) then Rt.stop("Bytes.count"); end; +end; + +proc checkFind(s, sub: []U8); +begin + if find(s, sub) # refFind(s, sub) then Rt.stop("Bytes.find"); end; +end; + +(* every length to 40, from every start 0 to 8, with one byte at every + place, among each kind of filler: the first place, the last, and + the places either side of a block of 8 or 16 *) +proc testEdges; +var n, o, p, f, g, i: INT; +begin + for o := 0 to 8 do + for n := 0 to 40 do + for f := 0 to 5 do + for g := 0 to 5 do + for i := 0 to 159 do buf[i] := fills[f]; end; + checkByte(buf[o..o + n], fills[g]); + for p := 0 to n - 1 do + buf[o + p] := fills[g]; + checkByte(buf[o..o + n], fills[g]); + buf[o + p] := fills[f]; + end; + end; + end; + end; + end; +end; + +(* random bytes from a few kinds, of every length to 150 *) +proc testRandom; +var n, i, k, o: INT; +begin + for n := 0 to 150 do + for k := 1 to 20 do + o := random(8); + for i := 0 to 159 do buf[i] := fills[random(2 + k mod 5)]; end; + for i := 0 to 5 do checkByte(buf[o..o + n], fills[i]); end; + checkByte(buf[o..o + n], 41H); + end; + end; +end; + +(* a sub of every length at every place of a long slice, among fillers + that are almost its bytes; the slice cut short just before and just + after it; also subs longer than the slice, and empty ones *) +proc testPlanted; +var n, p, i, f: INT; s: []U8; +begin + for f := 0 to 2 do + for n := 1 to 24 do + for i := 0 to 23 do subs[i] := U8(0F0H + random(2) + i mod 3); end; + for p := 0 to 159 - n do + for i := 0 to 159 do buf[i] := U8(0F0H + f); end; + COPY(buf[p..p + n], subs[0..n]); + checkFind(buf[0..160], subs[0..n]); + checkFind(buf[1..160], subs[0..n]); + checkFind(buf[0..p + n], subs[0..n]); + checkFind(buf[0..p + n - 1], subs[0..n]); + end; + end; + end; + if find(buf[0..5], "") # 0 then Rt.stop("Bytes.find, empty sub"); end; + if find(buf[0..0], "") # 0 then Rt.stop("Bytes.find, both empty"); end; + if find(buf[0..3], buf[0..4]) # -1 then Rt.stop("Bytes.find, sub longer"); end; + s := "abcabd"; + if (find(s, "abd") # 3) or (find(s, "bc") # 1) or (find(s, "d") # 5) or (find(s, s) # 0) + or (find(s, "abcabde") # -1) then Rt.stop("Bytes.find, text"); end; +end; + +(* random text and subs from two or three kinds of byte, so that one + end of the sub is often where the other isn't *) +proc testFind; +var n, m, i, k, o: INT; +begin + for n := 0 to 100 do + for k := 1 to 12 do + o := random(8); + for i := 0 to 159 do buf[i] := fills[3 + random(2 + k mod 2)]; end; + for m := 0 to 24 do + for i := 0 to 23 do subs[i] := fills[3 + random(2 + k mod 2)]; end; + checkFind(buf[o..o + n], subs[0..m]); + end; + end; + end; +end; + +proc main*; +begin + fills := [0, 1, 7FH, 80H, 81H, 0FFH]; + seed := 12345; + testEdges; + testRandom; + testPlanted; + testFind; +end; + +end Bytes. diff --git a/lib/Str.raz b/lib/Str.raz index 1365b75..425fd87 100644 --- a/lib/Str.raz +++ b/lib/Str.raz @@ -2,7 +2,7 @@ module orders, searches and hashes them, reads and writes their UTF-8, reads a number from one, and copies one to keep. *) module Str; -import Mem, Heap, Rt; +import Mem, Heap, Rt, Bytes; const least = -9223372036854775807 - 1; (* the smallest I64 *) @@ -54,12 +54,8 @@ (* Where sub first starts in s, or -1. *) proc find*(s, sub: []U8): INT; -var i: INT; begin - for i := 0 to LEN(s) - LEN(sub) do - if s[i..i + LEN(sub)] = sub then return i; end; - end; - return -1; + return Bytes.find(s, sub); end; (* Whether s starts with p. *) diff --git a/src/raz.pas b/src/raz.pas index fd67450..d3f3f35 100644 --- a/src/raz.pas +++ b/src/raz.pas @@ -34,7 +34,7 @@ { what git describe says, made by make: 0.3.0 at the tag v0.3.0, 0.3.0-5-g38d29a8 five commits past it } Version = {$I version.inc}; - Report = '2026-10.18'; { the version of doc/raz.md it follows } + Report = '2026-10.19'; { the version of doc/raz.md it follows } MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } MfdCloexec = 1; { an ELF still executes: the kernel opens it first } diff --git a/test/bench/Lines.raz b/test/bench/Lines.raz new file mode 100644 index 0000000..d121230 --- /dev/null +++ b/test/bench/Lines.raz @@ -0,0 +1,45 @@ +(* The lines of a text, counted all at once with Bytes.count, and walked + one by one with Bytes.index: lines.pas does the same with FPC's + IndexByte, which is assembly, and a counting loop. *) +module Lines; +import Out, Bytes; +check off; + +const + N = 16777216; + Rounds = 32; + Letters = "etaoinshrdlucmfw"; + +var text: [N]U8; + +proc main*; +var i, r, x, v, k, total, walked: 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; + total := 0; + walked := 0; + for r := 1 to Rounds do + total := total + Bytes.count(text, 10); + i := 0; + k := Bytes.index(text, 10); + while k >= 0 do + INC(walked); + i := i + k + 1; + k := Bytes.index(text[i..N], 10); + end; + end; + Out.int(total); + Out.str(" "); + Out.int(walked); + Out.ln; +end; + +end Lines. diff --git a/test/bench/lines.pas b/test/bench/lines.pas new file mode 100644 index 0000000..69f2270 --- /dev/null +++ b/test/bench/lines.pas @@ -0,0 +1,44 @@ +{ Lines.raz's twin. } +program lines; +{$mode objfpc} +const + N = 16777216; + Rounds = 32; + Letters = 'etaoinshrdlucmfw'; +var + text: array[0..N - 1] of byte; + i, r, x, v, k, total, walked: int64; + +function Count(p: pbyte; n: int64): int64; +var i, c: int64; +begin + c := 0; + for i := 0 to n - 1 do + if p[i] = 10 then inc(c); + Count := c; +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; + total := 0; + walked := 0; + for r := 1 to Rounds do begin + total := total + Count(@text[0], N); + i := 0; + k := IndexByte(text[0], N, 10); + while k >= 0 do begin + inc(walked); + i := i + k + 1; + if i >= N then break; + k := IndexByte(text[i], N - i, 10); + end; + end; + writeln(total, ' ', walked); +end.