Debian Codegraph @

contrib/simdutf/src/lsx/lsx_convert_utf16_to_utf8.cpp

raw
1 /*
2 The vectorized algorithm works on single SSE register i.e., it
3 loads eight 16-bit code units.
4
5 We consider three cases:
6 1. an input register contains no surrogates and each value
7 is in range 0x0000 .. 0x07ff.
8 2. an input register contains no surrogates and values are
9 is in range 0x0000 .. 0xffff.
10 3. an input register contains surrogates --- i.e. codepoints
11 can have 16 or 32 bits.
12
13 Ad 1.
14
15 When values are less than 0x0800, it means that a 16-bit code unit
16 can be converted into: 1) single UTF8 byte (when it's an ASCII
17 char) or 2) two UTF8 bytes.
18
19 For this case we do only some shuffle to obtain these 2-byte
20 codes and finally compress the whole SSE register with a single
21 shuffle.
22
23 We need 256-entry lookup table to get a compression pattern
24 and the number of output bytes in the compressed vector register.
25 Each entry occupies 17 bytes.
26
27 Ad 2.
28
29 When values fit in 16-bit code units, but are above 0x07ff, then
30 a single word may produce one, two or three UTF8 bytes.
31
32 We prepare data for all these three cases in two registers.
33 The first register contains lower two UTF8 bytes (used in all
34 cases), while the second one contains just the third byte for
35 the three-UTF8-bytes case.
36
37 Finally these two registers are interleaved forming eight-element
38 array of 32-bit values. The array spans two SSE registers.
39 The bytes from the registers are compressed using two shuffles.
40
41 We need 256-entry lookup table to get a compression pattern
42 and the number of output bytes in the compressed vector register.
43 Each entry occupies 17 bytes.
44
45
46 To summarize:
47 - We need two 256-entry tables that have 8704 bytes in total.
48 */
49 /*
50 Returns a pair: the first unprocessed byte from buf and utf8_output
51 A scalar routing should carry on the conversion of the tail.
52 */
53 template <endianness big_endian>
54 std::pair<const char16_t *, char *>
55 lsx_convert_utf16_to_utf8(const char16_t *buf, size_t len, char *utf8_out) {
56 uint8_t *utf8_output = reinterpret_cast<uint8_t *>(utf8_out);
57 const char16_t *end = buf + len;
58
59 const size_t safety_margin =
60 12; // to avoid overruns, see issue
61 // https://github.com/simdutf/simdutf/issues/92
62
63 __m128i v_07ff = __lsx_vreplgr2vr_h(uint16_t(0x7ff));
64 while (buf + 16 + safety_margin <= end) {
65 __m128i in = __lsx_vld(reinterpret_cast<const uint16_t *>(buf), 0);
66 if (!match_system(big_endian)) {
67 in = lsx_swap_bytes(in);
68 }
69 if (__lsx_bz_v(
70 __lsx_vslt_hu(__lsx_vrepli_h(0x7F), in))) { // ASCII fast path!!!!
71 // It is common enough that we have sequences of 16 consecutive ASCII
72 // characters.
73 __m128i nextin = __lsx_vld(reinterpret_cast<const uint16_t *>(buf), 16);
74 if (!match_system(big_endian)) {
75 nextin = lsx_swap_bytes(nextin);
76 }
77 if (__lsx_bz_v(__lsx_vslt_hu(__lsx_vrepli_h(0x7F), nextin))) {
78 // 1. pack the bytes
79 // obviously suboptimal.
80 __m128i utf8_packed = __lsx_vpickev_b(nextin, in);
81 // 2. store (16 bytes)
82 __lsx_vst(utf8_packed, utf8_output, 0);
83 // 3. adjust pointers
84 buf += 16;
85 utf8_output += 16;
86 continue; // we are done for this round!
87 } else {
88 // 1. pack the bytes
89 // obviously suboptimal.
90 __m128i utf8_packed = __lsx_vpickev_b(in, in);
91 // 2. store (8 bytes)
92 __lsx_vst(utf8_packed, utf8_output, 0);
93 // 3. adjust pointers
94 buf += 8;
95 utf8_output += 8;
96 in = nextin;
97 }
98 }
99
100 __m128i zero = __lsx_vldi(0);
101 if (__lsx_bz_v(__lsx_vslt_hu(v_07ff, in))) {
102 // 1. prepare 2-byte values
103 // input 16-bit word : [0000|0aaa|aabb|bbbb] x 8
104 // expected output : [110a|aaaa|10bb|bbbb] x 8
105 // t0 = [000a|aaaa|bbbb|bb00]
106 __m128i t0 = __lsx_vslli_h(in, 2);
107 // t1 = [000a|aaaa|0000|0000]
108 __m128i t1 = __lsx_vand_v(t0, __lsx_vldi(-2785 /*0x1f00*/));
109 // t2 = [0000|0000|00bb|bbbb]
110 __m128i t2 = __lsx_vand_v(in, __lsx_vrepli_h(0x3f));
111 // t3 = [000a|aaaa|00bb|bbbb]
112 __m128i t3 = __lsx_vor_v(t1, t2);
113 // t4 = [110a|aaaa|10bb|bbbb]
114 __m128i v_c080 = __lsx_vreplgr2vr_h(uint16_t(0xc080));
115 __m128i t4 = __lsx_vor_v(t3, v_c080);
116 // 2. merge ASCII and 2-byte codewords
117 __m128i one_byte_bytemask =
118 __lsx_vsle_hu(in, __lsx_vrepli_h(0x7F /*0x007F*/));
119 __m128i utf8_unpacked = __lsx_vbitsel_v(t4, in, one_byte_bytemask);
120 // 3. prepare bitmask for 8-bit lookup
121 uint32_t m2 = __lsx_vpickve2gr_bu(__lsx_vmskltz_h(one_byte_bytemask), 0);
122 // 4. pack the bytes
123 const uint8_t *row = &simdutf::tables::utf16_to_utf8::pack_1_2_utf8_bytes
124 [lsx_1_2_utf8_bytes_mask[m2]][0];
125 __m128i shuffle = __lsx_vld(row, 1);
126 __m128i utf8_packed = __lsx_vshuf_b(zero, utf8_unpacked, shuffle);
127 // 5. store bytes
128 __lsx_vst(utf8_packed, utf8_output, 0);
129 // 6. adjust pointers
130 buf += 8;
131 utf8_output += row[0];
132 continue;
133 }
134 __m128i surrogates_bytemask =
135 __lsx_vseq_h(__lsx_vand_v(in, __lsx_vldi(-2568 /*0xF800*/)),
136 __lsx_vldi(-2600 /*0xD800*/));
137 // It might seem like checking for surrogates_bitmask == 0xc000 could help.
138 // However, it is likely an uncommon occurrence.
139 if (__lsx_bz_v(surrogates_bytemask)) {
140 // case: code units from register produce either 1, 2 or 3 UTF-8 bytes
141 /* In this branch we handle three cases:
142 1. [0000|0000|0ccc|cccc] => [0ccc|cccc] -
143 single UFT-8 byte
144 2. [0000|0bbb|bbcc|cccc] => [110b|bbbb], [10cc|cccc] -
145 two UTF-8 bytes
146 3. [aaaa|bbbb|bbcc|cccc] => [1110|aaaa], [10bb|bbbb], [10cc|cccc] -
147 three UTF-8 bytes
148
149 We expand the input word (16-bit) into two code units (32-bit), thus
150 we have room for four bytes. However, we need five distinct bit
151 layouts. Note that the last byte in cases #2 and #3 is the same.
152
153 We precompute byte 1 for case #1 and the common byte for cases #2 & #3
154 in register t2.
155
156 We precompute byte 1 for case #3 and -- **conditionally** --
157 precompute either byte 1 for case #2 or byte 2 for case #3. Note that
158 they differ by exactly one bit.
159
160 Finally from these two code units we build proper UTF-8 sequence,
161 taking into account the case (i.e, the number of bytes to write).
162 */
163 /**
164 * Given [aaaa|bbbb|bbcc|cccc] our goal is to produce:
165 * t2 => [0ccc|cccc] [10cc|cccc]
166 * s4 => [1110|aaaa] ([110b|bbbb] OR [10bb|bbbb])
167 */
168 // [aaaa|bbbb|bbcc|cccc] => [bbcc|cccc|bbcc|cccc]
169 __m128i t0 = __lsx_vpickev_b(in, in);
170 t0 = __lsx_vilvl_b(t0, t0);
171
172 // [bbcc|cccc|bbcc|cccc] => [00cc|cccc|00cc|cccc]
173 __m128i v_3f7f = __lsx_vreplgr2vr_h(uint16_t(0x3F7F));
174 __m128i t1 = __lsx_vand_v(t0, v_3f7f);
175 // [00cc|cccc|0bcc|cccc] => [10cc|cccc|0bcc|cccc]
176 __m128i t2 = __lsx_vor_v(t1, __lsx_vldi(-2688 /*0x8000*/));
177
178 // s0: [aaaa|bbbb|bbcc|cccc] => [0000|0000|0000|aaaa]
179 __m128i s0 = __lsx_vsrli_h(in, 12);
180 // s1: [aaaa|bbbb|bbcc|cccc] => [0000|bbbb|bb00|0000]
181 __m128i s1 = __lsx_vslli_h(in, 2);
182 // s1: [aabb|bbbb|cccc|cc00] => [00bb|bbbb|0000|0000]
183 s1 = __lsx_vand_v(s1, __lsx_vldi(-2753 /*0x3F00*/));
184
185 // [00bb|bbbb|0000|aaaa]
186 __m128i s2 = __lsx_vor_v(s0, s1);
187 // s3: [00bb|bbbb|0000|aaaa] => [11bb|bbbb|1110|aaaa]
188 __m128i v_c0e0 = __lsx_vreplgr2vr_h(uint16_t(0xC0E0));
189 __m128i s3 = __lsx_vor_v(s2, v_c0e0);
190 __m128i one_or_two_bytes_bytemask = __lsx_vsle_hu(in, v_07ff);
191 __m128i m0 = __lsx_vandn_v(one_or_two_bytes_bytemask,
192 __lsx_vldi(-2752 /*0x4000*/));
193 __m128i s4 = __lsx_vxor_v(s3, m0);
194
195 // 4. expand code units 16-bit => 32-bit
196 __m128i out0 = __lsx_vilvl_h(s4, t2);
197 __m128i out1 = __lsx_vilvh_h(s4, t2);
198
199 // 5. compress 32-bit code units into 1, 2 or 3 bytes -- 2 x shuffle
200 __m128i one_byte_bytemask = __lsx_vsle_hu(in, __lsx_vrepli_h(0x7F));
201
202 __m128i one_or_two_bytes_bytemask_low =
203 __lsx_vilvl_h(one_or_two_bytes_bytemask, zero);
204 __m128i one_or_two_bytes_bytemask_high =
205 __lsx_vilvh_h(one_or_two_bytes_bytemask, zero);
206
207 __m128i one_byte_bytemask_low =
208 __lsx_vilvl_h(one_byte_bytemask, one_byte_bytemask);
209 __m128i one_byte_bytemask_high =
210 __lsx_vilvh_h(one_byte_bytemask, one_byte_bytemask);
211
212 const uint32_t mask0 = __lsx_vpickve2gr_bu(
213 __lsx_vmskltz_h(__lsx_vor_v(one_or_two_bytes_bytemask_low,
214 one_byte_bytemask_low)),
215 0);
216 const uint32_t mask1 = __lsx_vpickve2gr_bu(
217 __lsx_vmskltz_h(__lsx_vor_v(one_or_two_bytes_bytemask_high,
218 one_byte_bytemask_high)),
219 0);
220
221 const uint8_t *row0 =
222 &simdutf::tables::utf16_to_utf8::pack_1_2_3_utf8_bytes[mask0][0];
223 __m128i shuffle0 = __lsx_vld(row0, 1);
224 __m128i utf8_0 = __lsx_vshuf_b(zero, out0, shuffle0);
225
226 const uint8_t *row1 =
227 &simdutf::tables::utf16_to_utf8::pack_1_2_3_utf8_bytes[mask1][0];
228 __m128i shuffle1 = __lsx_vld(row1, 1);
229 __m128i utf8_1 = __lsx_vshuf_b(zero, out1, shuffle1);
230
231 __lsx_vst(utf8_0, utf8_output, 0);
232 utf8_output += row0[0];
233 __lsx_vst(utf8_1, utf8_output, 0);
234 utf8_output += row1[0];
235
236 buf += 8;
237 // surrogate pair(s) in a register
238 } else {
239 // Let us do a scalar fallback.
240 // It may seem wasteful to use scalar code, but being efficient with SIMD
241 // in the presence of surrogate pairs may require non-trivial tables.
242 size_t forward = 15;
243 size_t k = 0;
244 if (size_t(end - buf) < forward + 1) {
245 forward = size_t(end - buf - 1);
246 }
247 for (; k < forward; k++) {
248 uint16_t word = !match_system(big_endian)
249 ? scalar::utf16::swap_bytes(buf[k])
250 : buf[k];
251 if ((word & 0xFF80) == 0) {
252 *utf8_output++ = char(word);
253 } else if ((word & 0xF800) == 0) {
254 *utf8_output++ = char((word >> 6) | 0b11000000);
255 *utf8_output++ = char((word & 0b111111) | 0b10000000);
256 } else if ((word & 0xF800) != 0xD800) {
257 *utf8_output++ = char((word >> 12) | 0b11100000);
258 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
259 *utf8_output++ = char((word & 0b111111) | 0b10000000);
260 } else {
261 // must be a surrogate pair
262 uint16_t diff = uint16_t(word - 0xD800);
263 uint16_t next_word = !match_system(big_endian)
264 ? scalar::utf16::swap_bytes(buf[k + 1])
265 : buf[k + 1];
266 k++;
267 uint16_t diff2 = uint16_t(next_word - 0xDC00);
268 if ((diff | diff2) > 0x3FF) {
269 return std::make_pair(nullptr,
270 reinterpret_cast<char *>(utf8_output));
271 }
272 uint32_t value = (diff << 10) + diff2 + 0x10000;
273 *utf8_output++ = char((value >> 18) | 0b11110000);
274 *utf8_output++ = char(((value >> 12) & 0b111111) | 0b10000000);
275 *utf8_output++ = char(((value >> 6) & 0b111111) | 0b10000000);
276 *utf8_output++ = char((value & 0b111111) | 0b10000000);
277 }
278 }
279 buf += k;
280 }
281 } // while
282 return std::make_pair(buf, reinterpret_cast<char *>(utf8_output));
283 }
284
285 /*
286 Returns a pair: a result struct and utf8_output.
287 If there is an error, the count field of the result is the position of the
288 error. Otherwise, it is the position of the first unprocessed byte in buf
289 (even if finished). A scalar routing should carry on the conversion of the
290 tail if needed.
291 */
292 template <endianness big_endian>
293 std::pair<result, char *>
294 lsx_convert_utf16_to_utf8_with_errors(const char16_t *buf, size_t len,
295 char *utf8_out) {
296 uint8_t *utf8_output = reinterpret_cast<uint8_t *>(utf8_out);
297 const char16_t *start = buf;
298 const char16_t *end = buf + len;
299
300 const size_t safety_margin =
301 12; // to avoid overruns, see issue
302 // https://github.com/simdutf/simdutf/issues/92
303 while (buf + 16 + safety_margin <= end) {
304 __m128i in = __lsx_vld(reinterpret_cast<const uint16_t *>(buf), 0);
305 if (!match_system(big_endian)) {
306 in = lsx_swap_bytes(in);
307 }
308 if (__lsx_bz_v(
309 __lsx_vslt_hu(__lsx_vrepli_h(0x7F), in))) { // ASCII fast path!!!!
310 // It is common enough that we have sequences of 16 consecutive ASCII
311 // characters.
312 __m128i nextin = __lsx_vld(reinterpret_cast<const uint16_t *>(buf), 16);
313 if (!match_system(big_endian)) {
314 nextin = lsx_swap_bytes(nextin);
315 }
316 if (__lsx_bz_v(__lsx_vslt_hu(__lsx_vrepli_h(0x7F), nextin))) {
317 // 1. pack the bytes
318 // obviously suboptimal.
319 __m128i utf8_packed = __lsx_vpickev_b(nextin, in);
320 // 2. store (16 bytes)
321 __lsx_vst(utf8_packed, utf8_output, 0);
322 // 3. adjust pointers
323 buf += 16;
324 utf8_output += 16;
325 continue; // we are done for this round!
326 } else {
327 // 1. pack the bytes
328 // obviously suboptimal.
329 __m128i utf8_packed = __lsx_vpickev_b(in, in);
330 // 2. store (8 bytes)
331 __lsx_vst(utf8_packed, utf8_output, 0);
332 // 3. adjust pointers
333 buf += 8;
334 utf8_output += 8;
335 in = nextin;
336 }
337 }
338
339 __m128i v_07ff = __lsx_vreplgr2vr_h(uint16_t(0x7ff));
340 __m128i zero = __lsx_vldi(0);
341 if (__lsx_bz_v(__lsx_vslt_hu(v_07ff, in))) {
342 // 1. prepare 2-byte values
343 // input 16-bit word : [0000|0aaa|aabb|bbbb] x 8
344 // expected output : [110a|aaaa|10bb|bbbb] x 8
345 // t0 = [000a|aaaa|bbbb|bb00]
346 __m128i t0 = __lsx_vslli_h(in, 2);
347 // t1 = [000a|aaaa|0000|0000]
348 __m128i t1 = __lsx_vand_v(t0, __lsx_vldi(-2785 /*0x1f00*/));
349 // t2 = [0000|0000|00bb|bbbb]
350 __m128i t2 = __lsx_vand_v(in, __lsx_vrepli_h(0x3f));
351 // t3 = [000a|aaaa|00bb|bbbb]
352 __m128i t3 = __lsx_vor_v(t1, t2);
353 // t4 = [110a|aaaa|10bb|bbbb]
354 __m128i v_c080 = __lsx_vreplgr2vr_h(uint16_t(0xc080));
355 __m128i t4 = __lsx_vor_v(t3, v_c080);
356 // 2. merge ASCII and 2-byte codewords
357 __m128i one_byte_bytemask =
358 __lsx_vsle_hu(in, __lsx_vrepli_h(0x7F /*0x007F*/));
359 __m128i utf8_unpacked = __lsx_vbitsel_v(t4, in, one_byte_bytemask);
360 // 3. prepare bitmask for 8-bit lookup
361 uint32_t m2 = __lsx_vpickve2gr_bu(__lsx_vmskltz_h(one_byte_bytemask), 0);
362 // 4. pack the bytes
363 const uint8_t *row = &simdutf::tables::utf16_to_utf8::pack_1_2_utf8_bytes
364 [lsx_1_2_utf8_bytes_mask[m2]][0];
365 __m128i shuffle = __lsx_vld(row, 1);
366 __m128i utf8_packed = __lsx_vshuf_b(zero, utf8_unpacked, shuffle);
367 // 5. store bytes
368 __lsx_vst(utf8_packed, utf8_output, 0);
369 // 6. adjust pointers
370 buf += 8;
371 utf8_output += row[0];
372 continue;
373 }
374 __m128i surrogates_bytemask =
375 __lsx_vseq_h(__lsx_vand_v(in, __lsx_vldi(-2568 /*0xF800*/)),
376 __lsx_vldi(-2600 /*0xD800*/));
377 // It might seem like checking for surrogates_bitmask == 0xc000 could help.
378 // However, it is likely an uncommon occurrence.
379 if (__lsx_bz_v(surrogates_bytemask)) {
380 // case: code units from register produce either 1, 2 or 3 UTF-8 bytes
381 /* In this branch we handle three cases:
382 1. [0000|0000|0ccc|cccc] => [0ccc|cccc] -
383 single UFT-8 byte
384 2. [0000|0bbb|bbcc|cccc] => [110b|bbbb], [10cc|cccc] -
385 two UTF-8 bytes
386 3. [aaaa|bbbb|bbcc|cccc] => [1110|aaaa], [10bb|bbbb], [10cc|cccc] -
387 three UTF-8 bytes
388
389 We expand the input word (16-bit) into two code units (32-bit), thus
390 we have room for four bytes. However, we need five distinct bit
391 layouts. Note that the last byte in cases #2 and #3 is the same.
392
393 We precompute byte 1 for case #1 and the common byte for cases #2 & #3
394 in register t2.
395
396 We precompute byte 1 for case #3 and -- **conditionally** --
397 precompute either byte 1 for case #2 or byte 2 for case #3. Note that
398 they differ by exactly one bit.
399
400 Finally from these two code units we build proper UTF-8 sequence,
401 taking into account the case (i.e, the number of bytes to write).
402 */
403 /**
404 * Given [aaaa|bbbb|bbcc|cccc] our goal is to produce:
405 * t2 => [0ccc|cccc] [10cc|cccc]
406 * s4 => [1110|aaaa] ([110b|bbbb] OR [10bb|bbbb])
407 */
408 // [aaaa|bbbb|bbcc|cccc] => [bbcc|cccc|bbcc|cccc]
409 __m128i t0 = __lsx_vpickev_b(in, in);
410 t0 = __lsx_vilvl_b(t0, t0);
411
412 // [bbcc|cccc|bbcc|cccc] => [00cc|cccc|00cc|cccc]
413 __m128i v_3f7f = __lsx_vreplgr2vr_h(uint16_t(0x3F7F));
414 __m128i t1 = __lsx_vand_v(t0, v_3f7f);
415 // [00cc|cccc|0bcc|cccc] => [10cc|cccc|0bcc|cccc]
416 __m128i t2 = __lsx_vor_v(t1, __lsx_vldi(-2688));
417
418 // s0: [aaaa|bbbb|bbcc|cccc] => [0000|0000|0000|aaaa]
419 __m128i s0 = __lsx_vsrli_h(in, 12);
420 // s1: [aaaa|bbbb|bbcc|cccc] => [0000|bbbb|bb00|0000]
421 __m128i s1 = __lsx_vslli_h(in, 2);
422 // s1: [aabb|bbbb|cccc|cc00] => [00bb|bbbb|0000|0000]
423 s1 = __lsx_vand_v(s1, __lsx_vldi(-2753 /*0x3F00*/));
424
425 // [00bb|bbbb|0000|aaaa]
426 __m128i s2 = __lsx_vor_v(s0, s1);
427 // s3: [00bb|bbbb|0000|aaaa] => [11bb|bbbb|1110|aaaa]
428 __m128i v_c0e0 = __lsx_vreplgr2vr_h(uint16_t(0xC0E0));
429 __m128i s3 = __lsx_vor_v(s2, v_c0e0);
430 __m128i one_or_two_bytes_bytemask = __lsx_vsle_hu(in, v_07ff);
431 __m128i m0 = __lsx_vandn_v(one_or_two_bytes_bytemask,
432 __lsx_vldi(-2752 /*0x4000*/));
433 __m128i s4 = __lsx_vxor_v(s3, m0);
434
435 // 4. expand code units 16-bit => 32-bit
436 __m128i out0 = __lsx_vilvl_h(s4, t2);
437 __m128i out1 = __lsx_vilvh_h(s4, t2);
438
439 // 5. compress 32-bit code units into 1, 2 or 3 bytes -- 2 x shuffle
440 __m128i one_byte_bytemask = __lsx_vsle_hu(in, __lsx_vrepli_h(0x7F));
441
442 __m128i one_or_two_bytes_bytemask_low =
443 __lsx_vilvl_h(one_or_two_bytes_bytemask, zero);
444 __m128i one_or_two_bytes_bytemask_high =
445 __lsx_vilvh_h(one_or_two_bytes_bytemask, zero);
446
447 __m128i one_byte_bytemask_low =
448 __lsx_vilvl_h(one_byte_bytemask, one_byte_bytemask);
449 __m128i one_byte_bytemask_high =
450 __lsx_vilvh_h(one_byte_bytemask, one_byte_bytemask);
451
452 const uint32_t mask0 = __lsx_vpickve2gr_bu(
453 __lsx_vmskltz_h(__lsx_vor_v(one_or_two_bytes_bytemask_low,
454 one_byte_bytemask_low)),
455 0);
456 const uint32_t mask1 = __lsx_vpickve2gr_bu(
457 __lsx_vmskltz_h(__lsx_vor_v(one_or_two_bytes_bytemask_high,
458 one_byte_bytemask_high)),
459 0);
460
461 const uint8_t *row0 =
462 &simdutf::tables::utf16_to_utf8::pack_1_2_3_utf8_bytes[mask0][0];
463 __m128i shuffle0 = __lsx_vld(row0, 1);
464 __m128i utf8_0 = __lsx_vshuf_b(zero, out0, shuffle0);
465
466 const uint8_t *row1 =
467 &simdutf::tables::utf16_to_utf8::pack_1_2_3_utf8_bytes[mask1][0];
468 __m128i shuffle1 = __lsx_vld(row1, 1);
469 __m128i utf8_1 = __lsx_vshuf_b(zero, out1, shuffle1);
470
471 __lsx_vst(utf8_0, utf8_output, 0);
472 utf8_output += row0[0];
473 __lsx_vst(utf8_1, utf8_output, 0);
474 utf8_output += row1[0];
475
476 buf += 8;
477 // surrogate pair(s) in a register
478 } else {
479 // Let us do a scalar fallback.
480 // It may seem wasteful to use scalar code, but being efficient with SIMD
481 // in the presence of surrogate pairs may require non-trivial tables.
482 size_t forward = 15;
483 size_t k = 0;
484 if (size_t(end - buf) < forward + 1) {
485 forward = size_t(end - buf - 1);
486 }
487 for (; k < forward; k++) {
488 uint16_t word = !match_system(big_endian)
489 ? scalar::utf16::swap_bytes(buf[k])
490 : buf[k];
491 if ((word & 0xFF80) == 0) {
492 *utf8_output++ = char(word);
493 } else if ((word & 0xF800) == 0) {
494 *utf8_output++ = char((word >> 6) | 0b11000000);
495 *utf8_output++ = char((word & 0b111111) | 0b10000000);
496 } else if ((word & 0xF800) != 0xD800) {
497 *utf8_output++ = char((word >> 12) | 0b11100000);
498 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
499 *utf8_output++ = char((word & 0b111111) | 0b10000000);
500 } else {
501 // must be a surrogate pair
502 uint16_t diff = uint16_t(word - 0xD800);
503 uint16_t next_word = !match_system(big_endian)
504 ? scalar::utf16::swap_bytes(buf[k + 1])
505 : buf[k + 1];
506 k++;
507 uint16_t diff2 = uint16_t(next_word - 0xDC00);
508 if ((diff | diff2) > 0x3FF) {
509 return std::make_pair(
510 result(error_code::SURROGATE, buf - start + k - 1),
511 reinterpret_cast<char *>(utf8_output));
512 }
513 uint32_t value = (diff << 10) + diff2 + 0x10000;
514 *utf8_output++ = char((value >> 18) | 0b11110000);
515 *utf8_output++ = char(((value >> 12) & 0b111111) | 0b10000000);
516 *utf8_output++ = char(((value >> 6) & 0b111111) | 0b10000000);
517 *utf8_output++ = char((value & 0b111111) | 0b10000000);
518 }
519 }
520 buf += k;
521 }
522 } // while
523
524 return std::make_pair(result(error_code::SUCCESS, buf - start),
525 reinterpret_cast<char *>(utf8_output));
526 }