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.
| @@ -99,14 +99,15 @@ | ||
| 99 | 99 | defect, and the library grows only by what breaks no program written |
| 100 | 100 | for it. A wish for more, the owner's or yours, goes on the list in |
| 101 | 101 | `PLAN.md`, for a later thaw; it isn't built. v0.6.0 was such a thaw, |
| 102 | -by the owner's word: report 2026-10.12; the compiler follows 2026-10.18, | |
| 102 | +by the owner's word: report 2026-10.12; the compiler follows 2026-10.19, | |
| 103 | 103 | whose `Sys` writes without waiting, for rat's pastes (10.13), and |
| 104 | 104 | whose blank, `_ := f(x)`, drops a result that is otherwise warned of |
| 105 | 105 | (10.14), and whose `Math` has `pow`, the logarithms to 2 and 10, |
| 106 | 106 | rounding and the inverse functions, and `Sys` `space` (10.15), and |
| 107 | 107 | whose constants may be records and tables of them, with `Str.copy` |
| 108 | 108 | (10.16), and `COPY` (10.17), also by the owner's word, and whose code |
| 109 | -procedures take and return slices (10.18). Its parser | |
| 109 | +procedures take and return slices (10.18), and whose library has | |
| 110 | +`Bytes` (10.19). Its parser | |
| 110 | 111 | reaches the machine only through `gen.pas`. Keep |
| 111 | 112 | `doc/compiler.md`'s table of lines and its routine names true when the |
| 112 | 113 | compiler changes. |
| @@ -82,7 +82,7 @@ | ||
| 82 | 82 | # raz-lib(3), the library's manual page, made by tools/Doc.raz from |
| 83 | 83 | # its sources, in the order of the report's section 11 |
| 84 | 84 | LIBDOC = Rt Mem Heap Arena Lock Thread.linux-x86_64 Out Sys.linux-x86_64 \ |
| 85 | - Str Buf Fmt List Map Sort Math Files Opt C | |
| 85 | + Str Bytes.x86_64 Buf Fmt List Map Sort Math Files Opt C | |
| 86 | 86 | |
| 87 | 87 | man: build/raz-lib.3 |
| 88 | 88 |
| @@ -156,7 +156,7 @@ | ||
| 156 | 156 | |
| 157 | 157 | `Rt` (ending the program, failed checks, signals), `Sys` (system calls), `Out` |
| 158 | 158 | (buffered output), `C`, `Lock`, `Thread` (threads without C), `Mem`, |
| 159 | -`Heap`, `Arena`, `Str`, `Buf`, `Fmt`, `Math`, `Files`, `Opt` (a command | |
| 159 | +`Heap`, `Arena`, `Str`, `Bytes`, `Buf`, `Fmt`, `Math`, `Files`, `Opt` (a command | |
| 160 | 160 | line read through one table), `List(T)`, `Map(K, V; hash, eq)` and |
| 161 | 161 | `Sort(T; less)`. Section 11 of the report gives each one's interface, |
| 162 | 162 | and `tools/Doc.raz` prints it from the module's file: |
| @@ -0,0 +1,300 @@ | ||
| 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 any 64-bit target: eight bytes | |
| 4 | + at a time in a U64. Where the target has instructions for it, a | |
| 5 | + file of its own, as Bytes.x86_64.raz does, does more at a time; the | |
| 6 | + interface is the same. *) | |
| 7 | +module Bytes; | |
| 8 | +import Rt; | |
| 9 | + | |
| 10 | +const | |
| 11 | + Ones = 0101010101010101H; (* a one in each byte *) | |
| 12 | + Lows = 7F7F7F7F7F7F7F7FH; (* the low seven bits of each byte *) | |
| 13 | + Tops = 8080808080808080H; (* the top bit of each byte *) | |
| 14 | + | |
| 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; | |
| 23 | + | |
| 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; | |
| 31 | + | |
| 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; | |
| 37 | + | |
| 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; | |
| 45 | + | |
| 46 | +(* The place, 0 to 7, of the highest top bit that m sets (m is not 0). *) | |
| 47 | +proc highest(m: U64): INT; | |
| 48 | +var k: INT; | |
| 49 | +begin | |
| 50 | + k := 7; | |
| 51 | + while (m shr (8 * k)) & 80H = 0 do DEC(k); end; | |
| 52 | + return k; | |
| 53 | +end; | |
| 54 | + | |
| 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 | + | |
| 62 | +(* The first place of b in s, or -1. *) | |
| 63 | +proc index*(s: []U8; b: U8): INT; | |
| 64 | +var i: INT; m: U64; | |
| 65 | +begin | |
| 66 | + i := 0; | |
| 67 | + while i + 8 <= LEN(s) do | |
| 68 | + m := matches(word(s, i), b); | |
| 69 | + if m # 0 then return i + lowest(m); end; | |
| 70 | + i := i + 8; | |
| 71 | + end; | |
| 72 | + while i < LEN(s) do | |
| 73 | + if s[i] = b then return i; end; | |
| 74 | + INC(i); | |
| 75 | + end; | |
| 76 | + return -1; | |
| 77 | +end; | |
| 78 | + | |
| 79 | +(* The last place of b in s, or -1. *) | |
| 80 | +proc last*(s: []U8; b: U8): INT; | |
| 81 | +var i: INT; m: U64; | |
| 82 | +begin | |
| 83 | + i := LEN(s); | |
| 84 | + while i >= 8 do | |
| 85 | + m := matches(word(s, i - 8), b); | |
| 86 | + if m # 0 then return i - 8 + highest(m); end; | |
| 87 | + i := i - 8; | |
| 88 | + end; | |
| 89 | + while i > 0 do | |
| 90 | + DEC(i); | |
| 91 | + if s[i] = b then return i; end; | |
| 92 | + end; | |
| 93 | + return -1; | |
| 94 | +end; | |
| 95 | + | |
| 96 | +(* How many bytes of s are b. *) | |
| 97 | +proc count*(s: []U8; b: U8): INT; | |
| 98 | +var i, n: INT; | |
| 99 | +begin | |
| 100 | + i := 0; | |
| 101 | + n := 0; | |
| 102 | + while i + 8 <= LEN(s) do | |
| 103 | + n := n + ones(matches(word(s, i), b)); | |
| 104 | + i := i + 8; | |
| 105 | + end; | |
| 106 | + while i < LEN(s) do | |
| 107 | + if s[i] = b then INC(n); end; | |
| 108 | + INC(i); | |
| 109 | + end; | |
| 110 | + return n; | |
| 111 | +end; | |
| 112 | + | |
| 113 | +(* Where sub first starts in s, or -1; 0 for an empty sub. Eight places | |
| 114 | + are tried at once, by two bytes of sub, its first and its last, | |
| 115 | + each compared at its place: a place is checked whole only where | |
| 116 | + both agree. They are the two bytes farthest apart, so as far from | |
| 117 | + being the same test as two can be, and need no table of which bytes | |
| 118 | + are rare. *) | |
| 119 | +proc find*(s, sub: []U8): INT; | |
| 120 | +var i, j, n, lastStart: INT; m: U64; | |
| 121 | +begin | |
| 122 | + n := LEN(sub); | |
| 123 | + if n = 0 then return 0; end; | |
| 124 | + if n > LEN(s) then return -1; end; | |
| 125 | + if n = 1 then return index(s, sub[0]); end; | |
| 126 | + lastStart := LEN(s) - n; | |
| 127 | + i := 0; | |
| 128 | + while i + 7 <= lastStart do | |
| 129 | + m := matches(word(s, i), sub[0]) & matches(word(s, i + n - 1), sub[n - 1]); | |
| 130 | + while m # 0 do | |
| 131 | + j := i + lowest(m); | |
| 132 | + if s[j..j + n] = sub then return j; end; | |
| 133 | + m := m & (m - 1); | |
| 134 | + end; | |
| 135 | + i := i + 8; | |
| 136 | + end; | |
| 137 | + while i <= lastStart do | |
| 138 | + if s[i..i + n] = sub then return i; end; | |
| 139 | + INC(i); | |
| 140 | + end; | |
| 141 | + return -1; | |
| 142 | +end; | |
| 143 | + | |
| 144 | +(* The tests: each result against a plain loop over the same bytes. *) | |
| 145 | + | |
| 146 | +var | |
| 147 | + seed: U64; | |
| 148 | + fills: [6]U8; (* bytes that tempt the zero-byte test *) | |
| 149 | + buf: [160]U8; | |
| 150 | + subs: [24]U8; | |
| 151 | + | |
| 152 | +(* a pseudo-random number below n *) | |
| 153 | +proc random(n: INT): INT; | |
| 154 | +begin | |
| 155 | + seed := seed * 6364136223846793005 + 1442695040888963407; | |
| 156 | + return INT((seed shr 33) mod U64(n)); | |
| 157 | +end; | |
| 158 | + | |
| 159 | +proc refIndex(s: []U8; b: U8): INT; | |
| 160 | +var i: INT; | |
| 161 | +begin | |
| 162 | + for i := 0 to LEN(s) - 1 do | |
| 163 | + if s[i] = b then return i; end; | |
| 164 | + end; | |
| 165 | + return -1; | |
| 166 | +end; | |
| 167 | + | |
| 168 | +proc refLast(s: []U8; b: U8): INT; | |
| 169 | +var i: INT; | |
| 170 | +begin | |
| 171 | + for i := LEN(s) - 1 to 0 by -1 do | |
| 172 | + if s[i] = b then return i; end; | |
| 173 | + end; | |
| 174 | + return -1; | |
| 175 | +end; | |
| 176 | + | |
| 177 | +proc refCount(s: []U8; b: U8): INT; | |
| 178 | +var i, n: INT; | |
| 179 | +begin | |
| 180 | + n := 0; | |
| 181 | + for i := 0 to LEN(s) - 1 do | |
| 182 | + if s[i] = b then INC(n); end; | |
| 183 | + end; | |
| 184 | + return n; | |
| 185 | +end; | |
| 186 | + | |
| 187 | +proc refFind(s, sub: []U8): INT; | |
| 188 | +var i: INT; | |
| 189 | +begin | |
| 190 | + for i := 0 to LEN(s) - LEN(sub) do | |
| 191 | + if s[i..i + LEN(sub)] = sub then return i; end; | |
| 192 | + end; | |
| 193 | + return -1; | |
| 194 | +end; | |
| 195 | + | |
| 196 | +(* index, last and count of b in s agree with the plain loops *) | |
| 197 | +proc checkByte(s: []U8; b: U8); | |
| 198 | +begin | |
| 199 | + if index(s, b) # refIndex(s, b) then Rt.stop("Bytes.index"); end; | |
| 200 | + if last(s, b) # refLast(s, b) then Rt.stop("Bytes.last"); end; | |
| 201 | + if count(s, b) # refCount(s, b) then Rt.stop("Bytes.count"); end; | |
| 202 | +end; | |
| 203 | + | |
| 204 | +proc checkFind(s, sub: []U8); | |
| 205 | +begin | |
| 206 | + if find(s, sub) # refFind(s, sub) then Rt.stop("Bytes.find"); end; | |
| 207 | +end; | |
| 208 | + | |
| 209 | +(* every length to 40, from every start 0 to 8, with one byte at every | |
| 210 | + place, among each kind of filler: the first place, the last, and | |
| 211 | + the places either side of a block of 8 or 16 *) | |
| 212 | +proc testEdges; | |
| 213 | +var n, o, p, f, g, i: INT; | |
| 214 | +begin | |
| 215 | + for o := 0 to 8 do | |
| 216 | + for n := 0 to 40 do | |
| 217 | + for f := 0 to 5 do | |
| 218 | + for g := 0 to 5 do | |
| 219 | + for i := 0 to 159 do buf[i] := fills[f]; end; | |
| 220 | + checkByte(buf[o..o + n], fills[g]); | |
| 221 | + for p := 0 to n - 1 do | |
| 222 | + buf[o + p] := fills[g]; | |
| 223 | + checkByte(buf[o..o + n], fills[g]); | |
| 224 | + buf[o + p] := fills[f]; | |
| 225 | + end; | |
| 226 | + end; | |
| 227 | + end; | |
| 228 | + end; | |
| 229 | + end; | |
| 230 | +end; | |
| 231 | + | |
| 232 | +(* random bytes from a few kinds, of every length to 150 *) | |
| 233 | +proc testRandom; | |
| 234 | +var n, i, k, o: INT; | |
| 235 | +begin | |
| 236 | + for n := 0 to 150 do | |
| 237 | + for k := 1 to 20 do | |
| 238 | + o := random(8); | |
| 239 | + for i := 0 to 159 do buf[i] := fills[random(2 + k mod 5)]; end; | |
| 240 | + for i := 0 to 5 do checkByte(buf[o..o + n], fills[i]); end; | |
| 241 | + checkByte(buf[o..o + n], 41H); | |
| 242 | + end; | |
| 243 | + end; | |
| 244 | +end; | |
| 245 | + | |
| 246 | +(* a sub of every length at every place of a long slice, among fillers | |
| 247 | + that are almost its bytes; the slice cut short just before and just | |
| 248 | + after it; also subs longer than the slice, and empty ones *) | |
| 249 | +proc testPlanted; | |
| 250 | +var n, p, i, f: INT; s: []U8; | |
| 251 | +begin | |
| 252 | + for f := 0 to 2 do | |
| 253 | + for n := 1 to 24 do | |
| 254 | + for i := 0 to 23 do subs[i] := U8(0F0H + random(2) + i mod 3); end; | |
| 255 | + for p := 0 to 159 - n do | |
| 256 | + for i := 0 to 159 do buf[i] := U8(0F0H + f); end; | |
| 257 | + COPY(buf[p..p + n], subs[0..n]); | |
| 258 | + checkFind(buf[0..160], subs[0..n]); | |
| 259 | + checkFind(buf[1..160], subs[0..n]); | |
| 260 | + checkFind(buf[0..p + n], subs[0..n]); | |
| 261 | + checkFind(buf[0..p + n - 1], subs[0..n]); | |
| 262 | + end; | |
| 263 | + end; | |
| 264 | + end; | |
| 265 | + if find(buf[0..5], "") # 0 then Rt.stop("Bytes.find, empty sub"); end; | |
| 266 | + if find(buf[0..0], "") # 0 then Rt.stop("Bytes.find, both empty"); end; | |
| 267 | + if find(buf[0..3], buf[0..4]) # -1 then Rt.stop("Bytes.find, sub longer"); end; | |
| 268 | + s := "abcabd"; | |
| 269 | + if (find(s, "abd") # 3) or (find(s, "bc") # 1) or (find(s, "d") # 5) or (find(s, s) # 0) | |
| 270 | + or (find(s, "abcabde") # -1) then Rt.stop("Bytes.find, text"); end; | |
| 271 | +end; | |
| 272 | + | |
| 273 | +(* random text and subs from two or three kinds of byte, so that one | |
| 274 | + end of the sub is often where the other isn't *) | |
| 275 | +proc testFind; | |
| 276 | +var n, m, i, k, o: INT; | |
| 277 | +begin | |
| 278 | + for n := 0 to 100 do | |
| 279 | + for k := 1 to 12 do | |
| 280 | + o := random(8); | |
| 281 | + for i := 0 to 159 do buf[i] := fills[3 + random(2 + k mod 2)]; end; | |
| 282 | + for m := 0 to 24 do | |
| 283 | + for i := 0 to 23 do subs[i] := fills[3 + random(2 + k mod 2)]; end; | |
| 284 | + checkFind(buf[o..o + n], subs[0..m]); | |
| 285 | + end; | |
| 286 | + end; | |
| 287 | + end; | |
| 288 | +end; | |
| 289 | + | |
| 290 | +proc main*; | |
| 291 | +begin | |
| 292 | + fills := [0, 1, 7FH, 80H, 81H, 0FFH]; | |
| 293 | + seed := 12345; | |
| 294 | + testEdges; | |
| 295 | + testRandom; | |
| 296 | + testPlanted; | |
| 297 | + testFind; | |
| 298 | +end; | |
| 299 | + | |
| 300 | +end Bytes. |
| @@ -0,0 +1,348 @@ | ||
| 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. *) | |
| 6 | +module Bytes; | |
| 7 | +import Rt; | |
| 8 | + | |
| 9 | +(* The first place of b in s, or -1. Sixteen bytes at a time are | |
| 10 | + compared with b's sixteen copies (pcmpeqb), and pmovmskb makes of the | |
| 11 | + result a number with a bit set for each byte that matched. The bytes | |
| 12 | + left over, fewer than sixteen, are compared one by one. Where the | |
| 13 | + machine finds them: s's address in rdi, its length in rsi, b in dl. *) | |
| 14 | +proc index*(s: []U8; b: U8): INT; | |
| 15 | +code | |
| 16 | + movd xmm1, edx; | |
| 17 | + punpcklbw xmm1, xmm1; (* b in the low two bytes... *) | |
| 18 | + punpcklwd xmm1, xmm1; (* ...in the low four... *) | |
| 19 | + pshufd xmm1, xmm1, 0; (* ...and in all sixteen *) | |
| 20 | + xor ecx, ecx; | |
| 21 | + blocks: | |
| 22 | + lea rax, [rcx + 16]; | |
| 23 | + cmp rax, rsi; | |
| 24 | + ja tail; | |
| 25 | + movdqu xmm0, [rdi + rcx]; | |
| 26 | + pcmpeqb xmm0, xmm1; | |
| 27 | + pmovmskb eax, xmm0; | |
| 28 | + test eax, eax; | |
| 29 | + jnz found; | |
| 30 | + add rcx, 16; | |
| 31 | + jmp blocks; | |
| 32 | + found: | |
| 33 | + bsf eax, eax; (* the lowest bit set: the first match *) | |
| 34 | + add rax, rcx; | |
| 35 | + ret; | |
| 36 | + tail: | |
| 37 | + cmp rcx, rsi; | |
| 38 | + jae missing; | |
| 39 | + cmp [rdi + rcx], dl; | |
| 40 | + je here; | |
| 41 | + inc rcx; | |
| 42 | + jmp tail; | |
| 43 | + here: | |
| 44 | + mov rax, rcx; | |
| 45 | + ret; | |
| 46 | + missing: | |
| 47 | + mov rax, -1; | |
| 48 | + ret; | |
| 49 | +end index; | |
| 50 | + | |
| 51 | +(* The last place of b in s, or -1: as index, from the end. *) | |
| 52 | +proc last*(s: []U8; b: U8): INT; | |
| 53 | +code | |
| 54 | + movd xmm1, edx; | |
| 55 | + punpcklbw xmm1, xmm1; | |
| 56 | + punpcklwd xmm1, xmm1; | |
| 57 | + pshufd xmm1, xmm1, 0; | |
| 58 | + mov rcx, rsi; (* the bytes not yet seen are the first rcx *) | |
| 59 | + blocks: | |
| 60 | + cmp rcx, 16; | |
| 61 | + jb tail; | |
| 62 | + sub rcx, 16; | |
| 63 | + movdqu xmm0, [rdi + rcx]; | |
| 64 | + pcmpeqb xmm0, xmm1; | |
| 65 | + pmovmskb eax, xmm0; | |
| 66 | + test eax, eax; | |
| 67 | + jz blocks; | |
| 68 | + bsr eax, eax; (* the highest bit set: the last match *) | |
| 69 | + add rax, rcx; | |
| 70 | + ret; | |
| 71 | + tail: | |
| 72 | + test rcx, rcx; | |
| 73 | + jz missing; | |
| 74 | + dec rcx; | |
| 75 | + cmp [rdi + rcx], dl; | |
| 76 | + jne tail; | |
| 77 | + mov rax, rcx; | |
| 78 | + ret; | |
| 79 | + missing: | |
| 80 | + mov rax, -1; | |
| 81 | + ret; | |
| 82 | +end last; | |
| 83 | + | |
| 84 | +(* How many bytes of s are b: the bits of each block's number, counted. *) | |
| 85 | +proc count*(s: []U8; b: U8): INT; | |
| 86 | +code | |
| 87 | + movd xmm1, edx; | |
| 88 | + punpcklbw xmm1, xmm1; | |
| 89 | + punpcklwd xmm1, xmm1; | |
| 90 | + pshufd xmm1, xmm1, 0; | |
| 91 | + xor eax, eax; (* the count *) | |
| 92 | + xor ecx, ecx; | |
| 93 | + blocks: | |
| 94 | + lea r8, [rcx + 16]; | |
| 95 | + cmp r8, rsi; | |
| 96 | + ja tail; | |
| 97 | + movdqu xmm0, [rdi + rcx]; | |
| 98 | + pcmpeqb xmm0, xmm1; | |
| 99 | + pmovmskb r8d, xmm0; | |
| 100 | + popcnt r8d, r8d; | |
| 101 | + add rax, r8; | |
| 102 | + add rcx, 16; | |
| 103 | + jmp blocks; | |
| 104 | + tail: | |
| 105 | + cmp rcx, rsi; | |
| 106 | + jae done; | |
| 107 | + cmp [rdi + rcx], dl; | |
| 108 | + jne next; | |
| 109 | + inc rax; | |
| 110 | + next: | |
| 111 | + inc rcx; | |
| 112 | + jmp tail; | |
| 113 | + done: | |
| 114 | + ret; | |
| 115 | +end count; | |
| 116 | + | |
| 117 | +(* The first place i, from from on, with s[i] = a and s[i + gap] = b, | |
| 118 | + or -1; LEN(s) must be more than gap. Sixteen places are tried at | |
| 119 | + once: sixteen bytes are compared with a, sixteen from gap places on | |
| 120 | + with b, and the two results anded. The places in rdi and rsi (s), | |
| 121 | + rdx (gap), cl (a), r8b (b) and r9 (from). *) | |
| 122 | +proc pair(s: []U8; gap: INT; a, b: U8; from: INT): INT; | |
| 123 | +code | |
| 124 | + mov r11, rcx; (* a, for the places one by one *) | |
| 125 | + movd xmm1, ecx; | |
| 126 | + punpcklbw xmm1, xmm1; | |
| 127 | + punpcklwd xmm1, xmm1; | |
| 128 | + pshufd xmm1, xmm1, 0; | |
| 129 | + movd xmm2, r8d; | |
| 130 | + punpcklbw xmm2, xmm2; | |
| 131 | + punpcklwd xmm2, xmm2; | |
| 132 | + pshufd xmm2, xmm2, 0; | |
| 133 | + mov r10, rsi; | |
| 134 | + sub r10, rdx; (* the places are those below r10 *) | |
| 135 | + mov rax, r9; | |
| 136 | + blocks: | |
| 137 | + lea rcx, [rax + 16]; | |
| 138 | + cmp rcx, r10; | |
| 139 | + ja tail; | |
| 140 | + movdqu xmm0, [rdi + rax]; | |
| 141 | + pcmpeqb xmm0, xmm1; | |
| 142 | + lea rcx, [rax + rdx]; | |
| 143 | + movdqu xmm3, [rdi + rcx]; | |
| 144 | + pcmpeqb xmm3, xmm2; | |
| 145 | + pand xmm0, xmm3; | |
| 146 | + pmovmskb ecx, xmm0; | |
| 147 | + test ecx, ecx; | |
| 148 | + jnz found; | |
| 149 | + add rax, 16; | |
| 150 | + jmp blocks; | |
| 151 | + found: | |
| 152 | + bsf ecx, ecx; | |
| 153 | + add rax, rcx; | |
| 154 | + ret; | |
| 155 | + tail: | |
| 156 | + cmp rax, r10; | |
| 157 | + jae missing; | |
| 158 | + cmp [rdi + rax], r11b; | |
| 159 | + jne next; | |
| 160 | + lea rcx, [rax + rdx]; | |
| 161 | + cmp [rdi + rcx], r8b; | |
| 162 | + je here; | |
| 163 | + next: | |
| 164 | + inc rax; | |
| 165 | + jmp tail; | |
| 166 | + here: | |
| 167 | + ret; | |
| 168 | + missing: | |
| 169 | + mov rax, -1; | |
| 170 | + ret; | |
| 171 | +end pair; | |
| 172 | + | |
| 173 | +(* Where sub first starts in s, or -1; 0 for an empty sub. Places are | |
| 174 | + found by two bytes of sub, its first and its last, each compared at | |
| 175 | + its place (pair), and checked whole only where both agree. They are | |
| 176 | + the two bytes farthest apart, so as far from being the same test as | |
| 177 | + two can be, and need no table of which bytes are rare. *) | |
| 178 | +proc find*(s, sub: []U8): INT; | |
| 179 | +var i, n: INT; | |
| 180 | +begin | |
| 181 | + n := LEN(sub); | |
| 182 | + if n = 0 then return 0; end; | |
| 183 | + if n > LEN(s) then return -1; end; | |
| 184 | + i := pair(s, n - 1, sub[0], sub[n - 1], 0); | |
| 185 | + while i >= 0 do | |
| 186 | + if s[i..i + n] = sub then return i; end; | |
| 187 | + i := pair(s, n - 1, sub[0], sub[n - 1], i + 1); | |
| 188 | + end; | |
| 189 | + return -1; | |
| 190 | +end; | |
| 191 | + | |
| 192 | +(* The tests: each result against a plain loop over the same bytes. *) | |
| 193 | + | |
| 194 | +var | |
| 195 | + seed: U64; | |
| 196 | + fills: [6]U8; (* bytes that tempt the zero-byte test *) | |
| 197 | + buf: [160]U8; | |
| 198 | + subs: [24]U8; | |
| 199 | + | |
| 200 | +(* a pseudo-random number below n *) | |
| 201 | +proc random(n: INT): INT; | |
| 202 | +begin | |
| 203 | + seed := seed * 6364136223846793005 + 1442695040888963407; | |
| 204 | + return INT((seed shr 33) mod U64(n)); | |
| 205 | +end; | |
| 206 | + | |
| 207 | +proc refIndex(s: []U8; b: U8): INT; | |
| 208 | +var i: INT; | |
| 209 | +begin | |
| 210 | + for i := 0 to LEN(s) - 1 do | |
| 211 | + if s[i] = b then return i; end; | |
| 212 | + end; | |
| 213 | + return -1; | |
| 214 | +end; | |
| 215 | + | |
| 216 | +proc refLast(s: []U8; b: U8): INT; | |
| 217 | +var i: INT; | |
| 218 | +begin | |
| 219 | + for i := LEN(s) - 1 to 0 by -1 do | |
| 220 | + if s[i] = b then return i; end; | |
| 221 | + end; | |
| 222 | + return -1; | |
| 223 | +end; | |
| 224 | + | |
| 225 | +proc refCount(s: []U8; b: U8): INT; | |
| 226 | +var i, n: INT; | |
| 227 | +begin | |
| 228 | + n := 0; | |
| 229 | + for i := 0 to LEN(s) - 1 do | |
| 230 | + if s[i] = b then INC(n); end; | |
| 231 | + end; | |
| 232 | + return n; | |
| 233 | +end; | |
| 234 | + | |
| 235 | +proc refFind(s, sub: []U8): INT; | |
| 236 | +var i: INT; | |
| 237 | +begin | |
| 238 | + for i := 0 to LEN(s) - LEN(sub) do | |
| 239 | + if s[i..i + LEN(sub)] = sub then return i; end; | |
| 240 | + end; | |
| 241 | + return -1; | |
| 242 | +end; | |
| 243 | + | |
| 244 | +(* index, last and count of b in s agree with the plain loops *) | |
| 245 | +proc checkByte(s: []U8; b: U8); | |
| 246 | +begin | |
| 247 | + if index(s, b) # refIndex(s, b) then Rt.stop("Bytes.index"); end; | |
| 248 | + if last(s, b) # refLast(s, b) then Rt.stop("Bytes.last"); end; | |
| 249 | + if count(s, b) # refCount(s, b) then Rt.stop("Bytes.count"); end; | |
| 250 | +end; | |
| 251 | + | |
| 252 | +proc checkFind(s, sub: []U8); | |
| 253 | +begin | |
| 254 | + if find(s, sub) # refFind(s, sub) then Rt.stop("Bytes.find"); end; | |
| 255 | +end; | |
| 256 | + | |
| 257 | +(* every length to 40, from every start 0 to 8, with one byte at every | |
| 258 | + place, among each kind of filler: the first place, the last, and | |
| 259 | + the places either side of a block of 8 or 16 *) | |
| 260 | +proc testEdges; | |
| 261 | +var n, o, p, f, g, i: INT; | |
| 262 | +begin | |
| 263 | + for o := 0 to 8 do | |
| 264 | + for n := 0 to 40 do | |
| 265 | + for f := 0 to 5 do | |
| 266 | + for g := 0 to 5 do | |
| 267 | + for i := 0 to 159 do buf[i] := fills[f]; end; | |
| 268 | + checkByte(buf[o..o + n], fills[g]); | |
| 269 | + for p := 0 to n - 1 do | |
| 270 | + buf[o + p] := fills[g]; | |
| 271 | + checkByte(buf[o..o + n], fills[g]); | |
| 272 | + buf[o + p] := fills[f]; | |
| 273 | + end; | |
| 274 | + end; | |
| 275 | + end; | |
| 276 | + end; | |
| 277 | + end; | |
| 278 | +end; | |
| 279 | + | |
| 280 | +(* random bytes from a few kinds, of every length to 150 *) | |
| 281 | +proc testRandom; | |
| 282 | +var n, i, k, o: INT; | |
| 283 | +begin | |
| 284 | + for n := 0 to 150 do | |
| 285 | + for k := 1 to 20 do | |
| 286 | + o := random(8); | |
| 287 | + for i := 0 to 159 do buf[i] := fills[random(2 + k mod 5)]; end; | |
| 288 | + for i := 0 to 5 do checkByte(buf[o..o + n], fills[i]); end; | |
| 289 | + checkByte(buf[o..o + n], 41H); | |
| 290 | + end; | |
| 291 | + end; | |
| 292 | +end; | |
| 293 | + | |
| 294 | +(* a sub of every length at every place of a long slice, among fillers | |
| 295 | + that are almost its bytes; the slice cut short just before and just | |
| 296 | + after it; also subs longer than the slice, and empty ones *) | |
| 297 | +proc testPlanted; | |
| 298 | +var n, p, i, f: INT; s: []U8; | |
| 299 | +begin | |
| 300 | + for f := 0 to 2 do | |
| 301 | + for n := 1 to 24 do | |
| 302 | + for i := 0 to 23 do subs[i] := U8(0F0H + random(2) + i mod 3); end; | |
| 303 | + for p := 0 to 159 - n do | |
| 304 | + for i := 0 to 159 do buf[i] := U8(0F0H + f); end; | |
| 305 | + COPY(buf[p..p + n], subs[0..n]); | |
| 306 | + checkFind(buf[0..160], subs[0..n]); | |
| 307 | + checkFind(buf[1..160], subs[0..n]); | |
| 308 | + checkFind(buf[0..p + n], subs[0..n]); | |
| 309 | + checkFind(buf[0..p + n - 1], subs[0..n]); | |
| 310 | + end; | |
| 311 | + end; | |
| 312 | + end; | |
| 313 | + if find(buf[0..5], "") # 0 then Rt.stop("Bytes.find, empty sub"); end; | |
| 314 | + if find(buf[0..0], "") # 0 then Rt.stop("Bytes.find, both empty"); end; | |
| 315 | + if find(buf[0..3], buf[0..4]) # -1 then Rt.stop("Bytes.find, sub longer"); end; | |
| 316 | + s := "abcabd"; | |
| 317 | + if (find(s, "abd") # 3) or (find(s, "bc") # 1) or (find(s, "d") # 5) or (find(s, s) # 0) | |
| 318 | + or (find(s, "abcabde") # -1) then Rt.stop("Bytes.find, text"); end; | |
| 319 | +end; | |
| 320 | + | |
| 321 | +(* random text and subs from two or three kinds of byte, so that one | |
| 322 | + end of the sub is often where the other isn't *) | |
| 323 | +proc testFind; | |
| 324 | +var n, m, i, k, o: INT; | |
| 325 | +begin | |
| 326 | + for n := 0 to 100 do | |
| 327 | + for k := 1 to 12 do | |
| 328 | + o := random(8); | |
| 329 | + for i := 0 to 159 do buf[i] := fills[3 + random(2 + k mod 2)]; end; | |
| 330 | + for m := 0 to 24 do | |
| 331 | + for i := 0 to 23 do subs[i] := fills[3 + random(2 + k mod 2)]; end; | |
| 332 | + checkFind(buf[o..o + n], subs[0..m]); | |
| 333 | + end; | |
| 334 | + end; | |
| 335 | + end; | |
| 336 | +end; | |
| 337 | + | |
| 338 | +proc main*; | |
| 339 | +begin | |
| 340 | + fills := [0, 1, 7FH, 80H, 81H, 0FFH]; | |
| 341 | + seed := 12345; | |
| 342 | + testEdges; | |
| 343 | + testRandom; | |
| 344 | + testPlanted; | |
| 345 | + testFind; | |
| 346 | +end; | |
| 347 | + | |
| 348 | +end Bytes. |
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | module orders, searches and hashes them, reads and writes their |
| 3 | 3 | UTF-8, reads a number from one, and copies one to keep. *) |
| 4 | 4 | module Str; |
| 5 | -import Mem, Heap, Rt; | |
| 5 | +import Mem, Heap, Rt, Bytes; | |
| 6 | 6 | |
| 7 | 7 | const least = -9223372036854775807 - 1; (* the smallest I64 *) |
| 8 | 8 | |
| @@ -54,12 +54,8 @@ | ||
| 54 | 54 | |
| 55 | 55 | (* Where sub first starts in s, or -1. *) |
| 56 | 56 | proc find*(s, sub: []U8): INT; |
| 57 | -var i: INT; | |
| 58 | 57 | begin |
| 59 | - for i := 0 to LEN(s) - LEN(sub) do | |
| 60 | - if s[i..i + LEN(sub)] = sub then return i; end; | |
| 61 | - end; | |
| 62 | - return -1; | |
| 58 | + return Bytes.find(s, sub); | |
| 63 | 59 | end; |
| 64 | 60 | |
| 65 | 61 | (* Whether s starts with p. *) |
| @@ -34,7 +34,7 @@ | ||
| 34 | 34 | { what git describe says, made by make: 0.3.0 at the tag v0.3.0, |
| 35 | 35 | 0.3.0-5-g38d29a8 five commits past it } |
| 36 | 36 | Version = {$I version.inc}; |
| 37 | - Report = '2026-10.18'; { the version of doc/raz.md it follows } | |
| 37 | + Report = '2026-10.19'; { the version of doc/raz.md it follows } | |
| 38 | 38 | MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } |
| 39 | 39 | MfdCloexec = 1; { an ELF still executes: the kernel opens it first } |
| 40 | 40 |
| @@ -0,0 +1,45 @@ | ||
| 1 | +(* The lines of a text, counted all at once with Bytes.count, and walked | |
| 2 | + one by one with Bytes.index: lines.pas does the same with FPC's | |
| 3 | + IndexByte, which is assembly, and a counting loop. *) | |
| 4 | +module Lines; | |
| 5 | +import Out, Bytes; | |
| 6 | +check off; | |
| 7 | + | |
| 8 | +const | |
| 9 | + N = 16777216; | |
| 10 | + Rounds = 32; | |
| 11 | + Letters = "etaoinshrdlucmfw"; | |
| 12 | + | |
| 13 | +var text: [N]U8; | |
| 14 | + | |
| 15 | +proc main*; | |
| 16 | +var i, r, x, v, k, total, walked: INT; | |
| 17 | +begin | |
| 18 | + x := 1; | |
| 19 | + for i := 0 to N - 1 do | |
| 20 | + x := (x * 1103515245 + 12345) & 7FFFFFFFH; | |
| 21 | + v := (x shr 16) & 63; | |
| 22 | + if v = 0 then text[i] := 10; | |
| 23 | + elsif v < 10 then text[i] := U8(' '); | |
| 24 | + else text[i] := Letters[v & 15]; | |
| 25 | + end; | |
| 26 | + end; | |
| 27 | + total := 0; | |
| 28 | + walked := 0; | |
| 29 | + for r := 1 to Rounds do | |
| 30 | + total := total + Bytes.count(text, 10); | |
| 31 | + i := 0; | |
| 32 | + k := Bytes.index(text, 10); | |
| 33 | + while k >= 0 do | |
| 34 | + INC(walked); | |
| 35 | + i := i + k + 1; | |
| 36 | + k := Bytes.index(text[i..N], 10); | |
| 37 | + end; | |
| 38 | + end; | |
| 39 | + Out.int(total); | |
| 40 | + Out.str(" "); | |
| 41 | + Out.int(walked); | |
| 42 | + Out.ln; | |
| 43 | +end; | |
| 44 | + | |
| 45 | +end Lines. |
| @@ -0,0 +1,44 @@ | ||
| 1 | +{ Lines.raz's twin. } | |
| 2 | +program lines; | |
| 3 | +{$mode objfpc} | |
| 4 | +const | |
| 5 | + N = 16777216; | |
| 6 | + Rounds = 32; | |
| 7 | + Letters = 'etaoinshrdlucmfw'; | |
| 8 | +var | |
| 9 | + text: array[0..N - 1] of byte; | |
| 10 | + i, r, x, v, k, total, walked: int64; | |
| 11 | + | |
| 12 | +function Count(p: pbyte; n: int64): int64; | |
| 13 | +var i, c: int64; | |
| 14 | +begin | |
| 15 | + c := 0; | |
| 16 | + for i := 0 to n - 1 do | |
| 17 | + if p[i] = 10 then inc(c); | |
| 18 | + Count := c; | |
| 19 | +end; | |
| 20 | + | |
| 21 | +begin | |
| 22 | + x := 1; | |
| 23 | + for i := 0 to N - 1 do begin | |
| 24 | + x := (x * 1103515245 + 12345) and $7FFFFFFF; | |
| 25 | + v := (x shr 16) and 63; | |
| 26 | + if v = 0 then text[i] := 10 | |
| 27 | + else if v < 10 then text[i] := ord(' ') | |
| 28 | + else text[i] := ord(Letters[(v and 15) + 1]); | |
| 29 | + end; | |
| 30 | + total := 0; | |
| 31 | + walked := 0; | |
| 32 | + for r := 1 to Rounds do begin | |
| 33 | + total := total + Count(@text[0], N); | |
| 34 | + i := 0; | |
| 35 | + k := IndexByte(text[0], N, 10); | |
| 36 | + while k >= 0 do begin | |
| 37 | + inc(walked); | |
| 38 | + i := i + k + 1; | |
| 39 | + if i >= N then break; | |
| 40 | + k := IndexByte(text[i], N - i, 10); | |
| 41 | + end; | |
| 42 | + end; | |
| 43 | + writeln(total, ' ', walked); | |
| 44 | +end. |