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.

sse41.c 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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("sse4.1")
  30. #endif
  31. #ifndef __SSE2__
  32. #define __SSE2__
  33. #endif
  34. #ifndef __SSE__
  35. #define __SSE__
  36. #endif
  37. #ifndef __SSEE3__
  38. #define __SSEE3__
  39. #endif
  40. #ifndef __SSE4_1__
  41. #define __SSE4_1__
  42. #endif
  43. #include <smmintrin.h>
  44. /*
  45. * Map high nibble of "First Byte" to legal character length minus 1
  46. * 0x00 ~ 0xBF --> 0
  47. * 0xC0 ~ 0xDF --> 1
  48. * 0xE0 ~ 0xEF --> 2
  49. * 0xF0 ~ 0xFF --> 3
  50. */
  51. static const int8_t _first_len_tbl[] = {
  52. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3,
  53. };
  54. /* Map "First Byte" to 8-th item of range table (0xC2 ~ 0xF4) */
  55. static const int8_t _first_range_tbl[] = {
  56. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8,
  57. };
  58. /*
  59. * Range table, map range index to min and max values
  60. * Index 0 : 00 ~ 7F (First Byte, ascii)
  61. * Index 1,2,3: 80 ~ BF (Second, Third, Fourth Byte)
  62. * Index 4 : A0 ~ BF (Second Byte after E0)
  63. * Index 5 : 80 ~ 9F (Second Byte after ED)
  64. * Index 6 : 90 ~ BF (Second Byte after F0)
  65. * Index 7 : 80 ~ 8F (Second Byte after F4)
  66. * Index 8 : C2 ~ F4 (First Byte, non ascii)
  67. * Index 9~15 : illegal: i >= 127 && i <= -128
  68. */
  69. static const int8_t _range_min_tbl[] = {
  70. 0x00, 0x80, 0x80, 0x80, 0xA0, 0x80, 0x90, 0x80,
  71. 0xC2, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F,
  72. };
  73. static const int8_t _range_max_tbl[] = {
  74. 0x7F, 0xBF, 0xBF, 0xBF, 0xBF, 0x9F, 0xBF, 0x8F,
  75. 0xF4, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  76. };
  77. /*
  78. * Tables for fast handling of four special First Bytes(E0,ED,F0,F4), after
  79. * which the Second Byte are not 80~BF. It contains "range index adjustment".
  80. * +------------+---------------+------------------+----------------+
  81. * | First Byte | original range| range adjustment | adjusted range |
  82. * +------------+---------------+------------------+----------------+
  83. * | E0 | 2 | 2 | 4 |
  84. * +------------+---------------+------------------+----------------+
  85. * | ED | 2 | 3 | 5 |
  86. * +------------+---------------+------------------+----------------+
  87. * | F0 | 3 | 3 | 6 |
  88. * +------------+---------------+------------------+----------------+
  89. * | F4 | 4 | 4 | 8 |
  90. * +------------+---------------+------------------+----------------+
  91. */
  92. /* index1 -> E0, index14 -> ED */
  93. static const int8_t _df_ee_tbl[] = {
  94. 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0,
  95. };
  96. /* index1 -> F0, index5 -> F4 */
  97. static const int8_t _ef_fe_tbl[] = {
  98. 0, 3, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  99. };
  100. off_t
  101. rspamd_fast_utf8_validate_sse41 (const unsigned char *data, size_t len)
  102. __attribute__((__target__("sse4.1")));
  103. /* Return 0 - success, >0 - first error char(if RET_ERR_IDX = 1) */
  104. off_t
  105. rspamd_fast_utf8_validate_sse41 (const unsigned char *data, size_t len)
  106. {
  107. off_t err_pos = 1;
  108. if (len >= 16) {
  109. __m128i prev_input = _mm_set1_epi8 (0);
  110. __m128i prev_first_len = _mm_set1_epi8 (0);
  111. /* Cached tables */
  112. const __m128i first_len_tbl =
  113. _mm_lddqu_si128 ((const __m128i *) _first_len_tbl);
  114. const __m128i first_range_tbl =
  115. _mm_lddqu_si128 ((const __m128i *) _first_range_tbl);
  116. const __m128i range_min_tbl =
  117. _mm_lddqu_si128 ((const __m128i *) _range_min_tbl);
  118. const __m128i range_max_tbl =
  119. _mm_lddqu_si128 ((const __m128i *) _range_max_tbl);
  120. const __m128i df_ee_tbl =
  121. _mm_lddqu_si128 ((const __m128i *) _df_ee_tbl);
  122. const __m128i ef_fe_tbl =
  123. _mm_lddqu_si128 ((const __m128i *) _ef_fe_tbl);
  124. __m128i error = _mm_set1_epi8 (0);
  125. while (len >= 16) {
  126. const __m128i input = _mm_lddqu_si128 ((const __m128i *) data);
  127. /* high_nibbles = input >> 4 */
  128. const __m128i high_nibbles =
  129. _mm_and_si128 (_mm_srli_epi16 (input, 4), _mm_set1_epi8 (0x0F));
  130. /* first_len = legal character length minus 1 */
  131. /* 0 for 00~7F, 1 for C0~DF, 2 for E0~EF, 3 for F0~FF */
  132. /* first_len = first_len_tbl[high_nibbles] */
  133. __m128i first_len = _mm_shuffle_epi8 (first_len_tbl, high_nibbles);
  134. /* First Byte: set range index to 8 for bytes within 0xC0 ~ 0xFF */
  135. /* range = first_range_tbl[high_nibbles] */
  136. __m128i range = _mm_shuffle_epi8 (first_range_tbl, high_nibbles);
  137. /* Second Byte: set range index to first_len */
  138. /* 0 for 00~7F, 1 for C0~DF, 2 for E0~EF, 3 for F0~FF */
  139. /* range |= (first_len, prev_first_len) << 1 byte */
  140. range = _mm_or_si128 (
  141. range, _mm_alignr_epi8(first_len, prev_first_len, 15));
  142. /* Third Byte: set range index to saturate_sub(first_len, 1) */
  143. /* 0 for 00~7F, 0 for C0~DF, 1 for E0~EF, 2 for F0~FF */
  144. __m128i tmp1, tmp2;
  145. /* tmp1 = saturate_sub(first_len, 1) */
  146. tmp1 = _mm_subs_epu8 (first_len, _mm_set1_epi8 (1));
  147. /* tmp2 = saturate_sub(prev_first_len, 1) */
  148. tmp2 = _mm_subs_epu8 (prev_first_len, _mm_set1_epi8 (1));
  149. /* range |= (tmp1, tmp2) << 2 bytes */
  150. range = _mm_or_si128 (range, _mm_alignr_epi8(tmp1, tmp2, 14));
  151. /* Fourth Byte: set range index to saturate_sub(first_len, 2) */
  152. /* 0 for 00~7F, 0 for C0~DF, 0 for E0~EF, 1 for F0~FF */
  153. /* tmp1 = saturate_sub(first_len, 2) */
  154. tmp1 = _mm_subs_epu8 (first_len, _mm_set1_epi8 (2));
  155. /* tmp2 = saturate_sub(prev_first_len, 2) */
  156. tmp2 = _mm_subs_epu8 (prev_first_len, _mm_set1_epi8 (2));
  157. /* range |= (tmp1, tmp2) << 3 bytes */
  158. range = _mm_or_si128 (range, _mm_alignr_epi8(tmp1, tmp2, 13));
  159. /*
  160. * Now we have below range indices caluclated
  161. * Correct cases:
  162. * - 8 for C0~FF
  163. * - 3 for 1st byte after F0~FF
  164. * - 2 for 1st byte after E0~EF or 2nd byte after F0~FF
  165. * - 1 for 1st byte after C0~DF or 2nd byte after E0~EF or
  166. * 3rd byte after F0~FF
  167. * - 0 for others
  168. * Error cases:
  169. * 9,10,11 if non ascii First Byte overlaps
  170. * E.g., F1 80 C2 90 --> 8 3 10 2, where 10 indicates error
  171. */
  172. /* Adjust Second Byte range for special First Bytes(E0,ED,F0,F4) */
  173. /* Overlaps lead to index 9~15, which are illegal in range table */
  174. __m128i shift1, pos, range2;
  175. /* shift1 = (input, prev_input) << 1 byte */
  176. shift1 = _mm_alignr_epi8(input, prev_input, 15);
  177. pos = _mm_sub_epi8 (shift1, _mm_set1_epi8 (0xEF));
  178. /*
  179. * shift1: | EF F0 ... FE | FF 00 ... ... DE | DF E0 ... EE |
  180. * pos: | 0 1 15 | 16 17 239| 240 241 255|
  181. * pos-240: | 0 0 0 | 0 0 0 | 0 1 15 |
  182. * pos+112: | 112 113 127| >= 128 | >= 128 |
  183. */
  184. tmp1 = _mm_subs_epu8 (pos, _mm_set1_epi8 ((char)240));
  185. range2 = _mm_shuffle_epi8 (df_ee_tbl, tmp1);
  186. tmp2 = _mm_adds_epu8 (pos, _mm_set1_epi8 (112));
  187. range2 = _mm_add_epi8 (range2, _mm_shuffle_epi8 (ef_fe_tbl, tmp2));
  188. range = _mm_add_epi8 (range, range2);
  189. /* Load min and max values per calculated range index */
  190. __m128i minv = _mm_shuffle_epi8 (range_min_tbl, range);
  191. __m128i maxv = _mm_shuffle_epi8 (range_max_tbl, range);
  192. /* Check value range */
  193. error = _mm_cmplt_epi8(input, minv);
  194. error = _mm_or_si128(error, _mm_cmpgt_epi8(input, maxv));
  195. /* 5% performance drop from this conditional branch */
  196. if (!_mm_testz_si128(error, error)) {
  197. break;
  198. }
  199. prev_input = input;
  200. prev_first_len = first_len;
  201. data += 16;
  202. len -= 16;
  203. err_pos += 16;
  204. }
  205. /* Error in first 16 bytes */
  206. if (err_pos == 1) {
  207. goto do_naive;
  208. }
  209. /* Find previous token (not 80~BF) */
  210. int32_t token4 = _mm_extract_epi32 (prev_input, 3);
  211. const int8_t *token = (const int8_t *) &token4;
  212. int lookahead = 0;
  213. if (token[3] > (int8_t) 0xBF) {
  214. lookahead = 1;
  215. }
  216. else if (token[2] > (int8_t) 0xBF) {
  217. lookahead = 2;
  218. }
  219. else if (token[1] > (int8_t) 0xBF) {
  220. lookahead = 3;
  221. }
  222. data -= lookahead;
  223. len += lookahead;
  224. err_pos -= lookahead;
  225. }
  226. do_naive:
  227. if (len > 0) {
  228. off_t err_pos2 = rspamd_fast_utf8_validate_ref (data, len);
  229. if (err_pos2) {
  230. return err_pos + err_pos2 - 1;
  231. }
  232. }
  233. return 0;
  234. }
  235. #ifndef __clang__
  236. #pragma GCC pop_options
  237. #endif