Debian Codegraph @

contrib/simdutf/src/lsx/lsx_convert_utf16_to_latin1.cpp

raw
1 template <endianness big_endian>
2 std::pair<const char16_t *, char *>
3 lsx_convert_utf16_to_latin1(const char16_t *buf, size_t len,
4 char *latin1_output) {
5 const char16_t *end = buf + len;
6 while (buf + 16 <= end) {
7 __m128i in = __lsx_vld(reinterpret_cast<const uint16_t *>(buf), 0);
8 __m128i in1 = __lsx_vld(reinterpret_cast<const uint16_t *>(buf), 16);
9 if (!match_system(big_endian)) {
10 in = lsx_swap_bytes(in);
11 in1 = lsx_swap_bytes(in1);
12 }
13 if (__lsx_bz_v(__lsx_vpickod_b(in1, in))) {
14 // 1. pack the bytes
15 __m128i latin1_packed = __lsx_vpickev_b(in1, in);
16 // 2. store (8 bytes)
17 __lsx_vst(latin1_packed, reinterpret_cast<uint8_t *>(latin1_output), 0);
18 // 3. adjust pointers
19 buf += 16;
20 latin1_output += 16;
21 } else {
22 return std::make_pair(nullptr, reinterpret_cast<char *>(latin1_output));
23 }
24 } // while
25 return std::make_pair(buf, latin1_output);
26 }
27
28 template <endianness big_endian>
29 std::pair<result, char *>
30 lsx_convert_utf16_to_latin1_with_errors(const char16_t *buf, size_t len,
31 char *latin1_output) {
32 const char16_t *start = buf;
33 const char16_t *end = buf + len;
34 while (buf + 16 <= end) {
35 __m128i in = __lsx_vld(reinterpret_cast<const uint16_t *>(buf), 0);
36 __m128i in1 = __lsx_vld(reinterpret_cast<const uint16_t *>(buf), 16);
37 if (!match_system(big_endian)) {
38 in = lsx_swap_bytes(in);
39 in1 = lsx_swap_bytes(in1);
40 }
41 if (__lsx_bz_v(__lsx_vpickod_b(in1, in))) {
42 // 1. pack the bytes
43 __m128i latin1_packed = __lsx_vpickev_b(in1, in);
44 // 2. store (8 bytes)
45 __lsx_vst(latin1_packed, reinterpret_cast<uint8_t *>(latin1_output), 0);
46 // 3. adjust pointers
47 buf += 16;
48 latin1_output += 16;
49 } else {
50 // Let us do a scalar fallback.
51 for (int k = 0; k < 16; k++) {
52 uint16_t word = !match_system(big_endian)
53 ? scalar::utf16::swap_bytes(buf[k])
54 : buf[k];
55 if (word <= 0xff) {
56 *latin1_output++ = char(word);
57 } else {
58 return std::make_pair(result(error_code::TOO_LARGE, buf - start + k),
59 latin1_output);
60 }
61 }
62 }
63 } // while
64 return std::make_pair(result(error_code::SUCCESS, buf - start),
65 latin1_output);
66 }