1
0

simdutf8check.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #ifndef SIMDUTF8CHECK_H
  2. #define SIMDUTF8CHECK_H
  3. #include <stdbool.h>
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <string.h>
  7. #include <x86intrin.h>
  8. /*
  9. * legal utf-8 byte sequence
  10. * http://www.unicode.org/versions/Unicode6.0.0/ch03.pdf - page 94
  11. *
  12. * Code Points 1st 2s 3s 4s
  13. * U+0000..U+007F 00..7F
  14. * U+0080..U+07FF C2..DF 80..BF
  15. * U+0800..U+0FFF E0 A0..BF 80..BF
  16. * U+1000..U+CFFF E1..EC 80..BF 80..BF
  17. * U+D000..U+D7FF ED 80..9F 80..BF
  18. * U+E000..U+FFFF EE..EF 80..BF 80..BF
  19. * U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
  20. * U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
  21. * U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
  22. *
  23. */
  24. // all byte values must be no larger than 0xF4
  25. static inline void checkSmallerThan0xF4(__m128i current_bytes,
  26. __m128i *has_error) {
  27. // unsigned, saturates to 0 below max
  28. *has_error = _mm_or_si128(*has_error,
  29. _mm_subs_epu8(current_bytes, _mm_set1_epi8(0xF4)));
  30. }
  31. static inline __m128i continuationLengths(__m128i high_nibbles) {
  32. return _mm_shuffle_epi8(
  33. _mm_setr_epi8(1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII)
  34. 0, 0, 0, 0, // 10xx (continuation)
  35. 2, 2, // 110x
  36. 3, // 1110
  37. 4), // 1111, next should be 0 (not checked here)
  38. high_nibbles);
  39. }
  40. static inline __m128i carryContinuations(__m128i initial_lengths,
  41. __m128i previous_carries) {
  42. __m128i right1 =
  43. _mm_subs_epu8(_mm_alignr_epi8(initial_lengths, previous_carries, 16 - 1),
  44. _mm_set1_epi8(1));
  45. __m128i sum = _mm_add_epi8(initial_lengths, right1);
  46. __m128i right2 = _mm_subs_epu8(_mm_alignr_epi8(sum, previous_carries, 16 - 2),
  47. _mm_set1_epi8(2));
  48. return _mm_add_epi8(sum, right2);
  49. }
  50. static inline void checkContinuations(__m128i initial_lengths, __m128i carries,
  51. __m128i *has_error) {
  52. // overlap || underlap
  53. // carry > length && length > 0 || !(carry > length) && !(length > 0)
  54. // (carries > length) == (lengths > 0)
  55. __m128i overunder =
  56. _mm_cmpeq_epi8(_mm_cmpgt_epi8(carries, initial_lengths),
  57. _mm_cmpgt_epi8(initial_lengths, _mm_setzero_si128()));
  58. *has_error = _mm_or_si128(*has_error, overunder);
  59. }
  60. // when 0xED is found, next byte must be no larger than 0x9F
  61. // when 0xF4 is found, next byte must be no larger than 0x8F
  62. // next byte must be continuation, ie sign bit is set, so signed < is ok
  63. static inline void checkFirstContinuationMax(__m128i current_bytes,
  64. __m128i off1_current_bytes,
  65. __m128i *has_error) {
  66. __m128i maskED = _mm_cmpeq_epi8(off1_current_bytes, _mm_set1_epi8(0xED));
  67. __m128i maskF4 = _mm_cmpeq_epi8(off1_current_bytes, _mm_set1_epi8(0xF4));
  68. __m128i badfollowED =
  69. _mm_and_si128(_mm_cmpgt_epi8(current_bytes, _mm_set1_epi8(0x9F)), maskED);
  70. __m128i badfollowF4 =
  71. _mm_and_si128(_mm_cmpgt_epi8(current_bytes, _mm_set1_epi8(0x8F)), maskF4);
  72. *has_error = _mm_or_si128(*has_error, _mm_or_si128(badfollowED, badfollowF4));
  73. }
  74. // map off1_hibits => error condition
  75. // hibits off1 cur
  76. // C => < C2 && true
  77. // E => < E1 && < A0
  78. // F => < F1 && < 90
  79. // else false && false
  80. static inline void checkOverlong(__m128i current_bytes,
  81. __m128i off1_current_bytes, __m128i hibits,
  82. __m128i previous_hibits, __m128i *has_error) {
  83. __m128i off1_hibits = _mm_alignr_epi8(hibits, previous_hibits, 16 - 1);
  84. __m128i initial_mins = _mm_shuffle_epi8(
  85. _mm_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
  86. -128, -128, // 10xx => false
  87. 0xC2, -128, // 110x
  88. 0xE1, // 1110
  89. 0xF1),
  90. off1_hibits);
  91. __m128i initial_under = _mm_cmpgt_epi8(initial_mins, off1_current_bytes);
  92. __m128i second_mins = _mm_shuffle_epi8(
  93. _mm_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
  94. -128, -128, // 10xx => false
  95. 127, 127, // 110x => true
  96. 0xA0, // 1110
  97. 0x90),
  98. off1_hibits);
  99. __m128i second_under = _mm_cmpgt_epi8(second_mins, current_bytes);
  100. *has_error =
  101. _mm_or_si128(*has_error, _mm_and_si128(initial_under, second_under));
  102. }
  103. struct processed_utf_bytes {
  104. __m128i rawbytes;
  105. __m128i high_nibbles;
  106. __m128i carried_continuations;
  107. };
  108. static inline void count_nibbles(__m128i bytes,
  109. struct processed_utf_bytes *answer) {
  110. answer->rawbytes = bytes;
  111. answer->high_nibbles =
  112. _mm_and_si128(_mm_srli_epi16(bytes, 4), _mm_set1_epi8(0x0F));
  113. }
  114. // check whether the current bytes are valid UTF-8
  115. // at the end of the function, previous gets updated
  116. static struct processed_utf_bytes
  117. checkUTF8Bytes(__m128i current_bytes, struct processed_utf_bytes *previous,
  118. __m128i *has_error) {
  119. struct processed_utf_bytes pb;
  120. count_nibbles(current_bytes, &pb);
  121. checkSmallerThan0xF4(current_bytes, has_error);
  122. __m128i initial_lengths = continuationLengths(pb.high_nibbles);
  123. pb.carried_continuations =
  124. carryContinuations(initial_lengths, previous->carried_continuations);
  125. checkContinuations(initial_lengths, pb.carried_continuations, has_error);
  126. __m128i off1_current_bytes =
  127. _mm_alignr_epi8(pb.rawbytes, previous->rawbytes, 16 - 1);
  128. checkFirstContinuationMax(current_bytes, off1_current_bytes, has_error);
  129. checkOverlong(current_bytes, off1_current_bytes, pb.high_nibbles,
  130. previous->high_nibbles, has_error);
  131. return pb;
  132. }
  133. static bool validate_utf8_fast(const char *src, size_t len) {
  134. size_t i = 0;
  135. __m128i has_error = _mm_setzero_si128();
  136. struct processed_utf_bytes previous = {.rawbytes = _mm_setzero_si128(),
  137. .high_nibbles = _mm_setzero_si128(),
  138. .carried_continuations =
  139. _mm_setzero_si128()};
  140. if (len >= 16) {
  141. for (; i <= len - 16; i += 16) {
  142. __m128i current_bytes = _mm_loadu_si128((const __m128i *)(src + i));
  143. previous = checkUTF8Bytes(current_bytes, &previous, &has_error);
  144. }
  145. }
  146. // last part
  147. if (i < len) {
  148. char buffer[16];
  149. memset(buffer, 0, 16);
  150. memcpy(buffer, src + i, len - i);
  151. __m128i current_bytes = _mm_loadu_si128((const __m128i *)(buffer));
  152. previous = checkUTF8Bytes(current_bytes, &previous, &has_error);
  153. } else {
  154. has_error =
  155. _mm_or_si128(_mm_cmpgt_epi8(previous.carried_continuations,
  156. _mm_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  157. 9, 9, 9, 9, 9, 1)),
  158. has_error);
  159. }
  160. return _mm_testz_si128(has_error, has_error);
  161. }
  162. #ifdef __AVX2__
  163. /*****************************/
  164. static inline __m256i push_last_byte_of_a_to_b(__m256i a, __m256i b) {
  165. return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 15);
  166. }
  167. static inline __m256i push_last_2bytes_of_a_to_b(__m256i a, __m256i b) {
  168. return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 14);
  169. }
  170. // all byte values must be no larger than 0xF4
  171. static inline void avxcheckSmallerThan0xF4(__m256i current_bytes,
  172. __m256i *has_error) {
  173. // unsigned, saturates to 0 below max
  174. *has_error = _mm256_or_si256(
  175. *has_error, _mm256_subs_epu8(current_bytes, _mm256_set1_epi8(0xF4)));
  176. }
  177. static inline __m256i avxcontinuationLengths(__m256i high_nibbles) {
  178. return _mm256_shuffle_epi8(
  179. _mm256_setr_epi8(1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII)
  180. 0, 0, 0, 0, // 10xx (continuation)
  181. 2, 2, // 110x
  182. 3, // 1110
  183. 4, // 1111, next should be 0 (not checked here)
  184. 1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII)
  185. 0, 0, 0, 0, // 10xx (continuation)
  186. 2, 2, // 110x
  187. 3, // 1110
  188. 4 // 1111, next should be 0 (not checked here)
  189. ),
  190. high_nibbles);
  191. }
  192. static inline __m256i avxcarryContinuations(__m256i initial_lengths,
  193. __m256i previous_carries) {
  194. __m256i right1 = _mm256_subs_epu8(
  195. push_last_byte_of_a_to_b(previous_carries, initial_lengths),
  196. _mm256_set1_epi8(1));
  197. __m256i sum = _mm256_add_epi8(initial_lengths, right1);
  198. __m256i right2 = _mm256_subs_epu8(
  199. push_last_2bytes_of_a_to_b(previous_carries, sum), _mm256_set1_epi8(2));
  200. return _mm256_add_epi8(sum, right2);
  201. }
  202. static inline void avxcheckContinuations(__m256i initial_lengths,
  203. __m256i carries, __m256i *has_error) {
  204. // overlap || underlap
  205. // carry > length && length > 0 || !(carry > length) && !(length > 0)
  206. // (carries > length) == (lengths > 0)
  207. __m256i overunder = _mm256_cmpeq_epi8(
  208. _mm256_cmpgt_epi8(carries, initial_lengths),
  209. _mm256_cmpgt_epi8(initial_lengths, _mm256_setzero_si256()));
  210. *has_error = _mm256_or_si256(*has_error, overunder);
  211. }
  212. // when 0xED is found, next byte must be no larger than 0x9F
  213. // when 0xF4 is found, next byte must be no larger than 0x8F
  214. // next byte must be continuation, ie sign bit is set, so signed < is ok
  215. static inline void avxcheckFirstContinuationMax(__m256i current_bytes,
  216. __m256i off1_current_bytes,
  217. __m256i *has_error) {
  218. __m256i maskED =
  219. _mm256_cmpeq_epi8(off1_current_bytes, _mm256_set1_epi8(0xED));
  220. __m256i maskF4 =
  221. _mm256_cmpeq_epi8(off1_current_bytes, _mm256_set1_epi8(0xF4));
  222. __m256i badfollowED = _mm256_and_si256(
  223. _mm256_cmpgt_epi8(current_bytes, _mm256_set1_epi8(0x9F)), maskED);
  224. __m256i badfollowF4 = _mm256_and_si256(
  225. _mm256_cmpgt_epi8(current_bytes, _mm256_set1_epi8(0x8F)), maskF4);
  226. *has_error =
  227. _mm256_or_si256(*has_error, _mm256_or_si256(badfollowED, badfollowF4));
  228. }
  229. // map off1_hibits => error condition
  230. // hibits off1 cur
  231. // C => < C2 && true
  232. // E => < E1 && < A0
  233. // F => < F1 && < 90
  234. // else false && false
  235. static inline void avxcheckOverlong(__m256i current_bytes,
  236. __m256i off1_current_bytes, __m256i hibits,
  237. __m256i previous_hibits,
  238. __m256i *has_error) {
  239. __m256i off1_hibits = push_last_byte_of_a_to_b(previous_hibits, hibits);
  240. __m256i initial_mins = _mm256_shuffle_epi8(
  241. _mm256_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128,
  242. -128, -128, -128, // 10xx => false
  243. 0xC2, -128, // 110x
  244. 0xE1, // 1110
  245. 0xF1, -128, -128, -128, -128, -128, -128, -128, -128,
  246. -128, -128, -128, -128, // 10xx => false
  247. 0xC2, -128, // 110x
  248. 0xE1, // 1110
  249. 0xF1),
  250. off1_hibits);
  251. __m256i initial_under = _mm256_cmpgt_epi8(initial_mins, off1_current_bytes);
  252. __m256i second_mins = _mm256_shuffle_epi8(
  253. _mm256_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128,
  254. -128, -128, -128, // 10xx => false
  255. 127, 127, // 110x => true
  256. 0xA0, // 1110
  257. 0x90, -128, -128, -128, -128, -128, -128, -128, -128,
  258. -128, -128, -128, -128, // 10xx => false
  259. 127, 127, // 110x => true
  260. 0xA0, // 1110
  261. 0x90),
  262. off1_hibits);
  263. __m256i second_under = _mm256_cmpgt_epi8(second_mins, current_bytes);
  264. *has_error = _mm256_or_si256(*has_error,
  265. _mm256_and_si256(initial_under, second_under));
  266. }
  267. struct avx_processed_utf_bytes {
  268. __m256i rawbytes;
  269. __m256i high_nibbles;
  270. __m256i carried_continuations;
  271. };
  272. static inline void avx_count_nibbles(__m256i bytes,
  273. struct avx_processed_utf_bytes *answer) {
  274. answer->rawbytes = bytes;
  275. answer->high_nibbles =
  276. _mm256_and_si256(_mm256_srli_epi16(bytes, 4), _mm256_set1_epi8(0x0F));
  277. }
  278. // check whether the current bytes are valid UTF-8
  279. // at the end of the function, previous gets updated
  280. static struct avx_processed_utf_bytes
  281. avxcheckUTF8Bytes(__m256i current_bytes,
  282. struct avx_processed_utf_bytes *previous,
  283. __m256i *has_error) {
  284. struct avx_processed_utf_bytes pb;
  285. avx_count_nibbles(current_bytes, &pb);
  286. avxcheckSmallerThan0xF4(current_bytes, has_error);
  287. __m256i initial_lengths = avxcontinuationLengths(pb.high_nibbles);
  288. pb.carried_continuations =
  289. avxcarryContinuations(initial_lengths, previous->carried_continuations);
  290. avxcheckContinuations(initial_lengths, pb.carried_continuations, has_error);
  291. __m256i off1_current_bytes =
  292. push_last_byte_of_a_to_b(previous->rawbytes, pb.rawbytes);
  293. avxcheckFirstContinuationMax(current_bytes, off1_current_bytes, has_error);
  294. avxcheckOverlong(current_bytes, off1_current_bytes, pb.high_nibbles,
  295. previous->high_nibbles, has_error);
  296. return pb;
  297. }
  298. // check whether the current bytes are valid UTF-8
  299. // at the end of the function, previous gets updated
  300. static struct avx_processed_utf_bytes
  301. avxcheckUTF8Bytes_asciipath(__m256i current_bytes,
  302. struct avx_processed_utf_bytes *previous,
  303. __m256i *has_error) {
  304. if (_mm256_testz_si256(current_bytes,
  305. _mm256_set1_epi8(0x80))) { // fast ascii path
  306. *has_error = _mm256_or_si256(
  307. _mm256_cmpgt_epi8(previous->carried_continuations,
  308. _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  309. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  310. 9, 9, 9, 9, 9, 9, 9, 1)),
  311. *has_error);
  312. return *previous;
  313. }
  314. struct avx_processed_utf_bytes pb;
  315. avx_count_nibbles(current_bytes, &pb);
  316. avxcheckSmallerThan0xF4(current_bytes, has_error);
  317. __m256i initial_lengths = avxcontinuationLengths(pb.high_nibbles);
  318. pb.carried_continuations =
  319. avxcarryContinuations(initial_lengths, previous->carried_continuations);
  320. avxcheckContinuations(initial_lengths, pb.carried_continuations, has_error);
  321. __m256i off1_current_bytes =
  322. push_last_byte_of_a_to_b(previous->rawbytes, pb.rawbytes);
  323. avxcheckFirstContinuationMax(current_bytes, off1_current_bytes, has_error);
  324. avxcheckOverlong(current_bytes, off1_current_bytes, pb.high_nibbles,
  325. previous->high_nibbles, has_error);
  326. return pb;
  327. }
  328. static bool validate_utf8_fast_avx_asciipath(const char *src, size_t len) {
  329. size_t i = 0;
  330. __m256i has_error = _mm256_setzero_si256();
  331. struct avx_processed_utf_bytes previous = {
  332. .rawbytes = _mm256_setzero_si256(),
  333. .high_nibbles = _mm256_setzero_si256(),
  334. .carried_continuations = _mm256_setzero_si256()};
  335. if (len >= 32) {
  336. for (; i <= len - 32; i += 32) {
  337. __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(src + i));
  338. previous =
  339. avxcheckUTF8Bytes_asciipath(current_bytes, &previous, &has_error);
  340. }
  341. }
  342. // last part
  343. if (i < len) {
  344. char buffer[32];
  345. memset(buffer, 0, 32);
  346. memcpy(buffer, src + i, len - i);
  347. __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(buffer));
  348. previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error);
  349. } else {
  350. has_error = _mm256_or_si256(
  351. _mm256_cmpgt_epi8(previous.carried_continuations,
  352. _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  353. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  354. 9, 9, 9, 9, 9, 9, 9, 1)),
  355. has_error);
  356. }
  357. return _mm256_testz_si256(has_error, has_error);
  358. }
  359. static bool validate_utf8_fast_avx(const char *src, size_t len) {
  360. size_t i = 0;
  361. __m256i has_error = _mm256_setzero_si256();
  362. struct avx_processed_utf_bytes previous = {
  363. .rawbytes = _mm256_setzero_si256(),
  364. .high_nibbles = _mm256_setzero_si256(),
  365. .carried_continuations = _mm256_setzero_si256()};
  366. if (len >= 32) {
  367. for (; i <= len - 32; i += 32) {
  368. __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(src + i));
  369. previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error);
  370. }
  371. }
  372. // last part
  373. if (i < len) {
  374. char buffer[32];
  375. memset(buffer, 0, 32);
  376. memcpy(buffer, src + i, len - i);
  377. __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(buffer));
  378. previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error);
  379. } else {
  380. has_error = _mm256_or_si256(
  381. _mm256_cmpgt_epi8(previous.carried_continuations,
  382. _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  383. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  384. 9, 9, 9, 9, 9, 9, 9, 1)),
  385. has_error);
  386. }
  387. return _mm256_testz_si256(has_error, has_error);
  388. }
  389. #endif // __AVX2__
  390. #endif