| | @@ -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. |