Debian Codegraph @

contrib/simdutf/src/lsx/lsx_validate_utf16.cpp

raw
1 /*
2 In UTF-16 code units in range 0xD800 to 0xDFFF have special meaning.
3
4 In a vectorized algorithm we want to examine the most significant
5 nibble in order to select a fast path. If none of highest nibbles
6 are 0xD (13), than we are sure that UTF-16 chunk in a vector
7 register is valid.
8
9 Let us analyze what we need to check if the nibble is 0xD. The
10 value of the preceding nibble determines what we have:
11
12 0xd000 .. 0xd7ff - a valid word
13 0xd800 .. 0xdbff - low surrogate
14 0xdc00 .. 0xdfff - high surrogate
15
16 Other constraints we have to consider:
17 - there must not be two consecutive low surrogates (0xd800 .. 0xdbff)
18 - there must not be two consecutive high surrogates (0xdc00 .. 0xdfff)
19 - there must not be sole low surrogate nor high surrogate
20
21 We're going to build three bitmasks based on the 3rd nibble:
22 - V = valid word,
23 - L = low surrogate (0xd800 .. 0xdbff)
24 - H = high surrogate (0xdc00 .. 0xdfff)
25
26 0 1 2 3 4 5 6 7 <--- word index
27 [ V | L | H | L | H | V | V | L ]
28 1 0 0 0 0 1 1 0 - V = valid masks
29 0 1 0 1 0 0 0 1 - L = low surrogate
30 0 0 1 0 1 0 0 0 - H high surrogate
31
32
33 1 0 0 0 0 1 1 0 V = valid masks
34 0 1 0 1 0 0 0 0 a = L & (H >> 1)
35 0 0 1 0 1 0 0 0 b = a << 1
36 1 1 1 1 1 1 1 0 c = V | a | b
37 ^
38 the last bit can be zero, we just consume 7
39 code units and recheck this word in the next iteration
40 */
41
42 /* Returns:
43 - pointer to the last unprocessed character (a scalar fallback should check
44 the rest);
45 - nullptr if an error was detected.
46 */
47 template <endianness big_endian>
48 const char16_t *lsx_validate_utf16(const char16_t *input, size_t size) {
49 const char16_t *end = input + size;
50
51 const auto v_d8 = simd8<uint8_t>::splat(0xd8);
52 const auto v_f8 = simd8<uint8_t>::splat(0xf8);
53 const auto v_fc = simd8<uint8_t>::splat(0xfc);
54 const auto v_dc = simd8<uint8_t>::splat(0xdc);
55
56 while (input + simd16<uint16_t>::SIZE * 2 < end) {
57 // 0. Load data: since the validation takes into account only higher
58 // byte of each word, we compress the two vectors into one which
59 // consists only the higher bytes.
60 auto in0 = simd16<uint16_t>(input);
61 auto in1 =
62 simd16<uint16_t>(input + simd16<uint16_t>::SIZE / sizeof(char16_t));
63 if (big_endian) {
64 in0 = in0.swap_bytes();
65 in1 = in1.swap_bytes();
66 }
67 const auto in = simd8<uint8_t>(__lsx_vssrlni_bu_h(in1.value, in0.value, 8));
68
69 // 1. Check whether we have any 0xD800..DFFF word (0b1101'1xxx'yyyy'yyyy).
70 const auto surrogates_wordmask = (in & v_f8) == v_d8;
71 const uint16_t surrogates_bitmask =
72 static_cast<uint16_t>(surrogates_wordmask.to_bitmask());
73 if (surrogates_bitmask == 0x0000) {
74 input += 16;
75 } else {
76 // 2. We have some surrogates that have to be distinguished:
77 // - low surrogates: 0b1101'10xx'yyyy'yyyy (0xD800..0xDBFF)
78 // - high surrogates: 0b1101'11xx'yyyy'yyyy (0xDC00..0xDFFF)
79 //
80 // Fact: high surrogate has 11th bit set (3rd bit in the higher word)
81
82 // V - non-surrogate code units
83 // V = not surrogates_wordmask
84 const uint16_t V = static_cast<uint16_t>(~surrogates_bitmask);
85
86 // H - word-mask for high surrogates: the six highest bits are 0b1101'11
87 const auto vH = (in & v_fc) == v_dc;
88 const uint16_t H = static_cast<uint16_t>(vH.to_bitmask());
89
90 // L - word mask for low surrogates
91 // L = not H and surrogates_wordmask
92 const uint16_t L = static_cast<uint16_t>(~H & surrogates_bitmask);
93
94 const uint16_t a = static_cast<uint16_t>(
95 L & (H >> 1)); // A low surrogate must be followed by high one.
96 // (A low surrogate placed in the 7th register's word
97 // is an exception we handle.)
98 const uint16_t b = static_cast<uint16_t>(
99 a << 1); // Just mark that the opinput - startite fact is hold,
100 // thanks to that we have only two masks for valid case.
101 const uint16_t c = static_cast<uint16_t>(
102 V | a | b); // Combine all the masks into the final one.
103
104 if (c == 0xffff) {
105 // The whole input register contains valid UTF-16, i.e.,
106 // either single code units or proper surrogate pairs.
107 input += 16;
108 } else if (c == 0x7fff) {
109 // The 15 lower code units of the input register contains valid UTF-16.
110 // The 15th word may be either a low or high surrogate. It the next
111 // iteration we 1) check if the low surrogate is followed by a high
112 // one, 2) reject sole high surrogate.
113 input += 15;
114 } else {
115 return nullptr;
116 }
117 }
118 }
119
120 return input;
121 }
122
123 template <endianness big_endian>
124 const result lsx_validate_utf16_with_errors(const char16_t *input,
125 size_t size) {
126 const char16_t *start = input;
127 const char16_t *end = input + size;
128
129 const auto v_d8 = simd8<uint8_t>::splat(0xd8);
130 const auto v_f8 = simd8<uint8_t>::splat(0xf8);
131 const auto v_fc = simd8<uint8_t>::splat(0xfc);
132 const auto v_dc = simd8<uint8_t>::splat(0xdc);
133
134 while (input + simd16<uint16_t>::SIZE * 2 < end) {
135 // 0. Load data: since the validation takes into account only higher
136 // byte of each word, we compress the two vectors into one which
137 // consists only the higher bytes.
138 auto in0 = simd16<uint16_t>(input);
139 auto in1 =
140 simd16<uint16_t>(input + simd16<uint16_t>::SIZE / sizeof(char16_t));
141
142 if (big_endian) {
143 in0 = in0.swap_bytes();
144 in1 = in1.swap_bytes();
145 }
146
147 const auto in = simd8<uint8_t>(__lsx_vssrlni_bu_h(in1.value, in0.value, 8));
148
149 // 1. Check whether we have any 0xD800..DFFF word (0b1101'1xxx'yyyy'yyyy).
150 const auto surrogates_wordmask = (in & v_f8) == v_d8;
151 const uint16_t surrogates_bitmask =
152 static_cast<uint16_t>(surrogates_wordmask.to_bitmask());
153 if (surrogates_bitmask == 0x0000) {
154 input += 16;
155 } else {
156 // 2. We have some surrogates that have to be distinguished:
157 // - low surrogates: 0b1101'10xx'yyyy'yyyy (0xD800..0xDBFF)
158 // - high surrogates: 0b1101'11xx'yyyy'yyyy (0xDC00..0xDFFF)
159 //
160 // Fact: high surrogate has 11th bit set (3rd bit in the higher word)
161
162 // V - non-surrogate code units
163 // V = not surrogates_wordmask
164 const uint16_t V = static_cast<uint16_t>(~surrogates_bitmask);
165
166 // H - word-mask for high surrogates: the six highest bits are 0b1101'11
167 const auto vH = (in & v_fc) == v_dc;
168 const uint16_t H = static_cast<uint16_t>(vH.to_bitmask());
169
170 // L - word mask for low surrogates
171 // L = not H and surrogates_wordmask
172 const uint16_t L = static_cast<uint16_t>(~H & surrogates_bitmask);
173
174 const uint16_t a = static_cast<uint16_t>(
175 L & (H >> 1)); // A low surrogate must be followed by high one.
176 // (A low surrogate placed in the 7th register's word
177 // is an exception we handle.)
178 const uint16_t b = static_cast<uint16_t>(
179 a << 1); // Just mark that the opinput - startite fact is hold,
180 // thanks to that we have only two masks for valid case.
181 const uint16_t c = static_cast<uint16_t>(
182 V | a | b); // Combine all the masks into the final one.
183
184 if (c == 0xffff) {
185 // The whole input register contains valid UTF-16, i.e.,
186 // either single code units or proper surrogate pairs.
187 input += 16;
188 } else if (c == 0x7fff) {
189 // The 15 lower code units of the input register contains valid UTF-16.
190 // The 15th word may be either a low or high surrogate. It the next
191 // iteration we 1) check if the low surrogate is followed by a high
192 // one, 2) reject sole high surrogate.
193 input += 15;
194 } else {
195 return result(error_code::SURROGATE, input - start);
196 }
197 }
198 }
199
200 return result(error_code::SUCCESS, input - start);
201 }