Debian Codegraph @

contrib/simdutf/src/lsx/lsx_convert_utf16_to_utf32.cpp

raw
1 template <endianness big_endian>
2 std::pair<const char16_t *, char32_t *>
3 lsx_convert_utf16_to_utf32(const char16_t *buf, size_t len,
4 char32_t *utf32_out) {
5 uint32_t *utf32_output = reinterpret_cast<uint32_t *>(utf32_out);
6 const char16_t *end = buf + len;
7
8 __m128i zero = __lsx_vldi(0);
9 __m128i v_f800 = __lsx_vldi(-2568); /*0xF800*/
10 __m128i v_d800 = __lsx_vldi(-2600); /*0xD800*/
11
12 while (buf + 8 <= end) {
13 __m128i in = __lsx_vld(reinterpret_cast<const uint16_t *>(buf), 0);
14 if (!match_system(big_endian)) {
15 in = lsx_swap_bytes(in);
16 }
17
18 __m128i surrogates_bytemask =
19 __lsx_vseq_h(__lsx_vand_v(in, v_f800), v_d800);
20 // It might seem like checking for surrogates_bitmask == 0xc000 could help.
21 // However, it is likely an uncommon occurrence.
22 if (__lsx_bz_v(surrogates_bytemask)) {
23 // case: no surrogate pairs, extend all 16-bit code units to 32-bit code
24 // units
25 __lsx_vst(__lsx_vilvl_h(zero, in), utf32_output, 0);
26 __lsx_vst(__lsx_vilvh_h(zero, in), utf32_output, 16);
27 utf32_output += 8;
28 buf += 8;
29 // surrogate pair(s) in a register
30 } else {
31 // Let us do a scalar fallback.
32 // It may seem wasteful to use scalar code, but being efficient with SIMD
33 // in the presence of surrogate pairs may require non-trivial tables.
34 size_t forward = 15;
35 size_t k = 0;
36 if (size_t(end - buf) < forward + 1) {
37 forward = size_t(end - buf - 1);
38 }
39 for (; k < forward; k++) {
40 uint16_t word = !match_system(big_endian)
41 ? scalar::utf16::swap_bytes(buf[k])
42 : buf[k];
43 if ((word & 0xF800) != 0xD800) {
44 *utf32_output++ = char32_t(word);
45 } else {
46 // must be a surrogate pair
47 uint16_t diff = uint16_t(word - 0xD800);
48 uint16_t next_word = !match_system(big_endian)
49 ? scalar::utf16::swap_bytes(buf[k + 1])
50 : buf[k + 1];
51 k++;
52 uint16_t diff2 = uint16_t(next_word - 0xDC00);
53 if ((diff | diff2) > 0x3FF) {
54 return std::make_pair(nullptr,
55 reinterpret_cast<char32_t *>(utf32_output));
56 }
57 uint32_t value = (diff << 10) + diff2 + 0x10000;
58 *utf32_output++ = char32_t(value);
59 }
60 }
61 buf += k;
62 }
63 } // while
64 return std::make_pair(buf, reinterpret_cast<char32_t *>(utf32_output));
65 }
66
67 /*
68 Returns a pair: a result struct and utf8_output.
69 If there is an error, the count field of the result is the position of the
70 error. Otherwise, it is the position of the first unprocessed byte in buf
71 (even if finished). A scalar routing should carry on the conversion of the
72 tail if needed.
73 */
74 template <endianness big_endian>
75 std::pair<result, char32_t *>
76 lsx_convert_utf16_to_utf32_with_errors(const char16_t *buf, size_t len,
77 char32_t *utf32_out) {
78 uint32_t *utf32_output = reinterpret_cast<uint32_t *>(utf32_out);
79 const char16_t *start = buf;
80 const char16_t *end = buf + len;
81
82 __m128i zero = __lsx_vldi(0);
83 __m128i v_f800 = __lsx_vldi(-2568); /*0xF800*/
84 __m128i v_d800 = __lsx_vldi(-2600); /*0xD800*/
85
86 while (buf + 8 <= end) {
87 __m128i in = __lsx_vld(reinterpret_cast<const uint16_t *>(buf), 0);
88 if (!match_system(big_endian)) {
89 in = lsx_swap_bytes(in);
90 }
91
92 __m128i surrogates_bytemask =
93 __lsx_vseq_h(__lsx_vand_v(in, v_f800), v_d800);
94 if (__lsx_bz_v(surrogates_bytemask)) {
95 // case: no surrogate pairs, extend all 16-bit code units to 32-bit code
96 // units
97 __lsx_vst(__lsx_vilvl_h(zero, in), utf32_output, 0);
98 __lsx_vst(__lsx_vilvh_h(zero, in), utf32_output, 16);
99 utf32_output += 8;
100 buf += 8;
101 // surrogate pair(s) in a register
102 } else {
103 // Let us do a scalar fallback.
104 // It may seem wasteful to use scalar code, but being efficient with SIMD
105 // in the presence of surrogate pairs may require non-trivial tables.
106 size_t forward = 15;
107 size_t k = 0;
108 if (size_t(end - buf) < forward + 1) {
109 forward = size_t(end - buf - 1);
110 }
111 for (; k < forward; k++) {
112 uint16_t word = !match_system(big_endian)
113 ? scalar::utf16::swap_bytes(buf[k])
114 : buf[k];
115 if ((word & 0xF800) != 0xD800) {
116 *utf32_output++ = char32_t(word);
117 } else {
118 // must be a surrogate pair
119 uint16_t diff = uint16_t(word - 0xD800);
120 uint16_t next_word = !match_system(big_endian)
121 ? scalar::utf16::swap_bytes(buf[k + 1])
122 : buf[k + 1];
123 k++;
124 uint16_t diff2 = uint16_t(next_word - 0xDC00);
125 if ((diff | diff2) > 0x3FF) {
126 return std::make_pair(
127 result(error_code::SURROGATE, buf - start + k - 1),
128 reinterpret_cast<char32_t *>(utf32_output));
129 }
130 uint32_t value = (diff << 10) + diff2 + 0x10000;
131 *utf32_output++ = char32_t(value);
132 }
133 }
134 buf += k;
135 }
136 } // while
137 return std::make_pair(result(error_code::SUCCESS, buf - start),
138 reinterpret_cast<char32_t *>(utf32_output));
139 }