Debian Codegraph @

contrib/simdutf/src/lsx/implementation.cpp

raw
1 #include "simdutf/lsx/begin.h"
2 namespace simdutf {
3 namespace SIMDUTF_IMPLEMENTATION {
4 namespace {
5 #ifndef SIMDUTF_LSX_H
6 #error "lsx.h must be included"
7 #endif
8 using namespace simd;
9
10 // convert vmskltz/vmskgez/vmsknz to
11 // simdutf::tables::utf16_to_utf8::pack_1_2_utf8_bytes index
12 const uint8_t lsx_1_2_utf8_bytes_mask[] = {
13 0, 1, 4, 5, 16, 17, 20, 21, 64, 65, 68, 69, 80, 81, 84,
14 85, 2, 3, 6, 7, 18, 19, 22, 23, 66, 67, 70, 71, 82, 83,
15 86, 87, 8, 9, 12, 13, 24, 25, 28, 29, 72, 73, 76, 77, 88,
16 89, 92, 93, 10, 11, 14, 15, 26, 27, 30, 31, 74, 75, 78, 79,
17 90, 91, 94, 95, 32, 33, 36, 37, 48, 49, 52, 53, 96, 97, 100,
18 101, 112, 113, 116, 117, 34, 35, 38, 39, 50, 51, 54, 55, 98, 99,
19 102, 103, 114, 115, 118, 119, 40, 41, 44, 45, 56, 57, 60, 61, 104,
20 105, 108, 109, 120, 121, 124, 125, 42, 43, 46, 47, 58, 59, 62, 63,
21 106, 107, 110, 111, 122, 123, 126, 127, 128, 129, 132, 133, 144, 145, 148,
22 149, 192, 193, 196, 197, 208, 209, 212, 213, 130, 131, 134, 135, 146, 147,
23 150, 151, 194, 195, 198, 199, 210, 211, 214, 215, 136, 137, 140, 141, 152,
24 153, 156, 157, 200, 201, 204, 205, 216, 217, 220, 221, 138, 139, 142, 143,
25 154, 155, 158, 159, 202, 203, 206, 207, 218, 219, 222, 223, 160, 161, 164,
26 165, 176, 177, 180, 181, 224, 225, 228, 229, 240, 241, 244, 245, 162, 163,
27 166, 167, 178, 179, 182, 183, 226, 227, 230, 231, 242, 243, 246, 247, 168,
28 169, 172, 173, 184, 185, 188, 189, 232, 233, 236, 237, 248, 249, 252, 253,
29 170, 171, 174, 175, 186, 187, 190, 191, 234, 235, 238, 239, 250, 251, 254,
30 255};
31
32 simdutf_really_inline __m128i lsx_swap_bytes(__m128i vec) {
33 // const v16u8 shuf = {1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14};
34 // return __lsx_vshuf_b(__lsx_vldi(0), vec, shuf);
35 return __lsx_vshuf4i_b(vec, 0b10110001);
36 // return __lsx_vor_v(__lsx_vslli_h(vec, 8), __lsx_vsrli_h(vec, 8));
37 }
38
39 simdutf_really_inline bool is_ascii(const simd8x64<uint8_t> &input) {
40 return input.is_ascii();
41 }
42
43 simdutf_unused simdutf_really_inline simd8<bool>
44 must_be_continuation(const simd8<uint8_t> prev1, const simd8<uint8_t> prev2,
45 const simd8<uint8_t> prev3) {
46 simd8<bool> is_second_byte = prev1 >= uint8_t(0b11000000u);
47 simd8<bool> is_third_byte = prev2 >= uint8_t(0b11100000u);
48 simd8<bool> is_fourth_byte = prev3 >= uint8_t(0b11110000u);
49 // Use ^ instead of | for is_*_byte, because ^ is commutative, and the caller
50 // is using ^ as well. This will work fine because we only have to report
51 // errors for cases with 0-1 lead bytes. Multiple lead bytes implies 2
52 // overlapping multibyte characters, and if that happens, there is guaranteed
53 // to be at least *one* lead byte that is part of only 1 other multibyte
54 // character. The error will be detected there.
55 return is_second_byte ^ is_third_byte ^ is_fourth_byte;
56 }
57
58 simdutf_really_inline simd8<bool>
59 must_be_2_3_continuation(const simd8<uint8_t> prev2,
60 const simd8<uint8_t> prev3) {
61 simd8<bool> is_third_byte = prev2 >= uint8_t(0b11100000u);
62 simd8<bool> is_fourth_byte = prev3 >= uint8_t(0b11110000u);
63 return is_third_byte ^ is_fourth_byte;
64 }
65
66 // common functions for utf8 conversions
67 simdutf_really_inline __m128i convert_utf8_3_byte_to_utf16(__m128i in) {
68 // Low half contains 10bbbbbb|10cccccc
69 // High half contains 1110aaaa|1110aaaa
70 const v16u8 sh = {2, 1, 5, 4, 8, 7, 11, 10, 0, 0, 3, 3, 6, 6, 9, 9};
71 const v8u16 v0fff = {0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff};
72
73 __m128i perm = __lsx_vshuf_b(__lsx_vldi(0), in, (__m128i)sh);
74 // 1110aaaa => aaaa0000
75 __m128i perm_high = __lsx_vslli_b(__lsx_vbsrl_v(perm, 8), 4);
76 // 10bbbbbb 10cccccc => 0010bbbb bbcccccc
77 __m128i composed = __lsx_vbitsel_v(__lsx_vsrli_h(perm, 2), /* perm >> 2*/
78 perm, __lsx_vrepli_h(0x3f) /* 0x003f */);
79 // 0010bbbb bbcccccc => aaaabbbb bbcccccc
80 composed = __lsx_vbitsel_v(perm_high, composed, (__m128i)v0fff);
81
82 return composed;
83 }
84
85 simdutf_really_inline __m128i convert_utf8_2_byte_to_utf16(__m128i in) {
86 // 10bbbbb 110aaaaa => 00bbbbb 000aaaaa
87 __m128i composed = __lsx_vand_v(in, __lsx_vldi(0x3f));
88 // 00bbbbbb 000aaaaa => 00000aaa aabbbbbb
89 composed = __lsx_vbitsel_v(
90 __lsx_vsrli_h(__lsx_vslli_h(composed, 8), 2), /* (aaaaa << 8) >> 2 */
91 __lsx_vsrli_h(composed, 8), /* bbbbbb >> 8 */
92 __lsx_vrepli_h(0x3f)); /* 0x003f */
93 return composed;
94 }
95
96 simdutf_really_inline __m128i
97 convert_utf8_1_to_2_byte_to_utf16(__m128i in, size_t shufutf8_idx) {
98 // Converts 6 1-2 byte UTF-8 characters to 6 UTF-16 characters.
99 // This is a relatively easy scenario
100 // we process SIX (6) input code-code units. The max length in bytes of six
101 // code code units spanning between 1 and 2 bytes each is 12 bytes.
102 __m128i sh =
103 __lsx_vld(reinterpret_cast<const uint8_t *>(
104 simdutf::tables::utf8_to_utf16::shufutf8[shufutf8_idx]),
105 0);
106 // Shuffle
107 // 1 byte: 00000000 0bbbbbbb
108 // 2 byte: 110aaaaa 10bbbbbb
109 __m128i perm = __lsx_vshuf_b(__lsx_vldi(0), in, sh);
110 // 1 byte: 00000000 0bbbbbbb
111 // 2 byte: 00000000 00bbbbbb
112 __m128i ascii = __lsx_vand_v(perm, __lsx_vrepli_h(0x7f)); // 6 or 7 bits
113 // 1 byte: 00000000 00000000
114 // 2 byte: 00000aaa aa000000
115 const __m128i v1f00 = __lsx_vldi(-2785); // -2785(13bit) => 151f
116 __m128i composed = __lsx_vsrli_h(__lsx_vand_v(perm, v1f00), 2); // 5 bits
117 // Combine with a shift right accumulate
118 // 1 byte: 00000000 0bbbbbbb
119 // 2 byte: 00000aaa aabbbbbb
120 composed = __lsx_vadd_h(ascii, composed);
121 return composed;
122 }
123
124 #include "lsx/lsx_validate_utf16.cpp"
125 #include "lsx/lsx_validate_utf32le.cpp"
126
127 #include "lsx/lsx_convert_latin1_to_utf8.cpp"
128 #include "lsx/lsx_convert_latin1_to_utf16.cpp"
129 #include "lsx/lsx_convert_latin1_to_utf32.cpp"
130
131 #include "lsx/lsx_convert_utf8_to_utf16.cpp"
132 #include "lsx/lsx_convert_utf8_to_utf32.cpp"
133 #include "lsx/lsx_convert_utf8_to_latin1.cpp"
134
135 #include "lsx/lsx_convert_utf16_to_latin1.cpp"
136 #include "lsx/lsx_convert_utf16_to_utf8.cpp"
137 #include "lsx/lsx_convert_utf16_to_utf32.cpp"
138
139 #include "lsx/lsx_convert_utf32_to_latin1.cpp"
140 #include "lsx/lsx_convert_utf32_to_utf8.cpp"
141 #include "lsx/lsx_convert_utf32_to_utf16.cpp"
142 #include "lsx/lsx_base64.cpp"
143
144 } // namespace
145 } // namespace SIMDUTF_IMPLEMENTATION
146 } // namespace simdutf
147
148 #include "generic/buf_block_reader.h"
149 #include "generic/utf8_validation/utf8_lookup4_algorithm.h"
150 #include "generic/utf8_validation/utf8_validator.h"
151
152 // transcoding from UTF-8 to Latin 1
153 #include "generic/utf8_to_latin1/utf8_to_latin1.h"
154 #include "generic/utf8_to_latin1/valid_utf8_to_latin1.h"
155 // transcoding from UTF-8 to UTF-16
156 #include "generic/utf8_to_utf16/valid_utf8_to_utf16.h"
157 #include "generic/utf8_to_utf16/utf8_to_utf16.h"
158 // transcoding from UTF-8 to UTF-32
159 #include "generic/utf8_to_utf32/valid_utf8_to_utf32.h"
160 #include "generic/utf8_to_utf32/utf8_to_utf32.h"
161
162 #include "scalar/utf32_to_utf16/valid_utf32_to_utf16.h"
163 #include "scalar/utf32_to_utf16/utf32_to_utf16.h"
164
165 // other functions
166 #include "generic/utf8.h"
167 #include "generic/utf16.h"
168 #include "scalar/latin1.h"
169
170 //
171 // Implementation-specific overrides
172 //
173 namespace simdutf {
174 namespace SIMDUTF_IMPLEMENTATION {
175
176 simdutf_warn_unused int
177 implementation::detect_encodings(const char *input,
178 size_t length) const noexcept {
179 // If there is a BOM, then we trust it.
180 auto bom_encoding = simdutf::BOM::check_bom(input, length);
181 // todo: reimplement as a one-pass algorithm.
182 if (bom_encoding != encoding_type::unspecified) {
183 return bom_encoding;
184 }
185 int out = 0;
186 if (validate_utf8(input, length)) {
187 out |= encoding_type::UTF8;
188 }
189 if ((length % 2) == 0) {
190 if (validate_utf16le(reinterpret_cast<const char16_t *>(input),
191 length / 2)) {
192 out |= encoding_type::UTF16_LE;
193 }
194 }
195 if ((length % 4) == 0) {
196 if (validate_utf32(reinterpret_cast<const char32_t *>(input), length / 4)) {
197 out |= encoding_type::UTF32_LE;
198 }
199 }
200 return out;
201 }
202
203 simdutf_warn_unused bool
204 implementation::validate_utf8(const char *buf, size_t len) const noexcept {
205 return lsx::utf8_validation::generic_validate_utf8(buf, len);
206 }
207
208 simdutf_warn_unused result implementation::validate_utf8_with_errors(
209 const char *buf, size_t len) const noexcept {
210 return lsx::utf8_validation::generic_validate_utf8_with_errors(buf, len);
211 }
212
213 simdutf_warn_unused bool
214 implementation::validate_ascii(const char *buf, size_t len) const noexcept {
215 return lsx::utf8_validation::generic_validate_ascii(buf, len);
216 }
217
218 simdutf_warn_unused result implementation::validate_ascii_with_errors(
219 const char *buf, size_t len) const noexcept {
220 return lsx::utf8_validation::generic_validate_ascii_with_errors(buf, len);
221 }
222
223 simdutf_warn_unused bool
224 implementation::validate_utf16le(const char16_t *buf,
225 size_t len) const noexcept {
226 if (simdutf_unlikely(len == 0)) {
227 // empty input is valid. protected the implementation from nullptr.
228 return true;
229 }
230 const char16_t *tail = lsx_validate_utf16<endianness::LITTLE>(buf, len);
231 if (tail) {
232 return scalar::utf16::validate<endianness::LITTLE>(tail,
233 len - (tail - buf));
234 } else {
235 return false;
236 }
237 }
238
239 simdutf_warn_unused bool
240 implementation::validate_utf16be(const char16_t *buf,
241 size_t len) const noexcept {
242 if (simdutf_unlikely(len == 0)) {
243 // empty input is valid. protected the implementation from nullptr.
244 return true;
245 }
246 const char16_t *tail = lsx_validate_utf16<endianness::BIG>(buf, len);
247 if (tail) {
248 return scalar::utf16::validate<endianness::BIG>(tail, len - (tail - buf));
249 } else {
250 return false;
251 }
252 }
253
254 simdutf_warn_unused result implementation::validate_utf16le_with_errors(
255 const char16_t *buf, size_t len) const noexcept {
256 if (simdutf_unlikely(len == 0)) {
257 return result(error_code::SUCCESS, 0);
258 }
259 result res = lsx_validate_utf16_with_errors<endianness::LITTLE>(buf, len);
260 if (res.count != len) {
261 result scalar_res = scalar::utf16::validate_with_errors<endianness::LITTLE>(
262 buf + res.count, len - res.count);
263 return result(scalar_res.error, res.count + scalar_res.count);
264 } else {
265 return res;
266 }
267 }
268
269 simdutf_warn_unused result implementation::validate_utf16be_with_errors(
270 const char16_t *buf, size_t len) const noexcept {
271 if (simdutf_unlikely(len == 0)) {
272 return result(error_code::SUCCESS, 0);
273 }
274 result res = lsx_validate_utf16_with_errors<endianness::BIG>(buf, len);
275 if (res.count != len) {
276 result scalar_res = scalar::utf16::validate_with_errors<endianness::BIG>(
277 buf + res.count, len - res.count);
278 return result(scalar_res.error, res.count + scalar_res.count);
279 } else {
280 return res;
281 }
282 }
283
284 simdutf_warn_unused bool
285 implementation::validate_utf32(const char32_t *buf, size_t len) const noexcept {
286 if (simdutf_unlikely(len == 0)) {
287 // empty input is valid. protected the implementation from nullptr.
288 return true;
289 }
290 const char32_t *tail = lsx_validate_utf32le(buf, len);
291 if (tail) {
292 return scalar::utf32::validate(tail, len - (tail - buf));
293 } else {
294 return false;
295 }
296 }
297
298 simdutf_warn_unused result implementation::validate_utf32_with_errors(
299 const char32_t *buf, size_t len) const noexcept {
300 if (simdutf_unlikely(len == 0)) {
301 return result(error_code::SUCCESS, 0);
302 }
303 result res = lsx_validate_utf32le_with_errors(buf, len);
304 if (res.count != len) {
305 result scalar_res =
306 scalar::utf32::validate_with_errors(buf + res.count, len - res.count);
307 return result(scalar_res.error, res.count + scalar_res.count);
308 } else {
309 return res;
310 }
311 }
312
313 simdutf_warn_unused size_t implementation::convert_latin1_to_utf8(
314 const char *buf, size_t len, char *utf8_output) const noexcept {
315 std::pair<const char *, char *> ret =
316 lsx_convert_latin1_to_utf8(buf, len, utf8_output);
317 size_t converted_chars = ret.second - utf8_output;
318
319 if (ret.first != buf + len) {
320 const size_t scalar_converted_chars = scalar::latin1_to_utf8::convert(
321 ret.first, len - (ret.first - buf), ret.second);
322 converted_chars += scalar_converted_chars;
323 }
324 return converted_chars;
325 }
326
327 simdutf_warn_unused size_t implementation::convert_latin1_to_utf16le(
328 const char *buf, size_t len, char16_t *utf16_output) const noexcept {
329 std::pair<const char *, char16_t *> ret =
330 lsx_convert_latin1_to_utf16le(buf, len, utf16_output);
331 size_t converted_chars = ret.second - utf16_output;
332 if (ret.first != buf + len) {
333 const size_t scalar_converted_chars =
334 scalar::latin1_to_utf16::convert<endianness::LITTLE>(
335 ret.first, len - (ret.first - buf), ret.second);
336 converted_chars += scalar_converted_chars;
337 }
338 return converted_chars;
339 }
340
341 simdutf_warn_unused size_t implementation::convert_latin1_to_utf16be(
342 const char *buf, size_t len, char16_t *utf16_output) const noexcept {
343 std::pair<const char *, char16_t *> ret =
344 lsx_convert_latin1_to_utf16be(buf, len, utf16_output);
345 size_t converted_chars = ret.second - utf16_output;
346 if (ret.first != buf + len) {
347 const size_t scalar_converted_chars =
348 scalar::latin1_to_utf16::convert<endianness::BIG>(
349 ret.first, len - (ret.first - buf), ret.second);
350 converted_chars += scalar_converted_chars;
351 }
352 return converted_chars;
353 }
354
355 simdutf_warn_unused size_t implementation::convert_latin1_to_utf32(
356 const char *buf, size_t len, char32_t *utf32_output) const noexcept {
357 std::pair<const char *, char32_t *> ret =
358 lsx_convert_latin1_to_utf32(buf, len, utf32_output);
359 size_t converted_chars = ret.second - utf32_output;
360 if (ret.first != buf + len) {
361 const size_t scalar_converted_chars = scalar::latin1_to_utf32::convert(
362 ret.first, len - (ret.first - buf), ret.second);
363 converted_chars += scalar_converted_chars;
364 }
365 return converted_chars;
366 }
367
368 simdutf_warn_unused size_t implementation::convert_utf8_to_latin1(
369 const char *buf, size_t len, char *latin1_output) const noexcept {
370 utf8_to_latin1::validating_transcoder converter;
371 return converter.convert(buf, len, latin1_output);
372 }
373
374 simdutf_warn_unused result implementation::convert_utf8_to_latin1_with_errors(
375 const char *buf, size_t len, char *latin1_output) const noexcept {
376 utf8_to_latin1::validating_transcoder converter;
377 return converter.convert_with_errors(buf, len, latin1_output);
378 }
379
380 simdutf_warn_unused size_t implementation::convert_valid_utf8_to_latin1(
381 const char *buf, size_t len, char *latin1_output) const noexcept {
382 return lsx::utf8_to_latin1::convert_valid(buf, len, latin1_output);
383 }
384
385 simdutf_warn_unused size_t implementation::convert_utf8_to_utf16le(
386 const char *buf, size_t len, char16_t *utf16_output) const noexcept {
387 utf8_to_utf16::validating_transcoder converter;
388 return converter.convert<endianness::LITTLE>(buf, len, utf16_output);
389 }
390
391 simdutf_warn_unused size_t implementation::convert_utf8_to_utf16be(
392 const char *buf, size_t len, char16_t *utf16_output) const noexcept {
393 utf8_to_utf16::validating_transcoder converter;
394 return converter.convert<endianness::BIG>(buf, len, utf16_output);
395 }
396
397 simdutf_warn_unused result implementation::convert_utf8_to_utf16le_with_errors(
398 const char *buf, size_t len, char16_t *utf16_output) const noexcept {
399 utf8_to_utf16::validating_transcoder converter;
400 return converter.convert_with_errors<endianness::LITTLE>(buf, len,
401 utf16_output);
402 }
403
404 simdutf_warn_unused result implementation::convert_utf8_to_utf16be_with_errors(
405 const char *buf, size_t len, char16_t *utf16_output) const noexcept {
406 utf8_to_utf16::validating_transcoder converter;
407 return converter.convert_with_errors<endianness::BIG>(buf, len, utf16_output);
408 }
409
410 simdutf_warn_unused size_t implementation::convert_valid_utf8_to_utf16le(
411 const char *input, size_t size, char16_t *utf16_output) const noexcept {
412 return utf8_to_utf16::convert_valid<endianness::LITTLE>(input, size,
413 utf16_output);
414 }
415
416 simdutf_warn_unused size_t implementation::convert_valid_utf8_to_utf16be(
417 const char *input, size_t size, char16_t *utf16_output) const noexcept {
418 return utf8_to_utf16::convert_valid<endianness::BIG>(input, size,
419 utf16_output);
420 }
421
422 simdutf_warn_unused size_t implementation::convert_utf8_to_utf32(
423 const char *buf, size_t len, char32_t *utf32_output) const noexcept {
424 utf8_to_utf32::validating_transcoder converter;
425 return converter.convert(buf, len, utf32_output);
426 }
427
428 simdutf_warn_unused result implementation::convert_utf8_to_utf32_with_errors(
429 const char *buf, size_t len, char32_t *utf32_output) const noexcept {
430 utf8_to_utf32::validating_transcoder converter;
431 return converter.convert_with_errors(buf, len, utf32_output);
432 }
433
434 simdutf_warn_unused size_t implementation::convert_valid_utf8_to_utf32(
435 const char *input, size_t size, char32_t *utf32_output) const noexcept {
436 return utf8_to_utf32::convert_valid(input, size, utf32_output);
437 }
438
439 simdutf_warn_unused size_t implementation::convert_utf16le_to_latin1(
440 const char16_t *buf, size_t len, char *latin1_output) const noexcept {
441 std::pair<const char16_t *, char *> ret =
442 lsx_convert_utf16_to_latin1<endianness::LITTLE>(buf, len, latin1_output);
443 if (ret.first == nullptr) {
444 return 0;
445 }
446 size_t saved_bytes = ret.second - latin1_output;
447
448 if (ret.first != buf + len) {
449 const size_t scalar_saved_bytes =
450 scalar::utf16_to_latin1::convert<endianness::LITTLE>(
451 ret.first, len - (ret.first - buf), ret.second);
452 if (scalar_saved_bytes == 0) {
453 return 0;
454 }
455 saved_bytes += scalar_saved_bytes;
456 }
457 return saved_bytes;
458 }
459
460 simdutf_warn_unused size_t implementation::convert_utf16be_to_latin1(
461 const char16_t *buf, size_t len, char *latin1_output) const noexcept {
462 std::pair<const char16_t *, char *> ret =
463 lsx_convert_utf16_to_latin1<endianness::BIG>(buf, len, latin1_output);
464 if (ret.first == nullptr) {
465 return 0;
466 }
467 size_t saved_bytes = ret.second - latin1_output;
468
469 if (ret.first != buf + len) {
470 const size_t scalar_saved_bytes =
471 scalar::utf16_to_latin1::convert<endianness::BIG>(
472 ret.first, len - (ret.first - buf), ret.second);
473 if (scalar_saved_bytes == 0) {
474 return 0;
475 }
476 saved_bytes += scalar_saved_bytes;
477 }
478 return saved_bytes;
479 }
480
481 simdutf_warn_unused result
482 implementation::convert_utf16le_to_latin1_with_errors(
483 const char16_t *buf, size_t len, char *latin1_output) const noexcept {
484 std::pair<result, char *> ret =
485 lsx_convert_utf16_to_latin1_with_errors<endianness::LITTLE>(
486 buf, len, latin1_output);
487 if (ret.first.error) {
488 return ret.first;
489 } // Can return directly since scalar fallback already found correct
490 // ret.first.count
491 if (ret.first.count != len) { // All good so far, but not finished
492 result scalar_res =
493 scalar::utf16_to_latin1::convert_with_errors<endianness::LITTLE>(
494 buf + ret.first.count, len - ret.first.count, ret.second);
495 if (scalar_res.error) {
496 scalar_res.count += ret.first.count;
497 return scalar_res;
498 } else {
499 ret.second += scalar_res.count;
500 }
501 }
502 ret.first.count =
503 ret.second -
504 latin1_output; // Set count to the number of 8-bit code units written
505 return ret.first;
506 }
507
508 simdutf_warn_unused result
509 implementation::convert_utf16be_to_latin1_with_errors(
510 const char16_t *buf, size_t len, char *latin1_output) const noexcept {
511 std::pair<result, char *> ret =
512 lsx_convert_utf16_to_latin1_with_errors<endianness::BIG>(buf, len,
513 latin1_output);
514 if (ret.first.error) {
515 return ret.first;
516 } // Can return directly since scalar fallback already found correct
517 // ret.first.count
518 if (ret.first.count != len) { // All good so far, but not finished
519 result scalar_res =
520 scalar::utf16_to_latin1::convert_with_errors<endianness::BIG>(
521 buf + ret.first.count, len - ret.first.count, ret.second);
522 if (scalar_res.error) {
523 scalar_res.count += ret.first.count;
524 return scalar_res;
525 } else {
526 ret.second += scalar_res.count;
527 }
528 }
529 ret.first.count =
530 ret.second -
531 latin1_output; // Set count to the number of 8-bit code units written
532 return ret.first;
533 }
534
535 simdutf_warn_unused size_t implementation::convert_valid_utf16be_to_latin1(
536 const char16_t *buf, size_t len, char *latin1_output) const noexcept {
537 // optimization opportunity: implement a custom function.
538 return convert_utf16be_to_latin1(buf, len, latin1_output);
539 }
540
541 simdutf_warn_unused size_t implementation::convert_valid_utf16le_to_latin1(
542 const char16_t *buf, size_t len, char *latin1_output) const noexcept {
543 // optimization opportunity: implement a custom function.
544 return convert_utf16le_to_latin1(buf, len, latin1_output);
545 }
546
547 simdutf_warn_unused size_t implementation::convert_utf16le_to_utf8(
548 const char16_t *buf, size_t len, char *utf8_output) const noexcept {
549 std::pair<const char16_t *, char *> ret =
550 lsx_convert_utf16_to_utf8<endianness::LITTLE>(buf, len, utf8_output);
551 if (ret.first == nullptr) {
552 return 0;
553 }
554 size_t saved_bytes = ret.second - utf8_output;
555 if (ret.first != buf + len) {
556 const size_t scalar_saved_bytes =
557 scalar::utf16_to_utf8::convert<endianness::LITTLE>(
558 ret.first, len - (ret.first - buf), ret.second);
559 if (scalar_saved_bytes == 0) {
560 return 0;
561 }
562 saved_bytes += scalar_saved_bytes;
563 }
564 return saved_bytes;
565 }
566
567 simdutf_warn_unused size_t implementation::convert_utf16be_to_utf8(
568 const char16_t *buf, size_t len, char *utf8_output) const noexcept {
569 std::pair<const char16_t *, char *> ret =
570 lsx_convert_utf16_to_utf8<endianness::BIG>(buf, len, utf8_output);
571 if (ret.first == nullptr) {
572 return 0;
573 }
574 size_t saved_bytes = ret.second - utf8_output;
575 if (ret.first != buf + len) {
576 const size_t scalar_saved_bytes =
577 scalar::utf16_to_utf8::convert<endianness::BIG>(
578 ret.first, len - (ret.first - buf), ret.second);
579 if (scalar_saved_bytes == 0) {
580 return 0;
581 }
582 saved_bytes += scalar_saved_bytes;
583 }
584 return saved_bytes;
585 }
586
587 simdutf_warn_unused result implementation::convert_utf16le_to_utf8_with_errors(
588 const char16_t *buf, size_t len, char *utf8_output) const noexcept {
589 // ret.first.count is always the position in the buffer, not the number of
590 // code units written even if finished
591 std::pair<result, char *> ret =
592 lsx_convert_utf16_to_utf8_with_errors<endianness::LITTLE>(buf, len,
593 utf8_output);
594 if (ret.first.error) {
595 return ret.first;
596 } // Can return directly since scalar fallback already found correct
597 // ret.first.count
598 if (ret.first.count != len) { // All good so far, but not finished
599 result scalar_res =
600 scalar::utf16_to_utf8::convert_with_errors<endianness::LITTLE>(
601 buf + ret.first.count, len - ret.first.count, ret.second);
602 if (scalar_res.error) {
603 scalar_res.count += ret.first.count;
604 return scalar_res;
605 } else {
606 ret.second += scalar_res.count;
607 }
608 }
609 ret.first.count =
610 ret.second -
611 utf8_output; // Set count to the number of 8-bit code units written
612 return ret.first;
613 }
614
615 simdutf_warn_unused result implementation::convert_utf16be_to_utf8_with_errors(
616 const char16_t *buf, size_t len, char *utf8_output) const noexcept {
617 // ret.first.count is always the position in the buffer, not the number of
618 // code units written even if finished
619 std::pair<result, char *> ret =
620 lsx_convert_utf16_to_utf8_with_errors<endianness::BIG>(buf, len,
621 utf8_output);
622 if (ret.first.error) {
623 return ret.first;
624 } // Can return directly since scalar fallback already found correct
625 // ret.first.count
626 if (ret.first.count != len) { // All good so far, but not finished
627 result scalar_res =
628 scalar::utf16_to_utf8::convert_with_errors<endianness::BIG>(
629 buf + ret.first.count, len - ret.first.count, ret.second);
630 if (scalar_res.error) {
631 scalar_res.count += ret.first.count;
632 return scalar_res;
633 } else {
634 ret.second += scalar_res.count;
635 }
636 }
637 ret.first.count =
638 ret.second -
639 utf8_output; // Set count to the number of 8-bit code units written
640 return ret.first;
641 }
642
643 simdutf_warn_unused size_t implementation::convert_valid_utf16le_to_utf8(
644 const char16_t *buf, size_t len, char *utf8_output) const noexcept {
645 return convert_utf16le_to_utf8(buf, len, utf8_output);
646 }
647
648 simdutf_warn_unused size_t implementation::convert_valid_utf16be_to_utf8(
649 const char16_t *buf, size_t len, char *utf8_output) const noexcept {
650 return convert_utf16be_to_utf8(buf, len, utf8_output);
651 }
652
653 simdutf_warn_unused size_t implementation::convert_utf32_to_utf8(
654 const char32_t *buf, size_t len, char *utf8_output) const noexcept {
655 if (simdutf_unlikely(len == 0)) {
656 return 0;
657 }
658 std::pair<const char32_t *, char *> ret =
659 lsx_convert_utf32_to_utf8(buf, len, utf8_output);
660 if (ret.first == nullptr) {
661 return 0;
662 }
663 size_t saved_bytes = ret.second - utf8_output;
664 if (ret.first != buf + len) {
665 const size_t scalar_saved_bytes = scalar::utf32_to_utf8::convert(
666 ret.first, len - (ret.first - buf), ret.second);
667 if (scalar_saved_bytes == 0) {
668 return 0;
669 }
670 saved_bytes += scalar_saved_bytes;
671 }
672 return saved_bytes;
673 }
674
675 simdutf_warn_unused result implementation::convert_utf32_to_utf8_with_errors(
676 const char32_t *buf, size_t len, char *utf8_output) const noexcept {
677 if (simdutf_unlikely(len == 0)) {
678 return result(error_code::SUCCESS, 0);
679 }
680 // ret.first.count is always the position in the buffer, not the number of
681 // code units written even if finished
682 std::pair<result, char *> ret =
683 lsx_convert_utf32_to_utf8_with_errors(buf, len, utf8_output);
684 if (ret.first.count != len) {
685 result scalar_res = scalar::utf32_to_utf8::convert_with_errors(
686 buf + ret.first.count, len - ret.first.count, ret.second);
687 if (scalar_res.error) {
688 scalar_res.count += ret.first.count;
689 return scalar_res;
690 } else {
691 ret.second += scalar_res.count;
692 }
693 }
694 ret.first.count =
695 ret.second -
696 utf8_output; // Set count to the number of 8-bit code units written
697 return ret.first;
698 }
699
700 simdutf_warn_unused size_t implementation::convert_utf16le_to_utf32(
701 const char16_t *buf, size_t len, char32_t *utf32_output) const noexcept {
702 std::pair<const char16_t *, char32_t *> ret =
703 lsx_convert_utf16_to_utf32<endianness::LITTLE>(buf, len, utf32_output);
704 if (ret.first == nullptr) {
705 return 0;
706 }
707 size_t saved_bytes = ret.second - utf32_output;
708 if (ret.first != buf + len) {
709 const size_t scalar_saved_bytes =
710 scalar::utf16_to_utf32::convert<endianness::LITTLE>(
711 ret.first, len - (ret.first - buf), ret.second);
712 if (scalar_saved_bytes == 0) {
713 return 0;
714 }
715 saved_bytes += scalar_saved_bytes;
716 }
717 return saved_bytes;
718 }
719
720 simdutf_warn_unused size_t implementation::convert_utf16be_to_utf32(
721 const char16_t *buf, size_t len, char32_t *utf32_output) const noexcept {
722 std::pair<const char16_t *, char32_t *> ret =
723 lsx_convert_utf16_to_utf32<endianness::BIG>(buf, len, utf32_output);
724 if (ret.first == nullptr) {
725 return 0;
726 }
727 size_t saved_bytes = ret.second - utf32_output;
728 if (ret.first != buf + len) {
729 const size_t scalar_saved_bytes =
730 scalar::utf16_to_utf32::convert<endianness::BIG>(
731 ret.first, len - (ret.first - buf), ret.second);
732 if (scalar_saved_bytes == 0) {
733 return 0;
734 }
735 saved_bytes += scalar_saved_bytes;
736 }
737 return saved_bytes;
738 }
739
740 simdutf_warn_unused result implementation::convert_utf16le_to_utf32_with_errors(
741 const char16_t *buf, size_t len, char32_t *utf32_output) const noexcept {
742 // ret.first.count is always the position in the buffer, not the number of
743 // code units written even if finished
744 std::pair<result, char32_t *> ret =
745 lsx_convert_utf16_to_utf32_with_errors<endianness::LITTLE>(buf, len,
746 utf32_output);
747 if (ret.first.error) {
748 return ret.first;
749 } // Can return directly since scalar fallback already found correct
750 // ret.first.count
751 if (ret.first.count != len) { // All good so far, but not finished
752 result scalar_res =
753 scalar::utf16_to_utf32::convert_with_errors<endianness::LITTLE>(
754 buf + ret.first.count, len - ret.first.count, ret.second);
755 if (scalar_res.error) {
756 scalar_res.count += ret.first.count;
757 return scalar_res;
758 } else {
759 ret.second += scalar_res.count;
760 }
761 }
762 ret.first.count =
763 ret.second -
764 utf32_output; // Set count to the number of 8-bit code units written
765 return ret.first;
766 }
767
768 simdutf_warn_unused result implementation::convert_utf16be_to_utf32_with_errors(
769 const char16_t *buf, size_t len, char32_t *utf32_output) const noexcept {
770 // ret.first.count is always the position in the buffer, not the number of
771 // code units written even if finished
772 std::pair<result, char32_t *> ret =
773 lsx_convert_utf16_to_utf32_with_errors<endianness::BIG>(buf, len,
774 utf32_output);
775 if (ret.first.error) {
776 return ret.first;
777 } // Can return directly since scalar fallback already found correct
778 // ret.first.count
779 if (ret.first.count != len) { // All good so far, but not finished
780 result scalar_res =
781 scalar::utf16_to_utf32::convert_with_errors<endianness::BIG>(
782 buf + ret.first.count, len - ret.first.count, ret.second);
783 if (scalar_res.error) {
784 scalar_res.count += ret.first.count;
785 return scalar_res;
786 } else {
787 ret.second += scalar_res.count;
788 }
789 }
790 ret.first.count =
791 ret.second -
792 utf32_output; // Set count to the number of 8-bit code units written
793 return ret.first;
794 }
795
796 simdutf_warn_unused size_t implementation::convert_utf32_to_latin1(
797 const char32_t *buf, size_t len, char *latin1_output) const noexcept {
798 std::pair<const char32_t *, char *> ret =
799 lsx_convert_utf32_to_latin1(buf, len, latin1_output);
800 if (ret.first == nullptr) {
801 return 0;
802 }
803 size_t saved_bytes = ret.second - latin1_output;
804
805 if (ret.first != buf + len) {
806 const size_t scalar_saved_bytes = scalar::utf32_to_latin1::convert(
807 ret.first, len - (ret.first - buf), ret.second);
808 if (scalar_saved_bytes == 0) {
809 return 0;
810 }
811 saved_bytes += scalar_saved_bytes;
812 }
813 return saved_bytes;
814 }
815
816 simdutf_warn_unused result implementation::convert_utf32_to_latin1_with_errors(
817 const char32_t *buf, size_t len, char *latin1_output) const noexcept {
818 std::pair<result, char *> ret =
819 lsx_convert_utf32_to_latin1_with_errors(buf, len, latin1_output);
820 if (ret.first.error) {
821 return ret.first;
822 } // Can return directly since scalar fallback already found correct
823 // ret.first.count
824 if (ret.first.count != len) { // All good so far, but not finished
825 result scalar_res = scalar::utf32_to_latin1::convert_with_errors(
826 buf + ret.first.count, len - ret.first.count, ret.second);
827 if (scalar_res.error) {
828 scalar_res.count += ret.first.count;
829 return scalar_res;
830 } else {
831 ret.second += scalar_res.count;
832 }
833 }
834 ret.first.count =
835 ret.second -
836 latin1_output; // Set count to the number of 8-bit code units written
837 return ret.first;
838 }
839
840 simdutf_warn_unused size_t implementation::convert_valid_utf32_to_latin1(
841 const char32_t *buf, size_t len, char *latin1_output) const noexcept {
842 std::pair<const char32_t *, char *> ret =
843 lsx_convert_utf32_to_latin1(buf, len, latin1_output);
844 if (ret.first == nullptr) {
845 return 0;
846 }
847 size_t saved_bytes = ret.second - latin1_output;
848
849 if (ret.first != buf + len) {
850 const size_t scalar_saved_bytes = scalar::utf32_to_latin1::convert_valid(
851 ret.first, len - (ret.first - buf), ret.second);
852 saved_bytes += scalar_saved_bytes;
853 }
854 return saved_bytes;
855 }
856
857 simdutf_warn_unused size_t implementation::convert_valid_utf32_to_utf8(
858 const char32_t *buf, size_t len, char *utf8_output) const noexcept {
859 // optimization opportunity: implement a custom function.
860 return convert_utf32_to_utf8(buf, len, utf8_output);
861 }
862
863 simdutf_warn_unused size_t implementation::convert_utf32_to_utf16le(
864 const char32_t *buf, size_t len, char16_t *utf16_output) const noexcept {
865 std::pair<const char32_t *, char16_t *> ret =
866 lsx_convert_utf32_to_utf16<endianness::LITTLE>(buf, len, utf16_output);
867 if (ret.first == nullptr) {
868 return 0;
869 }
870 size_t saved_bytes = ret.second - utf16_output;
871 if (ret.first != buf + len) {
872 const size_t scalar_saved_bytes =
873 scalar::utf32_to_utf16::convert<endianness::LITTLE>(
874 ret.first, len - (ret.first - buf), ret.second);
875 if (scalar_saved_bytes == 0) {
876 return 0;
877 }
878 saved_bytes += scalar_saved_bytes;
879 }
880
881 return saved_bytes;
882 }
883
884 simdutf_warn_unused size_t implementation::convert_utf32_to_utf16be(
885 const char32_t *buf, size_t len, char16_t *utf16_output) const noexcept {
886 std::pair<const char32_t *, char16_t *> ret =
887 lsx_convert_utf32_to_utf16<endianness::BIG>(buf, len, utf16_output);
888 if (ret.first == nullptr) {
889 return 0;
890 }
891 size_t saved_bytes = ret.second - utf16_output;
892 if (ret.first != buf + len) {
893 const size_t scalar_saved_bytes =
894 scalar::utf32_to_utf16::convert<endianness::BIG>(
895 ret.first, len - (ret.first - buf), ret.second);
896 if (scalar_saved_bytes == 0) {
897 return 0;
898 }
899 saved_bytes += scalar_saved_bytes;
900 }
901 return saved_bytes;
902 }
903
904 simdutf_warn_unused result implementation::convert_utf32_to_utf16le_with_errors(
905 const char32_t *buf, size_t len, char16_t *utf16_output) const noexcept {
906 // ret.first.count is always the position in the buffer, not the number of
907 // code units written even if finished
908 std::pair<result, char16_t *> ret =
909 lsx_convert_utf32_to_utf16_with_errors<endianness::LITTLE>(buf, len,
910 utf16_output);
911 if (ret.first.count != len) {
912 result scalar_res =
913 scalar::utf32_to_utf16::convert_with_errors<endianness::LITTLE>(
914 buf + ret.first.count, len - ret.first.count, ret.second);
915 if (scalar_res.error) {
916 scalar_res.count += ret.first.count;
917 return scalar_res;
918 } else {
919 ret.second += scalar_res.count;
920 }
921 }
922 ret.first.count =
923 ret.second -
924 utf16_output; // Set count to the number of 8-bit code units written
925 return ret.first;
926 }
927
928 simdutf_warn_unused result implementation::convert_utf32_to_utf16be_with_errors(
929 const char32_t *buf, size_t len, char16_t *utf16_output) const noexcept {
930 // ret.first.count is always the position in the buffer, not the number of
931 // code units written even if finished
932 std::pair<result, char16_t *> ret =
933 lsx_convert_utf32_to_utf16_with_errors<endianness::BIG>(buf, len,
934 utf16_output);
935 if (ret.first.count != len) {
936 result scalar_res =
937 scalar::utf32_to_utf16::convert_with_errors<endianness::BIG>(
938 buf + ret.first.count, len - ret.first.count, ret.second);
939 if (scalar_res.error) {
940 scalar_res.count += ret.first.count;
941 return scalar_res;
942 } else {
943 ret.second += scalar_res.count;
944 }
945 }
946 ret.first.count =
947 ret.second -
948 utf16_output; // Set count to the number of 8-bit code units written
949 return ret.first;
950 }
951
952 simdutf_warn_unused size_t implementation::convert_valid_utf32_to_utf16le(
953 const char32_t *buf, size_t len, char16_t *utf16_output) const noexcept {
954 return convert_utf32_to_utf16le(buf, len, utf16_output);
955 }
956
957 simdutf_warn_unused size_t implementation::convert_valid_utf32_to_utf16be(
958 const char32_t *buf, size_t len, char16_t *utf16_output) const noexcept {
959 return convert_utf32_to_utf16be(buf, len, utf16_output);
960 }
961
962 simdutf_warn_unused size_t implementation::convert_valid_utf16le_to_utf32(
963 const char16_t *buf, size_t len, char32_t *utf32_output) const noexcept {
964 return convert_utf16le_to_utf32(buf, len, utf32_output);
965 }
966
967 simdutf_warn_unused size_t implementation::convert_valid_utf16be_to_utf32(
968 const char16_t *buf, size_t len, char32_t *utf32_output) const noexcept {
969 return convert_utf16be_to_utf32(buf, len, utf32_output);
970 }
971
972 void implementation::change_endianness_utf16(const char16_t *input,
973 size_t length,
974 char16_t *output) const noexcept {
975 utf16::change_endianness_utf16(input, length, output);
976 }
977
978 simdutf_warn_unused size_t implementation::count_utf16le(
979 const char16_t *input, size_t length) const noexcept {
980 return utf16::count_code_points<endianness::LITTLE>(input, length);
981 }
982
983 simdutf_warn_unused size_t implementation::count_utf16be(
984 const char16_t *input, size_t length) const noexcept {
985 return utf16::count_code_points<endianness::BIG>(input, length);
986 }
987
988 simdutf_warn_unused size_t
989 implementation::count_utf8(const char *input, size_t length) const noexcept {
990 return utf8::count_code_points(input, length);
991 }
992
993 simdutf_warn_unused size_t implementation::latin1_length_from_utf8(
994 const char *buf, size_t len) const noexcept {
995 return count_utf8(buf, len);
996 }
997
998 simdutf_warn_unused size_t
999 implementation::latin1_length_from_utf16(size_t length) const noexcept {
1000 return length;
1001 }
1002
1003 simdutf_warn_unused size_t
1004 implementation::latin1_length_from_utf32(size_t length) const noexcept {
1005 return length;
1006 }
1007
1008 simdutf_warn_unused size_t implementation::utf8_length_from_latin1(
1009 const char *input, size_t length) const noexcept {
1010 const uint8_t *data = reinterpret_cast<const uint8_t *>(input);
1011 const uint8_t *data_end = data + length;
1012 uint64_t result = 0;
1013 while (data + 16 < data_end) {
1014 uint64_t two_bytes = 0;
1015 __m128i input_vec = __lsx_vld(data, 0);
1016 two_bytes =
1017 __lsx_vpickve2gr_hu(__lsx_vpcnt_h(__lsx_vmskltz_b(input_vec)), 0);
1018 result += 16 + two_bytes;
1019 data += 16;
1020 }
1021 return result + scalar::latin1::utf8_length_from_latin1((const char *)data,
1022 data_end - data);
1023 }
1024
1025 simdutf_warn_unused size_t implementation::utf8_length_from_utf16le(
1026 const char16_t *input, size_t length) const noexcept {
1027 return utf16::utf8_length_from_utf16<endianness::LITTLE>(input, length);
1028 }
1029
1030 simdutf_warn_unused size_t implementation::utf8_length_from_utf16be(
1031 const char16_t *input, size_t length) const noexcept {
1032 return utf16::utf8_length_from_utf16<endianness::BIG>(input, length);
1033 }
1034
1035 simdutf_warn_unused size_t
1036 implementation::utf16_length_from_latin1(size_t length) const noexcept {
1037 return length;
1038 }
1039
1040 simdutf_warn_unused size_t
1041 implementation::utf32_length_from_latin1(size_t length) const noexcept {
1042 return length;
1043 }
1044
1045 simdutf_warn_unused size_t implementation::utf32_length_from_utf16le(
1046 const char16_t *input, size_t length) const noexcept {
1047 return utf16::utf32_length_from_utf16<endianness::LITTLE>(input, length);
1048 }
1049
1050 simdutf_warn_unused size_t implementation::utf32_length_from_utf16be(
1051 const char16_t *input, size_t length) const noexcept {
1052 return utf16::utf32_length_from_utf16<endianness::BIG>(input, length);
1053 }
1054
1055 simdutf_warn_unused size_t implementation::utf16_length_from_utf8(
1056 const char *input, size_t length) const noexcept {
1057 return utf8::utf16_length_from_utf8(input, length);
1058 }
1059
1060 simdutf_warn_unused size_t implementation::utf8_length_from_utf32(
1061 const char32_t *input, size_t length) const noexcept {
1062 const __m128i v_80 = __lsx_vrepli_w(0x80); /*0x00000080*/
1063 const __m128i v_800 = __lsx_vldi(-3832); /*0x00000800*/
1064 const __m128i v_10000 = __lsx_vldi(-3583); /*0x00010000*/
1065 size_t pos = 0;
1066 size_t count = 0;
1067 for (; pos + 4 <= length; pos += 4) {
1068 __m128i in = __lsx_vld(reinterpret_cast<const uint32_t *>(input + pos), 0);
1069 const __m128i ascii_bytes_bytemask = __lsx_vslt_w(in, v_80);
1070 const __m128i one_two_bytes_bytemask = __lsx_vslt_w(in, v_800);
1071 const __m128i two_bytes_bytemask =
1072 __lsx_vxor_v(one_two_bytes_bytemask, ascii_bytes_bytemask);
1073 const __m128i three_bytes_bytemask =
1074 __lsx_vxor_v(__lsx_vslt_w(in, v_10000), one_two_bytes_bytemask);
1075
1076 const uint32_t ascii_bytes_count = __lsx_vpickve2gr_bu(
1077 __lsx_vpcnt_b(__lsx_vmskltz_w(ascii_bytes_bytemask)), 0);
1078 const uint32_t two_bytes_count = __lsx_vpickve2gr_bu(
1079 __lsx_vpcnt_b(__lsx_vmskltz_w(two_bytes_bytemask)), 0);
1080 const uint32_t three_bytes_count = __lsx_vpickve2gr_bu(
1081 __lsx_vpcnt_b(__lsx_vmskltz_w(three_bytes_bytemask)), 0);
1082
1083 count +=
1084 16 - 3 * ascii_bytes_count - 2 * two_bytes_count - three_bytes_count;
1085 }
1086 return count +
1087 scalar::utf32::utf8_length_from_utf32(input + pos, length - pos);
1088 }
1089
1090 simdutf_warn_unused size_t implementation::utf16_length_from_utf32(
1091 const char32_t *input, size_t length) const noexcept {
1092 const __m128i v_ffff = __lsx_vldi(-2304); /*0x0000ffff*/
1093 size_t pos = 0;
1094 size_t count = 0;
1095 for (; pos + 4 <= length; pos += 4) {
1096 __m128i in = __lsx_vld(reinterpret_cast<const uint32_t *>(input + pos), 0);
1097 const __m128i surrogate_bytemask = __lsx_vslt_wu(v_ffff, in);
1098 size_t surrogate_count = __lsx_vpickve2gr_bu(
1099 __lsx_vpcnt_b(__lsx_vmskltz_w(surrogate_bytemask)), 0);
1100 count += 4 + surrogate_count;
1101 }
1102 return count +
1103 scalar::utf32::utf16_length_from_utf32(input + pos, length - pos);
1104 }
1105
1106 simdutf_warn_unused size_t implementation::utf32_length_from_utf8(
1107 const char *input, size_t length) const noexcept {
1108 return utf8::count_code_points(input, length);
1109 }
1110
1111 simdutf_warn_unused size_t implementation::maximal_binary_length_from_base64(
1112 const char *input, size_t length) const noexcept {
1113 return scalar::base64::maximal_binary_length_from_base64(input, length);
1114 }
1115
1116 simdutf_warn_unused result implementation::base64_to_binary(
1117 const char *input, size_t length, char *output, base64_options options,
1118 last_chunk_handling_options last_chunk_options) const noexcept {
1119 return (options & base64_url)
1120 ? compress_decode_base64<true>(output, input, length, options,
1121 last_chunk_options)
1122 : compress_decode_base64<false>(output, input, length, options,
1123 last_chunk_options);
1124 }
1125
1126 simdutf_warn_unused full_result implementation::base64_to_binary_details(
1127 const char *input, size_t length, char *output, base64_options options,
1128 last_chunk_handling_options last_chunk_options) const noexcept {
1129 return (options & base64_url)
1130 ? compress_decode_base64<true>(output, input, length, options,
1131 last_chunk_options)
1132 : compress_decode_base64<false>(output, input, length, options,
1133 last_chunk_options);
1134 }
1135
1136 simdutf_warn_unused size_t implementation::maximal_binary_length_from_base64(
1137 const char16_t *input, size_t length) const noexcept {
1138 return scalar::base64::maximal_binary_length_from_base64(input, length);
1139 }
1140
1141 simdutf_warn_unused result implementation::base64_to_binary(
1142 const char16_t *input, size_t length, char *output, base64_options options,
1143 last_chunk_handling_options last_chunk_options) const noexcept {
1144 return (options & base64_url)
1145 ? compress_decode_base64<true>(output, input, length, options,
1146 last_chunk_options)
1147 : compress_decode_base64<false>(output, input, length, options,
1148 last_chunk_options);
1149 }
1150
1151 simdutf_warn_unused full_result implementation::base64_to_binary_details(
1152 const char16_t *input, size_t length, char *output, base64_options options,
1153 last_chunk_handling_options last_chunk_options) const noexcept {
1154 return (options & base64_url)
1155 ? compress_decode_base64<true>(output, input, length, options,
1156 last_chunk_options)
1157 : compress_decode_base64<false>(output, input, length, options,
1158 last_chunk_options);
1159 }
1160
1161 simdutf_warn_unused size_t implementation::base64_length_from_binary(
1162 size_t length, base64_options options) const noexcept {
1163 return scalar::base64::base64_length_from_binary(length, options);
1164 }
1165
1166 size_t implementation::binary_to_base64(const char *input, size_t length,
1167 char *output,
1168 base64_options options) const noexcept {
1169 if (options & base64_url) {
1170 return encode_base64<true>(output, input, length, options);
1171 } else {
1172 return encode_base64<false>(output, input, length, options);
1173 }
1174 }
1175 } // namespace SIMDUTF_IMPLEMENTATION
1176 } // namespace simdutf
1177
1178 #include "simdutf/lsx/end.h"