2020-06-28 03:10:36 +02:00
|
|
|
// The Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) The Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2020 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
2020-06-28 04:59:41 +02:00
|
|
|
//
|
|
|
|
// utf16
|
|
|
|
//
|
|
|
|
|
2020-07-03 01:39:14 +02:00
|
|
|
/// Decodes one or two escaped surrogates (surrogate pair) aligned to the
|
|
|
|
/// front of the input block. If the surrogates are a pair which decode into
|
|
|
|
/// a single codepoint, only the first element of the return vector is used;
|
|
|
|
/// otherwise each surrogate decodes into each element. Three surrogates
|
|
|
|
/// cannot be decoded at once, so the last two elements are never used.
|
|
|
|
ircd::u32x4
|
|
|
|
ircd::utf16::decode_surrogate_aligned_next(const u8x16 input)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
const u8x16 is_hex[3]
|
|
|
|
{
|
|
|
|
input >= '0' && input <= '9',
|
|
|
|
input >= 'A' && input <= 'F',
|
|
|
|
input >= 'a' && input <= 'f',
|
|
|
|
};
|
|
|
|
|
|
|
|
const u8x16 hex_nibble
|
|
|
|
{
|
|
|
|
((input - 0x30) & is_hex[0])
|
|
|
|
| ((input - 0x41 + 0x0a) & is_hex[1])
|
|
|
|
| ((input - 0x61 + 0x0a) & is_hex[2])
|
|
|
|
};
|
|
|
|
|
2020-07-10 03:53:02 +02:00
|
|
|
const u8x16 is_hex_nibble
|
2020-07-03 01:39:14 +02:00
|
|
|
{
|
|
|
|
is_hex[0] | is_hex[1] | is_hex[2]
|
|
|
|
};
|
|
|
|
|
2020-07-05 04:56:06 +02:00
|
|
|
// Masks the starting byte (the '\' char) of each valid surrogate.
|
2020-07-10 03:53:02 +02:00
|
|
|
const u8x16 is_surrogate
|
2020-07-03 01:39:14 +02:00
|
|
|
{
|
2020-07-09 02:26:48 +02:00
|
|
|
(input == '\\') &
|
|
|
|
shr<8>(input == 'u') &
|
2020-07-03 01:39:14 +02:00
|
|
|
shr<16>(is_hex_nibble) &
|
|
|
|
shr<24>(is_hex_nibble) &
|
|
|
|
shr<32>(is_hex_nibble) &
|
|
|
|
shr<40>(is_hex_nibble)
|
|
|
|
};
|
|
|
|
|
2020-07-05 04:56:06 +02:00
|
|
|
// is_surrogate may leave byte[0] and byte[6] (and possibly byte[12] which
|
|
|
|
// we don't care about here) as 0xff. Our result will be 4 byte codepoints
|
|
|
|
// matching those 6 byte inputs, so we shift the byte[6] over to byte[4]
|
|
|
|
// and stiffen the mask about to be generated.
|
2020-07-03 01:39:14 +02:00
|
|
|
const u32x4 surrogate_mask
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
|
|
|
((u32x4(is_surrogate) & 0xff) | (u32x4(is_surrogate) >> 16)) == 0xffU
|
|
|
|
);
|
2020-07-05 04:56:06 +02:00
|
|
|
|
|
|
|
// Decide if one or two surrogates were actually input and assert that
|
|
|
|
// between both lanes if so.
|
|
|
|
const u32x4 surrogate_deuce
|
|
|
|
{
|
2020-07-09 02:26:48 +02:00
|
|
|
(surrogate_mask & shr<32>(surrogate_mask)) |
|
|
|
|
(surrogate_mask & shl<32>(surrogate_mask))
|
2020-07-05 04:56:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// ASCII to integral converion of the upper nibbles
|
|
|
|
const u8x16 hex_upper
|
|
|
|
{
|
2020-07-09 02:26:48 +02:00
|
|
|
shr<16>(hex_nibble)
|
2020-07-05 04:56:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// ASCII to integral converion of the lower nibbles
|
|
|
|
const u8x16 hex_lower
|
|
|
|
{
|
2020-07-09 02:26:48 +02:00
|
|
|
shr<24>(hex_nibble)
|
2020-07-05 04:56:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// pack upper and lower nibbles into bytes, though these have a space
|
|
|
|
// between them when 4 nibbles becomes 2 bytes
|
|
|
|
const u8x16 hex_byte
|
|
|
|
{
|
|
|
|
(hex_upper << 4) | hex_lower
|
|
|
|
};
|
|
|
|
|
|
|
|
// Result for one or two unpaired surrogates
|
|
|
|
const u32x4 codepoint_unpaired
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-07-05 04:56:06 +02:00
|
|
|
u8x16
|
|
|
|
{
|
|
|
|
hex_byte[2], hex_byte[0], 0, 0,
|
|
|
|
hex_byte[8], hex_byte[6], 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
}
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-05 04:56:06 +02:00
|
|
|
|
|
|
|
// Determine if the unpaired codepoints can make a surrogate pair
|
|
|
|
const u32x4 surrogate_pair_range
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-07-05 04:56:06 +02:00
|
|
|
codepoint_unpaired >= 0xd800U && codepoint_unpaired <= 0xdfffU
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-05 04:56:06 +02:00
|
|
|
|
|
|
|
// Mask lane[0] if the codepoints are actually a surrogate pair
|
|
|
|
const u32x4 surrogate_paired
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-07-09 02:26:48 +02:00
|
|
|
surrogate_pair_range & shr<32>(surrogate_pair_range)
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-05 04:56:06 +02:00
|
|
|
|
|
|
|
// Pre-processing shuffle for surrogate pair decode
|
|
|
|
const u32x4 codepoint_pre_paired
|
|
|
|
{
|
2020-07-09 02:26:48 +02:00
|
|
|
shr<16>(codepoint_unpaired) | codepoint_unpaired
|
2020-07-05 04:56:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Decode surrogate pair
|
|
|
|
const u32x4 codepoint_paired
|
|
|
|
{
|
|
|
|
0x10000U +
|
|
|
|
((codepoint_pre_paired & 0x000003ffU) << 10) +
|
|
|
|
((codepoint_pre_paired & 0x03ff0000U) >> 16)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Decide if the codepoint is in the supplementary plane (3+ bytes)
|
|
|
|
const u32x4 codepoint_high
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-07-05 04:56:06 +02:00
|
|
|
(codepoint_paired > 0xffffU) & surrogate_paired
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-05 04:56:06 +02:00
|
|
|
|
|
|
|
// Decide if the codepoint is in the BMP (2- bytes)
|
|
|
|
const u32x4 codepoint_low
|
|
|
|
{
|
2020-07-09 02:26:48 +02:00
|
|
|
(codepoint_paired <= 0xffffU) & ~(shl<32>(codepoint_high))
|
2020-07-05 04:56:06 +02:00
|
|
|
};
|
|
|
|
|
2020-09-04 06:40:47 +02:00
|
|
|
// When one surrogate is input, only lane[0]
|
|
|
|
const u32x4 ret_codepoint_single
|
|
|
|
{
|
|
|
|
codepoint_unpaired & ~surrogate_pair_range & ~surrogate_deuce
|
|
|
|
};
|
|
|
|
|
2020-07-05 04:56:06 +02:00
|
|
|
// When two surrogates in a pair are input, lane[0] only
|
|
|
|
const u32x4 ret_codepoint_paired
|
|
|
|
{
|
|
|
|
codepoint_paired & (surrogate_paired & surrogate_deuce)
|
2020-07-03 01:39:14 +02:00
|
|
|
};
|
|
|
|
|
2020-07-05 04:56:06 +02:00
|
|
|
// When two unrelated surrogates are input, lane[0] and lane[1]
|
|
|
|
const u32x4 ret_codepoint_unpaired
|
2020-07-03 01:39:14 +02:00
|
|
|
{
|
2020-07-05 04:56:06 +02:00
|
|
|
codepoint_unpaired & ~surrogate_pair_range & surrogate_deuce
|
2020-07-03 01:39:14 +02:00
|
|
|
};
|
|
|
|
|
2020-09-04 06:40:47 +02:00
|
|
|
static const u32x4
|
|
|
|
mask_one { -1U, 0U, 0U, 0U, },
|
|
|
|
mask_two { -1U, -1U, 0U, 0U, };
|
2020-07-03 01:39:14 +02:00
|
|
|
|
2020-07-05 04:56:06 +02:00
|
|
|
return 0
|
2020-09-04 06:40:47 +02:00
|
|
|
| (ret_codepoint_single & mask_one)
|
|
|
|
| (ret_codepoint_paired & mask_one)
|
|
|
|
| (ret_codepoint_unpaired & mask_two)
|
2020-07-05 04:56:06 +02:00
|
|
|
;
|
2020-07-03 01:39:14 +02:00
|
|
|
}
|
|
|
|
|
2020-06-30 04:23:54 +02:00
|
|
|
namespace ircd::utf16
|
2020-06-28 03:10:36 +02:00
|
|
|
{
|
2020-06-30 04:23:54 +02:00
|
|
|
static const u128x1 full_mask {~u128x1{0}};
|
2020-07-10 03:53:02 +02:00
|
|
|
extern const u8x16 truncation_table[6];
|
2020-06-28 03:10:36 +02:00
|
|
|
}
|
|
|
|
|
2020-06-30 04:23:54 +02:00
|
|
|
decltype(ircd::utf16::truncation_table)
|
|
|
|
ircd::utf16::truncation_table
|
|
|
|
{
|
|
|
|
~shl<0x30>(~full_mask),
|
|
|
|
~shl<0x28>(~full_mask),
|
|
|
|
~shl<0x20>(~full_mask),
|
|
|
|
~shl<0x18>(~full_mask),
|
|
|
|
~shl<0x10>(~full_mask),
|
|
|
|
~shl<0x08>(~full_mask),
|
|
|
|
};
|
|
|
|
|
2020-07-03 00:27:43 +02:00
|
|
|
/// scan for utf-16 surrogates including incomplete sequences truncated
|
|
|
|
/// by the end of the input; also matches a single trailing slash.
|
2020-06-29 01:41:23 +02:00
|
|
|
ircd::u8x16
|
2020-06-30 04:23:54 +02:00
|
|
|
ircd::utf16::find_surrogate_partial(const u8x16 input)
|
2020-06-29 01:41:23 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2020-07-10 03:53:02 +02:00
|
|
|
const u8x16 is_esc
|
|
|
|
(
|
2020-06-30 04:23:54 +02:00
|
|
|
input == '\\'
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-06-30 04:23:54 +02:00
|
|
|
|
2020-07-10 03:53:02 +02:00
|
|
|
const u8x16 is_u
|
|
|
|
(
|
2020-06-30 04:23:54 +02:00
|
|
|
input == 'u'
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const u8x16 hex_nibble[3]
|
|
|
|
{
|
|
|
|
input >= '0' && input <= '9',
|
|
|
|
input >= 'A' && input <= 'F',
|
|
|
|
input >= 'a' && input <= 'f',
|
2020-06-30 04:23:54 +02:00
|
|
|
};
|
|
|
|
|
2020-07-10 03:53:02 +02:00
|
|
|
const u8x16 is_hex_nibble
|
2020-06-29 01:41:23 +02:00
|
|
|
{
|
2020-07-10 03:53:02 +02:00
|
|
|
hex_nibble[0] | hex_nibble[1] | hex_nibble[2]
|
2020-06-29 01:41:23 +02:00
|
|
|
};
|
|
|
|
|
2020-07-10 03:53:02 +02:00
|
|
|
const u8x16 surrogate_sans[6]
|
2020-06-29 01:41:23 +02:00
|
|
|
{
|
2020-06-30 04:23:54 +02:00
|
|
|
// complete
|
|
|
|
is_esc
|
|
|
|
& shr<8>(is_u)
|
|
|
|
& shr<16>(is_hex_nibble) & shr<24>(is_hex_nibble)
|
|
|
|
& shr<32>(is_hex_nibble) & shr<40>(is_hex_nibble),
|
|
|
|
|
|
|
|
// sans 1
|
|
|
|
is_esc
|
|
|
|
& shr<8>(is_u)
|
|
|
|
& shr<16>(is_hex_nibble) & shr<24>(is_hex_nibble)
|
|
|
|
& shr<32>(is_hex_nibble),
|
|
|
|
|
|
|
|
// sans 2
|
|
|
|
is_esc
|
|
|
|
& shr<8>(is_u)
|
|
|
|
& shr<16>(is_hex_nibble) & shr<24>(is_hex_nibble),
|
|
|
|
|
|
|
|
// sans 3
|
|
|
|
is_esc
|
|
|
|
& shr<8>(is_u)
|
|
|
|
& shr<16>(is_hex_nibble),
|
|
|
|
|
|
|
|
// sans 4
|
|
|
|
is_esc
|
|
|
|
& shr<8>(is_u),
|
|
|
|
|
|
|
|
// sans 5
|
|
|
|
is_esc,
|
2020-06-29 01:41:23 +02:00
|
|
|
};
|
|
|
|
|
2020-07-10 03:53:02 +02:00
|
|
|
const u8x16 ret
|
2020-06-30 04:23:54 +02:00
|
|
|
{
|
|
|
|
(surrogate_sans[0] & truncation_table[0]) |
|
|
|
|
(surrogate_sans[1] & truncation_table[1]) |
|
|
|
|
(surrogate_sans[2] & truncation_table[2]) |
|
|
|
|
(surrogate_sans[3] & truncation_table[3]) |
|
|
|
|
(surrogate_sans[4] & truncation_table[4]) |
|
|
|
|
(surrogate_sans[5] & truncation_table[5])
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
2020-06-29 01:41:23 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 02:44:39 +02:00
|
|
|
namespace ircd::utf16
|
|
|
|
{
|
|
|
|
template u8x16 utf16::find_surrogate<u8x16>(const u8x16) noexcept;
|
|
|
|
template u8x32 utf16::find_surrogate<u8x32>(const u8x32) noexcept;
|
2020-10-02 05:34:14 +02:00
|
|
|
|
|
|
|
// Clang-10 is having trouble with this instantiation on aarch64
|
|
|
|
#if !defined(__clang__) || !defined(__aarch64__)
|
2020-09-04 02:44:39 +02:00
|
|
|
template u8x64 utf16::find_surrogate<u8x64>(const u8x64) noexcept;
|
2020-10-02 05:34:14 +02:00
|
|
|
#endif
|
2020-09-04 02:44:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class u8xN>
|
|
|
|
u8xN
|
|
|
|
ircd::utf16::find_surrogate(const u8xN input)
|
2020-06-29 01:41:23 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2020-09-04 02:44:39 +02:00
|
|
|
const u8xN hex_nibble[3]
|
2020-06-29 01:41:23 +02:00
|
|
|
{
|
2020-07-10 03:53:02 +02:00
|
|
|
input >= '0' && input <= '9',
|
|
|
|
input >= 'A' && input <= 'F',
|
|
|
|
input >= 'a' && input <= 'f',
|
|
|
|
};
|
|
|
|
|
2020-09-04 02:44:39 +02:00
|
|
|
const u8xN is_hex_nibble
|
2020-07-10 03:53:02 +02:00
|
|
|
{
|
|
|
|
hex_nibble[0] | hex_nibble[1] | hex_nibble[2]
|
2020-06-29 01:41:23 +02:00
|
|
|
};
|
|
|
|
|
2020-09-04 02:44:39 +02:00
|
|
|
const u8xN is_surrogate
|
2020-06-29 01:41:23 +02:00
|
|
|
{
|
2020-07-09 02:26:48 +02:00
|
|
|
(input == '\\') &
|
|
|
|
shr<8>(input == 'u') &
|
2020-06-29 01:41:23 +02:00
|
|
|
shr<16>(is_hex_nibble) &
|
|
|
|
shr<24>(is_hex_nibble) &
|
|
|
|
shr<32>(is_hex_nibble) &
|
|
|
|
shr<40>(is_hex_nibble)
|
|
|
|
};
|
|
|
|
|
|
|
|
return is_surrogate;
|
|
|
|
}
|
|
|
|
|
2020-06-28 04:59:41 +02:00
|
|
|
//
|
|
|
|
// utf8
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::u32x16
|
2021-08-11 13:31:32 +02:00
|
|
|
ircd::utf8::decode(const u8x16 string)
|
2020-06-28 04:59:41 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2021-08-11 13:31:32 +02:00
|
|
|
const u32x16 codepoints
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2021-08-11 13:31:32 +02:00
|
|
|
decode_sparse(string)
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-06-28 04:59:41 +02:00
|
|
|
|
2021-08-11 13:31:32 +02:00
|
|
|
const i32x16 zero_lane
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2021-08-11 13:31:32 +02:00
|
|
|
codepoints == 0
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-06-28 04:59:41 +02:00
|
|
|
|
2021-08-11 13:31:32 +02:00
|
|
|
// Lanes separating sparsely decoded codepoints are zero.
|
|
|
|
const i8x16 skip_lane
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2021-08-11 13:31:32 +02:00
|
|
|
lane_cast<i8x16>(zero_lane)
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-06-28 04:59:41 +02:00
|
|
|
|
2021-08-11 13:31:32 +02:00
|
|
|
// Actual NUL codepoints weren't altered by decode.
|
|
|
|
const i8x16 null_code
|
2021-07-31 09:14:59 +02:00
|
|
|
(
|
2021-08-11 13:31:32 +02:00
|
|
|
string == 0
|
2021-07-31 09:14:59 +02:00
|
|
|
);
|
2020-06-28 04:59:41 +02:00
|
|
|
|
2021-08-11 13:31:32 +02:00
|
|
|
// The pack will eliminate zero-value lanes except for legitimate NULs.
|
|
|
|
const i8x16 pack_mask
|
|
|
|
(
|
|
|
|
~null_code ^ skip_lane
|
|
|
|
);
|
2020-06-28 04:59:41 +02:00
|
|
|
|
2021-08-11 13:31:32 +02:00
|
|
|
const u32x16 ret
|
2021-07-31 09:14:59 +02:00
|
|
|
(
|
2021-08-11 13:31:32 +02:00
|
|
|
simd::pack(codepoints, pack_mask)
|
2021-07-31 09:14:59 +02:00
|
|
|
);
|
2020-06-28 04:59:41 +02:00
|
|
|
|
2021-08-11 13:31:32 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::u32x16
|
|
|
|
ircd::utf8::decode_sparse(const u8x16 string)
|
|
|
|
noexcept
|
|
|
|
{
|
2021-07-31 09:14:59 +02:00
|
|
|
const u8x16 len
|
|
|
|
(
|
2021-08-11 13:31:32 +02:00
|
|
|
length(string)
|
2021-07-31 09:14:59 +02:00
|
|
|
);
|
|
|
|
|
2021-08-11 13:31:32 +02:00
|
|
|
const u8x16 rem
|
2021-07-31 09:14:59 +02:00
|
|
|
(
|
2021-08-11 13:31:32 +02:00
|
|
|
len
|
|
|
|
| ((shl<0x18>(len) == 4) & 1)
|
|
|
|
| ((shl<0x10>(len) == 4) & 2)
|
|
|
|
| ((shl<0x10>(len) == 3) & 1)
|
|
|
|
| ((shl<0x08>(len) == 4) & 3)
|
|
|
|
| ((shl<0x08>(len) == 3) & 2)
|
|
|
|
| ((shl<0x08>(len) == 2) & 1)
|
2021-07-31 09:14:59 +02:00
|
|
|
);
|
2020-06-28 04:59:41 +02:00
|
|
|
|
2021-08-11 13:31:32 +02:00
|
|
|
const u8x16 bank[]
|
2020-06-28 04:59:41 +02:00
|
|
|
{
|
2021-08-11 13:31:32 +02:00
|
|
|
string & 0x3f,
|
|
|
|
string & 0xff,
|
|
|
|
string & 0x1f,
|
|
|
|
string & 0x0f,
|
|
|
|
string & 0x07,
|
2020-06-28 04:59:41 +02:00
|
|
|
};
|
|
|
|
|
2021-08-11 13:31:32 +02:00
|
|
|
const u8x16 select
|
|
|
|
(
|
|
|
|
0
|
|
|
|
| (bank[0] & (len == 0))
|
|
|
|
| (bank[1] & (len == 1))
|
|
|
|
| (bank[2] & (len == 2))
|
|
|
|
| (bank[3] & (len == 3))
|
|
|
|
| (bank[4] & (len == 4))
|
|
|
|
);
|
|
|
|
|
|
|
|
const u8x16 byte[]
|
2020-06-28 04:59:41 +02:00
|
|
|
{
|
2021-08-11 13:31:32 +02:00
|
|
|
select & (rem == 1),
|
|
|
|
select & (rem == 2),
|
|
|
|
select & (rem == 3),
|
|
|
|
select & (rem == 4),
|
2020-06-28 04:59:41 +02:00
|
|
|
};
|
|
|
|
|
2021-08-11 13:31:32 +02:00
|
|
|
const u8x16 move[]
|
|
|
|
{
|
|
|
|
shl<8 * 0>(byte[0]),
|
|
|
|
shl<8 * 1>(byte[1]),
|
|
|
|
shl<8 * 2>(byte[2]),
|
|
|
|
shl<8 * 3>(byte[3]),
|
|
|
|
};
|
2021-07-31 09:14:59 +02:00
|
|
|
|
2021-08-11 13:31:32 +02:00
|
|
|
const u32x16 pack[]
|
|
|
|
{
|
|
|
|
lane_cast<u32x16>(move[0]) << 0x00,
|
|
|
|
lane_cast<u32x16>(move[1]) << 0x06,
|
|
|
|
lane_cast<u32x16>(move[2]) << 0x0c,
|
|
|
|
lane_cast<u32x16>(move[3]) << 0x12,
|
|
|
|
};
|
2021-07-31 09:14:59 +02:00
|
|
|
|
2021-08-11 13:31:32 +02:00
|
|
|
const u32x16 ret
|
|
|
|
(
|
|
|
|
lane_cast<u32x16>(byte[0]) // pack[0] constrains clang opt
|
|
|
|
| pack[1]
|
|
|
|
| pack[2]
|
|
|
|
| pack[3]
|
|
|
|
);
|
2021-07-31 09:14:59 +02:00
|
|
|
|
|
|
|
return ret;
|
2020-06-28 04:59:41 +02:00
|
|
|
}
|
|
|
|
|
2020-07-03 23:00:09 +02:00
|
|
|
namespace ircd::utf8
|
|
|
|
{
|
2021-08-06 08:37:58 +02:00
|
|
|
template<class u32xN> static u32xN _encode_sparse(const u32xN codepoint) noexcept;
|
2020-07-03 23:00:09 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 02:44:39 +02:00
|
|
|
template<>
|
2020-07-03 23:00:09 +02:00
|
|
|
ircd::u32x4
|
2021-08-06 08:37:58 +02:00
|
|
|
ircd::utf8::encode_sparse(const u32x4 codepoint)
|
2020-07-03 23:00:09 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2021-08-06 08:37:58 +02:00
|
|
|
return _encode_sparse(codepoint);
|
2020-07-03 23:00:09 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 02:44:39 +02:00
|
|
|
template<>
|
2020-07-03 23:00:09 +02:00
|
|
|
ircd::u32x8
|
2021-08-06 08:37:58 +02:00
|
|
|
ircd::utf8::encode_sparse(const u32x8 codepoint)
|
2020-07-03 23:00:09 +02:00
|
|
|
noexcept
|
2020-07-15 02:48:29 +02:00
|
|
|
#ifdef __AVX2__
|
2020-07-03 23:00:09 +02:00
|
|
|
{
|
2021-08-06 08:37:58 +02:00
|
|
|
return _encode_sparse(codepoint);
|
2020-07-03 23:00:09 +02:00
|
|
|
}
|
2020-07-15 02:48:29 +02:00
|
|
|
#else // This block is only effective for GCC. Clang performs this automatically.
|
|
|
|
{
|
|
|
|
u32x4 cp[2];
|
|
|
|
for(size_t i(0); i < 2; ++i)
|
|
|
|
for(size_t j(0); j < 4; ++j)
|
2021-02-23 19:32:55 +01:00
|
|
|
cp[i][j] = codepoint[i * 4 + j];
|
2020-07-15 02:48:29 +02:00
|
|
|
|
2021-08-06 08:37:58 +02:00
|
|
|
cp[0] = _encode_sparse(cp[0]);
|
|
|
|
cp[1] = _encode_sparse(cp[1]);
|
2020-07-15 02:48:29 +02:00
|
|
|
|
|
|
|
u32x8 ret;
|
|
|
|
for(size_t i(0); i < 2; ++i)
|
|
|
|
for(size_t j(0); j < 4; ++j)
|
2021-02-23 19:32:55 +01:00
|
|
|
ret[i * 4 + j] = cp[i][j];
|
2020-07-15 02:48:29 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
2020-07-03 23:00:09 +02:00
|
|
|
|
2020-09-04 02:44:39 +02:00
|
|
|
template<>
|
2020-07-03 23:00:09 +02:00
|
|
|
ircd::u32x16
|
2021-08-06 08:37:58 +02:00
|
|
|
ircd::utf8::encode_sparse(const u32x16 codepoint)
|
2020-07-03 23:00:09 +02:00
|
|
|
noexcept
|
2020-07-15 02:48:29 +02:00
|
|
|
#ifdef __AVX512F__
|
2020-07-03 23:00:09 +02:00
|
|
|
{
|
2021-08-06 08:37:58 +02:00
|
|
|
return _encode_sparse(codepoint);
|
2020-07-03 23:00:09 +02:00
|
|
|
}
|
2020-07-15 02:48:29 +02:00
|
|
|
#else // This block is only effective for GCC. Clang performs this automatically.
|
|
|
|
{
|
|
|
|
u32x8 cp[2];
|
|
|
|
for(size_t i(0); i < 2; ++i)
|
|
|
|
for(size_t j(0); j < 8; ++j)
|
2021-02-23 19:32:55 +01:00
|
|
|
cp[i][j] = codepoint[i * 8 + j];
|
2020-07-15 02:48:29 +02:00
|
|
|
|
2021-08-06 08:37:58 +02:00
|
|
|
cp[0] = encode_sparse(cp[0]);
|
|
|
|
cp[1] = encode_sparse(cp[1]);
|
2020-07-15 02:48:29 +02:00
|
|
|
|
|
|
|
u32x16 ret;
|
|
|
|
for(size_t i(0); i < 2; ++i)
|
|
|
|
for(size_t j(0); j < 8; ++j)
|
2021-02-23 19:32:55 +01:00
|
|
|
ret[i * 8 + j] = cp[i][j];
|
2020-07-15 02:48:29 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
2020-07-03 23:00:09 +02:00
|
|
|
|
2020-06-28 03:10:36 +02:00
|
|
|
/// Transform multiple char32_t codepoints to their utf-8 encodings in
|
|
|
|
/// parallel, returning a sparse result in each char32_t (this does not
|
|
|
|
/// compress the result down).
|
2020-07-03 23:00:09 +02:00
|
|
|
template<class u32xN>
|
2020-07-15 02:48:29 +02:00
|
|
|
inline u32xN
|
2021-08-06 08:37:58 +02:00
|
|
|
ircd::utf8::_encode_sparse(const u32xN codepoint)
|
2020-06-28 03:10:36 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2020-07-03 23:00:09 +02:00
|
|
|
const u32xN len
|
2020-06-28 03:10:36 +02:00
|
|
|
{
|
|
|
|
length(codepoint)
|
|
|
|
};
|
|
|
|
|
2020-07-03 23:00:09 +02:00
|
|
|
const u32xN enc_2
|
2020-06-28 03:10:36 +02:00
|
|
|
{
|
|
|
|
(((codepoint >> 6) | 0xc0) & 0xff) // byte[0]
|
2021-04-11 23:56:24 +02:00
|
|
|
| ((((codepoint & 0x3f) | 0x80) & 0xff) << 8) // byte[1]
|
2020-06-28 03:10:36 +02:00
|
|
|
};
|
|
|
|
|
2020-07-03 23:00:09 +02:00
|
|
|
const u32xN enc_3
|
2020-06-28 03:10:36 +02:00
|
|
|
{
|
|
|
|
(((codepoint >> 12) | 0xe0) & 0xff) | // byte[0]
|
|
|
|
(((((codepoint >> 6) & 0x3f) | 0x80) & 0xff) << 8) | // byte[1]
|
2021-04-11 23:56:24 +02:00
|
|
|
((((codepoint & 0x3f) | 0x80) & 0xff) << 16) // byte[2]
|
2020-06-28 03:10:36 +02:00
|
|
|
};
|
|
|
|
|
2020-07-03 23:00:09 +02:00
|
|
|
const u32xN enc_4
|
2020-06-28 03:10:36 +02:00
|
|
|
{
|
|
|
|
(((codepoint >> 18) | 0xf0) & 0xff) | // byte[0]
|
|
|
|
(((((codepoint >> 12) & 0x3f) | 0x80) & 0xff) << 8) | // byte[1]
|
|
|
|
(((((codepoint >> 6) & 0x3f) | 0x80) & 0xff) << 16) | // byte[2]
|
|
|
|
((((codepoint & 0x3f) | 0x80) & 0xff) << 24) // byte[3]
|
|
|
|
};
|
|
|
|
|
|
|
|
return 0
|
|
|
|
| ((len == 0) & 0xFFFD)
|
|
|
|
| ((len == 1) & codepoint)
|
|
|
|
| ((len == 2) & enc_2)
|
|
|
|
| ((len == 3) & enc_3)
|
|
|
|
| ((len == 4) & enc_4)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2020-07-03 00:27:43 +02:00
|
|
|
namespace ircd::utf8
|
|
|
|
{
|
2020-09-04 02:44:39 +02:00
|
|
|
template<class u32xN> static u32xN _length(const u32xN codepoint) noexcept;
|
2020-07-03 00:27:43 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 02:44:39 +02:00
|
|
|
template<>
|
2020-07-03 00:27:43 +02:00
|
|
|
ircd::u32x4
|
|
|
|
ircd::utf8::length(const u32x4 codepoint)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return _length(codepoint);
|
|
|
|
}
|
|
|
|
|
2020-09-04 02:44:39 +02:00
|
|
|
template<>
|
2020-07-03 00:27:43 +02:00
|
|
|
ircd::u32x8
|
|
|
|
ircd::utf8::length(const u32x8 codepoint)
|
|
|
|
noexcept
|
2020-07-15 02:48:29 +02:00
|
|
|
#ifdef __AVX2__
|
2020-07-03 00:27:43 +02:00
|
|
|
{
|
|
|
|
return _length(codepoint);
|
|
|
|
}
|
2020-07-15 02:48:29 +02:00
|
|
|
#else // This block is only effective for GCC. Clang performs this automatically.
|
|
|
|
{
|
|
|
|
u32x4 cp[2];
|
|
|
|
for(size_t i(0); i < 2; ++i)
|
|
|
|
for(size_t j(0); j < 4; ++j)
|
2021-02-23 19:32:55 +01:00
|
|
|
cp[i][j] = codepoint[i * 4 + j];
|
2020-07-15 02:48:29 +02:00
|
|
|
|
|
|
|
cp[0] = _length(cp[0]);
|
|
|
|
cp[1] = _length(cp[1]);
|
|
|
|
|
|
|
|
u32x8 ret;
|
|
|
|
for(size_t i(0); i < 2; ++i)
|
|
|
|
for(size_t j(0); j < 4; ++j)
|
2021-02-23 19:32:55 +01:00
|
|
|
ret[i * 4 + j] = cp[i][j];
|
2020-07-15 02:48:29 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
2020-07-03 00:27:43 +02:00
|
|
|
|
2020-09-04 02:44:39 +02:00
|
|
|
template<>
|
2020-07-03 00:27:43 +02:00
|
|
|
ircd::u32x16
|
|
|
|
ircd::utf8::length(const u32x16 codepoint)
|
|
|
|
noexcept
|
2020-07-15 02:48:29 +02:00
|
|
|
#ifdef __AVX512F__
|
2020-07-03 00:27:43 +02:00
|
|
|
{
|
|
|
|
return _length(codepoint);
|
|
|
|
}
|
2020-07-15 02:48:29 +02:00
|
|
|
#else // This block is only effective for GCC. Clang performs this automatically.
|
|
|
|
{
|
|
|
|
u32x8 cp[2];
|
|
|
|
for(size_t i(0); i < 2; ++i)
|
|
|
|
for(size_t j(0); j < 8; ++j)
|
2021-02-23 19:32:55 +01:00
|
|
|
cp[i][j] = codepoint[i * 8 + j];
|
2020-07-15 02:48:29 +02:00
|
|
|
|
|
|
|
cp[0] = length(cp[0]);
|
|
|
|
cp[1] = length(cp[1]);
|
|
|
|
|
|
|
|
u32x16 ret;
|
|
|
|
for(size_t i(0); i < 2; ++i)
|
|
|
|
for(size_t j(0); j < 8; ++j)
|
2021-02-23 19:32:55 +01:00
|
|
|
ret[i * 8 + j] = cp[i][j];
|
2020-07-15 02:48:29 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
2020-07-03 00:27:43 +02:00
|
|
|
|
2020-06-28 03:10:36 +02:00
|
|
|
/// Determine the utf-8 encoding length of multiple codepoints in parallel.
|
|
|
|
/// The input vector char32_t codepoints and the output yields an integer
|
|
|
|
/// of 0-4 for each lane.
|
2020-07-03 00:27:43 +02:00
|
|
|
template<class u32xN>
|
2020-07-15 02:48:29 +02:00
|
|
|
inline u32xN
|
2020-07-03 00:27:43 +02:00
|
|
|
ircd::utf8::_length(const u32xN codepoint)
|
2020-06-28 03:10:36 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2020-07-10 03:53:02 +02:00
|
|
|
const u32xN len[5]
|
|
|
|
{
|
|
|
|
// length 1
|
|
|
|
codepoint <= 0x7f,
|
|
|
|
|
|
|
|
// length 2
|
|
|
|
codepoint <= 0x7ff && codepoint > 0x7f,
|
|
|
|
|
|
|
|
// length 3 low
|
|
|
|
codepoint <= 0xd7ff && codepoint > 0x7ff,
|
|
|
|
|
|
|
|
// length 3 high
|
|
|
|
codepoint <= 0xffff && codepoint > 0xdfff,
|
2020-06-28 03:10:36 +02:00
|
|
|
|
2020-07-10 03:53:02 +02:00
|
|
|
// length 4
|
|
|
|
codepoint <= 0x10ffff && codepoint > 0xffff,
|
|
|
|
};
|
|
|
|
|
|
|
|
[[gnu::unused]] // Preserved here for future reference
|
|
|
|
const u32xN len_3_err
|
|
|
|
(
|
|
|
|
codepoint <= 0xdfff && codepoint > 0xd7ff
|
|
|
|
);
|
|
|
|
|
|
|
|
[[gnu::unused]] // Preserved here for future reference
|
|
|
|
const u32xN len_err
|
|
|
|
{
|
|
|
|
(codepoint > 0x10ffff) | len_3_err
|
|
|
|
};
|
2020-06-28 03:10:36 +02:00
|
|
|
|
|
|
|
return 0
|
2020-07-10 03:53:02 +02:00
|
|
|
| (len[0] & 1)
|
|
|
|
| (len[1] & 2)
|
|
|
|
| (len[2] & 3)
|
|
|
|
| (len[3] & 3)
|
|
|
|
| (len[4] & 4)
|
2020-06-28 03:10:36 +02:00
|
|
|
;
|
|
|
|
}
|