Debian Codegraph @

contrib/simdutf/src/lsx/lsx_convert_utf8_to_utf32.cpp

raw
1 // Convert up to 12 bytes from utf8 to utf32 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 12).
5 size_t convert_masked_utf8_to_utf32(const char *input,
6 uint64_t utf8_end_of_code_point_mask,
7 char32_t *&utf32_out) {
8 // we use an approach where we try to process up to 12 input bytes.
9 // Why 12 input bytes and not 16? Because we are concerned with the size of
10 // the lookup tables. Also 12 is nicely divisible by two and three.
11 //
12 uint32_t *&utf32_output = reinterpret_cast<uint32_t *&>(utf32_out);
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 if ((utf8_end_of_code_point_mask & 0xffff) == 0xffff) {
24 // We process in chunks of 16 bytes.
25 // use fast implementation in src/simdutf/arm64/simd.h
26 // Ideally the compiler can keep the tables in registers.
27 simd8<int8_t> temp{in};
28 temp.store_ascii_as_utf32_tbl(utf32_out);
29 utf32_output += 16; // We wrote 16 32-bit characters.
30 return 16; // We consumed 16 bytes.
31 }
32 __m128i zero = __lsx_vldi(0);
33 if (input_utf8_end_of_code_point_mask == 0x924) {
34 // We want to take 4 3-byte UTF-8 code units and turn them into 4 4-byte
35 // UTF-32 code units. Convert to UTF-16
36 __m128i composed_utf16 = convert_utf8_3_byte_to_utf16(in);
37 __m128i utf32_low = __lsx_vilvl_h(zero, composed_utf16);
38
39 __lsx_vst(utf32_low, reinterpret_cast<uint32_t *>(utf32_output), 0);
40 utf32_output += 4; // We wrote 4 32-bit characters.
41 return 12; // We consumed 12 bytes.
42 }
43 // 2 byte sequences occur in short bursts in languages like Greek and Russian.
44 if (input_utf8_end_of_code_point_mask == 0xaaa) {
45 // We want to take 6 2-byte UTF-8 code units and turn them into 6 4-byte
46 // UTF-32 code units. Convert to UTF-16
47 __m128i composed_utf16 = convert_utf8_2_byte_to_utf16(in);
48
49 __m128i utf32_low = __lsx_vilvl_h(zero, composed_utf16);
50 __m128i utf32_high = __lsx_vilvh_h(zero, composed_utf16);
51
52 __lsx_vst(utf32_low, reinterpret_cast<uint32_t *>(utf32_output), 0);
53 __lsx_vst(utf32_high, reinterpret_cast<uint32_t *>(utf32_output), 16);
54 utf32_output += 6;
55 return 12; // We consumed 12 bytes.
56 }
57 /// Either no fast path or an unimportant fast path.
58
59 const uint8_t idx = simdutf::tables::utf8_to_utf16::utf8bigindex
60 [input_utf8_end_of_code_point_mask][0];
61 const uint8_t consumed = simdutf::tables::utf8_to_utf16::utf8bigindex
62 [input_utf8_end_of_code_point_mask][1];
63
64 if (idx < 64) {
65 // SIX (6) input code-code units
66 // Convert to UTF-16
67 __m128i composed_utf16 = convert_utf8_1_to_2_byte_to_utf16(in, idx);
68 __m128i utf32_low = __lsx_vilvl_h(zero, composed_utf16);
69 __m128i utf32_high = __lsx_vilvh_h(zero, composed_utf16);
70
71 __lsx_vst(utf32_low, reinterpret_cast<uint32_t *>(utf32_output), 0);
72 __lsx_vst(utf32_high, reinterpret_cast<uint32_t *>(utf32_output), 16);
73 utf32_output += 6;
74 return consumed;
75 } else if (idx < 145) {
76 // FOUR (4) input code-code units
77 // UTF-16 and UTF-32 use similar algorithms, but UTF-32 skips the narrowing.
78 __m128i sh = __lsx_vld(reinterpret_cast<const uint8_t *>(
79 simdutf::tables::utf8_to_utf16::shufutf8[idx]),
80 0);
81 // Shuffle
82 // 1 byte: 00000000 00000000 0ccccccc
83 // 2 byte: 00000000 110bbbbb 10cccccc
84 // 3 byte: 1110aaaa 10bbbbbb 10cccccc
85 sh = __lsx_vand_v(sh, __lsx_vldi(0x1f));
86 __m128i perm = __lsx_vshuf_b(zero, in, sh);
87 // Split
88 // 00000000 00000000 0ccccccc
89 __m128i ascii = __lsx_vand_v(perm, __lsx_vrepli_w(0x7F)); // 6 or 7 bits
90 // Note: unmasked
91 // xxxxxxxx aaaaxxxx xxxxxxxx
92 __m128i high =
93 __lsx_vsrli_w(__lsx_vand_v(perm, __lsx_vldi(0xf)), 4); // 4 bits
94 // Use 16 bit bic instead of and.
95 // The top bits will be corrected later in the bsl
96 // 00000000 10bbbbbb 00000000
97 __m128i middle =
98 __lsx_vand_v(perm, __lsx_vldi(-1758 /*0x0000FF00*/)); // 5 or 6 bits
99 // Combine low and middle with shift right accumulate
100 // 00000000 00xxbbbb bbcccccc
101 __m128i lowmid = __lsx_vor_v(ascii, __lsx_vsrli_w(middle, 2));
102 // Insert top 4 bits from high byte with bitwise select
103 // 00000000 aaaabbbb bbcccccc
104 __m128i composed =
105 __lsx_vbitsel_v(lowmid, high, __lsx_vldi(-3600 /*0x0000F000*/));
106 __lsx_vst(composed, utf32_output, 0);
107 utf32_output += 4; // We wrote 4 32-bit characters.
108 return consumed;
109 } else if (idx < 209) {
110 // THREE (3) input code-code units
111 if (input_utf8_end_of_code_point_mask == 0x888) {
112 // We want to take 3 4-byte UTF-8 code units and turn them into 3 4-byte
113 // UTF-32 code units. This uses the same method as the fixed 3 byte
114 // version, reversing and shift left insert. However, there is no need for
115 // a shuffle mask now, just rev16 and rev32.
116 //
117 // This version does not use the LUT, but 4 byte sequences are less common
118 // and the overhead of the extra memory access is less important than the
119 // early branch overhead in shorter sequences, so it comes last.
120
121 // Swap pairs of bytes
122 // 10dddddd|10cccccc|10bbbbbb|11110aaa
123 // 10cccccc 10dddddd|11110aaa 10bbbbbb
124 __m128i swap = lsx_swap_bytes(in);
125 // Shift left and insert
126 // xxxxcccc ccdddddd|xxxxxxxa aabbbbbb
127 __m128i merge1 = __lsx_vbitsel_v(__lsx_vsrli_h(swap, 2), swap,
128 __lsx_vrepli_h(0x3f /*0x003F*/));
129 // Shift insert again
130 // xxxxxxxx xxxaaabb bbbbcccc ccdddddd
131 __m128i merge2 =
132 __lsx_vbitsel_v(__lsx_vslli_w(merge1, 12), /* merge1 << 12 */
133 __lsx_vsrli_w(merge1, 16), /* merge1 >> 16 */
134 __lsx_vldi(-2545)); /*0x00000FFF*/
135 // Clear the garbage
136 // 00000000 000aaabb bbbbcccc ccdddddd
137 __m128i composed = __lsx_vand_v(merge2, __lsx_vldi(-2273 /*0x1FFFFF*/));
138 // Store
139 __lsx_vst(composed, utf32_output, 0);
140 utf32_output += 3; // We wrote 3 32-bit characters.
141 return 12; // We consumed 12 bytes.
142 }
143 // Unlike UTF-16, doing a fast codepath doesn't have nearly as much benefit
144 // due to surrogates no longer being involved.
145 __m128i sh = __lsx_vld(reinterpret_cast<const uint8_t *>(
146 simdutf::tables::utf8_to_utf16::shufutf8[idx]),
147 0);
148 // 1 byte: 00000000 00000000 00000000 0ddddddd
149 // 2 byte: 00000000 00000000 110ccccc 10dddddd
150 // 3 byte: 00000000 1110bbbb 10cccccc 10dddddd
151 // 4 byte: 11110aaa 10bbbbbb 10cccccc 10dddddd
152 sh = __lsx_vand_v(sh, __lsx_vldi(0x1f));
153 __m128i perm = __lsx_vshuf_b(zero, in, sh);
154
155 // Ascii
156 __m128i ascii = __lsx_vand_v(perm, __lsx_vrepli_w(0x7F));
157 __m128i middle = __lsx_vand_v(perm, __lsx_vldi(-3777 /*0x00003f00*/));
158 // 00000000 00000000 0000cccc ccdddddd
159 __m128i cd =
160 __lsx_vbitsel_v(__lsx_vsrli_w(middle, 2), ascii, __lsx_vrepli_w(0x3f));
161
162 __m128i correction = __lsx_vand_v(perm, __lsx_vldi(-3520 /*0x00400000*/));
163 __m128i corrected = __lsx_vadd_b(perm, __lsx_vsrli_w(correction, 1));
164 // Insert twice
165 // 00000000 000aaabb bbbbxxxx xxxxxxxx
166 __m128i corrected_srli2 =
167 __lsx_vsrli_w(__lsx_vand_v(corrected, __lsx_vrepli_b(0x7)), 2);
168 __m128i ab =
169 __lsx_vbitsel_v(corrected_srli2, corrected, __lsx_vrepli_h(0x3f));
170 ab = __lsx_vsrli_w(ab, 4);
171 // 00000000 000aaabb bbbbcccc ccdddddd
172 __m128i composed =
173 __lsx_vbitsel_v(ab, cd, __lsx_vldi(-2545 /*0x00000FFF*/));
174 // Store
175 __lsx_vst(composed, utf32_output, 0);
176 utf32_output += 3; // We wrote 3 32-bit characters.
177 return consumed;
178 } else {
179 // here we know that there is an error but we do not handle errors
180 return 12;
181 }
182 }