| 1 |
std::pair<const char32_t *, char *> |
| 2 |
lsx_convert_utf32_to_latin1(const char32_t *buf, size_t len, |
| 3 |
char *latin1_output) { |
| 4 |
const char32_t *end = buf + len; |
| 5 |
const v16u8 shuf_mask = {0, 4, 8, 12, 16, 20, 24, 28, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 6 |
__m128i v_ff = __lsx_vrepli_w(0xFF); |
| 7 |
|
| 8 |
while (buf + 16 <= end) { |
| 9 |
__m128i in1 = __lsx_vld(reinterpret_cast<const uint32_t *>(buf), 0); |
| 10 |
__m128i in2 = __lsx_vld(reinterpret_cast<const uint32_t *>(buf), 16); |
| 11 |
|
| 12 |
__m128i in12 = __lsx_vor_v(in1, in2); |
| 13 |
if (__lsx_bz_v(__lsx_vslt_wu(v_ff, in12))) { |
| 14 |
// 1. pack the bytes |
| 15 |
__m128i latin1_packed = __lsx_vshuf_b(in2, in1, (__m128i)shuf_mask); |
| 16 |
// 2. store (8 bytes) |
| 17 |
__lsx_vst(latin1_packed, reinterpret_cast<uint8_t *>(latin1_output), 0); |
| 18 |
// 3. adjust pointers |
| 19 |
buf += 8; |
| 20 |
latin1_output += 8; |
| 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 |
std::pair<result, char *> |
| 29 |
lsx_convert_utf32_to_latin1_with_errors(const char32_t *buf, size_t len, |
| 30 |
char *latin1_output) { |
| 31 |
const char32_t *start = buf; |
| 32 |
const char32_t *end = buf + len; |
| 33 |
|
| 34 |
const v16u8 shuf_mask = {0, 4, 8, 12, 16, 20, 24, 28, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 35 |
__m128i v_ff = __lsx_vrepli_w(0xFF); |
| 36 |
|
| 37 |
while (buf + 16 <= end) { |
| 38 |
__m128i in1 = __lsx_vld(reinterpret_cast<const uint32_t *>(buf), 0); |
| 39 |
__m128i in2 = __lsx_vld(reinterpret_cast<const uint32_t *>(buf), 16); |
| 40 |
|
| 41 |
__m128i in12 = __lsx_vor_v(in1, in2); |
| 42 |
|
| 43 |
if (__lsx_bz_v(__lsx_vslt_wu(v_ff, in12))) { |
| 44 |
// 1. pack the bytes |
| 45 |
__m128i latin1_packed = __lsx_vshuf_b(in2, in1, (__m128i)shuf_mask); |
| 46 |
// 2. store (8 bytes) |
| 47 |
__lsx_vst(latin1_packed, reinterpret_cast<uint8_t *>(latin1_output), 0); |
| 48 |
// 3. adjust pointers |
| 49 |
buf += 8; |
| 50 |
latin1_output += 8; |
| 51 |
} else { |
| 52 |
// Let us do a scalar fallback. |
| 53 |
for (int k = 0; k < 8; k++) { |
| 54 |
uint32_t word = 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 |
} |