Debian Codegraph @

rpython/rlib/fastutf8/src/utf8-sse4.c

raw symbols SCIP
1 #include "utf8.h"
2
3 #include <stddef.h>
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <xmmintrin.h>
7 #include <smmintrin.h>
8
9 #define BIT(B,P) ((B >> (P-1)) & 0x1)
10
11 void _print_mmx(const char * msg, __m128i chunk)
12 {
13 printf("%s:", msg);
14 // unpack the first 8 bytes, padding with zeros
15 uint64_t a = _mm_extract_epi64(chunk, 0);
16 uint64_t b = _mm_extract_epi64(chunk, 1);
17 printf("%.2x%.2x%.2x%.2x %.2x%.2x%.2x%.2x %.2x%.2x%.2x%.2x %.2x%.2x%.2x%.2x",
18 (unsigned char)((a >> 0) & 0xff),
19 (unsigned char)((a >> 8) & 0xff),
20 (unsigned char)((a >> 16) & 0xff),
21 (unsigned char)((a >> 24) & 0xff),
22
23 (unsigned char)((a >> 32) & 0xff),
24 (unsigned char)((a >> 40) & 0xff),
25 (unsigned char)((a >> 48) & 0xff),
26 (unsigned char)((a >> 56) & 0xff),
27
28 (unsigned char)((b >> 0) & 0xff),
29 (unsigned char)((b >> 8) & 0xff),
30 (unsigned char)((b >> 16) & 0xff),
31 (unsigned char)((b >> 24) & 0xff),
32
33 (unsigned char)((b >> 32) & 0xff),
34 (unsigned char)((b >> 40) & 0xff),
35 (unsigned char)((b >> 48) & 0xff),
36 (unsigned char)((b >> 56) & 0xff)
37 );
38
39 printf("\n");
40 }
41
42
43 ssize_t fu8_count_utf8_codepoints_sse4(const char * utf8, size_t len)
44 {
45 const uint8_t * encoded = (const uint8_t*)utf8;
46 __builtin_prefetch(encoded, 0, 0);
47 size_t num_codepoints = 0;
48 __m128i chunk;
49
50 if (len == 0) {
51 return 0;
52 }
53 __m128i zero = _mm_set1_epi8(0x00);
54
55 while (len >= 16) {
56 chunk = _mm_loadu_si128((__m128i*)encoded);
57 if (_mm_movemask_epi8(chunk) == 0) {
58 // valid ascii chars!
59 len -= 16;
60 encoded += 16;
61 num_codepoints += 16;
62 continue;
63 }
64 __builtin_prefetch(encoded+16, 0, 0);
65
66 __m128i count = _mm_set1_epi8(0x1);
67 //_print_mmx("chunk", chunk);
68 // fight against the fact that there is no comparison on unsigned values
69 __m128i chunk_signed = _mm_add_epi8(chunk, _mm_set1_epi8(0x80));
70 //_print_mmx("shunk", chunk_signed);
71
72 // ERROR checking
73 // checking procedure works the following way:
74 //
75 // 1) mark all continuation bytes with either 0x1, 0x3, 0x7 (one, two or three bytes continuation)
76 // 2) then check that there is no byte that has an invalid continuation
77 __m128i twobytemarker = _mm_cmplt_epi8(_mm_set1_epi8(0xc0-1-0x80), chunk_signed);
78 __m128i threebytemarker = _mm_cmplt_epi8(_mm_set1_epi8(0xe0-1-0x80), chunk_signed);
79 __m128i fourbytemarker = _mm_cmplt_epi8(_mm_set1_epi8(0xf0-1-0x80), chunk_signed);
80
81 // the general idea of the following code collects 0xff for each byte position
82 // in the variable contbytes.
83 // at the end check if each position in contbytes set to 0xff is a valid continuation byte
84
85 // check that 0xc0 > 0xc2
86 __m128i validtwobm = _mm_cmplt_epi8(_mm_set1_epi8(0xc2-1-0x80), chunk_signed);
87 if (_mm_movemask_epi8(_mm_xor_si128(validtwobm, twobytemarker)) != 0) {
88 // two byte marker should not be in range [0xc0-0xc2)
89 return -1;
90 }
91
92 __m128i state2 = _mm_andnot_si128(threebytemarker, twobytemarker);
93 __m128i contbytes = _mm_slli_si128(_mm_blendv_epi8(state2, _mm_set1_epi8(0x1), twobytemarker), 1);
94
95 if (_mm_movemask_epi8(threebytemarker) != 0) {
96 // contains at least one 3 byte marker
97 __m128i istate3 = _mm_andnot_si128(fourbytemarker, threebytemarker);
98 __m128i state3 = _mm_slli_si128(_mm_blendv_epi8(zero, _mm_set1_epi8(0x3), istate3), 1);
99 state3 = _mm_or_si128(state3, _mm_slli_si128(state3, 1));
100
101 contbytes = _mm_or_si128(contbytes, state3);
102
103 // range check
104 __m128i equal_e0 = _mm_cmpeq_epi8(_mm_blendv_epi8(zero, chunk_signed, istate3),
105 _mm_set1_epi8(0xe0-0x80));
106 if (_mm_movemask_epi8(equal_e0) != 0) {
107 __m128i mask = _mm_blendv_epi8(_mm_set1_epi8(0x7f), chunk_signed, _mm_slli_si128(equal_e0, 1));
108 __m128i check_surrogate = _mm_cmplt_epi8(mask, _mm_set1_epi8(0xa0-0x80));
109 if (_mm_movemask_epi8(check_surrogate) != 0) {
110 // invalid surrograte character!!!
111 return -1;
112 }
113 }
114
115 // verify that there are now surrogates
116 if (!ALLOW_SURROGATES) {
117 __m128i equal_ed = _mm_cmpeq_epi8(_mm_blendv_epi8(zero, chunk_signed, istate3),
118 _mm_set1_epi8(0xed-0x80));
119 if (_mm_movemask_epi8(equal_ed) != 0) {
120 __m128i mask = _mm_blendv_epi8(_mm_set1_epi8(0x80), chunk_signed, _mm_slli_si128(equal_ed, 1));
121 __m128i check_surrogate = _mm_cmpgt_epi8(mask, _mm_set1_epi8(0xa0-1-0x80));
122 if (_mm_movemask_epi8(check_surrogate) != 0) {
123 // invalid surrograte character!!!
124 return -1;
125 }
126 }
127 }
128 }
129
130 if (_mm_movemask_epi8(fourbytemarker) != 0) {
131 // contain a 4 byte marker
132 __m128i istate4 = _mm_slli_si128(_mm_blendv_epi8(zero, _mm_set1_epi8(0x7), fourbytemarker), 1);
133 __m128i state4 =_mm_or_si128(istate4, _mm_slli_si128(istate4, 1));
134 state4 =_mm_or_si128(state4, _mm_slli_si128(istate4, 2));
135
136 contbytes = _mm_or_si128(contbytes, state4);
137
138 // range check, filter out f0 and
139 __m128i equal_f0 = _mm_cmpeq_epi8(_mm_blendv_epi8(zero, chunk_signed, fourbytemarker),
140 _mm_set1_epi8(0xf0-0x80));
141 if (_mm_movemask_epi8(equal_f0) != 0) {
142 __m128i mask = _mm_blendv_epi8(_mm_set1_epi8(0x7f), chunk_signed, _mm_slli_si128(equal_f0, 1));
143 __m128i check_surrogate = _mm_cmplt_epi8(mask, _mm_set1_epi8(0x90-0x80));
144 if (_mm_movemask_epi8(check_surrogate) != 0) {
145 return -1;
146 }
147 }
148
149 __m128i equal_f4 = _mm_cmpeq_epi8(_mm_blendv_epi8(zero, chunk_signed, fourbytemarker),
150 _mm_set1_epi8(0xf4-0x80));
151 if (_mm_movemask_epi8(equal_f4) != 0) {
152 __m128i mask = _mm_blendv_epi8(_mm_set1_epi8(0x80), chunk_signed, _mm_slli_si128(equal_f4, 1));
153 __m128i check_surrogate = _mm_cmpgt_epi8(mask, _mm_set1_epi8(0x90-1-0x80));
154 if (_mm_movemask_epi8(check_surrogate) != 0) {
155 return -1;
156 }
157 }
158
159 __m128i equal_f5_gt = _mm_cmpgt_epi8(_mm_blendv_epi8(zero, chunk_signed, fourbytemarker),
160 _mm_set1_epi8(0xf4-0x80));
161 if (_mm_movemask_epi8(equal_f5_gt) != 0) {
162 return -1;
163 }
164 }
165
166 // now check that contbytes and the actual byte values have a valid
167 // continuation at each position the marker indicates to have one
168 __m128i check_cont = _mm_cmpgt_epi8(contbytes, zero);
169 __m128i contpos = _mm_and_si128(_mm_set1_epi8(0xc0), chunk);
170 contpos = _mm_cmpeq_epi8(_mm_set1_epi8(0x80), contpos);
171 __m128i validcont = _mm_xor_si128(check_cont, contpos);
172 if (_mm_movemask_epi8(validcont) != 0) {
173 // uff, nope, that is really not utf8
174 return -1;
175 }
176
177 // CORRECT, calculate the length
178 // copy 0x00 over to each place which is a continuation byte
179 count = _mm_blendv_epi8(count, zero, contpos);
180
181 // count the code points using 2x 32 bit hadd and one last 16 hadd
182 // the result will end up at the lowest position
183 count = _mm_hadd_epi32(count, count);
184 count = _mm_hadd_epi32(count, count);
185 count = _mm_hadd_epi16(count, count);
186 uint16_t c = _mm_extract_epi16(count, 0);
187
188 // these cases need to be handled:
189 // 16 byte boundary -> | <- 16 byte boundary
190 // -----------------------------------------+--------------------
191 // 1) 2 byte code point. e.g. ... c2 | 80 ...
192 // 2) 3 byte code point. e.g. ... e6 | 80 80 ...
193 // 3) 3 byte code point. e.g. ... e6 80 | 80 ...
194 // 4) 4 byte code point. e.g. ... f2 | 80 80 80 ...
195 // 5) 4 byte code point. e.g. ... f2 80 | 80 80 ...
196 // 6) 4 byte code point. e.g. ... f2 80 80 | 80 ...
197 //
198 int mask_chunk = _mm_movemask_epi8(chunk);
199 int mask_conti = _mm_movemask_epi8(contpos);
200
201 // little endian
202 int lenoff = 16;
203 int minus_codepoints = 0;
204 if (BIT(mask_chunk, 16) != 0 && BIT(mask_conti, 16) == 0) { // 1), 2), 4)
205 minus_codepoints = 1;
206 lenoff -= 1;
207 } else if (BIT(mask_chunk, 15) != 0 && BIT(mask_conti, 15) == 0 &&
208 BIT(mask_conti, 16) == 1) { // 3), 5)
209 minus_codepoints = 1;
210 lenoff -= 2;
211 } else if (BIT(mask_chunk, 14) != 0 && BIT(mask_conti, 14) == 0 &&
212 BIT(mask_conti, 15) == 1 && BIT(mask_conti, 16) == 1) { // 6)
213 minus_codepoints = 1;
214 lenoff -= 3;
215 }
216
217 num_codepoints += (c & 0xff) + ((c >> 8) & 0xff) - minus_codepoints;
218 len -= lenoff;
219 encoded += lenoff;
220 }
221
222 if (len == 0) {
223 return num_codepoints;
224 }
225
226 ssize_t result = fu8_count_utf8_codepoints_seq(encoded, len);
227 if (result == -1) {
228 return -1;
229 }
230
231 return num_codepoints + result;
232 }
233
234 ssize_t fu8_idx2bytepos_sse4(size_t index,
235 const uint8_t * utf8, size_t len,
236 struct fu8_idxtab * t)
237 {
238 return 0;
239 }