| 1 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 2 |
from rpython.rlib import objectmodel, unroll |
| 3 |
from rpython.rlib.rarithmetic import r_uint, intmask, LONG_BIT |
| 4 |
from rpython.jit.backend.detect_cpu import autodetect, ProcessorAutodetectError |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
USE_SIMD = False |
| 14 |
if LONG_BIT == 64: |
| 15 |
WORD_SIZE = 8 |
| 16 |
EVERY_BYTE_ONE = 0x0101010101010101 |
| 17 |
EVERY_BYTE_HIGHEST_BIT = 0x8080808080808080 |
| 18 |
try: |
| 19 |
if autodetect() == "x86-64": |
| 20 |
USE_SIMD = True |
| 21 |
except ProcessorAutodetectError: |
| 22 |
pass |
| 23 |
else: |
| 24 |
WORD_SIZE = 4 |
| 25 |
EVERY_BYTE_ONE = 0x01010101 |
| 26 |
EVERY_BYTE_HIGHEST_BIT = 0x80808080 |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
unrolling_wordsize = unroll.unrolling_iterable(range(WORD_SIZE)) |
| 32 |
|
| 33 |
def char_repeated_word_width(ch): |
| 34 |
return r_uint(EVERY_BYTE_ONE) * ord(ch) |
| 35 |
|
| 36 |
def any_char_zero(word): |
| 37 |
return (word - r_uint(EVERY_BYTE_ONE)) & ~word & r_uint(EVERY_BYTE_HIGHEST_BIT) |
| 38 |
|
| 39 |
def any_char_in_words_zero(*words): |
| 40 |
return _any_char_in_any_word_zero_accum(0, *words) |
| 41 |
|
| 42 |
def _any_char_in_any_word_zero_accum(accum, word, *words): |
| 43 |
accum |= (word - r_uint(EVERY_BYTE_ONE)) & ~word |
| 44 |
if not words: |
| 45 |
return accum & r_uint(EVERY_BYTE_HIGHEST_BIT) |
| 46 |
return _any_char_in_any_word_zero_accum(accum, *words) |
| 47 |
|
| 48 |
def print_chars(word): |
| 49 |
|
| 50 |
out = '' |
| 51 |
for i in range(WORD_SIZE): |
| 52 |
out += chr(word & 0xff) |
| 53 |
word >>= 8 |
| 54 |
return out |
| 55 |
|
| 56 |
def index_nonzero(word): |
| 57 |
|
| 58 |
assert word |
| 59 |
for i in unrolling_wordsize: |
| 60 |
if word & 0xff: |
| 61 |
return i |
| 62 |
word >>= 8 |
| 63 |
assert 0 |
| 64 |
|
| 65 |
def index_zero(word): |
| 66 |
|
| 67 |
assert any_char_zero(word) |
| 68 |
for i in unrolling_wordsize: |
| 69 |
if not word & 0xff: |
| 70 |
return i |
| 71 |
word >>= 8 |
| 72 |
assert 0 |
| 73 |
|
| 74 |
def set_high_bytes_to_zero(word, keep_bytes): |
| 75 |
mask = ((~r_uint(0)) << (8 * keep_bytes)) |
| 76 |
return word & ~mask |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
@objectmodel.always_inline |
| 81 |
def position_string_ender(word): |
| 82 |
maskquote = char_repeated_word_width('"') |
| 83 |
maskbackslash = char_repeated_word_width('\\') |
| 84 |
maskx20 = char_repeated_word_width(chr(0xff - 0x1f)) |
| 85 |
|
| 86 |
|
| 87 |
x1 = maskquote ^ word |
| 88 |
x2 = maskbackslash ^ word |
| 89 |
|
| 90 |
x3 = maskx20 & word |
| 91 |
return any_char_in_words_zero(x1, x2, x3) |
| 92 |
|
| 93 |
@objectmodel.always_inline |
| 94 |
def find_end_of_string_simd_unaligned(ll_chars, startpos, length): |
| 95 |
ch = ll_chars[startpos] |
| 96 |
strhash = (ord(ch) << 7) ^ 0x345678 |
| 97 |
|
| 98 |
wordarray = rffi.cast(rffi.UNSIGNEDP, rffi.ptradd(ll_chars, startpos)) |
| 99 |
num_safe_reads = (length - startpos) // WORD_SIZE |
| 100 |
|
| 101 |
bits = 0 |
| 102 |
for i in range(num_safe_reads): |
| 103 |
word = wordarray[i] |
| 104 |
cond = position_string_ender(word) |
| 105 |
if cond: |
| 106 |
break |
| 107 |
bits |= word |
| 108 |
strhash = intmask((1000003 * strhash) ^ intmask(word)) |
| 109 |
else: |
| 110 |
|
| 111 |
word = 0 |
| 112 |
shift = 0 |
| 113 |
i = startpos + num_safe_reads * WORD_SIZE |
| 114 |
while True: |
| 115 |
|
| 116 |
ch = ll_chars[i] |
| 117 |
if ch == '"' or ch == '\\' or ch < '\x20': |
| 118 |
break |
| 119 |
i += 1 |
| 120 |
bits |= ord(ch) |
| 121 |
word |= ord(ch) << shift |
| 122 |
shift += 8 |
| 123 |
if shift: |
| 124 |
strhash = intmask((1000003 * strhash) ^ intmask(word)) |
| 125 |
|
| 126 |
nonascii = bool(bits & char_repeated_word_width(chr(0x80))) |
| 127 |
return strhash, nonascii, i |
| 128 |
|
| 129 |
|
| 130 |
nonzero = index_nonzero(cond) |
| 131 |
endposition = startpos + i * WORD_SIZE + nonzero |
| 132 |
if nonzero: |
| 133 |
word = set_high_bytes_to_zero(word, nonzero) |
| 134 |
bits |= word |
| 135 |
strhash = intmask((1000003 * strhash) ^ intmask(word)) |
| 136 |
|
| 137 |
nonascii = bool(bits & char_repeated_word_width(chr(0x80))) |
| 138 |
|
| 139 |
return strhash, nonascii, endposition |
| 140 |
|
| 141 |
@objectmodel.always_inline |
| 142 |
def find_end_of_string_simd_unaligned_no_hash(ll_chars, startpos, length): |
| 143 |
ch = ll_chars[startpos] |
| 144 |
|
| 145 |
wordarray = rffi.cast(rffi.UNSIGNEDP, rffi.ptradd(ll_chars, startpos)) |
| 146 |
num_safe_reads = (length - startpos) // WORD_SIZE |
| 147 |
|
| 148 |
bits = 0 |
| 149 |
for i in range(num_safe_reads): |
| 150 |
word = wordarray[i] |
| 151 |
cond = position_string_ender(word) |
| 152 |
if cond: |
| 153 |
break |
| 154 |
bits |= word |
| 155 |
else: |
| 156 |
|
| 157 |
i = startpos + num_safe_reads * WORD_SIZE |
| 158 |
while True: |
| 159 |
|
| 160 |
ch = ll_chars[i] |
| 161 |
if ch == '"' or ch == '\\' or ch < '\x20': |
| 162 |
break |
| 163 |
i += 1 |
| 164 |
bits |= ord(ch) |
| 165 |
|
| 166 |
nonascii = bool(bits & char_repeated_word_width(chr(0x80))) |
| 167 |
return nonascii, i |
| 168 |
|
| 169 |
|
| 170 |
nonzero = index_nonzero(cond) |
| 171 |
endposition = startpos + i * WORD_SIZE + nonzero |
| 172 |
if nonzero: |
| 173 |
word = set_high_bytes_to_zero(word, nonzero) |
| 174 |
bits |= word |
| 175 |
|
| 176 |
nonascii = bool(bits & char_repeated_word_width(chr(0x80))) |
| 177 |
|
| 178 |
return nonascii, endposition |
| 179 |
|
| 180 |
|
| 181 |
@objectmodel.always_inline |
| 182 |
def find_end_of_string_slow(ll_chars, i, length): |
| 183 |
ch = ll_chars[i] |
| 184 |
strhash = (ord(ch) << 7) ^ 0x345678 |
| 185 |
word = 0 |
| 186 |
shift = 0 |
| 187 |
|
| 188 |
bits = 0 |
| 189 |
|
| 190 |
while True: |
| 191 |
|
| 192 |
|
| 193 |
ch = ll_chars[i] |
| 194 |
if ch == '"' or ch == '\\' or ch < '\x20': |
| 195 |
break |
| 196 |
i += 1 |
| 197 |
bits |= ord(ch) |
| 198 |
|
| 199 |
word |= ord(ch) << shift |
| 200 |
shift += 8 |
| 201 |
if shift == WORD_SIZE * 8: |
| 202 |
strhash = intmask((1000003 * strhash) ^ word) |
| 203 |
shift = 0 |
| 204 |
word = 0 |
| 205 |
|
| 206 |
if shift: |
| 207 |
strhash = intmask((1000003 * strhash) ^ word) |
| 208 |
return strhash, bool(bits & 0x80), i |
| 209 |
|
| 210 |
@objectmodel.always_inline |
| 211 |
def find_end_of_string_slow_no_hash(ll_chars, i, length): |
| 212 |
bits = 0 |
| 213 |
while True: |
| 214 |
|
| 215 |
|
| 216 |
ch = ll_chars[i] |
| 217 |
if ch == '"' or ch == '\\' or ch < '\x20': |
| 218 |
break |
| 219 |
i += 1 |
| 220 |
bits |= ord(ch) |
| 221 |
return bool(bits & 0x80), i |
| 222 |
|
| 223 |
if USE_SIMD: |
| 224 |
find_end_of_string = find_end_of_string_simd_unaligned |
| 225 |
find_end_of_string_no_hash = find_end_of_string_simd_unaligned_no_hash |
| 226 |
else: |
| 227 |
find_end_of_string = find_end_of_string_slow |
| 228 |
find_end_of_string_no_hash = find_end_of_string_slow_no_hash |