You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

avx2.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Yibo Cai
  5. * Copyright (c) 2019 Vsevolod Stakhov
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #include "config.h"
  25. #include "fastutf8.h"
  26. #include "platform_config.h"
  27. #ifndef __clang__
  28. #pragma GCC push_options
  29. #pragma GCC target("avx2")
  30. #endif
  31. #ifndef __SSE2__
  32. #define __SSE2__
  33. #endif
  34. #ifndef __SSE__
  35. #define __SSE__
  36. #endif
  37. #ifndef __SSE4_2__
  38. #define __SSE4_2__
  39. #endif
  40. #ifndef __SSE4_1__
  41. #define __SSE4_1__
  42. #endif
  43. #ifndef __SSEE3__
  44. #define __SSEE3__
  45. #endif
  46. #ifndef __AVX__
  47. #define __AVX__
  48. #endif
  49. #ifndef __AVX2__
  50. #define __AVX2__
  51. #endif
  52. #include <immintrin.h>
  53. /*
  54. * Map high nibble of "First Byte" to legal character length minus 1
  55. * 0x00 ~ 0xBF --> 0
  56. * 0xC0 ~ 0xDF --> 1
  57. * 0xE0 ~ 0xEF --> 2
  58. * 0xF0 ~ 0xFF --> 3
  59. */
  60. static const int8_t _first_len_tbl[] = {
  61. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3,
  62. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3,
  63. };
  64. /* Map "First Byte" to 8-th item of range table (0xC2 ~ 0xF4) */
  65. static const int8_t _first_range_tbl[] = {
  66. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8,
  67. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8,
  68. };
  69. /*
  70. * Range table, map range index to min and max values
  71. * Index 0 : 00 ~ 7F (First Byte, ascii)
  72. * Index 1,2,3: 80 ~ BF (Second, Third, Fourth Byte)
  73. * Index 4 : A0 ~ BF (Second Byte after E0)
  74. * Index 5 : 80 ~ 9F (Second Byte after ED)
  75. * Index 6 : 90 ~ BF (Second Byte after F0)
  76. * Index 7 : 80 ~ 8F (Second Byte after F4)
  77. * Index 8 : C2 ~ F4 (First Byte, non ascii)
  78. * Index 9~15 : illegal: i >= 127 && i <= -128
  79. */
  80. static const int8_t _range_min_tbl[] = {
  81. 0x00, 0x80, 0x80, 0x80, 0xA0, 0x80, 0x90, 0x80,
  82. 0xC2, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F,
  83. 0x00, 0x80, 0x80, 0x80, 0xA0, 0x80, 0x90, 0x80,
  84. 0xC2, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F,
  85. };
  86. static const int8_t _range_max_tbl[] = {
  87. 0x7F, 0xBF, 0xBF, 0xBF, 0xBF, 0x9F, 0xBF, 0x8F,
  88. 0xF4, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  89. 0x7F, 0xBF, 0xBF, 0xBF, 0xBF, 0x9F, 0xBF, 0x8F,
  90. 0xF4, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  91. };
  92. /*
  93. * Tables for fast handling of four special First Bytes(E0,ED,F0,F4), after
  94. * which the Second Byte are not 80~BF. It contains "range index adjustment".
  95. * +------------+---------------+------------------+----------------+
  96. * | First Byte | original range| range adjustment | adjusted range |
  97. * +------------+---------------+------------------+----------------+
  98. * | E0 | 2 | 2 | 4 |
  99. * +------------+---------------+------------------+----------------+
  100. * | ED | 2 | 3 | 5 |
  101. * +------------+---------------+------------------+----------------+
  102. * | F0 | 3 | 3 | 6 |
  103. * +------------+---------------+------------------+----------------+
  104. * | F4 | 4 | 4 | 8 |
  105. * +------------+---------------+------------------+----------------+
  106. */
  107. /* index1 -> E0, index14 -> ED */
  108. static const int8_t _df_ee_tbl[] = {
  109. 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0,
  110. 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0,
  111. };
  112. /* index1 -> F0, index5 -> F4 */
  113. static const int8_t _ef_fe_tbl[] = {
  114. 0, 3, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  115. 0, 3, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  116. };
  117. static inline __m256i push_last_byte_of_a_to_b(__m256i a, __m256i b)
  118. __attribute__((__target__("avx2")));
  119. static inline __m256i push_last_byte_of_a_to_b(__m256i a, __m256i b)
  120. {
  121. return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 15);
  122. }
  123. static inline __m256i push_last_2bytes_of_a_to_b(__m256i a, __m256i b)
  124. __attribute__((__target__("avx2")));
  125. static inline __m256i push_last_2bytes_of_a_to_b(__m256i a, __m256i b)
  126. {
  127. return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 14);
  128. }
  129. static inline __m256i push_last_3bytes_of_a_to_b(__m256i a, __m256i b)
  130. __attribute__((__target__("avx2")));
  131. static inline __m256i push_last_3bytes_of_a_to_b(__m256i a, __m256i b)
  132. {
  133. return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 13);
  134. }
  135. off_t rspamd_fast_utf8_validate_avx2 (const unsigned char *data, size_t len)
  136. __attribute__((__target__("avx2")));
  137. /* 5x faster than naive method */
  138. /* Return 0 - success, -1 - error, >0 - first error char(if RET_ERR_IDX = 1) */
  139. off_t rspamd_fast_utf8_validate_avx2 (const unsigned char *data, size_t len)
  140. {
  141. off_t err_pos = 1;
  142. if (len >= 32) {
  143. __m256i prev_input = _mm256_set1_epi8 (0);
  144. __m256i prev_first_len = _mm256_set1_epi8 (0);
  145. /* Cached tables */
  146. const __m256i first_len_tbl =
  147. _mm256_lddqu_si256 ((const __m256i *) _first_len_tbl);
  148. const __m256i first_range_tbl =
  149. _mm256_lddqu_si256 ((const __m256i *) _first_range_tbl);
  150. const __m256i range_min_tbl =
  151. _mm256_lddqu_si256 ((const __m256i *) _range_min_tbl);
  152. const __m256i range_max_tbl =
  153. _mm256_lddqu_si256 ((const __m256i *) _range_max_tbl);
  154. const __m256i df_ee_tbl =
  155. _mm256_lddqu_si256 ((const __m256i *) _df_ee_tbl);
  156. const __m256i ef_fe_tbl =
  157. _mm256_lddqu_si256 ((const __m256i *) _ef_fe_tbl);
  158. __m256i error = _mm256_set1_epi8 (0);
  159. while (len >= 32) {
  160. const __m256i input = _mm256_lddqu_si256 ((const __m256i *) data);
  161. /* high_nibbles = input >> 4 */
  162. const __m256i high_nibbles =
  163. _mm256_and_si256 (_mm256_srli_epi16 (input, 4), _mm256_set1_epi8 (0x0F));
  164. /* first_len = legal character length minus 1 */
  165. /* 0 for 00~7F, 1 for C0~DF, 2 for E0~EF, 3 for F0~FF */
  166. /* first_len = first_len_tbl[high_nibbles] */
  167. __m256i first_len = _mm256_shuffle_epi8 (first_len_tbl, high_nibbles);
  168. /* First Byte: set range index to 8 for bytes within 0xC0 ~ 0xFF */
  169. /* range = first_range_tbl[high_nibbles] */
  170. __m256i range = _mm256_shuffle_epi8 (first_range_tbl, high_nibbles);
  171. /* Second Byte: set range index to first_len */
  172. /* 0 for 00~7F, 1 for C0~DF, 2 for E0~EF, 3 for F0~FF */
  173. /* range |= (first_len, prev_first_len) << 1 byte */
  174. range = _mm256_or_si256 (
  175. range, push_last_byte_of_a_to_b (prev_first_len, first_len));
  176. /* Third Byte: set range index to saturate_sub(first_len, 1) */
  177. /* 0 for 00~7F, 0 for C0~DF, 1 for E0~EF, 2 for F0~FF */
  178. __m256i tmp1, tmp2;
  179. /* tmp1 = saturate_sub(first_len, 1) */
  180. tmp1 = _mm256_subs_epu8 (first_len, _mm256_set1_epi8 (1));
  181. /* tmp2 = saturate_sub(prev_first_len, 1) */
  182. tmp2 = _mm256_subs_epu8 (prev_first_len, _mm256_set1_epi8 (1));
  183. /* range |= (tmp1, tmp2) << 2 bytes */
  184. range = _mm256_or_si256 (range, push_last_2bytes_of_a_to_b (tmp2, tmp1));
  185. /* Fourth Byte: set range index to saturate_sub(first_len, 2) */
  186. /* 0 for 00~7F, 0 for C0~DF, 0 for E0~EF, 1 for F0~FF */
  187. /* tmp1 = saturate_sub(first_len, 2) */
  188. tmp1 = _mm256_subs_epu8 (first_len, _mm256_set1_epi8 (2));
  189. /* tmp2 = saturate_sub(prev_first_len, 2) */
  190. tmp2 = _mm256_subs_epu8 (prev_first_len, _mm256_set1_epi8 (2));
  191. /* range |= (tmp1, tmp2) << 3 bytes */
  192. range = _mm256_or_si256 (range, push_last_3bytes_of_a_to_b (tmp2, tmp1));
  193. /*
  194. * Now we have below range indices caluclated
  195. * Correct cases:
  196. * - 8 for C0~FF
  197. * - 3 for 1st byte after F0~FF
  198. * - 2 for 1st byte after E0~EF or 2nd byte after F0~FF
  199. * - 1 for 1st byte after C0~DF or 2nd byte after E0~EF or
  200. * 3rd byte after F0~FF
  201. * - 0 for others
  202. * Error cases:
  203. * 9,10,11 if non ascii First Byte overlaps
  204. * E.g., F1 80 C2 90 --> 8 3 10 2, where 10 indicates error
  205. */
  206. /* Adjust Second Byte range for special First Bytes(E0,ED,F0,F4) */
  207. /* Overlaps lead to index 9~15, which are illegal in range table */
  208. __m256i shift1, pos, range2;
  209. /* shift1 = (input, prev_input) << 1 byte */
  210. shift1 = push_last_byte_of_a_to_b (prev_input, input);
  211. pos = _mm256_sub_epi8 (shift1, _mm256_set1_epi8 (0xEF));
  212. /*
  213. * shift1: | EF F0 ... FE | FF 00 ... ... DE | DF E0 ... EE |
  214. * pos: | 0 1 15 | 16 17 239| 240 241 255|
  215. * pos-240: | 0 0 0 | 0 0 0 | 0 1 15 |
  216. * pos+112: | 112 113 127| >= 128 | >= 128 |
  217. */
  218. tmp1 = _mm256_subs_epu8 (pos, _mm256_set1_epi8 ((char)240));
  219. range2 = _mm256_shuffle_epi8 (df_ee_tbl, tmp1);
  220. tmp2 = _mm256_adds_epu8 (pos, _mm256_set1_epi8 (112));
  221. range2 = _mm256_add_epi8 (range2, _mm256_shuffle_epi8 (ef_fe_tbl, tmp2));
  222. range = _mm256_add_epi8 (range, range2);
  223. /* Load min and max values per calculated range index */
  224. __m256i minv = _mm256_shuffle_epi8 (range_min_tbl, range);
  225. __m256i maxv = _mm256_shuffle_epi8 (range_max_tbl, range);
  226. /* Check value range */
  227. error = _mm256_cmpgt_epi8(minv, input);
  228. error = _mm256_or_si256(error, _mm256_cmpgt_epi8(input, maxv));
  229. /* 5% performance drop from this conditional branch */
  230. if (!_mm256_testz_si256(error, error)) {
  231. break;
  232. }
  233. prev_input = input;
  234. prev_first_len = first_len;
  235. data += 32;
  236. len -= 32;
  237. err_pos += 32;
  238. }
  239. /* Error in first 16 bytes */
  240. if (err_pos == 1) {
  241. goto do_naive;
  242. }
  243. /* Find previous token (not 80~BF) */
  244. int32_t token4 = _mm256_extract_epi32 (prev_input, 7);
  245. const int8_t *token = (const int8_t *) &token4;
  246. int lookahead = 0;
  247. if (token[3] > (int8_t) 0xBF) {
  248. lookahead = 1;
  249. }
  250. else if (token[2] > (int8_t) 0xBF) {
  251. lookahead = 2;
  252. }
  253. else if (token[1] > (int8_t) 0xBF) {
  254. lookahead = 3;
  255. }
  256. data -= lookahead;
  257. len += lookahead;
  258. err_pos -= lookahead;
  259. }
  260. /* Check remaining bytes with naive method */
  261. do_naive:
  262. if (len > 0) {
  263. off_t err_pos2 = rspamd_fast_utf8_validate_ref (data, len);
  264. if (err_pos2) {
  265. return err_pos + err_pos2 - 1;
  266. }
  267. }
  268. return 0;
  269. }
  270. #ifndef __clang__
  271. #pragma GCC pop_options
  272. #endif