#ifndef VILE_BLOCK_SCAN_H #define VILE_BLOCK_SCAN_H #include #if !defined(__TINYC__) && (defined(__GNUC__) || defined(__clang__)) #define VILE_BLOCK_GNU_INTRINSICS 1 #endif /* Each block caller has proved that 16 bytes are available. Loads are unaligned, * read-only, and never inspect padding past the source. Returning a scalar * avoids copying SIMD structs through the generated V wrappers. */ #if defined(__SSE2__) && defined(VILE_BLOCK_GNU_INTRINSICS) #include static inline int vile_block_non_ascii(const unsigned char *source) { return _mm_movemask_epi8(_mm_loadu_si128((const __m128i *)source)) != 0; } static inline int vile_block_alphanumeric_prefix(const unsigned char *source) { __m128i bytes = _mm_loadu_si128((const __m128i *)source); __m128i folded = _mm_or_si128(bytes, _mm_set1_epi8(0x20)); __m128i letters = _mm_and_si128(_mm_cmpgt_epi8(folded, _mm_set1_epi8('a' - 1)), _mm_cmpgt_epi8(_mm_set1_epi8('z' + 1), folded)); __m128i digits = _mm_and_si128(_mm_cmpgt_epi8(bytes, _mm_set1_epi8('0' - 1)), _mm_cmpgt_epi8(_mm_set1_epi8('9' + 1), bytes)); unsigned int stop = (unsigned int)~_mm_movemask_epi8(_mm_or_si128(letters, digits)) & 0xffff; return stop == 0 ? 16 : __builtin_ctz(stop); } static inline int vile_block_string_special(const unsigned char *source) { __m128i bytes = _mm_loadu_si128((const __m128i *)source); __m128i quotes = _mm_or_si128(_mm_cmpeq_epi8(bytes, _mm_set1_epi8('"')), _mm_cmpeq_epi8(bytes, _mm_set1_epi8('\\'))); __m128i lines = _mm_or_si128(_mm_cmpeq_epi8(bytes, _mm_set1_epi8('\r')), _mm_cmpeq_epi8(bytes, _mm_set1_epi8('\n'))); return _mm_movemask_epi8(_mm_or_si128(quotes, lines)) != 0; } #elif defined(__aarch64__) && defined(__ARM_NEON) && defined(VILE_BLOCK_GNU_INTRINSICS) #include static inline int vile_block_non_ascii(const unsigned char *source) { return vmaxvq_u8(vld1q_u8(source)) >= 0x80; } static inline int vile_block_alphanumeric_prefix(const unsigned char *source) { uint8x16_t bytes = vld1q_u8(source); uint8x16_t folded = vorrq_u8(bytes, vdupq_n_u8(0x20)); uint8x16_t letters = vandq_u8(vcgeq_u8(folded, vdupq_n_u8('a')), vcleq_u8(folded, vdupq_n_u8('z'))); uint8x16_t digits = vandq_u8(vcgeq_u8(bytes, vdupq_n_u8('0')), vcleq_u8(bytes, vdupq_n_u8('9'))); static const unsigned char lanes[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; return vminvq_u8(vbslq_u8(vorrq_u8(letters, digits), vdupq_n_u8(16), vld1q_u8(lanes))); } static inline int vile_block_string_special(const unsigned char *source) { uint8x16_t bytes = vld1q_u8(source); uint8x16_t quotes = vorrq_u8(vceqq_u8(bytes, vdupq_n_u8('"')), vceqq_u8(bytes, vdupq_n_u8('\\'))); uint8x16_t lines = vorrq_u8(vceqq_u8(bytes, vdupq_n_u8('\r')), vceqq_u8(bytes, vdupq_n_u8('\n'))); return vmaxvq_u8(vorrq_u8(quotes, lines)) != 0; } #else static inline int vile_block_non_ascii(const unsigned char *source) { unsigned int high = 0; for (int i = 0; i < 16; ++i) high |= source[i]; return (high & 0x80) != 0; } static inline int vile_block_alphanumeric_prefix(const unsigned char *source) { for (int i = 0; i < 16; ++i) { unsigned char c = source[i]; if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) return i; } return 16; } static inline int vile_block_string_special(const unsigned char *source) { for (int i = 0; i < 16; ++i) { unsigned char c = source[i]; if (c == '"' || c == '\\' || c == '\r' || c == '\n') return 1; } return 0; } #endif /* Used only after the V scanner has encountered a legacy string escape. * Consume ordinary bytes and validated quote/backslash pairs in one bounded * run. Extended escapes, line endings and real closing quotes return to V. * A mask is reused for all escapes within a block; escaped quote/backslash * bits are discarded together, including pairs crossing the last lane. */ #if defined(VILE_BLOCK_GNU_INTRINSICS) __attribute__((noinline)) #endif static int64_t vile_string_legacy_run(const unsigned char *source, int64_t length) { int64_t i = 0; #if defined(VILE_BLOCK_GNU_INTRINSICS) && (defined(__SSE2__) || (defined(__aarch64__) && defined(__ARM_NEON))) while (i <= length - 16) { #if defined(__SSE2__) __m128i bytes = _mm_loadu_si128((const __m128i *)(source + i)); __m128i quotes = _mm_or_si128(_mm_cmpeq_epi8(bytes, _mm_set1_epi8('"')), _mm_cmpeq_epi8(bytes, _mm_set1_epi8('\\'))); __m128i lines = _mm_or_si128(_mm_cmpeq_epi8(bytes, _mm_set1_epi8('\r')), _mm_cmpeq_epi8(bytes, _mm_set1_epi8('\n'))); unsigned int mask = (unsigned int)_mm_movemask_epi8(_mm_or_si128(quotes, lines)); #else uint8x16_t bytes = vld1q_u8(source + i); uint8x16_t quotes = vorrq_u8(vceqq_u8(bytes, vdupq_n_u8('"')), vceqq_u8(bytes, vdupq_n_u8('\\'))); uint8x16_t lines = vorrq_u8(vceqq_u8(bytes, vdupq_n_u8('\r')), vceqq_u8(bytes, vdupq_n_u8('\n'))); uint8x16_t stop = vorrq_u8(quotes, lines); static const unsigned char bit_values[8] = {1,2,4,8,16,32,64,128}; uint8x8_t bits = vld1_u8(bit_values); unsigned int mask = (unsigned int)vaddv_u8(vand_u8(vget_low_u8(stop), bits)) | ((unsigned int)vaddv_u8(vand_u8(vget_high_u8(stop), bits)) << 8); #endif int64_t consumed = i + 16; while (mask) { int lane = __builtin_ctz(mask); int64_t at = i + lane; if (source[at] != '\\' || at + 1 >= length || (source[at + 1] != '"' && source[at + 1] != '\\')) return at; if (at + 2 > consumed) consumed = at + 2; mask &= ~((1u << (lane + 2)) - 1u); } i = consumed; } #endif while (i < length) { unsigned char c = source[i]; if (c == '\\') { if (i + 1 >= length || (source[i + 1] != '"' && source[i + 1] != '\\')) return i; i += 2; } else { if (c == '"' || c == '\r' || c == '\n') return i; ++i; } } return i; } /* The short scalar prefix uses an exact byte table: one membership load and * one termination branch, independent of letter/digit/punctuation category. */ static const unsigned char vile_subsequent[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; static inline int vile_identifier_subsequent(unsigned char c) { return vile_subsequent[c]; } /* A byte belongs to the R5RS alphabet when its high-nibble bit is set * in the mask selected by its low nibble. Two byte shuffles classify every * subsequent character, including punctuation, without a scalar rescan. */ #if defined(VILE_BLOCK_GNU_INTRINSICS) && (defined(__x86_64__) || defined(__i386__)) #include static const unsigned char vile_subsequent_low[16] = {184, 252, 248, 248, 252, 252, 252, 248, 248, 248, 252, 84, 88, 92, 252, 124}; static const unsigned char vile_subsequent_high[16] = {1,2,4,8,16,32,64,128,0,0,0,0,0,0,0,0}; __attribute__((target("avx2"), noinline)) static int64_t vile_identifier_avx2(const unsigned char *source, int64_t length) { __m256i low = _mm256_broadcastsi128_si256(_mm_loadu_si128((const __m128i *)vile_subsequent_low)); __m256i high = _mm256_broadcastsi128_si256(_mm_loadu_si128((const __m128i *)vile_subsequent_high)); __m256i nibble = _mm256_set1_epi8(15); int64_t i = 0; for (; i <= length - 32; i += 32) { __m256i c = _mm256_loadu_si256((const __m256i *)(source + i)); __m256i allowed = _mm256_and_si256(_mm256_shuffle_epi8(low, _mm256_and_si256(c, nibble)), _mm256_shuffle_epi8(high, _mm256_and_si256(_mm256_srli_epi16(c, 4), nibble))); unsigned int stop = (unsigned int)_mm256_movemask_epi8(_mm256_cmpeq_epi8(allowed, _mm256_setzero_si256())); if (stop) return i + __builtin_ctz(stop); } while (i < length && vile_identifier_subsequent(source[i])) ++i; return i; } __attribute__((target("ssse3"), noinline)) static int64_t vile_identifier_ssse3(const unsigned char *source, int64_t length) { __m128i low = _mm_loadu_si128((const __m128i *)vile_subsequent_low); __m128i high = _mm_loadu_si128((const __m128i *)vile_subsequent_high); __m128i nibble = _mm_set1_epi8(15); int64_t i = 0; for (; i <= length - 16; i += 16) { __m128i c = _mm_loadu_si128((const __m128i *)(source + i)); __m128i allowed = _mm_and_si128(_mm_shuffle_epi8(low, _mm_and_si128(c, nibble)), _mm_shuffle_epi8(high, _mm_and_si128(_mm_srli_epi16(c, 4), nibble))); unsigned int stop = (unsigned int)_mm_movemask_epi8(_mm_cmpeq_epi8(allowed, _mm_setzero_si128())); if (stop) return i + __builtin_ctz(stop); } while (i < length && vile_identifier_subsequent(source[i])) ++i; return i; } #endif /* Long-run setup stays out of the short-token leaf function. */ #if defined(VILE_BLOCK_GNU_INTRINSICS) __attribute__((noinline)) #endif static int64_t vile_identifier_long(const unsigned char *source, int64_t length) { int64_t i = 16; #if defined(VILE_BLOCK_GNU_INTRINSICS) && (defined(__x86_64__) || defined(__i386__)) if (length - i >= 64) { if (__builtin_cpu_supports("avx2")) return i + vile_identifier_avx2(source + i, length - i); if (__builtin_cpu_supports("ssse3")) return i + vile_identifier_ssse3(source + i, length - i); } #endif while (i <= length - 16) { int count = vile_block_alphanumeric_prefix(source + i); i += count; if (count != 16) break; } while (i < length && vile_identifier_subsequent(source[i])) ++i; return i; } /* Borrow exactly length bytes. Short identifiers need no stack frame, SIMD * setup or CPU dispatch. Finishing 16 valid bytes tail-calls the long kernel. */ #if defined(VILE_BLOCK_GNU_INTRINSICS) __attribute__((noinline)) #endif static int64_t vile_identifier_run(const unsigned char *source, int64_t length) { int64_t prefix = length < 16 ? length : 16; for (int64_t i = 0; i < prefix; ++i) { if (!vile_identifier_subsequent(source[i])) return i; } if (length <= 16) return length; return vile_identifier_long(source, length); } /* Source UTF8 is validated once by the scanner. Ordinary high bytes cannot * contain ASCII specials; only C2 85 and E2 80 A8/A9 need extra line handling. * Candidate masks avoid decoding ordinary Unicode scalars, while inspecting * each candidate against the complete bounded source also handles block edges. */ static inline int vile_unicode_line_at(const unsigned char *source, int64_t length, int64_t at, int paragraph) { unsigned char c = source[at]; if (c == 0xc2) return at + 1 < length && source[at + 1] == 0x85; return c == 0xe2 && at + 2 < length && source[at + 1] == 0x80 && (source[at + 2] == 0xa8 || (paragraph && source[at + 2] == 0xa9)); } static inline int64_t vile_unicode_run(const unsigned char *source, int64_t length, int string_mode, int unicode_mode) { int64_t i = 0; #if defined(__SSE2__) && defined(VILE_BLOCK_GNU_INTRINSICS) for (; i <= length - 16; i += 16) { __m128i bytes = _mm_loadu_si128((const __m128i *)(source + i)); __m128i stop = _mm_or_si128(_mm_cmpeq_epi8(bytes, _mm_set1_epi8('\r')), _mm_cmpeq_epi8(bytes, _mm_set1_epi8('\n'))); if (string_mode) stop = _mm_or_si128(stop, _mm_or_si128(_mm_cmpeq_epi8(bytes, _mm_set1_epi8('"')), _mm_cmpeq_epi8(bytes, _mm_set1_epi8('\\')))); unsigned int ascii = (unsigned int)_mm_movemask_epi8(stop); unsigned int mask = ascii; if (unicode_mode) { __m128i candidates = _mm_or_si128(_mm_cmpeq_epi8(bytes, _mm_set1_epi8((char)0xc2)), _mm_cmpeq_epi8(bytes, _mm_set1_epi8((char)0xe2))); mask |= (unsigned int)_mm_movemask_epi8(candidates); } while (mask) { int lane = __builtin_ctz(mask); if ((ascii & (1u << lane)) || (unicode_mode && vile_unicode_line_at(source, length, i + lane, !string_mode))) return i + lane; mask &= mask - 1; } } #elif defined(__aarch64__) && defined(__ARM_NEON) && defined(VILE_BLOCK_GNU_INTRINSICS) for (; i <= length - 16; i += 16) { uint8x16_t bytes = vld1q_u8(source + i); uint8x16_t stop = vorrq_u8(vceqq_u8(bytes, vdupq_n_u8('\r')), vceqq_u8(bytes, vdupq_n_u8('\n'))); if (string_mode) stop = vorrq_u8(stop, vorrq_u8(vceqq_u8(bytes, vdupq_n_u8('"')),vceqq_u8(bytes, vdupq_n_u8('\\')))); uint8x16_t candidates = vdupq_n_u8(0); if (unicode_mode) candidates = vorrq_u8(vceqq_u8(bytes,vdupq_n_u8(0xc2)),vceqq_u8(bytes,vdupq_n_u8(0xe2))); if (vmaxvq_u8(vorrq_u8(stop,candidates)) != 0) { for (int lane = 0; lane < 16; ++lane) { unsigned char c = source[i + lane]; if (c == '\r' || c == '\n' || (string_mode && (c == '"' || c == '\\')) || (unicode_mode && vile_unicode_line_at(source,length,i + lane,!string_mode))) return i + lane; } } } #endif for (; i < length; ++i) { unsigned char c = source[i]; if (c == '\r' || c == '\n' || (string_mode && (c == '"' || c == '\\')) || (unicode_mode && vile_unicode_line_at(source,length,i,!string_mode))) return i; } return length; } #if defined(VILE_BLOCK_GNU_INTRINSICS) __attribute__((noinline)) #endif static int64_t vile_string_unicode_run(const unsigned char *source, int64_t length) { return vile_unicode_run(source,length,1,1); } #if defined(VILE_BLOCK_GNU_INTRINSICS) __attribute__((noinline)) #endif static int64_t vile_comment_unicode_run(const unsigned char *source, int64_t length) { return vile_unicode_run(source,length,0,1); } #if defined(VILE_BLOCK_GNU_INTRINSICS) __attribute__((noinline)) #endif static int64_t vile_comment_ascii_run(const unsigned char *source, int64_t length) { return vile_unicode_run(source,length,0,0); } #endif