Debian Codegraph @

contrib/simdutf/src/lsx/lsx_convert_utf32_to_utf8.cpp

raw
1 std::pair<const char32_t *, char *>
2 lsx_convert_utf32_to_utf8(const char32_t *buf, size_t len, char *utf8_out) {
3 uint8_t *utf8_output = reinterpret_cast<uint8_t *>(utf8_out);
4 const char32_t *end = buf + len;
5
6 __m128i v_c080 = __lsx_vreplgr2vr_h(uint16_t(0xC080));
7 __m128i v_07ff = __lsx_vreplgr2vr_h(uint16_t(0x7FF));
8 __m128i v_dfff = __lsx_vreplgr2vr_h(uint16_t(0xDFFF));
9 __m128i v_d800 = __lsx_vldi(-2600); /*0xD800*/
10 __m128i forbidden_bytemask = __lsx_vldi(0x0);
11
12 const size_t safety_margin =
13 12; // to avoid overruns, see issue
14 // https://github.com/simdutf/simdutf/issues/92
15
16 while (buf + 16 + safety_margin < end) {
17 __m128i in = __lsx_vld(reinterpret_cast<const uint32_t *>(buf), 0);
18 __m128i nextin = __lsx_vld(reinterpret_cast<const uint32_t *>(buf), 16);
19
20 // Check if no bits set above 16th
21 if (__lsx_bz_v(__lsx_vpickod_h(in, nextin))) {
22 // Pack UTF-32 to UTF-16 safely (without surrogate pairs)
23 // Apply UTF-16 => UTF-8 routine (lsx_convert_utf16_to_utf8.cpp)
24 __m128i utf16_packed = __lsx_vpickev_h(nextin, in);
25
26 if (__lsx_bz_v(__lsx_vslt_hu(__lsx_vrepli_h(0x7F),
27 utf16_packed))) { // ASCII fast path!!!!
28 // 1. pack the bytes
29 // obviously suboptimal.
30 __m128i utf8_packed = __lsx_vpickev_b(utf16_packed, utf16_packed);
31 // 2. store (8 bytes)
32 __lsx_vst(utf8_packed, utf8_output, 0);
33 // 3. adjust pointers
34 buf += 8;
35 utf8_output += 8;
36 continue; // we are done for this round!
37 }
38 __m128i zero = __lsx_vldi(0);
39 if (__lsx_bz_v(__lsx_vslt_hu(v_07ff, utf16_packed))) {
40 // 1. prepare 2-byte values
41 // input 16-bit word : [0000|0aaa|aabb|bbbb] x 8
42 // expected output : [110a|aaaa|10bb|bbbb] x 8
43
44 // t0 = [000a|aaaa|bbbb|bb00]
45 const __m128i t0 = __lsx_vslli_h(utf16_packed, 2);
46 // t1 = [000a|aaaa|0000|0000]
47 const __m128i t1 = __lsx_vand_v(t0, __lsx_vldi(-2785 /*0x1f00*/));
48 // t2 = [0000|0000|00bb|bbbb]
49 const __m128i t2 = __lsx_vand_v(utf16_packed, __lsx_vrepli_h(0x3f));
50 // t3 = [000a|aaaa|00bb|bbbb]
51 const __m128i t3 = __lsx_vor_v(t1, t2);
52 // t4 = [110a|aaaa|10bb|bbbb]
53 const __m128i t4 = __lsx_vor_v(t3, v_c080);
54 // 2. merge ASCII and 2-byte codewords
55 __m128i one_byte_bytemask =
56 __lsx_vsle_hu(utf16_packed, __lsx_vrepli_h(0x7F /*0x007F*/));
57 __m128i utf8_unpacked =
58 __lsx_vbitsel_v(t4, utf16_packed, one_byte_bytemask);
59 // 3. prepare bitmask for 8-bit lookup
60 uint32_t m2 =
61 __lsx_vpickve2gr_bu(__lsx_vmskltz_h(one_byte_bytemask), 0);
62 // 4. pack the bytes
63 const uint8_t *row =
64 &simdutf::tables::utf16_to_utf8::pack_1_2_utf8_bytes
65 [lsx_1_2_utf8_bytes_mask[m2]][0];
66 __m128i shuffle = __lsx_vld(row, 1);
67 __m128i utf8_packed = __lsx_vshuf_b(zero, utf8_unpacked, shuffle);
68 // 5. store bytes
69 __lsx_vst(utf8_packed, utf8_output, 0);
70
71 // 6. adjust pointers
72 buf += 8;
73 utf8_output += row[0];
74 continue;
75 } else {
76 // case: code units from register produce either 1, 2 or 3 UTF-8 bytes
77 forbidden_bytemask = __lsx_vor_v(
78 __lsx_vand_v(
79 __lsx_vsle_h(utf16_packed, v_dfff), // utf16_packed <= 0xdfff
80 __lsx_vsle_h(v_d800, utf16_packed)), // utf16_packed >= 0xd800
81 forbidden_bytemask);
82 /* In this branch we handle three cases:
83 1. [0000|0000|0ccc|cccc] => [0ccc|cccc] - single
84 UFT-8 byte
85 2. [0000|0bbb|bbcc|cccc] => [110b|bbbb], [10cc|cccc] - two
86 UTF-8 bytes
87 3. [aaaa|bbbb|bbcc|cccc] => [1110|aaaa], [10bb|bbbb], [10cc|cccc] - three
88 UTF-8 bytes
89
90 We expand the input word (16-bit) into two code units (32-bit), thus
91 we have room for four bytes. However, we need five distinct bit
92 layouts. Note that the last byte in cases #2 and #3 is the same.
93
94 We precompute byte 1 for case #1 and the common byte for cases #2 & #3
95 in register t2.
96
97 We precompute byte 1 for case #3 and -- **conditionally** -- precompute
98 either byte 1 for case #2 or byte 2 for case #3. Note that they
99 differ by exactly one bit.
100
101 Finally from these two code units we build proper UTF-8 sequence, taking
102 into account the case (i.e, the number of bytes to write).
103 */
104 /**
105 * Given [aaaa|bbbb|bbcc|cccc] our goal is to produce:
106 * t2 => [0ccc|cccc] [10cc|cccc]
107 * s4 => [1110|aaaa] ([110b|bbbb] OR [10bb|bbbb])
108 */
109 // [aaaa|bbbb|bbcc|cccc] => [bbcc|cccc|bbcc|cccc]
110 __m128i t0 = __lsx_vpickev_b(utf16_packed, utf16_packed);
111 t0 = __lsx_vilvl_b(t0, t0);
112 // [bbcc|cccc|bbcc|cccc] => [00cc|cccc|0bcc|cccc]
113 __m128i v_3f7f = __lsx_vreplgr2vr_h(uint16_t(0x3F7F));
114 __m128i t1 = __lsx_vand_v(t0, v_3f7f);
115 // [00cc|cccc|0bcc|cccc] => [10cc|cccc|0bcc|cccc]
116 __m128i t2 = __lsx_vor_v(t1, __lsx_vldi(-2688 /*0x8000*/));
117
118 // s0: [aaaa|bbbb|bbcc|cccc] => [0000|0000|0000|aaaa]
119 __m128i s0 = __lsx_vsrli_h(utf16_packed, 12);
120 // s1: [aaaa|bbbb|bbcc|cccc] => [0000|bbbb|bb00|0000]
121 __m128i s1 = __lsx_vslli_h(utf16_packed, 2);
122 // [0000|bbbb|bb00|0000] => [00bb|bbbb|0000|0000]
123 s1 = __lsx_vand_v(s1, __lsx_vldi(-2753 /*0x3F00*/));
124 // [00bb|bbbb|0000|aaaa]
125 __m128i s2 = __lsx_vor_v(s0, s1);
126 // s3: [00bb|bbbb|0000|aaaa] => [11bb|bbbb|1110|aaaa]
127 __m128i v_c0e0 = __lsx_vreplgr2vr_h(uint16_t(0xC0E0));
128 __m128i s3 = __lsx_vor_v(s2, v_c0e0);
129 // __m128i v_07ff = vmovq_n_u16((uint16_t)0x07FF);
130 __m128i one_or_two_bytes_bytemask = __lsx_vsle_hu(utf16_packed, v_07ff);
131 __m128i m0 = __lsx_vandn_v(one_or_two_bytes_bytemask,
132 __lsx_vldi(-2752 /*0x4000*/));
133 __m128i s4 = __lsx_vxor_v(s3, m0);
134
135 // 4. expand code units 16-bit => 32-bit
136 __m128i out0 = __lsx_vilvl_h(s4, t2);
137 __m128i out1 = __lsx_vilvh_h(s4, t2);
138
139 // 5. compress 32-bit code units into 1, 2 or 3 bytes -- 2 x shuffle
140 __m128i one_byte_bytemask =
141 __lsx_vsle_hu(utf16_packed, __lsx_vrepli_h(0x7F));
142
143 __m128i one_or_two_bytes_bytemask_u16_to_u32_low =
144 __lsx_vilvl_h(one_or_two_bytes_bytemask, zero);
145 __m128i one_or_two_bytes_bytemask_u16_to_u32_high =
146 __lsx_vilvh_h(one_or_two_bytes_bytemask, zero);
147
148 __m128i one_byte_bytemask_u16_to_u32_low =
149 __lsx_vilvl_h(one_byte_bytemask, one_byte_bytemask);
150 __m128i one_byte_bytemask_u16_to_u32_high =
151 __lsx_vilvh_h(one_byte_bytemask, one_byte_bytemask);
152
153 const uint32_t mask0 =
154 __lsx_vpickve2gr_bu(__lsx_vmskltz_h(__lsx_vor_v(
155 one_or_two_bytes_bytemask_u16_to_u32_low,
156 one_byte_bytemask_u16_to_u32_low)),
157 0);
158 const uint32_t mask1 =
159 __lsx_vpickve2gr_bu(__lsx_vmskltz_h(__lsx_vor_v(
160 one_or_two_bytes_bytemask_u16_to_u32_high,
161 one_byte_bytemask_u16_to_u32_high)),
162 0);
163
164 const uint8_t *row0 =
165 &simdutf::tables::utf16_to_utf8::pack_1_2_3_utf8_bytes[mask0][0];
166 __m128i shuffle0 = __lsx_vld(row0, 1);
167 __m128i utf8_0 = __lsx_vshuf_b(zero, out0, shuffle0);
168
169 const uint8_t *row1 =
170 &simdutf::tables::utf16_to_utf8::pack_1_2_3_utf8_bytes[mask1][0];
171 __m128i shuffle1 = __lsx_vld(row1, 1);
172 __m128i utf8_1 = __lsx_vshuf_b(zero, out1, shuffle1);
173
174 __lsx_vst(utf8_0, utf8_output, 0);
175 utf8_output += row0[0];
176 __lsx_vst(utf8_1, utf8_output, 0);
177 utf8_output += row1[0];
178
179 buf += 8;
180 }
181 // At least one 32-bit word will produce a surrogate pair in UTF-16 <=>
182 // will produce four UTF-8 bytes.
183 } else {
184 // Let us do a scalar fallback.
185 // It may seem wasteful to use scalar code, but being efficient with SIMD
186 // in the presence of surrogate pairs may require non-trivial tables.
187 size_t forward = 15;
188 size_t k = 0;
189 if (size_t(end - buf) < forward + 1) {
190 forward = size_t(end - buf - 1);
191 }
192 for (; k < forward; k++) {
193 uint32_t word = buf[k];
194 if ((word & 0xFFFFFF80) == 0) {
195 *utf8_output++ = char(word);
196 } else if ((word & 0xFFFFF800) == 0) {
197 *utf8_output++ = char((word >> 6) | 0b11000000);
198 *utf8_output++ = char((word & 0b111111) | 0b10000000);
199 } else if ((word & 0xFFFF0000) == 0) {
200 if (word >= 0xD800 && word <= 0xDFFF) {
201 return std::make_pair(nullptr,
202 reinterpret_cast<char *>(utf8_output));
203 }
204 *utf8_output++ = char((word >> 12) | 0b11100000);
205 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
206 *utf8_output++ = char((word & 0b111111) | 0b10000000);
207 } else {
208 if (word > 0x10FFFF) {
209 return std::make_pair(nullptr,
210 reinterpret_cast<char *>(utf8_output));
211 }
212 *utf8_output++ = char((word >> 18) | 0b11110000);
213 *utf8_output++ = char(((word >> 12) & 0b111111) | 0b10000000);
214 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
215 *utf8_output++ = char((word & 0b111111) | 0b10000000);
216 }
217 }
218 buf += k;
219 }
220 } // while
221
222 // check for invalid input
223 if (__lsx_bnz_v(forbidden_bytemask)) {
224 return std::make_pair(nullptr, reinterpret_cast<char *>(utf8_output));
225 }
226 return std::make_pair(buf, reinterpret_cast<char *>(utf8_output));
227 }
228
229 std::pair<result, char *>
230 lsx_convert_utf32_to_utf8_with_errors(const char32_t *buf, size_t len,
231 char *utf8_out) {
232 uint8_t *utf8_output = reinterpret_cast<uint8_t *>(utf8_out);
233 const char32_t *start = buf;
234 const char32_t *end = buf + len;
235
236 __m128i v_c080 = __lsx_vreplgr2vr_h(uint16_t(0xC080));
237 __m128i v_07ff = __lsx_vreplgr2vr_h(uint16_t(0x7FF));
238 __m128i v_dfff = __lsx_vreplgr2vr_h(uint16_t(0xDFFF));
239 __m128i v_d800 = __lsx_vldi(-2600); /*0xD800*/
240 __m128i forbidden_bytemask = __lsx_vldi(0x0);
241 const size_t safety_margin =
242 12; // to avoid overruns, see issue
243 // https://github.com/simdutf/simdutf/issues/92
244
245 while (buf + 16 + safety_margin < end) {
246 __m128i in = __lsx_vld(reinterpret_cast<const uint32_t *>(buf), 0);
247 __m128i nextin = __lsx_vld(reinterpret_cast<const uint32_t *>(buf), 16);
248
249 // Check if no bits set above 16th
250 if (__lsx_bz_v(__lsx_vpickod_h(in, nextin))) {
251 // Pack UTF-32 to UTF-16 safely (without surrogate pairs)
252 // Apply UTF-16 => UTF-8 routine (lsx_convert_utf16_to_utf8.cpp)
253 __m128i utf16_packed = __lsx_vpickev_h(nextin, in);
254
255 if (__lsx_bz_v(__lsx_vslt_hu(__lsx_vrepli_h(0x7F),
256 utf16_packed))) { // ASCII fast path!!!!
257 // 1. pack the bytes
258 // obviously suboptimal.
259 __m128i utf8_packed = __lsx_vpickev_b(utf16_packed, utf16_packed);
260 // 2. store (8 bytes)
261 __lsx_vst(utf8_packed, utf8_output, 0);
262 // 3. adjust pointers
263 buf += 8;
264 utf8_output += 8;
265 continue; // we are done for this round!
266 }
267 __m128i zero = __lsx_vldi(0);
268 if (__lsx_bz_v(__lsx_vslt_hu(v_07ff, utf16_packed))) {
269 // 1. prepare 2-byte values
270 // input 16-bit word : [0000|0aaa|aabb|bbbb] x 8
271 // expected output : [110a|aaaa|10bb|bbbb] x 8
272
273 // t0 = [000a|aaaa|bbbb|bb00]
274 const __m128i t0 = __lsx_vslli_h(utf16_packed, 2);
275 // t1 = [000a|aaaa|0000|0000]
276 const __m128i t1 = __lsx_vand_v(t0, __lsx_vldi(-2785 /*0x1f00*/));
277 // t2 = [0000|0000|00bb|bbbb]
278 const __m128i t2 = __lsx_vand_v(utf16_packed, __lsx_vrepli_h(0x3f));
279 // t3 = [000a|aaaa|00bb|bbbb]
280 const __m128i t3 = __lsx_vor_v(t1, t2);
281 // t4 = [110a|aaaa|10bb|bbbb]
282 const __m128i t4 = __lsx_vor_v(t3, v_c080);
283 // 2. merge ASCII and 2-byte codewords
284 __m128i one_byte_bytemask =
285 __lsx_vsle_hu(utf16_packed, __lsx_vrepli_h(0x7F /*0x007F*/));
286 __m128i utf8_unpacked =
287 __lsx_vbitsel_v(t4, utf16_packed, one_byte_bytemask);
288 // 3. prepare bitmask for 8-bit lookup
289 uint32_t m2 =
290 __lsx_vpickve2gr_bu(__lsx_vmskltz_h(one_byte_bytemask), 0);
291 // 4. pack the bytes
292 const uint8_t *row =
293 &simdutf::tables::utf16_to_utf8::pack_1_2_utf8_bytes
294 [lsx_1_2_utf8_bytes_mask[m2]][0];
295 __m128i shuffle = __lsx_vld(row, 1);
296 __m128i utf8_packed = __lsx_vshuf_b(zero, utf8_unpacked, shuffle);
297 // 5. store bytes
298 __lsx_vst(utf8_packed, utf8_output, 0);
299
300 // 6. adjust pointers
301 buf += 8;
302 utf8_output += row[0];
303 continue;
304 } else {
305 // case: code units from register produce either 1, 2 or 3 UTF-8 bytes
306 forbidden_bytemask = __lsx_vor_v(
307 __lsx_vand_v(
308 __lsx_vsle_h(utf16_packed, v_dfff), // utf16_packed <= 0xdfff
309 __lsx_vsle_h(v_d800, utf16_packed)), // utf16_packed >= 0xd800
310 forbidden_bytemask);
311 if (__lsx_bnz_v(forbidden_bytemask)) {
312 return std::make_pair(result(error_code::SURROGATE, buf - start),
313 reinterpret_cast<char *>(utf8_output));
314 }
315 /* In this branch we handle three cases:
316 1. [0000|0000|0ccc|cccc] => [0ccc|cccc] - single
317 UFT-8 byte
318 2. [0000|0bbb|bbcc|cccc] => [110b|bbbb], [10cc|cccc] - two
319 UTF-8 bytes
320 3. [aaaa|bbbb|bbcc|cccc] => [1110|aaaa], [10bb|bbbb], [10cc|cccc] - three
321 UTF-8 bytes
322
323 We expand the input word (16-bit) into two code units (32-bit), thus
324 we have room for four bytes. However, we need five distinct bit
325 layouts. Note that the last byte in cases #2 and #3 is the same.
326
327 We precompute byte 1 for case #1 and the common byte for cases #2 & #3
328 in register t2.
329
330 We precompute byte 1 for case #3 and -- **conditionally** -- precompute
331 either byte 1 for case #2 or byte 2 for case #3. Note that they
332 differ by exactly one bit.
333
334 Finally from these two code units we build proper UTF-8 sequence, taking
335 into account the case (i.e, the number of bytes to write).
336 */
337 /**
338 * Given [aaaa|bbbb|bbcc|cccc] our goal is to produce:
339 * t2 => [0ccc|cccc] [10cc|cccc]
340 * s4 => [1110|aaaa] ([110b|bbbb] OR [10bb|bbbb])
341 */
342 // [aaaa|bbbb|bbcc|cccc] => [bbcc|cccc|bbcc|cccc]
343 __m128i t0 = __lsx_vpickev_b(utf16_packed, utf16_packed);
344 t0 = __lsx_vilvl_b(t0, t0);
345 // [bbcc|cccc|bbcc|cccc] => [00cc|cccc|0bcc|cccc]
346 __m128i v_3f7f = __lsx_vreplgr2vr_h(uint16_t(0x3F7F));
347 __m128i t1 = __lsx_vand_v(t0, v_3f7f);
348 // [00cc|cccc|0bcc|cccc] => [10cc|cccc|0bcc|cccc]
349 __m128i t2 = __lsx_vor_v(t1, __lsx_vldi(-2688 /*0x8000*/));
350
351 // s0: [aaaa|bbbb|bbcc|cccc] => [0000|0000|0000|aaaa]
352 __m128i s0 = __lsx_vsrli_h(utf16_packed, 12);
353 // s1: [aaaa|bbbb|bbcc|cccc] => [0000|bbbb|bb00|0000]
354 __m128i s1 = __lsx_vslli_h(utf16_packed, 2);
355 // [0000|bbbb|bb00|0000] => [00bb|bbbb|0000|0000]
356 s1 = __lsx_vand_v(s1, __lsx_vldi(-2753 /*0x3F00*/));
357 // [00bb|bbbb|0000|aaaa]
358 __m128i s2 = __lsx_vor_v(s0, s1);
359 // s3: [00bb|bbbb|0000|aaaa] => [11bb|bbbb|1110|aaaa]
360 __m128i v_c0e0 = __lsx_vreplgr2vr_h(uint16_t(0xC0E0));
361 __m128i s3 = __lsx_vor_v(s2, v_c0e0);
362 // __m128i v_07ff = vmovq_n_u16((uint16_t)0x07FF);
363 __m128i one_or_two_bytes_bytemask = __lsx_vsle_hu(utf16_packed, v_07ff);
364 __m128i m0 = __lsx_vandn_v(one_or_two_bytes_bytemask,
365 __lsx_vldi(-2752 /*0x4000*/));
366 __m128i s4 = __lsx_vxor_v(s3, m0);
367
368 // 4. expand code units 16-bit => 32-bit
369 __m128i out0 = __lsx_vilvl_h(s4, t2);
370 __m128i out1 = __lsx_vilvh_h(s4, t2);
371
372 // 5. compress 32-bit code units into 1, 2 or 3 bytes -- 2 x shuffle
373 __m128i one_byte_bytemask =
374 __lsx_vsle_hu(utf16_packed, __lsx_vrepli_h(0x7F));
375
376 __m128i one_or_two_bytes_bytemask_u16_to_u32_low =
377 __lsx_vilvl_h(one_or_two_bytes_bytemask, zero);
378 __m128i one_or_two_bytes_bytemask_u16_to_u32_high =
379 __lsx_vilvh_h(one_or_two_bytes_bytemask, zero);
380
381 __m128i one_byte_bytemask_u16_to_u32_low =
382 __lsx_vilvl_h(one_byte_bytemask, one_byte_bytemask);
383 __m128i one_byte_bytemask_u16_to_u32_high =
384 __lsx_vilvh_h(one_byte_bytemask, one_byte_bytemask);
385
386 const uint32_t mask0 =
387 __lsx_vpickve2gr_bu(__lsx_vmskltz_h(__lsx_vor_v(
388 one_or_two_bytes_bytemask_u16_to_u32_low,
389 one_byte_bytemask_u16_to_u32_low)),
390 0);
391 const uint32_t mask1 =
392 __lsx_vpickve2gr_bu(__lsx_vmskltz_h(__lsx_vor_v(
393 one_or_two_bytes_bytemask_u16_to_u32_high,
394 one_byte_bytemask_u16_to_u32_high)),
395 0);
396
397 const uint8_t *row0 =
398 &simdutf::tables::utf16_to_utf8::pack_1_2_3_utf8_bytes[mask0][0];
399 __m128i shuffle0 = __lsx_vld(row0, 1);
400 __m128i utf8_0 = __lsx_vshuf_b(zero, out0, shuffle0);
401
402 const uint8_t *row1 =
403 &simdutf::tables::utf16_to_utf8::pack_1_2_3_utf8_bytes[mask1][0];
404 __m128i shuffle1 = __lsx_vld(row1, 1);
405 __m128i utf8_1 = __lsx_vshuf_b(zero, out1, shuffle1);
406
407 __lsx_vst(utf8_0, utf8_output, 0);
408 utf8_output += row0[0];
409 __lsx_vst(utf8_1, utf8_output, 0);
410 utf8_output += row1[0];
411
412 buf += 8;
413 }
414 // At least one 32-bit word will produce a surrogate pair in UTF-16 <=>
415 // will produce four UTF-8 bytes.
416 } else {
417 // Let us do a scalar fallback.
418 // It may seem wasteful to use scalar code, but being efficient with SIMD
419 // in the presence of surrogate pairs may require non-trivial tables.
420 size_t forward = 15;
421 size_t k = 0;
422 if (size_t(end - buf) < forward + 1) {
423 forward = size_t(end - buf - 1);
424 }
425 for (; k < forward; k++) {
426 uint32_t word = buf[k];
427 if ((word & 0xFFFFFF80) == 0) {
428 *utf8_output++ = char(word);
429 } else if ((word & 0xFFFFF800) == 0) {
430 *utf8_output++ = char((word >> 6) | 0b11000000);
431 *utf8_output++ = char((word & 0b111111) | 0b10000000);
432 } else if ((word & 0xFFFF0000) == 0) {
433 if (word >= 0xD800 && word <= 0xDFFF) {
434 return std::make_pair(
435 result(error_code::SURROGATE, buf - start + k),
436 reinterpret_cast<char *>(utf8_output));
437 }
438 *utf8_output++ = char((word >> 12) | 0b11100000);
439 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
440 *utf8_output++ = char((word & 0b111111) | 0b10000000);
441 } else {
442 if (word > 0x10FFFF) {
443 return std::make_pair(
444 result(error_code::TOO_LARGE, buf - start + k),
445 reinterpret_cast<char *>(utf8_output));
446 }
447 *utf8_output++ = char((word >> 18) | 0b11110000);
448 *utf8_output++ = char(((word >> 12) & 0b111111) | 0b10000000);
449 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
450 *utf8_output++ = char((word & 0b111111) | 0b10000000);
451 }
452 }
453 buf += k;
454 }
455 } // while
456
457 return std::make_pair(result(error_code::SUCCESS, buf - start),
458 reinterpret_cast<char *>(utf8_output));
459 }