vile / scheme / port_scan.h
84 lines · 79 sloc · 3.84 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1#ifndef VILE_SCHEME_PORT_SCAN_H
2#define VILE_SCHEME_PORT_SCAN_H
3
4#include <stdint.h>
5#include <stddef.h>
6#include <string.h>
7
8#if defined(__SSE2__) && (defined(__GNUC__) || defined(__clang__)) && !defined(__TINYC__)
9#include <emmintrin.h>
10#define VILE_PORT_SSE2 1
11#endif
12
13static inline int64_t vile_scheme_port_quote(const uint8_t *source, int64_t length) {
14 if (!length) return 0;
15 const uint8_t *found = (const uint8_t *)memchr(source, '"', (size_t)length);
16 return found ? (int64_t)(found - source) : length;
17}
18
19static inline int64_t vile_scheme_port_word_end(const uint8_t *source, int64_t length) {
20 int64_t i = 0;
21#ifdef VILE_PORT_SSE2
22 const __m128i eight = _mm_set1_epi8(8);
23 const __m128i fourteen = _mm_set1_epi8(14);
24 const __m128i space = _mm_set1_epi8(' ');
25 const __m128i low_bit_clear = _mm_set1_epi8((char)0xfe);
26 const __m128i left_paren = _mm_set1_epi8('(');
27 const __m128i quote = _mm_set1_epi8('"');
28 const __m128i semicolon = _mm_set1_epi8(';');
29 while (length - i >= 16) {
30 const __m128i bytes = _mm_loadu_si128((const __m128i *)(source + i));
31 __m128i hits = _mm_and_si128(_mm_cmpgt_epi8(bytes, eight), _mm_cmpgt_epi8(fourteen, bytes));
32 hits = _mm_or_si128(hits, _mm_cmpeq_epi8(bytes, space));
33 hits = _mm_or_si128(hits, _mm_cmpeq_epi8(_mm_and_si128(bytes, low_bit_clear), left_paren));
34 hits = _mm_or_si128(hits, _mm_cmpeq_epi8(bytes, quote));
35 hits = _mm_or_si128(hits, _mm_cmpeq_epi8(bytes, semicolon));
36 const unsigned mask = (unsigned)_mm_movemask_epi8(hits);
37 if (mask) return i + __builtin_ctz(mask);
38 i += 16;
39 }
40#endif
41 for (; i < length; ++i) {
42 const unsigned byte = source[i];
43 if (byte == ' ' || (byte >= 9 && byte <= 13) || byte == '(' || byte == ')' || byte == '"' || byte == ';') return i;
44 }
45 return length;
46}
47
48/* Stop at extension events as well as ordinary delimiters. High bytes go to
49 * the scalar Unicode classifier; all vector loads remain wholly in bounds. */
50static inline int64_t vile_scheme_port_event_end(const uint8_t *source, int64_t length) {
51 int64_t i = 0;
52#ifdef VILE_PORT_SSE2
53 const __m128i eight = _mm_set1_epi8(8), fourteen = _mm_set1_epi8(14);
54 const __m128i space = _mm_set1_epi8(' '), low_bit_clear = _mm_set1_epi8((char)0xfe);
55 const __m128i left_paren = _mm_set1_epi8('('), quote = _mm_set1_epi8('"');
56 const __m128i semicolon = _mm_set1_epi8(';'), hash = _mm_set1_epi8('#');
57 const __m128i bar = _mm_set1_epi8('|'), slash = _mm_set1_epi8(92);
58 while (length - i >= 16) {
59 const __m128i bytes = _mm_loadu_si128((const __m128i *)(source + i));
60 __m128i hits = _mm_and_si128(_mm_cmpgt_epi8(bytes,eight),_mm_cmpgt_epi8(fourteen,bytes));
61 hits = _mm_or_si128(hits,_mm_cmpeq_epi8(bytes,space));
62 hits = _mm_or_si128(hits,_mm_cmpeq_epi8(_mm_and_si128(bytes,low_bit_clear),left_paren));
63 hits = _mm_or_si128(hits,_mm_cmpeq_epi8(bytes,quote));
64 hits = _mm_or_si128(hits,_mm_cmpeq_epi8(bytes,semicolon));
65 hits = _mm_or_si128(hits,_mm_cmpeq_epi8(bytes,hash));
66 hits = _mm_or_si128(hits,_mm_cmpeq_epi8(bytes,bar));
67 hits = _mm_or_si128(hits,_mm_cmpeq_epi8(bytes,slash));
68 const unsigned mask = (unsigned)_mm_movemask_epi8(_mm_or_si128(hits,bytes));
69 if (mask) return i + __builtin_ctz(mask);
70 i += 16;
71 }
72#endif
73 for (; i < length; ++i) {
74 const unsigned b = source[i];
75 if (b >= 128 || b == ' ' || (b >= 9 && b <= 13) || b == '(' || b == ')' || b == '"' || b == ';' || b == '#' || b == '|' || b == 92) return i;
76 }
77 return length;
78}
79static inline int64_t vile_scheme_port_quote_end(const uint8_t *source, int64_t length, uint8_t delimiter) {
80 if (!length) return 0;
81 const uint8_t *found = (const uint8_t *)memchr(source, delimiter, (size_t)length);
82 return found ? (int64_t)(found-source) : length;
83}
84#endif
85