Debian Codegraph @

contrib/simdutf/src/lsx/lsx_convert_utf32_to_utf16.cpp

raw
1 template <endianness big_endian>
2 std::pair<const char32_t *, char16_t *>
3 lsx_convert_utf32_to_utf16(const char32_t *buf, size_t len,
4 char16_t *utf16_out) {
5 uint16_t *utf16_output = reinterpret_cast<uint16_t *>(utf16_out);
6 const char32_t *end = buf + len;
7
8 __m128i forbidden_bytemask = __lsx_vrepli_h(0);
9 __m128i v_d800 = __lsx_vldi(-2600); /*0xD800*/
10 __m128i v_dfff = __lsx_vreplgr2vr_h(uint16_t(0xdfff));
11 while (buf + 8 <= end) {
12 __m128i in0 = __lsx_vld(reinterpret_cast<const uint32_t *>(buf), 0);
13 __m128i in1 = __lsx_vld(reinterpret_cast<const uint32_t *>(buf), 16);
14
15 // Check if no bits set above 16th
16 if (__lsx_bz_v(__lsx_vpickod_h(in1, in0))) {
17 __m128i utf16_packed = __lsx_vpickev_h(in1, in0);
18 forbidden_bytemask = __lsx_vor_v(
19 __lsx_vand_v(
20 __lsx_vsle_h(utf16_packed, v_dfff), // utf16_packed <= 0xdfff
21 __lsx_vsle_h(v_d800, utf16_packed)), // utf16_packed >= 0xd800
22 forbidden_bytemask);
23
24 if (!match_system(big_endian)) {
25 utf16_packed = lsx_swap_bytes(utf16_packed);
26 }
27 __lsx_vst(utf16_packed, utf16_output, 0);
28 utf16_output += 8;
29 buf += 8;
30 } else {
31 size_t forward = 3;
32 size_t k = 0;
33 if (size_t(end - buf) < forward + 1) {
34 forward = size_t(end - buf - 1);
35 }
36 for (; k < forward; k++) {
37 uint32_t word = buf[k];
38 if ((word & 0xFFFF0000) == 0) {
39 // will not generate a surrogate pair
40 if (word >= 0xD800 && word <= 0xDFFF) {
41 return std::make_pair(nullptr,
42 reinterpret_cast<char16_t *>(utf16_output));
43 }
44 *utf16_output++ = !match_system(big_endian)
45 ? char16_t(word >> 8 | word << 8)
46 : char16_t(word);
47 } else {
48 // will generate a surrogate pair
49 if (word > 0x10FFFF) {
50 return std::make_pair(nullptr,
51 reinterpret_cast<char16_t *>(utf16_output));
52 }
53 word -= 0x10000;
54 uint16_t high_surrogate = uint16_t(0xD800 + (word >> 10));
55 uint16_t low_surrogate = uint16_t(0xDC00 + (word & 0x3FF));
56 if (!match_system(big_endian)) {
57 high_surrogate =
58 uint16_t(high_surrogate >> 8 | high_surrogate << 8);
59 low_surrogate = uint16_t(low_surrogate << 8 | low_surrogate >> 8);
60 }
61 *utf16_output++ = char16_t(high_surrogate);
62 *utf16_output++ = char16_t(low_surrogate);
63 }
64 }
65 buf += k;
66 }
67 }
68
69 // check for invalid input
70 if (__lsx_bnz_v(forbidden_bytemask)) {
71 return std::make_pair(nullptr, reinterpret_cast<char16_t *>(utf16_output));
72 }
73 return std::make_pair(buf, reinterpret_cast<char16_t *>(utf16_output));
74 }
75
76 template <endianness big_endian>
77 std::pair<result, char16_t *>
78 lsx_convert_utf32_to_utf16_with_errors(const char32_t *buf, size_t len,
79 char16_t *utf16_out) {
80 uint16_t *utf16_output = reinterpret_cast<uint16_t *>(utf16_out);
81 const char32_t *start = buf;
82 const char32_t *end = buf + len;
83
84 __m128i forbidden_bytemask = __lsx_vrepli_h(0);
85 __m128i v_d800 = __lsx_vldi(-2600); /*0xD800*/
86 __m128i v_dfff = __lsx_vreplgr2vr_h(uint16_t(0xdfff));
87
88 while (buf + 8 <= end) {
89 __m128i in0 = __lsx_vld(reinterpret_cast<const uint32_t *>(buf), 0);
90 __m128i in1 = __lsx_vld(reinterpret_cast<const uint32_t *>(buf), 16);
91 // Check if no bits set above 16th
92 if (__lsx_bz_v(__lsx_vpickod_h(in1, in0))) {
93 __m128i utf16_packed = __lsx_vpickev_h(in1, in0);
94
95 forbidden_bytemask = __lsx_vor_v(
96 __lsx_vand_v(
97 __lsx_vsle_h(utf16_packed, v_dfff), // utf16_packed <= 0xdfff
98 __lsx_vsle_h(v_d800, utf16_packed)), // utf16_packed >= 0xd800
99 forbidden_bytemask);
100 if (__lsx_bnz_v(forbidden_bytemask)) {
101 return std::make_pair(result(error_code::SURROGATE, buf - start),
102 reinterpret_cast<char16_t *>(utf16_output));
103 }
104
105 if (!match_system(big_endian)) {
106 utf16_packed = lsx_swap_bytes(utf16_packed);
107 }
108
109 __lsx_vst(utf16_packed, utf16_output, 0);
110 utf16_output += 8;
111 buf += 8;
112 } else {
113 size_t forward = 3;
114 size_t k = 0;
115 if (size_t(end - buf) < forward + 1) {
116 forward = size_t(end - buf - 1);
117 }
118 for (; k < forward; k++) {
119 uint32_t word = buf[k];
120 if ((word & 0xFFFF0000) == 0) {
121 // will not generate a surrogate pair
122 if (word >= 0xD800 && word <= 0xDFFF) {
123 return std::make_pair(
124 result(error_code::SURROGATE, buf - start + k),
125 reinterpret_cast<char16_t *>(utf16_output));
126 }
127 *utf16_output++ = !match_system(big_endian)
128 ? char16_t(word >> 8 | word << 8)
129 : char16_t(word);
130 } else {
131 // will generate a surrogate pair
132 if (word > 0x10FFFF) {
133 return std::make_pair(
134 result(error_code::TOO_LARGE, buf - start + k),
135 reinterpret_cast<char16_t *>(utf16_output));
136 }
137 word -= 0x10000;
138 uint16_t high_surrogate = uint16_t(0xD800 + (word >> 10));
139 uint16_t low_surrogate = uint16_t(0xDC00 + (word & 0x3FF));
140 if (!match_system(big_endian)) {
141 high_surrogate =
142 uint16_t(high_surrogate >> 8 | high_surrogate << 8);
143 low_surrogate = uint16_t(low_surrogate << 8 | low_surrogate >> 8);
144 }
145 *utf16_output++ = char16_t(high_surrogate);
146 *utf16_output++ = char16_t(low_surrogate);
147 }
148 }
149 buf += k;
150 }
151 }
152
153 return std::make_pair(result(error_code::SUCCESS, buf - start),
154 reinterpret_cast<char16_t *>(utf16_output));
155 }