Debian Codegraph @

contrib/simdutf/src/lsx/lsx_convert_utf8_to_utf16.cpp

raw
1 // Convert up to 16 bytes from utf8 to utf16 using a mask indicating the
2 // end of the code points. Only the least significant 12 bits of the mask
3 // are accessed.
4 // It returns how many bytes were consumed (up to 16, usually 12).
5 template <endianness big_endian>
6 size_t convert_masked_utf8_to_utf16(const char *input,
7 uint64_t utf8_end_of_code_point_mask,
8 char16_t *&utf16_output) {
9 // we use an approach where we try to process up to 12 input bytes.
10 // Why 12 input bytes and not 16? Because we are concerned with the size of
11 // the lookup tables. Also 12 is nicely divisible by two and three.
12 //
13 __m128i in = __lsx_vld(reinterpret_cast<const uint8_t *>(input), 0);
14 const uint16_t input_utf8_end_of_code_point_mask =
15 utf8_end_of_code_point_mask & 0xfff;
16 //
17 // Optimization note: our main path below is load-latency dependent. Thus it
18 // is maybe beneficial to have fast paths that depend on branch prediction but
19 // have less latency. This results in more instructions but, potentially, also
20 // higher speeds.
21
22 // We first try a few fast paths.
23 // The obvious first test is ASCII, which actually consumes the full 16.
24 if ((utf8_end_of_code_point_mask & 0xFFFF) == 0xFFFF) {
25 // We process in chunks of 16 bytes
26 // The routine in simd.h is reused.
27 simd8<int8_t> temp{in};
28 temp.store_ascii_as_utf16<big_endian>(utf16_output);
29 utf16_output += 16; // We wrote 16 16-bit characters.
30 return 16; // We consumed 16 bytes.
31 }
32
33 uint64_t buffer[2];
34 // 3 byte sequences are the next most common, as seen in CJK, which has long
35 // sequences of these.
36 if (input_utf8_end_of_code_point_mask == 0x924) {
37 // We want to take 4 3-byte UTF-8 code units and turn them into 4 2-byte
38 // UTF-16 code units.
39 __m128i composed = convert_utf8_3_byte_to_utf16(in);
40 // Byte swap if necessary
41 if (!match_system(big_endian)) {
42 composed = lsx_swap_bytes(composed);
43 }
44
45 __lsx_vst(composed, reinterpret_cast<uint16_t *>(utf16_output), 0);
46 utf16_output += 4; // We wrote 4 16-bit characters.
47 return 12; // We consumed 12 bytes.
48 }
49
50 // 2 byte sequences occur in short bursts in languages like Greek and Russian.
51 if ((utf8_end_of_code_point_mask & 0xFFFF) == 0xAAAA) {
52 // We want to take 6 2-byte UTF-8 code units and turn them into 6 2-byte
53 // UTF-16 code units.
54 __m128i composed = convert_utf8_2_byte_to_utf16(in);
55 // Byte swap if necessary
56 if (!match_system(big_endian)) {
57 composed = lsx_swap_bytes(composed);
58 }
59
60 __lsx_vst(composed, reinterpret_cast<uint16_t *>(utf16_output), 0);
61 utf16_output += 6; // We wrote 6 16-bit characters.
62 return 12; // We consumed 12 bytes.
63 }
64
65 /// We do not have a fast path available, or the fast path is unimportant, so
66 /// we fallback.
67 const uint8_t idx = simdutf::tables::utf8_to_utf16::utf8bigindex
68 [input_utf8_end_of_code_point_mask][0];
69
70 const uint8_t consumed = simdutf::tables::utf8_to_utf16::utf8bigindex
71 [input_utf8_end_of_code_point_mask][1];
72 const __m128i zero = __lsx_vldi(0);
73 if (idx < 64) {
74 // SIX (6) input code-code units
75 // Convert to UTF-16
76 __m128i composed = convert_utf8_1_to_2_byte_to_utf16(in, idx);
77 // Byte swap if necessary
78 if (!match_system(big_endian)) {
79 composed = lsx_swap_bytes(composed);
80 }
81 // Store
82 __lsx_vst(composed, reinterpret_cast<uint16_t *>(utf16_output), 0);
83 utf16_output += 6; // We wrote 6 16-bit characters.
84 return consumed;
85 } else if (idx < 145) {
86 // FOUR (4) input code-code units
87 // UTF-16 and UTF-32 use similar algorithms, but UTF-32 skips the narrowing.
88 __m128i sh = __lsx_vld(reinterpret_cast<const uint8_t *>(
89 simdutf::tables::utf8_to_utf16::shufutf8[idx]),
90 0);
91 // XXX: depending on the system scalar instructions might be faster.
92 // 1 byte: 00000000 00000000 0ccccccc
93 // 2 byte: 00000000 110bbbbb 10cccccc
94 // 3 byte: 1110aaaa 10bbbbbb 10cccccc
95 sh = __lsx_vand_v(sh, __lsx_vldi(0x1f));
96 __m128i perm = __lsx_vshuf_b(zero, in, sh);
97 // 1 byte: 00000000 0ccccccc
98 // 2 byte: xx0bbbbb x0cccccc
99 // 3 byte: xxbbbbbb x0cccccc
100 __m128i lowperm = __lsx_vpickev_h(perm, perm);
101 // 1 byte: 00000000 00000000
102 // 2 byte: 00000000 00000000
103 // 3 byte: 00000000 1110aaaa
104 __m128i highperm = __lsx_vpickod_h(perm, perm);
105 // 3 byte: aaaa0000 00000000
106 highperm = __lsx_vslli_h(highperm, 12);
107 // ASCII
108 // 1 byte: 00000000 0ccccccc
109 // 2+byte: 00000000 00cccccc
110 __m128i ascii = __lsx_vand_v(lowperm, __lsx_vrepli_h(0x7f));
111 // 1 byte: 00000000 00000000
112 // 2 byte: xx0bbbbb 00000000
113 // 3 byte: xxbbbbbb 00000000
114 __m128i middlebyte = __lsx_vand_v(lowperm, __lsx_vldi(-2561) /*0xFF00*/);
115 // 1 byte: 00000000 0ccccccc
116 // 2 byte: 0010bbbb bbcccccc
117 // 3 byte: 0010bbbb bbcccccc
118 __m128i composed = __lsx_vor_v(__lsx_vsrli_h(middlebyte, 2), ascii);
119
120 __m128i v0fff = __lsx_vreplgr2vr_h(uint16_t(0xfff));
121 // aaaabbbb bbcccccc
122 composed = __lsx_vbitsel_v(highperm, composed, v0fff);
123
124 if (!match_system(big_endian)) {
125 composed = lsx_swap_bytes(composed);
126 }
127
128 __lsx_vst(composed, reinterpret_cast<uint16_t *>(utf16_output), 0);
129 utf16_output += 4; // We wrote 4 16-bit codepoints
130 return consumed;
131 } else if (idx < 209) {
132 // THREE (3) input code-code units
133 if (input_utf8_end_of_code_point_mask == 0x888) {
134 // We want to take 3 4-byte UTF-8 code units and turn them into 3 4-byte
135 // UTF-16 pairs. Generating surrogate pairs is a little tricky though, but
136 // it is easier when we can assume they are all pairs. This version does
137 // not use the LUT, but 4 byte sequences are less common and the overhead
138 // of the extra memory access is less important than the early branch
139 // overhead in shorter sequences.
140
141 // Swap byte pairs
142 // 10dddddd 10cccccc|10bbbbbb 11110aaa
143 // 10cccccc 10dddddd|11110aaa 10bbbbbb
144 __m128i swap = lsx_swap_bytes(in);
145 // Shift left 2 bits
146 // cccccc00 dddddd00 xxxxxxxx bbbbbb00
147 __m128i shift = __lsx_vslli_b(swap, 2);
148 // Create a magic number containing the low 2 bits of the trail surrogate
149 // and all the corrections needed to create the pair. UTF-8 4b prefix =
150 // -0x0000|0xF000 surrogate offset = -0x0000|0x0040 (0x10000 << 6)
151 // surrogate high = +0x0000|0xD800
152 // surrogate low = +0xDC00|0x0000
153 // -------------------------------
154 // = +0xDC00|0xE7C0
155 __m128i magic = __lsx_vreplgr2vr_w(uint32_t(0xDC00E7C0));
156 // Generate unadjusted trail surrogate minus lowest 2 bits
157 // vec(0000FF00) = __lsx_vldi(-1758)
158 // xxxxxxxx xxxxxxxx|11110aaa bbbbbb00
159 __m128i trail =
160 __lsx_vbitsel_v(shift, swap, __lsx_vldi(-1758 /*0000FF00*/));
161 // Insert low 2 bits of trail surrogate to magic number for later
162 // 11011100 00000000 11100111 110000cc
163 __m128i magic_with_low_2 = __lsx_vor_v(__lsx_vsrli_w(shift, 30), magic);
164
165 // Generate lead surrogate
166 // xxxxcccc ccdddddd|xxxxxxxx xxxxxxxx
167 // 000000cc ccdddddd|xxxxxxxx xxxxxxxx
168 __m128i lead = __lsx_vbitsel_v(
169 __lsx_vsrli_h(__lsx_vand_v(shift, __lsx_vldi(0x3F)), 4), swap,
170 __lsx_vrepli_h(0x3f /* 0x003f*/));
171
172 // Blend pairs
173 // __lsx_vldi(-1741) => vec(0x0000FFFF)
174 // 000000cc ccdddddd|11110aaa bbbbbb00
175 __m128i blend =
176 __lsx_vbitsel_v(lead, trail, __lsx_vldi(-1741) /* (0x0000FFFF)*4 */);
177
178 // Add magic number to finish the result
179 // 110111CC CCDDDDDD|110110AA BBBBBBCC
180 __m128i composed = __lsx_vadd_h(blend, magic_with_low_2);
181 // Byte swap if necessary
182 if (!match_system(big_endian)) {
183 composed = lsx_swap_bytes(composed);
184 }
185 // __lsx_vst(composed, reinterpret_cast<uint16_t *>(utf16_output), 0);
186 __lsx_vst(composed, reinterpret_cast<uint16_t *>(buffer), 0);
187 std::memcpy(utf16_output, buffer, 12);
188 utf16_output += 6; // We 3 32-bit surrogate pairs.
189 return 12; // We consumed 12 bytes.
190 }
191 // 3 1-4 byte sequences
192 __m128i sh = __lsx_vld(reinterpret_cast<const uint8_t *>(
193 simdutf::tables::utf8_to_utf16::shufutf8[idx]),
194 0);
195 // 1 byte: 00000000 00000000 00000000 0ddddddd
196 // 3 byte: 00000000 00000000 110ccccc 10dddddd
197 // 3 byte: 00000000 1110bbbb 10cccccc 10dddddd
198 // 4 byte: 11110aaa 10bbbbbb 10cccccc 10dddddd
199 sh = __lsx_vand_v(sh, __lsx_vldi(0x1f));
200 __m128i perm = __lsx_vshuf_b(zero, in, sh);
201 // added to fix issue https://github.com/simdutf/simdutf/issues/514
202 // We only want to write 2 * 16-bit code units when that is actually what we
203 // have. Unfortunately, we cannot trust the input. So it is possible to get
204 // 0xff as an input byte and it should not result in a surrogate pair. We
205 // need to check for that.
206 uint32_t permbuffer[4];
207 __lsx_vst(perm, permbuffer, 0);
208 // Mask the low and middle bytes
209 // 00000000 00000000 00000000 0ddddddd
210 __m128i ascii = __lsx_vand_v(perm, __lsx_vrepli_w(0x7f));
211 // Because the surrogates need more work, the high surrogate is computed
212 // first.
213 __m128i middlehigh = __lsx_vslli_w(perm, 2);
214 // 00000000 00000000 00cccccc 00000000
215 __m128i middlebyte = __lsx_vand_v(perm, __lsx_vldi(-3777) /* 0x00003F00 */);
216 // Start assembling the sequence. Since the 4th byte is in the same position
217 // as it would be in a surrogate and there is no dependency, shift left
218 // instead of right. 3 byte: 00000000 10bbbbxx xxxxxxxx xxxxxxxx 4 byte:
219 // 11110aaa bbbbbbxx xxxxxxxx xxxxxxxx
220 __m128i ab =
221 __lsx_vbitsel_v(middlehigh, perm, __lsx_vldi(-1656) /*0xFF000000*/);
222 // Top 16 bits contains the high ten bits of the surrogate pair before
223 // correction 3 byte: 00000000 10bbbbcc|cccc0000 00000000 4 byte: 11110aaa
224 // bbbbbbcc|cccc0000 00000000 - high 10 bits correct w/o correction
225 __m128i v_fffc0000 = __lsx_vreplgr2vr_w(uint32_t(0xFFFC0000));
226 __m128i abc = __lsx_vbitsel_v(__lsx_vslli_w(middlebyte, 4), ab, v_fffc0000);
227 // Combine the low 6 or 7 bits by a shift right accumulate
228 // 3 byte: 00000000 00000010|bbbbcccc ccdddddd - low 16 bits correct
229 // 4 byte: 00000011 110aaabb|bbbbcccc ccdddddd - low 10 bits correct w/o
230 // correction
231 __m128i composed = __lsx_vor_v(ascii, __lsx_vsrli_w(abc, 6));
232 // After this is for surrogates
233 // Blend the low and high surrogates
234 // 4 byte: 11110aaa bbbbbbcc|bbbbcccc ccdddddd
235 __m128i mixed =
236 __lsx_vbitsel_v(abc, composed, __lsx_vldi(-1741) /*0x0000FFFF*/);
237 // Clear the upper 6 bits of the low surrogate. Don't clear the upper bits
238 // yet as 0x10000 was not subtracted from the codepoint yet. 4 byte:
239 // 11110aaa bbbbbbcc|000000cc ccdddddd
240 __m128i v_ffff03ff = __lsx_vreplgr2vr_w(uint32_t(0xFFFF03FF));
241 __m128i masked_pair = __lsx_vand_v(mixed, v_ffff03ff);
242 // Correct the remaining UTF-8 prefix, surrogate offset, and add the
243 // surrogate prefixes in one magic 16-bit addition. similar magic number but
244 // without the continue byte adjust and halfword swapped UTF-8 4b prefix =
245 // -0xF000|0x0000 surrogate offset = -0x0040|0x0000 (0x10000 << 6)
246 // surrogate high = +0xD800|0x0000
247 // surrogate low = +0x0000|0xDC00
248 // -----------------------------------
249 // = +0xE7C0|0xDC00
250 __m128i magic = __lsx_vreplgr2vr_w(uint32_t(0xE7C0DC00));
251 // 4 byte: 110110AA BBBBBBCC|110111CC CCDDDDDD - surrogate pair complete
252 __m128i surrogates = __lsx_vadd_w(masked_pair, magic);
253 // If the high bit is 1 (s32 less than zero), this needs a surrogate pair
254 __m128i is_pair = __lsx_vslt_w(perm, zero);
255 // Select either the 4 byte surrogate pair or the 2 byte solo codepoint
256 // 3 byte: 0xxxxxxx xxxxxxxx|bbbbcccc ccdddddd
257 // 4 byte: 110110AA BBBBBBCC|110111CC CCDDDDDD
258 __m128i selected = __lsx_vbitsel_v(composed, surrogates, is_pair);
259 // Byte swap if necessary
260 if (!match_system(big_endian)) {
261 selected = lsx_swap_bytes(selected);
262 }
263 // Attempting to shuffle and store would be complex, just scalarize.
264 uint32_t buffer_tmp[4];
265 __lsx_vst(selected, buffer_tmp, 0);
266 // Test for the top bit of the surrogate mask. Remove due to issue 514
267 // const uint32_t SURROGATE_MASK = match_system(big_endian) ? 0x80000000 :
268 // 0x00800000;
269 for (size_t i = 0; i < 3; i++) {
270 // Surrogate
271 // Used to be if (buffer[i] & SURROGATE_MASK) {
272 // See discussion above.
273 // patch for issue https://github.com/simdutf/simdutf/issues/514
274 if ((permbuffer[i] & 0xf8000000) == 0xf0000000) {
275 utf16_output[0] = uint16_t(buffer_tmp[i] >> 16);
276 utf16_output[1] = uint16_t(buffer_tmp[i] & 0xFFFF);
277 utf16_output += 2;
278 } else {
279 utf16_output[0] = uint16_t(buffer_tmp[i] & 0xFFFF);
280 utf16_output++;
281 }
282 }
283 return consumed;
284 } else {
285 // here we know that there is an error but we do not handle errors
286 return 12;
287 }
288 }