1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
// file included directly
// File contains conversion procedure from possibly invalid UTF-8 strings.
/**
* Attempts to convert up to len 1-byte code units from in (in UTF-8 format) to
* out.
* Returns the position of the input and output after the processing is
* completed. Upon error, the output is set to null.
*/
template <endianness big_endian>
utf8_to_utf16_result
fast_avx512_convert_utf8_to_utf16(const char *in, size_t len, char16_t *out) {
const char *const final_in = in + len;
bool result = true;
while (result) {
if (final_in - in >= 64) {
result = process_block_utf8_to_utf16<SIMDUTF_FULL, big_endian>(
in, out, final_in - in);
} else if (in < final_in) {
result = process_block_utf8_to_utf16<SIMDUTF_TAIL, big_endian>(
in, out, final_in - in);
} else {
break;
}
}
if (!result) {
out = nullptr;
}
return std::make_pair(in, out);
}
template <endianness big_endian>
simdutf::result fast_avx512_convert_utf8_to_utf16_with_errors(const char *in,
size_t len,
char16_t *out) {
const char *const init_in = in;
const char16_t *const init_out = out;
const char *const final_in = in + len;
bool result = true;
while (result) {
if (final_in - in >= 64) {
result = process_block_utf8_to_utf16<SIMDUTF_FULL, big_endian>(
in, out, final_in - in);
} else if (in < final_in) {
result = process_block_utf8_to_utf16<SIMDUTF_TAIL, big_endian>(
in, out, final_in - in);
} else {
break;
}
}
if (!result) {
size_t pos = size_t(in - init_in);
if (pos < len && (init_in[pos] & 0xc0) == 0x80 && pos >= 64) {
// We must check whether we are the fourth continuation byte
bool c1 = (init_in[pos - 1] & 0xc0) == 0x80;
bool c2 = (init_in[pos - 2] & 0xc0) == 0x80;
bool c3 = (init_in[pos - 3] & 0xc0) == 0x80;
if (c1 && c2 && c3) {
return {simdutf::TOO_LONG, pos};
}
}
// rewind_and_convert_with_errors will seek a potential error from in
// onward, with the ability to go back up to in - init_in bytes, and read
// final_in - in bytes forward.
simdutf::result res =
scalar::utf8_to_utf16::rewind_and_convert_with_errors<big_endian>(
in - init_in, in, final_in - in, out);
res.count += (in - init_in);
return res;
} else {
return simdutf::result(error_code::SUCCESS, out - init_out);
}
}
template <endianness big_endian, typename OUTPUT>
// todo: replace with the utf-8 to utf-16 routine adapted to utf-32. This code
// is legacy.
std::pair<const char *, OUTPUT *>
validating_utf8_to_fixed_length(const char *str, size_t len, OUTPUT *dwords) {
constexpr bool UTF32 = std::is_same<OUTPUT, uint32_t>::value;
constexpr bool UTF16 = std::is_same<OUTPUT, char16_t>::value;
static_assert(
UTF32 or UTF16,
"output type has to be uint32_t (for UTF-32) or char16_t (for UTF-16)");
static_assert(!(UTF32 and big_endian),
"we do not currently support big-endian UTF-32");
const char *ptr = str;
const char *end = ptr + len;
__m512i byteflip = _mm512_setr_epi64(0x0607040502030001, 0x0e0f0c0d0a0b0809,
0x0607040502030001, 0x0e0f0c0d0a0b0809,
0x0607040502030001, 0x0e0f0c0d0a0b0809,
0x0607040502030001, 0x0e0f0c0d0a0b0809);
OUTPUT *output = dwords;
avx512_utf8_checker checker{};
/**
* In the main loop, we consume 64 bytes per iteration,
* but we access 64 + 4 bytes.
* We use masked writes to avoid overruns, see
* https://github.com/simdutf/simdutf/issues/471
*/
while (end - ptr >= 64 + 4) {
const __m512i utf8 = _mm512_loadu_si512((const __m512i *)ptr);
if (checker.check_next_input(utf8)) {
SIMDUTF_ICELAKE_STORE_ASCII(UTF32, utf8, output)
output += 64;
ptr += 64;
continue;
}
const __m512i lane0 = broadcast_epi128<0>(utf8);
const __m512i lane1 = broadcast_epi128<1>(utf8);
int valid_count0;
__m512i vec0 = expand_and_identify(lane0, lane1, valid_count0);
const __m512i lane2 = broadcast_epi128<2>(utf8);
int valid_count1;
__m512i vec1 = expand_and_identify(lane1, lane2, valid_count1);
if (valid_count0 + valid_count1 <= 16) {
vec0 = _mm512_mask_expand_epi32(
vec0, __mmask16(((1 << valid_count1) - 1) << valid_count0), vec1);
valid_count0 += valid_count1;
vec0 = expand_utf8_to_utf32(vec0);
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec0, valid_count0, true)
} else {
vec0 = expand_utf8_to_utf32(vec0);
vec1 = expand_utf8_to_utf32(vec1);
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec0, valid_count0, true)
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec1, valid_count1, true)
}
const __m512i lane3 = broadcast_epi128<3>(utf8);
int valid_count2;
__m512i vec2 = expand_and_identify(lane2, lane3, valid_count2);
uint32_t tmp1;
::memcpy(&tmp1, ptr + 64, sizeof(tmp1));
const __m512i lane4 = _mm512_set1_epi32(tmp1);
int valid_count3;
__m512i vec3 = expand_and_identify(lane3, lane4, valid_count3);
if (valid_count2 + valid_count3 <= 16) {
vec2 = _mm512_mask_expand_epi32(
vec2, __mmask16(((1 << valid_count3) - 1) << valid_count2), vec3);
valid_count2 += valid_count3;
vec2 = expand_utf8_to_utf32(vec2);
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec2, valid_count2, true)
} else {
vec2 = expand_utf8_to_utf32(vec2);
vec3 = expand_utf8_to_utf32(vec3);
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec2, valid_count2, true)
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec3, valid_count3, true)
}
ptr += 4 * 16;
}
const char *validatedptr = ptr; // validated up to ptr
// For the final pass, we validate 64 bytes, but we only transcode
// 3*16 bytes, so we may end up double-validating 16 bytes.
if (end - ptr >= 64) {
const __m512i utf8 = _mm512_loadu_si512((const __m512i *)ptr);
if (checker.check_next_input(utf8)) {
SIMDUTF_ICELAKE_STORE_ASCII(UTF32, utf8, output)
output += 64;
ptr += 64;
} else {
const __m512i lane0 = broadcast_epi128<0>(utf8);
const __m512i lane1 = broadcast_epi128<1>(utf8);
int valid_count0;
__m512i vec0 = expand_and_identify(lane0, lane1, valid_count0);
const __m512i lane2 = broadcast_epi128<2>(utf8);
int valid_count1;
__m512i vec1 = expand_and_identify(lane1, lane2, valid_count1);
if (valid_count0 + valid_count1 <= 16) {
vec0 = _mm512_mask_expand_epi32(
vec0, __mmask16(((1 << valid_count1) - 1) << valid_count0), vec1);
valid_count0 += valid_count1;
vec0 = expand_utf8_to_utf32(vec0);
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec0, valid_count0, true)
} else {
vec0 = expand_utf8_to_utf32(vec0);
vec1 = expand_utf8_to_utf32(vec1);
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec0, valid_count0, true)
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec1, valid_count1, true)
}
const __m512i lane3 = broadcast_epi128<3>(utf8);
SIMDUTF_ICELAKE_TRANSCODE16(lane2, lane3, true)
ptr += 3 * 16;
}
validatedptr += 4 * 16;
}
if (end != validatedptr) {
const __m512i utf8 =
_mm512_maskz_loadu_epi8(~UINT64_C(0) >> (64 - (end - validatedptr)),
(const __m512i *)validatedptr);
checker.check_next_input(utf8);
}
checker.check_eof();
if (checker.errors()) {
return {ptr, nullptr}; // We found an error.
}
return {ptr, output};
}
// Like validating_utf8_to_fixed_length but returns as soon as an error is
// identified todo: replace with the utf-8 to utf-16 routine adapted to utf-32.
// This code is legacy.
template <endianness big_endian, typename OUTPUT>
std::tuple<const char *, OUTPUT *, bool>
validating_utf8_to_fixed_length_with_constant_checks(const char *str,
size_t len,
OUTPUT *dwords) {
constexpr bool UTF32 = std::is_same<OUTPUT, uint32_t>::value;
constexpr bool UTF16 = std::is_same<OUTPUT, char16_t>::value;
static_assert(
UTF32 or UTF16,
"output type has to be uint32_t (for UTF-32) or char16_t (for UTF-16)");
static_assert(!(UTF32 and big_endian),
"we do not currently support big-endian UTF-32");
const char *ptr = str;
const char *end = ptr + len;
__m512i byteflip = _mm512_setr_epi64(0x0607040502030001, 0x0e0f0c0d0a0b0809,
0x0607040502030001, 0x0e0f0c0d0a0b0809,
0x0607040502030001, 0x0e0f0c0d0a0b0809,
0x0607040502030001, 0x0e0f0c0d0a0b0809);
OUTPUT *output = dwords;
avx512_utf8_checker checker{};
/**
* In the main loop, we consume 64 bytes per iteration,
* but we access 64 + 4 bytes.
*/
while (end - ptr >= 4 + 64) {
const __m512i utf8 = _mm512_loadu_si512((const __m512i *)ptr);
bool ascii = checker.check_next_input(utf8);
if (checker.errors()) {
return {ptr, output, false}; // We found an error.
}
if (ascii) {
SIMDUTF_ICELAKE_STORE_ASCII(UTF32, utf8, output)
output += 64;
ptr += 64;
continue;
}
const __m512i lane0 = broadcast_epi128<0>(utf8);
const __m512i lane1 = broadcast_epi128<1>(utf8);
int valid_count0;
__m512i vec0 = expand_and_identify(lane0, lane1, valid_count0);
const __m512i lane2 = broadcast_epi128<2>(utf8);
int valid_count1;
__m512i vec1 = expand_and_identify(lane1, lane2, valid_count1);
if (valid_count0 + valid_count1 <= 16) {
vec0 = _mm512_mask_expand_epi32(
vec0, __mmask16(((1 << valid_count1) - 1) << valid_count0), vec1);
valid_count0 += valid_count1;
vec0 = expand_utf8_to_utf32(vec0);
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec0, valid_count0, true)
} else {
vec0 = expand_utf8_to_utf32(vec0);
vec1 = expand_utf8_to_utf32(vec1);
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec0, valid_count0, true)
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec1, valid_count1, true)
}
const __m512i lane3 = broadcast_epi128<3>(utf8);
int valid_count2;
__m512i vec2 = expand_and_identify(lane2, lane3, valid_count2);
uint32_t tmp1;
::memcpy(&tmp1, ptr + 64, sizeof(tmp1));
const __m512i lane4 = _mm512_set1_epi32(tmp1);
int valid_count3;
__m512i vec3 = expand_and_identify(lane3, lane4, valid_count3);
if (valid_count2 + valid_count3 <= 16) {
vec2 = _mm512_mask_expand_epi32(
vec2, __mmask16(((1 << valid_count3) - 1) << valid_count2), vec3);
valid_count2 += valid_count3;
vec2 = expand_utf8_to_utf32(vec2);
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec2, valid_count2, true)
} else {
vec2 = expand_utf8_to_utf32(vec2);
vec3 = expand_utf8_to_utf32(vec3);
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec2, valid_count2, true)
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec3, valid_count3, true)
}
ptr += 4 * 16;
}
const char *validatedptr = ptr; // validated up to ptr
// For the final pass, we validate 64 bytes, but we only transcode
// 3*16 bytes, so we may end up double-validating 16 bytes.
if (end - ptr >= 64) {
const __m512i utf8 = _mm512_loadu_si512((const __m512i *)ptr);
bool ascii = checker.check_next_input(utf8);
if (checker.errors()) {
return {ptr, output, false}; // We found an error.
}
if (ascii) {
SIMDUTF_ICELAKE_STORE_ASCII(UTF32, utf8, output)
output += 64;
ptr += 64;
} else {
const __m512i lane0 = broadcast_epi128<0>(utf8);
const __m512i lane1 = broadcast_epi128<1>(utf8);
int valid_count0;
__m512i vec0 = expand_and_identify(lane0, lane1, valid_count0);
const __m512i lane2 = broadcast_epi128<2>(utf8);
int valid_count1;
__m512i vec1 = expand_and_identify(lane1, lane2, valid_count1);
if (valid_count0 + valid_count1 <= 16) {
vec0 = _mm512_mask_expand_epi32(
vec0, __mmask16(((1 << valid_count1) - 1) << valid_count0), vec1);
valid_count0 += valid_count1;
vec0 = expand_utf8_to_utf32(vec0);
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec0, valid_count0, true)
} else {
vec0 = expand_utf8_to_utf32(vec0);
vec1 = expand_utf8_to_utf32(vec1);
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec0, valid_count0, true)
SIMDUTF_ICELAKE_WRITE_UTF16_OR_UTF32(vec1, valid_count1, true)
}
const __m512i lane3 = broadcast_epi128<3>(utf8);
SIMDUTF_ICELAKE_TRANSCODE16(lane2, lane3, true)
ptr += 3 * 16;
}
validatedptr += 4 * 16;
}
if (end != validatedptr) {
const __m512i utf8 =
_mm512_maskz_loadu_epi8(~UINT64_C(0) >> (64 - (end - validatedptr)),
(const __m512i *)validatedptr);
checker.check_next_input(utf8);
}
checker.check_eof();
if (checker.errors()) {
return {ptr, output, false}; // We found an error.
}
return {ptr, output, true};
}
|