| 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 | import SYSTEM; |
| 12 | check off; |
| 13 | |
| 14 | const |
| 15 | N = 16777216; |
| 16 | Rounds = 16; |
| 17 | Letters = "etaoinshrdlucmfw"; |
| 18 | Ones = 0101010101010101H; |
| 19 | Tops = 8080808080808080H; |
| 20 | Word = "error"; |
| 21 | A = 1; (* the two bytes sought: the r at 1 and the o at 3 *) |
| 22 | B = 3; |
| 23 | |
| 24 | var text: [N]U8; |
| 25 | |
| 26 | (* the top bit of each byte of v that is zero, the lowest one exactly *) |
| 27 | proc zeros(v: U64): U64; |
| 28 | begin |
| 29 | return (v - Ones) & ~v & Tops; |
| 30 | end; |
| 31 | |
| 32 | (* the place of the lowest byte whose top bit m sets: the ones below |
| 33 | that bit, a one in each byte below it and one in its own, summed |
| 34 | into the top byte by the multiply *) |
| 35 | proc lowest(m: U64): INT; |
| 36 | begin |
| 37 | return INT((((m & (0 - m)) - 1) & Ones) * Ones shr 56) - 1; |
| 38 | end; |
| 39 | |
| 40 | (* where the first line feed in s is, or LEN(s) *) |
| 41 | proc lineEnd(s: []U8): INT; |
| 42 | var i: INT; p: ^U64; m: U64; |
| 43 | begin |
| 44 | i := 0; |
| 45 | while i + 8 <= LEN(s) do |
| 46 | p := ^U64(SYSTEM.ADR(s[i])); |
| 47 | m := zeros(p^ xor 0A0A0A0A0A0A0A0AH); |
| 48 | if m # 0 then return i + lowest(m); end; |
| 49 | i := i + 8; |
| 50 | end; |
| 51 | while (i < LEN(s)) and (s[i] # 10) do INC(i); end; |
| 52 | return i; |
| 53 | end; |
| 54 | |
| 55 | (* where Word first starts in s, or -1 *) |
| 56 | proc find(s: []U8): INT; |
| 57 | var i, j, last: INT; p, q: ^U64; m, wa, wb: U64; |
| 58 | begin |
| 59 | last := LEN(s) - LEN(Word); |
| 60 | wa := U64(Word[A]) * Ones; |
| 61 | wb := U64(Word[B]) * Ones; |
| 62 | i := 0; |
| 63 | while i + 7 <= last do |
| 64 | p := ^U64(SYSTEM.ADR(s[i + A])); |
| 65 | q := ^U64(SYSTEM.ADR(s[i + B])); |
| 66 | m := zeros(p^ xor wa) & zeros(q^ xor wb); |
| 67 | while m # 0 do |
| 68 | j := i + lowest(m); |
| 69 | if s[j..j + LEN(Word)] = Word then return j; end; |
| 70 | m := m & (m - 1); |
| 71 | end; |
| 72 | i := i + 8; |
| 73 | end; |
| 74 | while i <= last do |
| 75 | if s[i..i + LEN(Word)] = Word then return i; end; |
| 76 | INC(i); |
| 77 | end; |
| 78 | return -1; |
| 79 | end; |
| 80 | |
| 81 | (* how many lines s has, a line feed ending each *) |
| 82 | proc lines(s: []U8): INT; |
| 83 | var i, n: INT; |
| 84 | begin |
| 85 | i := 0; |
| 86 | n := 0; |
| 87 | while i < LEN(s) do |
| 88 | i := i + lineEnd(s[i..LEN(s)]) + 1; |
| 89 | INC(n); |
| 90 | end; |
| 91 | return n; |
| 92 | end; |
| 93 | |
| 94 | (* how many lines of s hold Word: from each match on to its line's end *) |
| 95 | proc matching(s: []U8): INT; |
| 96 | var i, k, n: INT; |
| 97 | begin |
| 98 | i := 0; |
| 99 | n := 0; |
| 100 | k := find(s); |
| 101 | while k >= 0 do |
| 102 | INC(n); |
| 103 | i := i + k; |
| 104 | i := i + lineEnd(s[i..LEN(s)]) + 1; |
| 105 | if i >= LEN(s) then return n; end; |
| 106 | k := find(s[i..LEN(s)]); |
| 107 | end; |
| 108 | return n; |
| 109 | end; |
| 110 | |
| 111 | proc main*; |
| 112 | var i, r, x, v, total, hits: INT; |
| 113 | begin |
| 114 | x := 1; |
| 115 | for i := 0 to N - 1 do |
| 116 | x := (x * 1103515245 + 12345) & 7FFFFFFFH; |
| 117 | v := (x shr 16) & 63; |
| 118 | if v = 0 then text[i] := 10; |
| 119 | elsif v < 10 then text[i] := U8(' '); |
| 120 | else text[i] := Letters[v & 15]; |
| 121 | end; |
| 122 | end; |
| 123 | text[N - 1] := 10; |
| 124 | (* by chance the word is about four times in the text: some more *) |
| 125 | i := 0; |
| 126 | while i < N - 1000 do |
| 127 | COPY(text[i..i + LEN(Word)], Word); |
| 128 | i := i + 997; |
| 129 | end; |
| 130 | total := 0; |
| 131 | hits := 0; |
| 132 | for r := 1 to Rounds do |
| 133 | total := total + lines(text); |
| 134 | hits := hits + matching(text); |
| 135 | end; |
| 136 | Out.int(total); |
| 137 | Out.str(" "); |
| 138 | Out.int(hits); |
| 139 | Out.ln; |
| 140 | end; |
| 141 | |
| 142 | end Grep. |