| 1 |
size_t convert_masked_utf8_to_latin1(const char *input, |
| 2 |
uint64_t utf8_end_of_code_point_mask, |
| 3 |
char *&latin1_output) { |
| 4 |
// we use an approach where we try to process up to 12 input bytes. |
| 5 |
// Why 12 input bytes and not 16? Because we are concerned with the size of |
| 6 |
// the lookup tables. Also 12 is nicely divisible by two and three. |
| 7 |
// |
| 8 |
__m128i in = __lsx_vld(reinterpret_cast<const uint8_t *>(input), 0); |
| 9 |
|
| 10 |
const uint16_t input_utf8_end_of_code_point_mask = |
| 11 |
utf8_end_of_code_point_mask & 0xfff; |
| 12 |
// Optimization note: our main path below is load-latency dependent. Thus it |
| 13 |
// is maybe beneficial to have fast paths that depend on branch prediction but |
| 14 |
// have less latency. This results in more instructions but, potentially, also |
| 15 |
// higher speeds. |
| 16 |
|
| 17 |
// We first try a few fast paths. |
| 18 |
// The obvious first test is ASCII, which actually consumes the full 16. |
| 19 |
if ((utf8_end_of_code_point_mask & 0xFFFF) == 0xFFFF) { |
| 20 |
// We process in chunks of 16 bytes |
| 21 |
__lsx_vst(in, reinterpret_cast<uint8_t *>(latin1_output), 0); |
| 22 |
latin1_output += 16; // We wrote 16 18-bit characters. |
| 23 |
return 16; // We consumed 16 bytes. |
| 24 |
} |
| 25 |
/// We do not have a fast path available, or the fast path is unimportant, so |
| 26 |
/// we fallback. |
| 27 |
const uint8_t idx = simdutf::tables::utf8_to_utf16::utf8bigindex |
| 28 |
[input_utf8_end_of_code_point_mask][0]; |
| 29 |
|
| 30 |
const uint8_t consumed = simdutf::tables::utf8_to_utf16::utf8bigindex |
| 31 |
[input_utf8_end_of_code_point_mask][1]; |
| 32 |
// this indicates an invalid input: |
| 33 |
if (idx >= 64) { |
| 34 |
return consumed; |
| 35 |
} |
| 36 |
// Here we should have (idx < 64), if not, there is a bug in the validation or |
| 37 |
// elsewhere. SIX (6) input code-code units this is a relatively easy scenario |
| 38 |
// we process SIX (6) input code-code units. The max length in bytes of six |
| 39 |
// code code units spanning between 1 and 2 bytes each is 12 bytes. Converts 6 |
| 40 |
// 1-2 byte UTF-8 characters to 6 UTF-16 characters. This is a relatively easy |
| 41 |
// scenario we process SIX (6) input code-code units. The max length in bytes |
| 42 |
// of six code code units spanning between 1 and 2 bytes each is 12 bytes. |
| 43 |
__m128i sh = __lsx_vld(reinterpret_cast<const uint8_t *>( |
| 44 |
simdutf::tables::utf8_to_utf16::shufutf8[idx]), |
| 45 |
0); |
| 46 |
// Shuffle |
| 47 |
// 1 byte: 00000000 0bbbbbbb |
| 48 |
// 2 byte: 110aaaaa 10bbbbbb |
| 49 |
sh = __lsx_vand_v(sh, __lsx_vldi(0x1f)); |
| 50 |
__m128i perm = __lsx_vshuf_b(__lsx_vldi(0), in, sh); |
| 51 |
// ascii mask |
| 52 |
// 1 byte: 11111111 11111111 |
| 53 |
// 2 byte: 00000000 00000000 |
| 54 |
__m128i ascii_mask = __lsx_vslt_bu(perm, __lsx_vldi(0x80)); |
| 55 |
// utf8 mask |
| 56 |
// 1 byte: 00000000 00000000 |
| 57 |
// 2 byte: 00111111 00111111 |
| 58 |
__m128i utf8_mask = __lsx_vand_v(__lsx_vsle_bu(__lsx_vldi(0x80), perm), |
| 59 |
__lsx_vldi(0b00111111)); |
| 60 |
// mask |
| 61 |
// 1 byte: 11111111 11111111 |
| 62 |
// 2 byte: 00111111 00111111 |
| 63 |
__m128i mask = __lsx_vor_v(utf8_mask, ascii_mask); |
| 64 |
|
| 65 |
__m128i composed = __lsx_vbitsel_v(__lsx_vsrli_h(perm, 2), perm, mask); |
| 66 |
// writing 8 bytes even though we only care about the first 6 bytes. |
| 67 |
__m128i latin1_packed = __lsx_vpickev_b(__lsx_vldi(0), composed); |
| 68 |
|
| 69 |
uint64_t buffer[2]; |
| 70 |
// __lsx_vst(latin1_packed, reinterpret_cast<uint8_t *>(latin1_output), 0); |
| 71 |
__lsx_vst(latin1_packed, reinterpret_cast<uint8_t *>(buffer), 0); |
| 72 |
std::memcpy(latin1_output, buffer, 6); |
| 73 |
latin1_output += 6; // We wrote 6 bytes. |
| 74 |
return consumed; |
| 75 |
} |