| | @@ -12,36 +12,25 @@ |
| 12 | 12 | Lows = 7F7F7F7F7F7F7F7FH; (* the low seven bits of each byte *) |
| 13 | 13 | Tops = 8080808080808080H; (* the top bit of each byte *) |
| 14 | 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; |
| 15 | +(* Every loop below tests eight bytes w for b in one expression, written |
| 16 | + out where it is used, since a call in a loop would push the loop's |
| 17 | + values out of their registers into the frame. With v = w xor (b in |
| 18 | + each byte), a byte of w is b when the byte of v is zero. A byte of v |
| 19 | + is zero when its low seven bits are, which adding 7FH to them shows, |
| 20 | + as no sum overflows into the next byte, and its top bit is. So |
| 23 | 21 | |
| 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; |
| 22 | + ~(((v & Lows) + Lows) | v | Lows) & Tops |
| 31 | 23 | |
| 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; |
| 24 | + is the top bit of each byte of w that is b, and no other bit; call it |
| 25 | + m. The place, 0 to 7, of the lowest bit of m (m is not 0) is the |
| 26 | + ones below that bit, one in each byte up to its own, summed into the |
| 27 | + top byte by a multiply, less one; the highest is found by a loop. |
| 28 | + The bits m sets are counted by moving each down to bit 0 of its |
| 29 | + byte and summing them into the top byte by a multiply. |
| 37 | 30 | |
| 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; |
| 31 | + A word is read through a U64 pointer to s[i], with i + 8 not past |
| 32 | + LEN(s); the address needn't be a multiple of 8, and x86-64 reads |
| 33 | + such a word as it does any. *) |
| 45 | 34 | |
| 46 | 35 | (* The place, 0 to 7, of the highest top bit that m sets (m is not 0). *) |
| 47 | 36 | proc highest(m: U64): INT; |
| | @@ -52,21 +41,17 @@ |
| 52 | 41 | return k; |
| 53 | 42 | end; |
| 54 | 43 | |
| 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 | 44 | (* The first place of b in s, or -1. *) |
| 63 | 45 | proc index*(s: []U8; b: U8): INT; |
| 64 | | -var i: INT; m: U64; |
| 46 | +var i: INT; m, v, bs: U64; p: ^U64; |
| 65 | 47 | begin |
| 48 | + bs := U64(b) * Ones; |
| 66 | 49 | i := 0; |
| 67 | 50 | while i + 8 <= LEN(s) do |
| 68 | | - m := matches(word(s, i), b); |
| 69 | | - if m # 0 then return i + lowest(m); end; |
| 51 | + p := ^U64(ADR(s[i])); |
| 52 | + v := p^ xor bs; |
| 53 | + m := ~(((v & Lows) + Lows) | v | Lows) & Tops; |
| 54 | + if m # 0 then return i + INT((((m & (0 - m)) - 1) & Ones) * Ones shr 56) - 1; end; |
| 70 | 55 | i := i + 8; |
| 71 | 56 | end; |
| 72 | 57 | while i < LEN(s) do |
| | @@ -78,11 +63,14 @@ |
| 78 | 63 | |
| 79 | 64 | (* The last place of b in s, or -1. *) |
| 80 | 65 | proc last*(s: []U8; b: U8): INT; |
| 81 | | -var i: INT; m: U64; |
| 66 | +var i: INT; m, v, bs: U64; p: ^U64; |
| 82 | 67 | begin |
| 68 | + bs := U64(b) * Ones; |
| 83 | 69 | i := LEN(s); |
| 84 | 70 | while i >= 8 do |
| 85 | | - m := matches(word(s, i - 8), b); |
| 71 | + p := ^U64(ADR(s[i - 8])); |
| 72 | + v := p^ xor bs; |
| 73 | + m := ~(((v & Lows) + Lows) | v | Lows) & Tops; |
| 86 | 74 | if m # 0 then return i - 8 + highest(m); end; |
| 87 | 75 | i := i - 8; |
| 88 | 76 | end; |
| | @@ -95,12 +83,15 @@ |
| 95 | 83 | |
| 96 | 84 | (* How many bytes of s are b. *) |
| 97 | 85 | proc count*(s: []U8; b: U8): INT; |
| 98 | | -var i, n: INT; |
| 86 | +var i, n: INT; v, bs: U64; p: ^U64; |
| 99 | 87 | begin |
| 88 | + bs := U64(b) * Ones; |
| 100 | 89 | i := 0; |
| 101 | 90 | n := 0; |
| 102 | 91 | while i + 8 <= LEN(s) do |
| 103 | | - n := n + ones(matches(word(s, i), b)); |
| 92 | + p := ^U64(ADR(s[i])); |
| 93 | + v := p^ xor bs; |
| 94 | + n := n + INT(((~(((v & Lows) + Lows) | v | Lows) & Tops) shr 7) * Ones shr 56); |
| 104 | 95 | i := i + 8; |
| 105 | 96 | end; |
| 106 | 97 | while i < LEN(s) do |
| | @@ -117,18 +108,24 @@ |
| 117 | 108 | being the same test as two can be, and need no table of which bytes |
| 118 | 109 | are rare. *) |
| 119 | 110 | proc find*(s, sub: []U8): INT; |
| 120 | | -var i, j, n, lastStart: INT; m: U64; |
| 111 | +var i, j, n, lastStart: INT; m, v, w, b0, b1: U64; p: ^U64; |
| 121 | 112 | begin |
| 122 | 113 | n := LEN(sub); |
| 123 | 114 | if n = 0 then return 0; end; |
| 124 | 115 | if n > LEN(s) then return -1; end; |
| 125 | 116 | if n = 1 then return index(s, sub[0]); end; |
| 126 | 117 | lastStart := LEN(s) - n; |
| 118 | + b0 := U64(sub[0]) * Ones; |
| 119 | + b1 := U64(sub[n - 1]) * Ones; |
| 127 | 120 | i := 0; |
| 128 | 121 | while i + 7 <= lastStart do |
| 129 | | - m := matches(word(s, i), sub[0]) & matches(word(s, i + n - 1), sub[n - 1]); |
| 122 | + p := ^U64(ADR(s[i])); |
| 123 | + v := p^ xor b0; |
| 124 | + p := ^U64(ADR(s[i + n - 1])); |
| 125 | + w := p^ xor b1; |
| 126 | + m := ~(((v & Lows) + Lows) | v | Lows) & ~(((w & Lows) + Lows) | w | Lows) & Tops; |
| 130 | 127 | while m # 0 do |
| 131 | | - j := i + lowest(m); |
| 128 | + j := i + INT((((m & (0 - m)) - 1) & Ones) * Ones shr 56) - 1; |
| 132 | 129 | if s[j..j + n] = sub then return j; end; |
| 133 | 130 | m := m & (m - 1); |
| 134 | 131 | end; |