| 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 | import SYSTEM; |
| 10 | |
| 11 | const |
| 12 | Ones = 0101010101010101H; (* a one in each byte *) |
| 13 | Lows = 7F7F7F7F7F7F7F7FH; (* the low seven bits of each byte *) |
| 14 | Tops = 8080808080808080H; (* the top bit of each byte *) |
| 15 | |
| 16 | (* Every loop below tests eight bytes w for b in one expression, written |
| 17 | out where it is used, since a call in a loop would push the loop's |
| 18 | values out of their registers into the frame. With v = w xor (b in |
| 19 | each byte), a byte of w is b when the byte of v is zero. A byte of v |
| 20 | is zero when its low seven bits are, which adding 7FH to them shows, |
| 21 | as no sum overflows into the next byte, and its top bit is. So |
| 22 | |
| 23 | ~(((v & Lows) + Lows) | v | Lows) & Tops |
| 24 | |
| 25 | is the top bit of each byte of w that is b, and no other bit; call it |
| 26 | m. The place, 0 to 7, of the lowest bit of m (m is not 0) is the |
| 27 | ones below that bit, one in each byte up to its own, summed into the |
| 28 | top byte by a multiply, less one; the highest is found by a loop. |
| 29 | The bits m sets are counted by moving each down to bit 0 of its |
| 30 | byte and summing them into the top byte by a multiply. |
| 31 | |
| 32 | A word is read through a U64 pointer to s[i], with i + 8 not past |
| 33 | LEN(s); the address needn't be a multiple of 8, and x86-64 reads |
| 34 | such a word as it does any. *) |
| 35 | |
| 36 | (* The place, 0 to 7, of the highest top bit that m sets (m is not 0). *) |
| 37 | proc highest(m: U64): INT; |
| 38 | var k: INT; |
| 39 | begin |
| 40 | k := 7; |
| 41 | while (m shr (8 * k)) & 80H = 0 do DEC(k); end; |
| 42 | return k; |
| 43 | end; |
| 44 | |
| 45 | (* The first place of b in s, or -1. *) |
| 46 | proc index*(s: []const U8; b: U8): INT; |
| 47 | var i: INT; m, v, bs: U64; p: ^U64; |
| 48 | begin |
| 49 | bs := U64(b) * Ones; |
| 50 | i := 0; |
| 51 | while i + 8 <= LEN(s) do |
| 52 | p := ^U64(SYSTEM.PTR(s[i..LEN(s)])); |
| 53 | v := p^ xor bs; |
| 54 | m := ~(((v & Lows) + Lows) | v | Lows) & Tops; |
| 55 | if m # 0 then return i + INT((((m & (0 - m)) - 1) & Ones) * Ones shr 56) - 1; end; |
| 56 | i := i + 8; |
| 57 | end; |
| 58 | while i < LEN(s) do |
| 59 | if s[i] = b then return i; end; |
| 60 | INC(i); |
| 61 | end; |
| 62 | return -1; |
| 63 | end; |
| 64 | |
| 65 | (* The last place of b in s, or -1. *) |
| 66 | proc last*(s: []const U8; b: U8): INT; |
| 67 | var i: INT; m, v, bs: U64; p: ^U64; |
| 68 | begin |
| 69 | bs := U64(b) * Ones; |
| 70 | i := LEN(s); |
| 71 | while i >= 8 do |
| 72 | p := ^U64(SYSTEM.PTR(s[i - 8..LEN(s)])); |
| 73 | v := p^ xor bs; |
| 74 | m := ~(((v & Lows) + Lows) | v | Lows) & Tops; |
| 75 | if m # 0 then return i - 8 + highest(m); end; |
| 76 | i := i - 8; |
| 77 | end; |
| 78 | while i > 0 do |
| 79 | DEC(i); |
| 80 | if s[i] = b then return i; end; |
| 81 | end; |
| 82 | return -1; |
| 83 | end; |
| 84 | |
| 85 | (* How many bytes of s are b. *) |
| 86 | proc count*(s: []const U8; b: U8): INT; |
| 87 | var i, n: INT; v, bs: U64; p: ^U64; |
| 88 | begin |
| 89 | bs := U64(b) * Ones; |
| 90 | i := 0; |
| 91 | n := 0; |
| 92 | while i + 8 <= LEN(s) do |
| 93 | p := ^U64(SYSTEM.PTR(s[i..LEN(s)])); |
| 94 | v := p^ xor bs; |
| 95 | n := n + INT(((~(((v & Lows) + Lows) | v | Lows) & Tops) shr 7) * Ones shr 56); |
| 96 | i := i + 8; |
| 97 | end; |
| 98 | while i < LEN(s) do |
| 99 | if s[i] = b then INC(n); end; |
| 100 | INC(i); |
| 101 | end; |
| 102 | return n; |
| 103 | end; |
| 104 | |
| 105 | (* Where sub first starts in s, or -1; 0 for an empty sub. Eight places |
| 106 | are tried at once, by two bytes of sub, its first and its last, |
| 107 | each compared at its place: a place is checked whole only where |
| 108 | both agree. They are the two bytes farthest apart, so as far from |
| 109 | being the same test as two can be, and need no table of which bytes |
| 110 | are rare. *) |
| 111 | proc find*(s, sub: []const U8): INT; |
| 112 | var i, j, n, lastStart: INT; m, v, w, b0, b1: U64; p: ^U64; |
| 113 | begin |
| 114 | n := LEN(sub); |
| 115 | if n = 0 then return 0; end; |
| 116 | if n > LEN(s) then return -1; end; |
| 117 | if n = 1 then return index(s, sub[0]); end; |
| 118 | lastStart := LEN(s) - n; |
| 119 | b0 := U64(sub[0]) * Ones; |
| 120 | b1 := U64(sub[n - 1]) * Ones; |
| 121 | i := 0; |
| 122 | while i + 7 <= lastStart do |
| 123 | p := ^U64(SYSTEM.PTR(s[i..LEN(s)])); |
| 124 | v := p^ xor b0; |
| 125 | p := ^U64(SYSTEM.PTR(s[i + n - 1..LEN(s)])); |
| 126 | w := p^ xor b1; |
| 127 | m := ~(((v & Lows) + Lows) | v | Lows) & ~(((w & Lows) + Lows) | w | Lows) & Tops; |
| 128 | while m # 0 do |
| 129 | j := i + INT((((m & (0 - m)) - 1) & Ones) * Ones shr 56) - 1; |
| 130 | if s[j..j + n] = sub then return j; end; |
| 131 | m := m & (m - 1); |
| 132 | end; |
| 133 | i := i + 8; |
| 134 | end; |
| 135 | while i <= lastStart do |
| 136 | if s[i..i + n] = sub then return i; end; |
| 137 | INC(i); |
| 138 | end; |
| 139 | return -1; |
| 140 | end; |
| 141 | |
| 142 | (* The tests: each result against a plain loop over the same bytes. *) |
| 143 | |
| 144 | var |
| 145 | seed: U64; |
| 146 | fills: [6]U8; (* bytes that tempt the zero-byte test *) |
| 147 | buf: [160]U8; |
| 148 | subs: [24]U8; |
| 149 | |
| 150 | (* a pseudo-random number below n *) |
| 151 | proc random(n: INT): INT; |
| 152 | begin |
| 153 | seed := seed * 6364136223846793005 + 1442695040888963407; |
| 154 | return INT((seed shr 33) mod U64(n)); |
| 155 | end; |
| 156 | |
| 157 | proc refIndex(s: []const U8; b: U8): INT; |
| 158 | var i: INT; |
| 159 | begin |
| 160 | for i := 0 to LEN(s) - 1 do |
| 161 | if s[i] = b then return i; end; |
| 162 | end; |
| 163 | return -1; |
| 164 | end; |
| 165 | |
| 166 | proc refLast(s: []const U8; b: U8): INT; |
| 167 | var i: INT; |
| 168 | begin |
| 169 | for i := LEN(s) - 1 to 0 by -1 do |
| 170 | if s[i] = b then return i; end; |
| 171 | end; |
| 172 | return -1; |
| 173 | end; |
| 174 | |
| 175 | proc refCount(s: []const U8; b: U8): INT; |
| 176 | var i, n: INT; |
| 177 | begin |
| 178 | n := 0; |
| 179 | for i := 0 to LEN(s) - 1 do |
| 180 | if s[i] = b then INC(n); end; |
| 181 | end; |
| 182 | return n; |
| 183 | end; |
| 184 | |
| 185 | proc refFind(s, sub: []const U8): INT; |
| 186 | var i: INT; |
| 187 | begin |
| 188 | for i := 0 to LEN(s) - LEN(sub) do |
| 189 | if s[i..i + LEN(sub)] = sub then return i; end; |
| 190 | end; |
| 191 | return -1; |
| 192 | end; |
| 193 | |
| 194 | (* index, last and count of b in s agree with the plain loops *) |
| 195 | proc checkByte(s: []const U8; b: U8); |
| 196 | begin |
| 197 | if index(s, b) # refIndex(s, b) then Rt.stop("Bytes.index"); end; |
| 198 | if last(s, b) # refLast(s, b) then Rt.stop("Bytes.last"); end; |
| 199 | if count(s, b) # refCount(s, b) then Rt.stop("Bytes.count"); end; |
| 200 | end; |
| 201 | |
| 202 | proc checkFind(s, sub: []const U8); |
| 203 | begin |
| 204 | if find(s, sub) # refFind(s, sub) then Rt.stop("Bytes.find"); end; |
| 205 | end; |
| 206 | |
| 207 | (* every length to 40, from every start 0 to 8, with one byte at every |
| 208 | place, among each kind of filler: the first place, the last, and |
| 209 | the places either side of a block of 8 or 16 *) |
| 210 | proc testEdges; |
| 211 | var n, o, p, f, g, i: INT; |
| 212 | begin |
| 213 | for o := 0 to 8 do |
| 214 | for n := 0 to 40 do |
| 215 | for f := 0 to 5 do |
| 216 | for g := 0 to 5 do |
| 217 | for i := 0 to 159 do buf[i] := fills[f]; end; |
| 218 | checkByte(buf[o..o + n], fills[g]); |
| 219 | for p := 0 to n - 1 do |
| 220 | buf[o + p] := fills[g]; |
| 221 | checkByte(buf[o..o + n], fills[g]); |
| 222 | buf[o + p] := fills[f]; |
| 223 | end; |
| 224 | end; |
| 225 | end; |
| 226 | end; |
| 227 | end; |
| 228 | end; |
| 229 | |
| 230 | (* random bytes from a few kinds, of every length to 150 *) |
| 231 | proc testRandom; |
| 232 | var n, i, k, o: INT; |
| 233 | begin |
| 234 | for n := 0 to 150 do |
| 235 | for k := 1 to 20 do |
| 236 | o := random(8); |
| 237 | for i := 0 to 159 do buf[i] := fills[random(2 + k mod 5)]; end; |
| 238 | for i := 0 to 5 do checkByte(buf[o..o + n], fills[i]); end; |
| 239 | checkByte(buf[o..o + n], 41H); |
| 240 | end; |
| 241 | end; |
| 242 | end; |
| 243 | |
| 244 | (* a sub of every length at every place of a long slice, among fillers |
| 245 | that are almost its bytes; the slice cut short just before and just |
| 246 | after it; also subs longer than the slice, and empty ones *) |
| 247 | proc testPlanted; |
| 248 | var n, p, i, f: INT; s: []const U8; |
| 249 | begin |
| 250 | for f := 0 to 2 do |
| 251 | for n := 1 to 24 do |
| 252 | for i := 0 to 23 do subs[i] := U8(0F0H + random(2) + i mod 3); end; |
| 253 | for p := 0 to 159 - n do |
| 254 | for i := 0 to 159 do buf[i] := U8(0F0H + f); end; |
| 255 | COPY(buf[p..p + n], subs[0..n]); |
| 256 | checkFind(buf[0..160], subs[0..n]); |
| 257 | checkFind(buf[1..160], subs[0..n]); |
| 258 | checkFind(buf[0..p + n], subs[0..n]); |
| 259 | checkFind(buf[0..p + n - 1], subs[0..n]); |
| 260 | end; |
| 261 | end; |
| 262 | end; |
| 263 | if find(buf[0..5], "") # 0 then Rt.stop("Bytes.find, empty sub"); end; |
| 264 | if find(buf[0..0], "") # 0 then Rt.stop("Bytes.find, both empty"); end; |
| 265 | if find(buf[0..3], buf[0..4]) # -1 then Rt.stop("Bytes.find, sub longer"); end; |
| 266 | s := "abcabd"; |
| 267 | if (find(s, "abd") # 3) or (find(s, "bc") # 1) or (find(s, "d") # 5) or (find(s, s) # 0) |
| 268 | or (find(s, "abcabde") # -1) then Rt.stop("Bytes.find, text"); end; |
| 269 | end; |
| 270 | |
| 271 | (* random text and subs from two or three kinds of byte, so that one |
| 272 | end of the sub is often where the other isn't *) |
| 273 | proc testFind; |
| 274 | var n, m, i, k, o: INT; |
| 275 | begin |
| 276 | for n := 0 to 100 do |
| 277 | for k := 1 to 12 do |
| 278 | o := random(8); |
| 279 | for i := 0 to 159 do buf[i] := fills[3 + random(2 + k mod 2)]; end; |
| 280 | for m := 0 to 24 do |
| 281 | for i := 0 to 23 do subs[i] := fills[3 + random(2 + k mod 2)]; end; |
| 282 | checkFind(buf[o..o + n], subs[0..m]); |
| 283 | end; |
| 284 | end; |
| 285 | end; |
| 286 | end; |
| 287 | |
| 288 | proc main*; |
| 289 | begin |
| 290 | fills := [0, 1, 7FH, 80H, 81H, 0FFH]; |
| 291 | seed := 12345; |
| 292 | testEdges; |
| 293 | testRandom; |
| 294 | testPlanted; |
| 295 | testFind; |
| 296 | end; |
| 297 | |
| 298 | end Bytes. |