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
|
2020-06-29 02:04:27 +02:00
|
|
|
ircd::utf8::decode(const u8x16 string)
|
2020-06-28 04:59:41 +02:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
const u32x16 in
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
|
|
|
simd::lane_cast<u32x16, u8x16>(string)
|
|
|
|
);
|
2020-06-28 04:59:41 +02:00
|
|
|
|
|
|
|
const u32x16 is_single
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-06-28 04:59:41 +02:00
|
|
|
(in & 0x80) == 0
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-06-28 04:59:41 +02:00
|
|
|
|
|
|
|
const u32x16 is_lead
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-06-28 04:59:41 +02:00
|
|
|
(in - 0xc2) <= 0x32
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-06-28 04:59:41 +02:00
|
|
|
|
|
|
|
const u32x16 is_trail
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-06-28 04:59:41 +02:00
|
|
|
in >= 0x80 && in < 0xbf
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-06-28 04:59:41 +02:00
|
|
|
|
|
|
|
const u32x16 expect_trail
|
|
|
|
{
|
|
|
|
(((in >= 0xe0) & 1) + ((in >= 0xf0) & 1) + 1) & is_lead
|
|
|
|
};
|
|
|
|
|
|
|
|
const u32x16 expect_length
|
|
|
|
{
|
|
|
|
expect_trail + 1
|
|
|
|
};
|
|
|
|
|
|
|
|
const u32x16 shift[4]
|
|
|
|
{
|
|
|
|
in << 0,
|
|
|
|
in << 8,
|
|
|
|
in << 16,
|
|
|
|
in << 24,
|
|
|
|
};
|
|
|
|
|
|
|
|
const u32x16 multibyte_packs
|
|
|
|
{
|
|
|
|
in[0x00] | shift[0x01][0x01] | shift[0x02][0x02] | shift[0x03][0x03],
|
|
|
|
in[0x01] | shift[0x01][0x02] | shift[0x02][0x03] | shift[0x03][0x04],
|
|
|
|
in[0x02] | shift[0x01][0x03] | shift[0x02][0x04] | shift[0x03][0x05],
|
|
|
|
in[0x03] | shift[0x01][0x04] | shift[0x02][0x05] | shift[0x03][0x06],
|
|
|
|
in[0x04] | shift[0x01][0x05] | shift[0x02][0x06] | shift[0x03][0x07],
|
|
|
|
in[0x05] | shift[0x01][0x06] | shift[0x02][0x07] | shift[0x03][0x08],
|
|
|
|
in[0x06] | shift[0x01][0x07] | shift[0x02][0x08] | shift[0x03][0x09],
|
|
|
|
in[0x07] | shift[0x01][0x08] | shift[0x02][0x09] | shift[0x03][0x0a],
|
|
|
|
in[0x08] | shift[0x01][0x09] | shift[0x02][0x0a] | shift[0x03][0x0b],
|
|
|
|
in[0x09] | shift[0x01][0x0a] | shift[0x02][0x0b] | shift[0x03][0x0c],
|
|
|
|
in[0x0a] | shift[0x01][0x0b] | shift[0x02][0x0c] | shift[0x03][0x0d],
|
|
|
|
in[0x0b] | shift[0x01][0x0c] | shift[0x02][0x0d] | shift[0x03][0x0e],
|
|
|
|
in[0x0c] | shift[0x01][0x0d] | shift[0x02][0x0e] | shift[0x03][0x0f],
|
|
|
|
in[0x0d] | shift[0x01][0x0e] | shift[0x02][0x0f] | shift[0x03][0x0f],
|
|
|
|
in[0x0e] | shift[0x01][0x0f] | shift[0x02][0x0f] | shift[0x03][0x0f],
|
|
|
|
in[0x0f] | shift[0x01][0x0f] | shift[0x02][0x0f] | shift[0x03][0x0f],
|
|
|
|
};
|
|
|
|
|
|
|
|
const u32x16 multibyte
|
|
|
|
{
|
|
|
|
0
|
|
|
|
| (multibyte_packs & (expect_length == 1) & 0x000000ffU)
|
|
|
|
| (multibyte_packs & (expect_length == 2) & 0x0000ffffU)
|
|
|
|
| (multibyte_packs & (expect_length == 3) & 0x00ffffffU)
|
|
|
|
| (multibyte_packs & (expect_length == 4) & 0xffffffffU)
|
|
|
|
};
|
|
|
|
|
|
|
|
const u32x16 integers
|
|
|
|
{
|
|
|
|
(in & is_single) | (multibyte & is_lead)
|
|
|
|
};
|
|
|
|
|
|
|
|
return integers;
|
|
|
|
}
|
|
|
|
|
2020-07-03 23:00:09 +02:00
|
|
|
namespace ircd::utf8
|
|
|
|
{
|
2020-09-04 02:44:39 +02:00
|
|
|
template<class u32xN> static u32xN _encode(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
|
|
|
|
ircd::utf8::encode(const u32x4 codepoint)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return _encode(codepoint);
|
|
|
|
}
|
|
|
|
|
2020-09-04 02:44:39 +02:00
|
|
|
template<>
|
2020-07-03 23:00:09 +02:00
|
|
|
ircd::u32x8
|
|
|
|
ircd::utf8::encode(const u32x8 codepoint)
|
|
|
|
noexcept
|
2020-07-15 02:48:29 +02:00
|
|
|
#ifdef __AVX2__
|
2020-07-03 23:00:09 +02:00
|
|
|
{
|
|
|
|
return _encode(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)
|
|
|
|
cp[i][j] = codepoint[(i + 1) * j];
|
|
|
|
|
|
|
|
cp[0] = _encode(cp[0]);
|
|
|
|
cp[1] = _encode(cp[1]);
|
|
|
|
|
|
|
|
u32x8 ret;
|
|
|
|
for(size_t i(0); i < 2; ++i)
|
|
|
|
for(size_t j(0); j < 4; ++j)
|
|
|
|
ret[(i + 1) * j] = cp[i][j];
|
|
|
|
|
|
|
|
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
|
|
|
|
ircd::utf8::encode(const u32x16 codepoint)
|
|
|
|
noexcept
|
2020-07-15 02:48:29 +02:00
|
|
|
#ifdef __AVX512F__
|
2020-07-03 23:00:09 +02:00
|
|
|
{
|
|
|
|
return _encode(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)
|
|
|
|
cp[i][j] = codepoint[(i + 1) * j];
|
|
|
|
|
|
|
|
cp[0] = encode(cp[0]);
|
|
|
|
cp[1] = encode(cp[1]);
|
|
|
|
|
|
|
|
u32x16 ret;
|
|
|
|
for(size_t i(0); i < 2; ++i)
|
|
|
|
for(size_t j(0); j < 8; ++j)
|
|
|
|
ret[(i + 1) * j] = cp[i][j];
|
|
|
|
|
|
|
|
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
|
2020-07-03 23:00:09 +02:00
|
|
|
ircd::utf8::_encode(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]
|
|
|
|
| ((((codepoint & 0x3f) | 0x80) &0xff) << 8) // byte[1]
|
|
|
|
};
|
|
|
|
|
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]
|
|
|
|
((((codepoint & 0x3f) | 0x80) & 0xff) << 16) // byte[3]
|
|
|
|
};
|
|
|
|
|
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)
|
|
|
|
cp[i][j] = codepoint[(i + 1) * j];
|
|
|
|
|
|
|
|
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)
|
|
|
|
ret[(i + 1) * j] = cp[i][j];
|
|
|
|
|
|
|
|
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)
|
|
|
|
cp[i][j] = codepoint[(i + 1) * j];
|
|
|
|
|
|
|
|
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)
|
|
|
|
ret[(i + 1) * j] = cp[i][j];
|
|
|
|
|
|
|
|
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
|
|
|
;
|
|
|
|
}
|