Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
#[cfg(any(feature = "aarch64_neon", target_feature = "neon"))]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) mod neon;
|
||||
|
||||
#[inline]
|
||||
#[cfg(any(feature = "aarch64_neon", target_feature = "neon"))]
|
||||
pub(crate) unsafe fn validate_utf8_basic(input: &[u8]) -> Result<(), crate::basic::Utf8Error> {
|
||||
if input.len() < super::helpers::SIMD_CHUNK_SIZE {
|
||||
return super::validate_utf8_basic_fallback(input);
|
||||
}
|
||||
|
||||
validate_utf8_basic_neon(input)
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
#[cfg(any(feature = "aarch64_neon", target_feature = "neon"))]
|
||||
unsafe fn validate_utf8_basic_neon(input: &[u8]) -> Result<(), crate::basic::Utf8Error> {
|
||||
neon::validate_utf8_basic(input)
|
||||
}
|
||||
|
||||
#[cfg(not(any(feature = "aarch64_neon", target_feature = "neon")))]
|
||||
pub(crate) use super::validate_utf8_basic_fallback as validate_utf8_basic;
|
||||
|
||||
#[inline]
|
||||
#[cfg(any(feature = "aarch64_neon", target_feature = "neon"))]
|
||||
pub(crate) unsafe fn validate_utf8_compat(input: &[u8]) -> Result<(), crate::compat::Utf8Error> {
|
||||
if input.len() < super::helpers::SIMD_CHUNK_SIZE {
|
||||
return super::validate_utf8_compat_fallback(input);
|
||||
}
|
||||
|
||||
validate_utf8_compat_neon(input)
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
#[cfg(any(feature = "aarch64_neon", target_feature = "neon"))]
|
||||
unsafe fn validate_utf8_compat_neon(input: &[u8]) -> Result<(), crate::compat::Utf8Error> {
|
||||
neon::validate_utf8_compat(input)
|
||||
}
|
||||
|
||||
#[cfg(not(any(feature = "aarch64_neon", target_feature = "neon")))]
|
||||
pub(crate) use super::validate_utf8_compat_fallback as validate_utf8_compat;
|
||||
+244
@@ -0,0 +1,244 @@
|
||||
//! Contains the aarch64 UTF-8 validation implementation.
|
||||
|
||||
use core::arch::aarch64::{
|
||||
uint8x16_t, vandq_u8, vcgtq_u8, vdupq_n_u8, veorq_u8, vextq_u8, vld1q_u8, vmaxvq_u8,
|
||||
vmovq_n_u8, vorrq_u8, vqsubq_u8, vqtbl1q_u8, vshrq_n_u8,
|
||||
};
|
||||
|
||||
use crate::implementation::helpers::Utf8CheckAlgorithm;
|
||||
|
||||
// aarch64 SIMD primitives
|
||||
|
||||
type SimdU8Value = crate::implementation::helpers::SimdU8Value<uint8x16_t>;
|
||||
|
||||
impl SimdU8Value {
|
||||
#[inline]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
unsafe fn from_32_cut_off_leading(
|
||||
_v0: u8,
|
||||
_v1: u8,
|
||||
_v2: u8,
|
||||
_v3: u8,
|
||||
_v4: u8,
|
||||
_v5: u8,
|
||||
_v6: u8,
|
||||
_v7: u8,
|
||||
_v8: u8,
|
||||
_v9: u8,
|
||||
_v10: u8,
|
||||
_v11: u8,
|
||||
_v12: u8,
|
||||
_v13: u8,
|
||||
_v14: u8,
|
||||
_v15: u8,
|
||||
v16: u8,
|
||||
v17: u8,
|
||||
v18: u8,
|
||||
v19: u8,
|
||||
v20: u8,
|
||||
v21: u8,
|
||||
v22: u8,
|
||||
v23: u8,
|
||||
v24: u8,
|
||||
v25: u8,
|
||||
v26: u8,
|
||||
v27: u8,
|
||||
v28: u8,
|
||||
v29: u8,
|
||||
v30: u8,
|
||||
v31: u8,
|
||||
) -> Self {
|
||||
let arr: [u8; 16] = [
|
||||
v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31,
|
||||
];
|
||||
Self::from(vld1q_u8(arr.as_ptr()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
unsafe fn repeat_16(
|
||||
v0: u8,
|
||||
v1: u8,
|
||||
v2: u8,
|
||||
v3: u8,
|
||||
v4: u8,
|
||||
v5: u8,
|
||||
v6: u8,
|
||||
v7: u8,
|
||||
v8: u8,
|
||||
v9: u8,
|
||||
v10: u8,
|
||||
v11: u8,
|
||||
v12: u8,
|
||||
v13: u8,
|
||||
v14: u8,
|
||||
v15: u8,
|
||||
) -> Self {
|
||||
let arr: [u8; 16] = [
|
||||
v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15,
|
||||
];
|
||||
Self::from(vld1q_u8(arr.as_ptr()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
unsafe fn load_from(ptr: *const u8) -> Self {
|
||||
// WORKAROUND for https://github.com/rust-lang/stdarch/issues/1148
|
||||
// The vld1q_u8 intrinsic is currently broken, it treats it as individual
|
||||
// byte loads so the compiler sometimes decides it is a better to load
|
||||
// individual bytes to "optimize" a subsequent SIMD shuffle
|
||||
//
|
||||
// This code forces a full 128-bit load.
|
||||
let mut dst = core::mem::MaybeUninit::<uint8x16_t>::uninit();
|
||||
core::ptr::copy_nonoverlapping(
|
||||
ptr.cast::<u8>(),
|
||||
dst.as_mut_ptr().cast::<u8>(),
|
||||
core::mem::size_of::<uint8x16_t>(),
|
||||
);
|
||||
Self::from(dst.assume_init())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
unsafe fn lookup_16(
|
||||
self,
|
||||
v0: u8,
|
||||
v1: u8,
|
||||
v2: u8,
|
||||
v3: u8,
|
||||
v4: u8,
|
||||
v5: u8,
|
||||
v6: u8,
|
||||
v7: u8,
|
||||
v8: u8,
|
||||
v9: u8,
|
||||
v10: u8,
|
||||
v11: u8,
|
||||
v12: u8,
|
||||
v13: u8,
|
||||
v14: u8,
|
||||
v15: u8,
|
||||
) -> Self {
|
||||
Self::from(vqtbl1q_u8(
|
||||
Self::repeat_16(
|
||||
v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15,
|
||||
)
|
||||
.0,
|
||||
self.0,
|
||||
))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
unsafe fn splat(val: u8) -> Self {
|
||||
Self::from(vmovq_n_u8(val))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
unsafe fn splat0() -> Self {
|
||||
Self::from(vdupq_n_u8(0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn or(self, b: Self) -> Self {
|
||||
Self::from(vorrq_u8(self.0, b.0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn and(self, b: Self) -> Self {
|
||||
Self::from(vandq_u8(self.0, b.0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn xor(self, b: Self) -> Self {
|
||||
Self::from(veorq_u8(self.0, b.0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn saturating_sub(self, b: Self) -> Self {
|
||||
Self::from(vqsubq_u8(self.0, b.0))
|
||||
}
|
||||
|
||||
// ugly but shr<N> requires const generics
|
||||
|
||||
#[allow(clippy::cast_lossless)]
|
||||
#[inline]
|
||||
unsafe fn shr4(self) -> Self {
|
||||
Self::from(vshrq_n_u8(self.0, 4))
|
||||
}
|
||||
|
||||
// ugly but prev<N> requires const generics
|
||||
|
||||
#[allow(clippy::cast_lossless)]
|
||||
#[inline]
|
||||
unsafe fn prev1(self, prev: Self) -> Self {
|
||||
Self::from(vextq_u8(prev.0, self.0, 16 - 1))
|
||||
}
|
||||
|
||||
// ugly but prev<N> requires const generics
|
||||
|
||||
#[allow(clippy::cast_lossless)]
|
||||
#[inline]
|
||||
unsafe fn prev2(self, prev: Self) -> Self {
|
||||
Self::from(vextq_u8(prev.0, self.0, 16 - 2))
|
||||
}
|
||||
|
||||
// ugly but prev<N> requires const generics
|
||||
|
||||
#[allow(clippy::cast_lossless)]
|
||||
#[inline]
|
||||
unsafe fn prev3(self, prev: Self) -> Self {
|
||||
Self::from(vextq_u8(prev.0, self.0, 16 - 3))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn unsigned_gt(self, other: Self) -> Self {
|
||||
Self::from(vcgtq_u8(self.0, other.0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn any_bit_set(self) -> bool {
|
||||
vmaxvq_u8(self.0) != 0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn is_ascii(self) -> bool {
|
||||
vmaxvq_u8(self.0) < 0b1000_0000_u8
|
||||
}
|
||||
}
|
||||
|
||||
impl From<uint8x16_t> for SimdU8Value {
|
||||
#[inline]
|
||||
fn from(val: uint8x16_t) -> Self {
|
||||
Self(val)
|
||||
}
|
||||
}
|
||||
|
||||
impl Utf8CheckAlgorithm<SimdU8Value> {
|
||||
#[inline]
|
||||
unsafe fn must_be_2_3_continuation(prev2: SimdU8Value, prev3: SimdU8Value) -> SimdU8Value {
|
||||
let is_third_byte = prev2.unsigned_gt(SimdU8Value::splat(0b1110_0000 - 1));
|
||||
let is_fourth_byte = prev3.unsigned_gt(SimdU8Value::splat(0b1111_0000 - 1));
|
||||
|
||||
is_third_byte.or(is_fourth_byte)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(feature = "aarch64_neon_prefetch")]
|
||||
unsafe fn simd_prefetch(ptr: *const u8) {
|
||||
use core::arch::aarch64::{_prefetch, _PREFETCH_LOCALITY3, _PREFETCH_READ};
|
||||
_prefetch(ptr.cast::<i8>(), _PREFETCH_READ, _PREFETCH_LOCALITY3);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(not(feature = "aarch64_neon_prefetch"))]
|
||||
unsafe fn simd_prefetch(_ptr: *const u8) {}
|
||||
|
||||
const PREFETCH: bool = false;
|
||||
use crate::implementation::helpers::TempSimdChunkA16 as TempSimdChunk;
|
||||
simd_input_128_bit!("not_used");
|
||||
algorithm_simd!("not_used");
|
||||
+578
@@ -0,0 +1,578 @@
|
||||
/// Macros requires newtypes in scope:
|
||||
/// `SimdU8Value` - implementation of SIMD primitives
|
||||
/// `SimdInput` - which holds 64 bytes of SIMD input
|
||||
/// `TempSimdChunk` - correctly aligned `TempSimdChunk`, either `TempSimdChunkA16` or `TempSimdChunkA32`
|
||||
|
||||
macro_rules! algorithm_simd {
|
||||
($feat:expr) => {
|
||||
use crate::{basic, compat};
|
||||
|
||||
impl Utf8CheckAlgorithm<SimdU8Value> {
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn default() -> Self {
|
||||
Self {
|
||||
prev: SimdU8Value::splat0(),
|
||||
incomplete: SimdU8Value::splat0(),
|
||||
error: SimdU8Value::splat0(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn check_incomplete_pending(&mut self) {
|
||||
self.error = self.error.or(self.incomplete);
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn is_incomplete(input: SimdU8Value) -> SimdU8Value {
|
||||
input.saturating_sub(SimdU8Value::from_32_cut_off_leading(
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0xff,
|
||||
0b1111_0000 - 1,
|
||||
0b1110_0000 - 1,
|
||||
0b1100_0000 - 1,
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
#[allow(clippy::too_many_lines)]
|
||||
unsafe fn check_special_cases(input: SimdU8Value, prev1: SimdU8Value) -> SimdU8Value {
|
||||
const TOO_SHORT: u8 = 1 << 0;
|
||||
const TOO_LONG: u8 = 1 << 1;
|
||||
const OVERLONG_3: u8 = 1 << 2;
|
||||
const SURROGATE: u8 = 1 << 4;
|
||||
const OVERLONG_2: u8 = 1 << 5;
|
||||
const TWO_CONTS: u8 = 1 << 7;
|
||||
const TOO_LARGE: u8 = 1 << 3;
|
||||
const TOO_LARGE_1000: u8 = 1 << 6;
|
||||
const OVERLONG_4: u8 = 1 << 6;
|
||||
const CARRY: u8 = TOO_SHORT | TOO_LONG | TWO_CONTS;
|
||||
|
||||
let byte_1_high = prev1.shr4().lookup_16(
|
||||
TOO_LONG,
|
||||
TOO_LONG,
|
||||
TOO_LONG,
|
||||
TOO_LONG,
|
||||
TOO_LONG,
|
||||
TOO_LONG,
|
||||
TOO_LONG,
|
||||
TOO_LONG,
|
||||
TWO_CONTS,
|
||||
TWO_CONTS,
|
||||
TWO_CONTS,
|
||||
TWO_CONTS,
|
||||
TOO_SHORT | OVERLONG_2,
|
||||
TOO_SHORT,
|
||||
TOO_SHORT | OVERLONG_3 | SURROGATE,
|
||||
TOO_SHORT | TOO_LARGE | TOO_LARGE_1000 | OVERLONG_4,
|
||||
);
|
||||
|
||||
let byte_1_low = prev1.and(SimdU8Value::splat(0x0F)).lookup_16(
|
||||
CARRY | OVERLONG_3 | OVERLONG_2 | OVERLONG_4,
|
||||
CARRY | OVERLONG_2,
|
||||
CARRY,
|
||||
CARRY,
|
||||
CARRY | TOO_LARGE,
|
||||
CARRY | TOO_LARGE | TOO_LARGE_1000,
|
||||
CARRY | TOO_LARGE | TOO_LARGE_1000,
|
||||
CARRY | TOO_LARGE | TOO_LARGE_1000,
|
||||
CARRY | TOO_LARGE | TOO_LARGE_1000,
|
||||
CARRY | TOO_LARGE | TOO_LARGE_1000,
|
||||
CARRY | TOO_LARGE | TOO_LARGE_1000,
|
||||
CARRY | TOO_LARGE | TOO_LARGE_1000,
|
||||
CARRY | TOO_LARGE | TOO_LARGE_1000,
|
||||
CARRY | TOO_LARGE | TOO_LARGE_1000 | SURROGATE,
|
||||
CARRY | TOO_LARGE | TOO_LARGE_1000,
|
||||
CARRY | TOO_LARGE | TOO_LARGE_1000,
|
||||
);
|
||||
|
||||
let byte_2_high = input.shr4().lookup_16(
|
||||
TOO_SHORT,
|
||||
TOO_SHORT,
|
||||
TOO_SHORT,
|
||||
TOO_SHORT,
|
||||
TOO_SHORT,
|
||||
TOO_SHORT,
|
||||
TOO_SHORT,
|
||||
TOO_SHORT,
|
||||
TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE_1000 | OVERLONG_4,
|
||||
TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE,
|
||||
TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE,
|
||||
TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE,
|
||||
TOO_SHORT,
|
||||
TOO_SHORT,
|
||||
TOO_SHORT,
|
||||
TOO_SHORT,
|
||||
);
|
||||
|
||||
byte_1_high.and(byte_1_low).and(byte_2_high)
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn check_multibyte_lengths(
|
||||
input: SimdU8Value,
|
||||
prev: SimdU8Value,
|
||||
special_cases: SimdU8Value,
|
||||
) -> SimdU8Value {
|
||||
let prev2 = input.prev2(prev);
|
||||
let prev3 = input.prev3(prev);
|
||||
let must23 = Self::must_be_2_3_continuation(prev2, prev3);
|
||||
let must23_80 = must23.and(SimdU8Value::splat(0x80));
|
||||
must23_80.xor(special_cases)
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn has_error(&self) -> bool {
|
||||
self.error.any_bit_set()
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn check_bytes(&mut self, input: SimdU8Value) {
|
||||
let prev1 = input.prev1(self.prev);
|
||||
let sc = Self::check_special_cases(input, prev1);
|
||||
self.error = self
|
||||
.error
|
||||
.or(Self::check_multibyte_lengths(input, self.prev, sc));
|
||||
self.prev = input;
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn check_utf8(&mut self, input: SimdInput) {
|
||||
if input.is_ascii() {
|
||||
self.check_incomplete_pending();
|
||||
} else {
|
||||
self.check_block(input);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn check_block(&mut self, input: SimdInput) {
|
||||
// WORKAROUND
|
||||
// necessary because the for loop is not unrolled on ARM64
|
||||
if input.vals.len() == 2 {
|
||||
self.check_bytes(*input.vals.get_unchecked(0));
|
||||
self.check_bytes(*input.vals.get_unchecked(1));
|
||||
self.incomplete = Self::is_incomplete(*input.vals.get_unchecked(1));
|
||||
} else if input.vals.len() == 4 {
|
||||
self.check_bytes(*input.vals.get_unchecked(0));
|
||||
self.check_bytes(*input.vals.get_unchecked(1));
|
||||
self.check_bytes(*input.vals.get_unchecked(2));
|
||||
self.check_bytes(*input.vals.get_unchecked(3));
|
||||
self.incomplete = Self::is_incomplete(*input.vals.get_unchecked(3));
|
||||
} else {
|
||||
panic!("Unsupported number of chunks");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Validation implementation for CPUs supporting the SIMD extension (see module).
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns the zero-sized [`basic::Utf8Error`] on failure.
|
||||
///
|
||||
/// # Safety
|
||||
/// This function is inherently unsafe because it is compiled with SIMD extensions
|
||||
/// enabled. Make sure that the CPU supports it before calling.
|
||||
///
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
pub unsafe fn validate_utf8_basic(
|
||||
input: &[u8],
|
||||
) -> core::result::Result<(), basic::Utf8Error> {
|
||||
use crate::implementation::helpers::SIMD_CHUNK_SIZE;
|
||||
let len = input.len();
|
||||
let mut algorithm = Utf8CheckAlgorithm::<SimdU8Value>::default();
|
||||
let mut idx: usize = 0;
|
||||
let iter_lim = len - (len % SIMD_CHUNK_SIZE);
|
||||
|
||||
while idx < iter_lim {
|
||||
let simd_input = SimdInput::new(input.get_unchecked(idx as usize..));
|
||||
idx += SIMD_CHUNK_SIZE;
|
||||
if !simd_input.is_ascii() {
|
||||
algorithm.check_block(simd_input);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while idx < iter_lim {
|
||||
if PREFETCH {
|
||||
simd_prefetch(input.as_ptr().add(idx + SIMD_CHUNK_SIZE * 2));
|
||||
}
|
||||
let input = SimdInput::new(input.get_unchecked(idx as usize..));
|
||||
algorithm.check_utf8(input);
|
||||
idx += SIMD_CHUNK_SIZE;
|
||||
}
|
||||
|
||||
if idx < len {
|
||||
let mut tmpbuf = TempSimdChunk::new();
|
||||
crate::implementation::helpers::memcpy_unaligned_nonoverlapping_inline_opt_lt_64(
|
||||
input.as_ptr().add(idx),
|
||||
tmpbuf.0.as_mut_ptr(),
|
||||
len - idx,
|
||||
);
|
||||
let simd_input = SimdInput::new(&tmpbuf.0);
|
||||
algorithm.check_utf8(simd_input);
|
||||
}
|
||||
algorithm.check_incomplete_pending();
|
||||
if algorithm.has_error() {
|
||||
Err(basic::Utf8Error {})
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Validation implementation for CPUs supporting the SIMD extension (see module).
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns [`compat::Utf8Error`] with detailed error information on failure.
|
||||
///
|
||||
/// # Safety
|
||||
/// This function is inherently unsafe because it is compiled with SIMD extensions
|
||||
/// enabled. Make sure that the CPU supports it before calling.
|
||||
///
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
pub unsafe fn validate_utf8_compat(
|
||||
input: &[u8],
|
||||
) -> core::result::Result<(), compat::Utf8Error> {
|
||||
validate_utf8_compat_simd0(input)
|
||||
.map_err(|idx| crate::implementation::helpers::get_compat_error(input, idx))
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn validate_utf8_compat_simd0(input: &[u8]) -> core::result::Result<(), usize> {
|
||||
use crate::implementation::helpers::SIMD_CHUNK_SIZE;
|
||||
let len = input.len();
|
||||
let mut algorithm = Utf8CheckAlgorithm::<SimdU8Value>::default();
|
||||
let mut idx: usize = 0;
|
||||
let mut only_ascii = true;
|
||||
let iter_lim = len - (len % SIMD_CHUNK_SIZE);
|
||||
|
||||
'outer: loop {
|
||||
if only_ascii {
|
||||
while idx < iter_lim {
|
||||
let simd_input = SimdInput::new(input.get_unchecked(idx as usize..));
|
||||
if !simd_input.is_ascii() {
|
||||
algorithm.check_block(simd_input);
|
||||
if algorithm.has_error() {
|
||||
return Err(idx);
|
||||
} else {
|
||||
only_ascii = false;
|
||||
idx += SIMD_CHUNK_SIZE;
|
||||
continue 'outer;
|
||||
}
|
||||
}
|
||||
idx += SIMD_CHUNK_SIZE;
|
||||
}
|
||||
} else {
|
||||
while idx < iter_lim {
|
||||
if PREFETCH {
|
||||
simd_prefetch(input.as_ptr().add(idx + SIMD_CHUNK_SIZE * 2));
|
||||
}
|
||||
let simd_input = SimdInput::new(input.get_unchecked(idx as usize..));
|
||||
if simd_input.is_ascii() {
|
||||
algorithm.check_incomplete_pending();
|
||||
if algorithm.has_error() {
|
||||
return Err(idx);
|
||||
} else {
|
||||
// we are in pure ASCII territory again
|
||||
only_ascii = true;
|
||||
idx += SIMD_CHUNK_SIZE;
|
||||
continue 'outer;
|
||||
}
|
||||
} else {
|
||||
algorithm.check_block(simd_input);
|
||||
if algorithm.has_error() {
|
||||
return Err(idx);
|
||||
}
|
||||
}
|
||||
idx += SIMD_CHUNK_SIZE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
if idx < len {
|
||||
let mut tmpbuf = TempSimdChunk::new();
|
||||
crate::implementation::helpers::memcpy_unaligned_nonoverlapping_inline_opt_lt_64(
|
||||
input.as_ptr().add(idx),
|
||||
tmpbuf.0.as_mut_ptr(),
|
||||
len - idx,
|
||||
);
|
||||
let simd_input = SimdInput::new(&tmpbuf.0);
|
||||
|
||||
algorithm.check_utf8(simd_input);
|
||||
}
|
||||
algorithm.check_incomplete_pending();
|
||||
if algorithm.has_error() {
|
||||
Err(idx)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Low-level implementation of the [`basic::imp::Utf8Validator`] trait.
|
||||
///
|
||||
/// This is implementation requires CPU SIMD features specified by the module it resides in.
|
||||
/// It is undefined behavior to call it if the required CPU features are not
|
||||
/// available.
|
||||
#[cfg(feature = "public_imp")]
|
||||
pub struct Utf8ValidatorImp {
|
||||
algorithm: Utf8CheckAlgorithm<SimdU8Value>,
|
||||
incomplete_data: [u8; 64],
|
||||
incomplete_len: usize,
|
||||
}
|
||||
|
||||
#[cfg(feature = "public_imp")]
|
||||
impl Utf8ValidatorImp {
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn update_from_incomplete_data(&mut self) {
|
||||
let simd_input = SimdInput::new(&self.incomplete_data);
|
||||
self.algorithm.check_utf8(simd_input);
|
||||
self.incomplete_len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "public_imp")]
|
||||
impl basic::imp::Utf8Validator for Utf8ValidatorImp {
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
#[must_use]
|
||||
unsafe fn new() -> Self {
|
||||
Self {
|
||||
algorithm: Utf8CheckAlgorithm::<SimdU8Value>::default(),
|
||||
incomplete_data: [0; 64],
|
||||
incomplete_len: 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn update(&mut self, mut input: &[u8]) {
|
||||
use crate::implementation::helpers::SIMD_CHUNK_SIZE;
|
||||
if input.is_empty() {
|
||||
return;
|
||||
}
|
||||
if self.incomplete_len != 0 {
|
||||
let to_copy =
|
||||
core::cmp::min(SIMD_CHUNK_SIZE - self.incomplete_len, input.len());
|
||||
self.incomplete_data
|
||||
.as_mut_ptr()
|
||||
.add(self.incomplete_len)
|
||||
.copy_from_nonoverlapping(input.as_ptr(), to_copy);
|
||||
if self.incomplete_len + to_copy == SIMD_CHUNK_SIZE {
|
||||
self.update_from_incomplete_data();
|
||||
input = &input[to_copy..];
|
||||
} else {
|
||||
self.incomplete_len += to_copy;
|
||||
return;
|
||||
}
|
||||
}
|
||||
let len = input.len();
|
||||
let mut idx: usize = 0;
|
||||
let iter_lim = len - (len % SIMD_CHUNK_SIZE);
|
||||
while idx < iter_lim {
|
||||
let input = SimdInput::new(input.get_unchecked(idx as usize..));
|
||||
self.algorithm.check_utf8(input);
|
||||
idx += SIMD_CHUNK_SIZE;
|
||||
}
|
||||
if idx < len {
|
||||
let to_copy = len - idx;
|
||||
self.incomplete_data
|
||||
.as_mut_ptr()
|
||||
.copy_from_nonoverlapping(input.as_ptr().add(idx), to_copy);
|
||||
self.incomplete_len = to_copy;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn finalize(mut self) -> core::result::Result<(), basic::Utf8Error> {
|
||||
if self.incomplete_len != 0 {
|
||||
for i in &mut self.incomplete_data[self.incomplete_len..] {
|
||||
*i = 0;
|
||||
}
|
||||
self.update_from_incomplete_data();
|
||||
}
|
||||
self.algorithm.check_incomplete_pending();
|
||||
if self.algorithm.has_error() {
|
||||
Err(basic::Utf8Error {})
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Low-level implementation of the [`basic::imp::ChunkedUtf8Validator`] trait.
|
||||
///
|
||||
/// This is implementation requires CPU SIMD features specified by the module it resides in.
|
||||
/// It is undefined behavior to call it if the required CPU features are not
|
||||
/// available.
|
||||
#[cfg(feature = "public_imp")]
|
||||
pub struct ChunkedUtf8ValidatorImp {
|
||||
algorithm: Utf8CheckAlgorithm<SimdU8Value>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "public_imp")]
|
||||
impl basic::imp::ChunkedUtf8Validator for ChunkedUtf8ValidatorImp {
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
#[must_use]
|
||||
unsafe fn new() -> Self {
|
||||
Self {
|
||||
algorithm: Utf8CheckAlgorithm::<SimdU8Value>::default(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn update_from_chunks(&mut self, input: &[u8]) {
|
||||
use crate::implementation::helpers::SIMD_CHUNK_SIZE;
|
||||
|
||||
assert!(
|
||||
input.len() % SIMD_CHUNK_SIZE == 0,
|
||||
"Input size must be a multiple of 64."
|
||||
);
|
||||
for chunk in input.chunks_exact(SIMD_CHUNK_SIZE) {
|
||||
let input = SimdInput::new(chunk);
|
||||
self.algorithm.check_utf8(input);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn finalize(
|
||||
mut self,
|
||||
remaining_input: core::option::Option<&[u8]>,
|
||||
) -> core::result::Result<(), basic::Utf8Error> {
|
||||
use crate::implementation::helpers::SIMD_CHUNK_SIZE;
|
||||
|
||||
if let Some(mut remaining_input) = remaining_input {
|
||||
if !remaining_input.is_empty() {
|
||||
let len = remaining_input.len();
|
||||
let chunks_lim = len - (len % SIMD_CHUNK_SIZE);
|
||||
if chunks_lim > 0 {
|
||||
self.update_from_chunks(&remaining_input[..chunks_lim]);
|
||||
}
|
||||
let rem = len - chunks_lim;
|
||||
if rem > 0 {
|
||||
remaining_input = &remaining_input[chunks_lim..];
|
||||
let mut tmpbuf = TempSimdChunk::new();
|
||||
tmpbuf.0.as_mut_ptr().copy_from_nonoverlapping(
|
||||
remaining_input.as_ptr(),
|
||||
remaining_input.len(),
|
||||
);
|
||||
let simd_input = SimdInput::new(&tmpbuf.0);
|
||||
self.algorithm.check_utf8(simd_input);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.algorithm.check_incomplete_pending();
|
||||
if self.algorithm.has_error() {
|
||||
Err(basic::Utf8Error {})
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! simd_input_128_bit {
|
||||
($feat:expr) => {
|
||||
#[repr(C)]
|
||||
struct SimdInput {
|
||||
vals: [SimdU8Value; 4],
|
||||
}
|
||||
|
||||
impl SimdInput {
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
unsafe fn new(ptr: &[u8]) -> Self {
|
||||
Self {
|
||||
vals: [
|
||||
SimdU8Value::load_from(ptr.as_ptr()),
|
||||
SimdU8Value::load_from(ptr.as_ptr().add(16)),
|
||||
SimdU8Value::load_from(ptr.as_ptr().add(32)),
|
||||
SimdU8Value::load_from(ptr.as_ptr().add(48)),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn is_ascii(&self) -> bool {
|
||||
let r1 = self.vals[0].or(self.vals[1]);
|
||||
let r2 = self.vals[2].or(self.vals[3]);
|
||||
let r = r1.or(r2);
|
||||
r.is_ascii()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! simd_input_256_bit {
|
||||
($feat:expr) => {
|
||||
#[repr(C)]
|
||||
struct SimdInput {
|
||||
vals: [SimdU8Value; 2],
|
||||
}
|
||||
|
||||
impl SimdInput {
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
unsafe fn new(ptr: &[u8]) -> Self {
|
||||
Self {
|
||||
vals: [
|
||||
SimdU8Value::load_from(ptr.as_ptr()),
|
||||
SimdU8Value::load_from(ptr.as_ptr().add(32)),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_arch="aarch64"), target_feature(enable = $feat))]
|
||||
#[inline]
|
||||
unsafe fn is_ascii(&self) -> bool {
|
||||
self.vals[0].or(self.vals[1]).is_ascii()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
type Utf8ErrorCompat = crate::compat::Utf8Error;
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn validate_utf8_at_offset(input: &[u8], offset: usize) -> Result<(), Utf8ErrorCompat> {
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
match core::str::from_utf8(&input[offset..]) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(Utf8ErrorCompat {
|
||||
valid_up_to: err.valid_up_to() + offset,
|
||||
error_len: err.error_len().map(|len| {
|
||||
// never truncates since std::str::err::Utf8Error::error_len() never returns value larger than 4
|
||||
len as u8
|
||||
}),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
#[cold]
|
||||
#[allow(dead_code)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
pub(crate) fn get_compat_error(input: &[u8], failing_block_pos: usize) -> Utf8ErrorCompat {
|
||||
let offset = if failing_block_pos == 0 {
|
||||
// Error must be in this block since it is the first.
|
||||
0
|
||||
} else {
|
||||
// The previous block is OK except for a possible continuation over the block boundary.
|
||||
// We go backwards over the last three bytes of the previous block and find the
|
||||
// last non-continuation byte as a starting point for an std validation. If the last
|
||||
// three bytes are all continuation bytes then the previous block ends with a four byte
|
||||
// UTF-8 codepoint, is thus complete and valid UTF-8. We start the check with the
|
||||
// current block in that case.
|
||||
(1..=3)
|
||||
.find(|i| input[failing_block_pos - i] >> 6 != 0b10)
|
||||
.map_or(failing_block_pos, |i| failing_block_pos - i)
|
||||
};
|
||||
// UNWRAP: safe because the SIMD UTF-8 validation found an error
|
||||
validate_utf8_at_offset(input, offset).unwrap_err()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) unsafe fn memcpy_unaligned_nonoverlapping_inline_opt_lt_64(
|
||||
mut src: *const u8,
|
||||
mut dest: *mut u8,
|
||||
mut len: usize,
|
||||
) {
|
||||
// This gets properly auto-vectorized on AVX 2 and SSE 4.2
|
||||
#[inline]
|
||||
unsafe fn memcpy_u64(src: &mut *const u8, dest: &mut *mut u8) {
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
dest.cast::<u64>()
|
||||
.write_unaligned(src.cast::<u64>().read_unaligned());
|
||||
*src = src.offset(8);
|
||||
*dest = dest.offset(8);
|
||||
}
|
||||
if len >= 32 {
|
||||
memcpy_u64(&mut src, &mut dest);
|
||||
memcpy_u64(&mut src, &mut dest);
|
||||
memcpy_u64(&mut src, &mut dest);
|
||||
memcpy_u64(&mut src, &mut dest);
|
||||
len -= 32;
|
||||
}
|
||||
if len >= 16 {
|
||||
memcpy_u64(&mut src, &mut dest);
|
||||
memcpy_u64(&mut src, &mut dest);
|
||||
len -= 16;
|
||||
}
|
||||
if len >= 8 {
|
||||
memcpy_u64(&mut src, &mut dest);
|
||||
len -= 8;
|
||||
}
|
||||
while len > 0 {
|
||||
*dest = *src;
|
||||
src = src.offset(1);
|
||||
dest = dest.offset(1);
|
||||
len -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) const SIMD_CHUNK_SIZE: usize = 64;
|
||||
|
||||
#[repr(C, align(32))]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) struct Utf8CheckAlgorithm<T> {
|
||||
pub(crate) prev: T,
|
||||
pub(crate) incomplete: T,
|
||||
pub(crate) error: T,
|
||||
}
|
||||
|
||||
#[repr(C, align(16))]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) struct TempSimdChunkA16(pub(crate) [u8; SIMD_CHUNK_SIZE]);
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl TempSimdChunkA16 {
|
||||
#[inline]
|
||||
pub(crate) const fn new() -> Self {
|
||||
Self([0; SIMD_CHUNK_SIZE])
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C, align(32))]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) struct TempSimdChunkA32(pub(crate) [u8; SIMD_CHUNK_SIZE]);
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl TempSimdChunkA32 {
|
||||
#[inline]
|
||||
pub(crate) const fn new() -> Self {
|
||||
Self([0; SIMD_CHUNK_SIZE])
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) struct SimdU8Value<T>(pub(crate) T)
|
||||
where
|
||||
T: Copy;
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
//! Contains UTF-8 validation implementations.
|
||||
|
||||
type Utf8ErrorCompat = crate::compat::Utf8Error;
|
||||
type Utf8ErrorBasic = crate::basic::Utf8Error;
|
||||
|
||||
#[allow(unused_macros)]
|
||||
#[macro_use]
|
||||
mod algorithm;
|
||||
|
||||
pub(crate) mod helpers;
|
||||
|
||||
// UTF-8 validation function types
|
||||
|
||||
#[allow(dead_code)]
|
||||
type ValidateUtf8Fn = unsafe fn(input: &[u8]) -> Result<(), Utf8ErrorBasic>;
|
||||
|
||||
#[allow(dead_code)]
|
||||
type ValidateUtf8CompatFn = unsafe fn(input: &[u8]) -> Result<(), Utf8ErrorCompat>;
|
||||
|
||||
// x86 implementation
|
||||
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
pub(crate) mod x86;
|
||||
|
||||
/// Fn needed instead of re-import, otherwise not inlined in non-std case
|
||||
#[allow(clippy::inline_always)]
|
||||
#[inline(always)]
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
pub(super) unsafe fn validate_utf8_basic(input: &[u8]) -> Result<(), Utf8ErrorBasic> {
|
||||
x86::validate_utf8_basic(input)
|
||||
}
|
||||
|
||||
/// Fn needed instead of re-import, otherwise not inlined in non-std case
|
||||
#[allow(clippy::inline_always)]
|
||||
#[inline(always)]
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
pub(super) unsafe fn validate_utf8_compat(input: &[u8]) -> Result<(), Utf8ErrorCompat> {
|
||||
x86::validate_utf8_compat(input)
|
||||
}
|
||||
|
||||
// aarch64 implementation
|
||||
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
pub(crate) mod aarch64;
|
||||
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
pub(super) use aarch64::validate_utf8_basic;
|
||||
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
pub(super) use aarch64::validate_utf8_compat;
|
||||
|
||||
// wasm32 implementation
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub(crate) mod wasm32;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub(super) use wasm32::validate_utf8_basic;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub(super) use wasm32::validate_utf8_compat;
|
||||
|
||||
// fallback for unsupported architectures
|
||||
|
||||
#[cfg(not(any(
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64",
|
||||
target_arch = "aarch64",
|
||||
target_arch = "wasm32"
|
||||
)))]
|
||||
pub(super) use validate_utf8_basic_fallback as validate_utf8_basic;
|
||||
|
||||
#[cfg(not(any(
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64",
|
||||
target_arch = "aarch64",
|
||||
target_arch = "wasm32"
|
||||
)))]
|
||||
pub(super) use validate_utf8_compat_fallback as validate_utf8_compat;
|
||||
|
||||
// fallback method implementations
|
||||
|
||||
#[inline]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn validate_utf8_basic_fallback(input: &[u8]) -> Result<(), Utf8ErrorBasic> {
|
||||
match core::str::from_utf8(input) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(Utf8ErrorBasic {}),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn validate_utf8_compat_fallback(input: &[u8]) -> Result<(), Utf8ErrorCompat> {
|
||||
helpers::validate_utf8_at_offset(input, 0)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#[cfg(target_feature = "simd128")]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) mod simd128;
|
||||
|
||||
#[inline]
|
||||
#[cfg(target_feature = "simd128")]
|
||||
pub(crate) unsafe fn validate_utf8_basic(input: &[u8]) -> Result<(), crate::basic::Utf8Error> {
|
||||
if input.len() < super::helpers::SIMD_CHUNK_SIZE {
|
||||
return super::validate_utf8_basic_fallback(input);
|
||||
}
|
||||
|
||||
validate_utf8_basic_simd128(input)
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
#[cfg(target_feature = "simd128")]
|
||||
unsafe fn validate_utf8_basic_simd128(input: &[u8]) -> Result<(), crate::basic::Utf8Error> {
|
||||
simd128::validate_utf8_basic(input)
|
||||
}
|
||||
|
||||
#[cfg(not(target_feature = "simd128"))]
|
||||
pub(crate) use super::validate_utf8_basic_fallback as validate_utf8_basic;
|
||||
|
||||
#[inline]
|
||||
#[cfg(target_feature = "simd128")]
|
||||
pub(crate) unsafe fn validate_utf8_compat(input: &[u8]) -> Result<(), crate::compat::Utf8Error> {
|
||||
if input.len() < super::helpers::SIMD_CHUNK_SIZE {
|
||||
return super::validate_utf8_compat_fallback(input);
|
||||
}
|
||||
|
||||
validate_utf8_compat_simd128(input)
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
#[cfg(target_feature = "simd128")]
|
||||
unsafe fn validate_utf8_compat_simd128(input: &[u8]) -> Result<(), crate::compat::Utf8Error> {
|
||||
simd128::validate_utf8_compat(input)
|
||||
}
|
||||
|
||||
#[cfg(not(target_feature = "simd128"))]
|
||||
pub(crate) use super::validate_utf8_compat_fallback as validate_utf8_compat;
|
||||
@@ -0,0 +1,285 @@
|
||||
//! Contains the wasm32 UTF-8 validation implementation.
|
||||
|
||||
use core::arch::wasm32::{
|
||||
u8x16, u8x16_all_true, u8x16_gt, u8x16_lt, u8x16_shr, u8x16_shuffle, u8x16_splat,
|
||||
u8x16_sub_sat, u8x16_swizzle, v128, v128_and, v128_any_true, v128_or, v128_xor,
|
||||
};
|
||||
|
||||
use crate::implementation::helpers::Utf8CheckAlgorithm;
|
||||
|
||||
// wasm32 SIMD primitives
|
||||
|
||||
type SimdU8Value = crate::implementation::helpers::SimdU8Value<v128>;
|
||||
|
||||
#[repr(C, align(16))]
|
||||
struct AlignV128Array([u8; 16]);
|
||||
|
||||
impl SimdU8Value {
|
||||
#[inline]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
unsafe fn from_32_cut_off_leading(
|
||||
_v0: u8,
|
||||
_v1: u8,
|
||||
_v2: u8,
|
||||
_v3: u8,
|
||||
_v4: u8,
|
||||
_v5: u8,
|
||||
_v6: u8,
|
||||
_v7: u8,
|
||||
_v8: u8,
|
||||
_v9: u8,
|
||||
_v10: u8,
|
||||
_v11: u8,
|
||||
_v12: u8,
|
||||
_v13: u8,
|
||||
_v14: u8,
|
||||
_v15: u8,
|
||||
v16: u8,
|
||||
v17: u8,
|
||||
v18: u8,
|
||||
v19: u8,
|
||||
v20: u8,
|
||||
v21: u8,
|
||||
v22: u8,
|
||||
v23: u8,
|
||||
v24: u8,
|
||||
v25: u8,
|
||||
v26: u8,
|
||||
v27: u8,
|
||||
v28: u8,
|
||||
v29: u8,
|
||||
v30: u8,
|
||||
v31: u8,
|
||||
) -> Self {
|
||||
let arr = AlignV128Array([
|
||||
v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31,
|
||||
]);
|
||||
Self::from(*(arr.0.as_ptr().cast::<v128>()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
unsafe fn repeat_16(
|
||||
v0: u8,
|
||||
v1: u8,
|
||||
v2: u8,
|
||||
v3: u8,
|
||||
v4: u8,
|
||||
v5: u8,
|
||||
v6: u8,
|
||||
v7: u8,
|
||||
v8: u8,
|
||||
v9: u8,
|
||||
v10: u8,
|
||||
v11: u8,
|
||||
v12: u8,
|
||||
v13: u8,
|
||||
v14: u8,
|
||||
v15: u8,
|
||||
) -> Self {
|
||||
let arr = AlignV128Array([
|
||||
v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15,
|
||||
]);
|
||||
Self::from(*(arr.0.as_ptr().cast::<v128>()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn load_from(ptr: *const u8) -> Self {
|
||||
Self::from(ptr.cast::<v128>().read_unaligned())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
unsafe fn lookup_16(
|
||||
self,
|
||||
v0: u8,
|
||||
v1: u8,
|
||||
v2: u8,
|
||||
v3: u8,
|
||||
v4: u8,
|
||||
v5: u8,
|
||||
v6: u8,
|
||||
v7: u8,
|
||||
v8: u8,
|
||||
v9: u8,
|
||||
v10: u8,
|
||||
v11: u8,
|
||||
v12: u8,
|
||||
v13: u8,
|
||||
v14: u8,
|
||||
v15: u8,
|
||||
) -> Self {
|
||||
Self::from(u8x16_swizzle(
|
||||
Self::repeat_16(
|
||||
v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15,
|
||||
)
|
||||
.0,
|
||||
self.0,
|
||||
))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
unsafe fn splat(val: u8) -> Self {
|
||||
Self::from(u8x16_splat(val))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
unsafe fn splat0() -> Self {
|
||||
Self::from(u8x16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn or(self, b: Self) -> Self {
|
||||
Self::from(v128_or(self.0, b.0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn and(self, b: Self) -> Self {
|
||||
Self::from(v128_and(self.0, b.0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn xor(self, b: Self) -> Self {
|
||||
Self::from(v128_xor(self.0, b.0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn saturating_sub(self, b: Self) -> Self {
|
||||
Self::from(u8x16_sub_sat(self.0, b.0))
|
||||
}
|
||||
|
||||
// ugly but shr<N> requires const generics
|
||||
|
||||
#[allow(clippy::cast_lossless)]
|
||||
#[inline]
|
||||
unsafe fn shr4(self) -> Self {
|
||||
Self::from(u8x16_shr(self.0, 4))
|
||||
}
|
||||
|
||||
// ugly but prev<N> requires const generics
|
||||
|
||||
// TODO make this into a macro
|
||||
|
||||
#[allow(clippy::cast_lossless)]
|
||||
#[inline]
|
||||
unsafe fn prev1(self, prev: Self) -> Self {
|
||||
Self::from(u8x16_shuffle::<
|
||||
15,
|
||||
16,
|
||||
17,
|
||||
18,
|
||||
19,
|
||||
20,
|
||||
21,
|
||||
22,
|
||||
23,
|
||||
24,
|
||||
25,
|
||||
26,
|
||||
27,
|
||||
28,
|
||||
29,
|
||||
30,
|
||||
>(prev.0, self.0))
|
||||
}
|
||||
|
||||
// ugly but prev<N> requires const generics
|
||||
|
||||
#[allow(clippy::cast_lossless)]
|
||||
#[inline]
|
||||
unsafe fn prev2(self, prev: Self) -> Self {
|
||||
Self::from(u8x16_shuffle::<
|
||||
14,
|
||||
15,
|
||||
16,
|
||||
17,
|
||||
18,
|
||||
19,
|
||||
20,
|
||||
21,
|
||||
22,
|
||||
23,
|
||||
24,
|
||||
25,
|
||||
26,
|
||||
27,
|
||||
28,
|
||||
29,
|
||||
>(prev.0, self.0))
|
||||
}
|
||||
|
||||
// ugly but prev<N> requires const generics
|
||||
|
||||
#[allow(clippy::cast_lossless)]
|
||||
#[inline]
|
||||
unsafe fn prev3(self, prev: Self) -> Self {
|
||||
Self::from(u8x16_shuffle::<
|
||||
13,
|
||||
14,
|
||||
15,
|
||||
16,
|
||||
17,
|
||||
18,
|
||||
19,
|
||||
20,
|
||||
21,
|
||||
22,
|
||||
23,
|
||||
24,
|
||||
25,
|
||||
26,
|
||||
27,
|
||||
28,
|
||||
>(prev.0, self.0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn unsigned_gt(self, other: Self) -> Self {
|
||||
Self::from(u8x16_gt(self.0, other.0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn any_bit_set(self) -> bool {
|
||||
v128_any_true(self.0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn is_ascii(self) -> bool {
|
||||
// We don't want to use u8x16_bitmask as that is inefficient on NEON.
|
||||
// For x86 shifts should also be avoided.
|
||||
u8x16_all_true(u8x16_lt(self.0, u8x16_splat(0b1000_0000_u8)))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<v128> for SimdU8Value {
|
||||
#[inline]
|
||||
fn from(v: v128) -> Self {
|
||||
Self(v)
|
||||
}
|
||||
}
|
||||
|
||||
impl Utf8CheckAlgorithm<SimdU8Value> {
|
||||
#[inline]
|
||||
unsafe fn must_be_2_3_continuation(prev2: SimdU8Value, prev3: SimdU8Value) -> SimdU8Value {
|
||||
let is_third_byte = prev2.unsigned_gt(SimdU8Value::splat(0b1110_0000 - 1));
|
||||
let is_fourth_byte = prev3.unsigned_gt(SimdU8Value::splat(0b1111_0000 - 1));
|
||||
|
||||
is_third_byte.or(is_fourth_byte)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
const fn simd_prefetch(_ptr: *const u8) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
const PREFETCH: bool = false;
|
||||
use crate::implementation::helpers::TempSimdChunkA16 as TempSimdChunk;
|
||||
simd_input_128_bit!("simd128");
|
||||
algorithm_simd!("simd128");
|
||||
+261
@@ -0,0 +1,261 @@
|
||||
//! Contains the x86-64/x86 AVX2 UTF-8 validation implementation.
|
||||
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
|
||||
#[cfg(target_arch = "x86")]
|
||||
use core::arch::x86::{
|
||||
__m256i, _mm256_alignr_epi8, _mm256_and_si256, _mm256_cmpgt_epi8, _mm256_loadu_si256,
|
||||
_mm256_movemask_epi8, _mm256_or_si256, _mm256_permute2x128_si256, _mm256_set1_epi8,
|
||||
_mm256_setr_epi8, _mm256_setzero_si256, _mm256_shuffle_epi8, _mm256_srli_epi16,
|
||||
_mm256_subs_epu8, _mm256_testz_si256, _mm256_xor_si256, _mm_prefetch, _MM_HINT_T0,
|
||||
};
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
use core::arch::x86_64::{
|
||||
__m256i, _mm256_alignr_epi8, _mm256_and_si256, _mm256_cmpgt_epi8, _mm256_loadu_si256,
|
||||
_mm256_movemask_epi8, _mm256_or_si256, _mm256_permute2x128_si256, _mm256_set1_epi8,
|
||||
_mm256_setr_epi8, _mm256_setzero_si256, _mm256_shuffle_epi8, _mm256_srli_epi16,
|
||||
_mm256_subs_epu8, _mm256_testz_si256, _mm256_xor_si256, _mm_prefetch, _MM_HINT_T0,
|
||||
};
|
||||
|
||||
use crate::implementation::helpers::Utf8CheckAlgorithm;
|
||||
|
||||
// AVX 2 SIMD primitives
|
||||
|
||||
type SimdU8Value = crate::implementation::helpers::SimdU8Value<__m256i>;
|
||||
|
||||
impl SimdU8Value {
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn from_32_cut_off_leading(
|
||||
v0: u8,
|
||||
v1: u8,
|
||||
v2: u8,
|
||||
v3: u8,
|
||||
v4: u8,
|
||||
v5: u8,
|
||||
v6: u8,
|
||||
v7: u8,
|
||||
v8: u8,
|
||||
v9: u8,
|
||||
v10: u8,
|
||||
v11: u8,
|
||||
v12: u8,
|
||||
v13: u8,
|
||||
v14: u8,
|
||||
v15: u8,
|
||||
v16: u8,
|
||||
v17: u8,
|
||||
v18: u8,
|
||||
v19: u8,
|
||||
v20: u8,
|
||||
v21: u8,
|
||||
v22: u8,
|
||||
v23: u8,
|
||||
v24: u8,
|
||||
v25: u8,
|
||||
v26: u8,
|
||||
v27: u8,
|
||||
v28: u8,
|
||||
v29: u8,
|
||||
v30: u8,
|
||||
v31: u8,
|
||||
) -> Self {
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
Self::from(_mm256_setr_epi8(
|
||||
v0 as i8, v1 as i8, v2 as i8, v3 as i8, v4 as i8, v5 as i8, v6 as i8, v7 as i8,
|
||||
v8 as i8, v9 as i8, v10 as i8, v11 as i8, v12 as i8, v13 as i8, v14 as i8, v15 as i8,
|
||||
v16 as i8, v17 as i8, v18 as i8, v19 as i8, v20 as i8, v21 as i8, v22 as i8, v23 as i8,
|
||||
v24 as i8, v25 as i8, v26 as i8, v27 as i8, v28 as i8, v29 as i8, v30 as i8, v31 as i8,
|
||||
))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn repeat_16(
|
||||
v0: u8,
|
||||
v1: u8,
|
||||
v2: u8,
|
||||
v3: u8,
|
||||
v4: u8,
|
||||
v5: u8,
|
||||
v6: u8,
|
||||
v7: u8,
|
||||
v8: u8,
|
||||
v9: u8,
|
||||
v10: u8,
|
||||
v11: u8,
|
||||
v12: u8,
|
||||
v13: u8,
|
||||
v14: u8,
|
||||
v15: u8,
|
||||
) -> Self {
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
Self::from_32_cut_off_leading(
|
||||
v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v0, v1, v2, v3,
|
||||
v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15,
|
||||
)
|
||||
}
|
||||
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn load_from(ptr: *const u8) -> Self {
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
Self::from(_mm256_loadu_si256(ptr.cast::<__m256i>()))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn lookup_16(
|
||||
self,
|
||||
v0: u8,
|
||||
v1: u8,
|
||||
v2: u8,
|
||||
v3: u8,
|
||||
v4: u8,
|
||||
v5: u8,
|
||||
v6: u8,
|
||||
v7: u8,
|
||||
v8: u8,
|
||||
v9: u8,
|
||||
v10: u8,
|
||||
v11: u8,
|
||||
v12: u8,
|
||||
v13: u8,
|
||||
v14: u8,
|
||||
v15: u8,
|
||||
) -> Self {
|
||||
Self::from(_mm256_shuffle_epi8(
|
||||
Self::repeat_16(
|
||||
v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15,
|
||||
)
|
||||
.0,
|
||||
self.0,
|
||||
))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn splat(val: u8) -> Self {
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
Self::from(_mm256_set1_epi8(val as i8))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn splat0() -> Self {
|
||||
Self::from(_mm256_setzero_si256())
|
||||
}
|
||||
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn or(self, b: Self) -> Self {
|
||||
Self::from(_mm256_or_si256(self.0, b.0))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn and(self, b: Self) -> Self {
|
||||
Self::from(_mm256_and_si256(self.0, b.0))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn xor(self, b: Self) -> Self {
|
||||
Self::from(_mm256_xor_si256(self.0, b.0))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn saturating_sub(self, b: Self) -> Self {
|
||||
Self::from(_mm256_subs_epu8(self.0, b.0))
|
||||
}
|
||||
|
||||
// ugly but shr<N> requires const generics
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn shr4(self) -> Self {
|
||||
Self::from(_mm256_srli_epi16(self.0, 4)).and(Self::splat(0xFF >> 4))
|
||||
}
|
||||
|
||||
// ugly but prev<N> requires const generics
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn prev1(self, prev: Self) -> Self {
|
||||
Self::from(_mm256_alignr_epi8(
|
||||
self.0,
|
||||
_mm256_permute2x128_si256(prev.0, self.0, 0x21),
|
||||
16 - 1,
|
||||
))
|
||||
}
|
||||
|
||||
// ugly but prev<N> requires const generics
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn prev2(self, prev: Self) -> Self {
|
||||
Self::from(_mm256_alignr_epi8(
|
||||
self.0,
|
||||
_mm256_permute2x128_si256(prev.0, self.0, 0x21),
|
||||
16 - 2,
|
||||
))
|
||||
}
|
||||
|
||||
// ugly but prev<N> requires const generics
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn prev3(self, prev: Self) -> Self {
|
||||
Self::from(_mm256_alignr_epi8(
|
||||
self.0,
|
||||
_mm256_permute2x128_si256(prev.0, self.0, 0x21),
|
||||
16 - 3,
|
||||
))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn signed_gt(self, other: Self) -> Self {
|
||||
Self::from(_mm256_cmpgt_epi8(self.0, other.0))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn any_bit_set(self) -> bool {
|
||||
_mm256_testz_si256(self.0, self.0) != 1
|
||||
}
|
||||
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn is_ascii(self) -> bool {
|
||||
_mm256_movemask_epi8(self.0) == 0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<__m256i> for SimdU8Value {
|
||||
#[inline]
|
||||
fn from(val: __m256i) -> Self {
|
||||
Self(val)
|
||||
}
|
||||
}
|
||||
|
||||
impl Utf8CheckAlgorithm<SimdU8Value> {
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn must_be_2_3_continuation(prev2: SimdU8Value, prev3: SimdU8Value) -> SimdU8Value {
|
||||
let is_third_byte = prev2.saturating_sub(SimdU8Value::splat(0b1110_0000 - 1));
|
||||
let is_fourth_byte = prev3.saturating_sub(SimdU8Value::splat(0b1111_0000 - 1));
|
||||
|
||||
is_third_byte
|
||||
.or(is_fourth_byte)
|
||||
.signed_gt(SimdU8Value::splat0())
|
||||
}
|
||||
}
|
||||
|
||||
#[target_feature(enable = "avx2")]
|
||||
#[inline]
|
||||
unsafe fn simd_prefetch(ptr: *const u8) {
|
||||
_mm_prefetch(ptr.cast::<i8>(), _MM_HINT_T0);
|
||||
}
|
||||
|
||||
const PREFETCH: bool = true;
|
||||
use crate::implementation::helpers::TempSimdChunkA32 as TempSimdChunk;
|
||||
simd_input_256_bit!("avx2");
|
||||
algorithm_simd!("avx2");
|
||||
+199
@@ -0,0 +1,199 @@
|
||||
#[allow(dead_code)]
|
||||
pub(crate) mod avx2;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) mod sse42;
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use super::helpers::SIMD_CHUNK_SIZE;
|
||||
|
||||
// validate_utf8_basic() std: implementation auto-selection
|
||||
|
||||
#[cfg(all(feature = "std", not(target_feature = "avx2")))]
|
||||
#[inline]
|
||||
pub(crate) unsafe fn validate_utf8_basic(
|
||||
input: &[u8],
|
||||
) -> core::result::Result<(), crate::basic::Utf8Error> {
|
||||
use core::mem;
|
||||
use std::sync::atomic::{AtomicPtr, Ordering};
|
||||
|
||||
type FnRaw = *mut ();
|
||||
|
||||
static FN: AtomicPtr<()> = AtomicPtr::new(get_fastest as FnRaw);
|
||||
|
||||
unsafe fn get_fastest(input: &[u8]) -> core::result::Result<(), crate::basic::Utf8Error> {
|
||||
let fun = get_fastest_available_implementation_basic();
|
||||
FN.store(fun as FnRaw, Ordering::Relaxed);
|
||||
(fun)(input)
|
||||
}
|
||||
|
||||
if input.len() < SIMD_CHUNK_SIZE {
|
||||
return super::validate_utf8_basic_fallback(input);
|
||||
}
|
||||
|
||||
let fun = FN.load(Ordering::Relaxed);
|
||||
mem::transmute::<FnRaw, super::ValidateUtf8Fn>(fun)(input)
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "std", not(target_feature = "avx2")))]
|
||||
#[inline]
|
||||
fn get_fastest_available_implementation_basic() -> super::ValidateUtf8Fn {
|
||||
if std::is_x86_feature_detected!("avx2") {
|
||||
avx2::validate_utf8_basic
|
||||
} else if std::is_x86_feature_detected!("sse4.2") {
|
||||
sse42::validate_utf8_basic
|
||||
} else {
|
||||
super::validate_utf8_basic_fallback
|
||||
}
|
||||
}
|
||||
|
||||
// validate_utf8_basic() no-std: implementation selection by config
|
||||
|
||||
#[cfg(target_feature = "avx2")]
|
||||
pub(crate) unsafe fn validate_utf8_basic(
|
||||
input: &[u8],
|
||||
) -> core::result::Result<(), crate::basic::Utf8Error> {
|
||||
if input.len() < SIMD_CHUNK_SIZE {
|
||||
return super::validate_utf8_basic_fallback(input);
|
||||
}
|
||||
|
||||
validate_utf8_basic_avx2(input)
|
||||
}
|
||||
|
||||
#[cfg(target_feature = "avx2")]
|
||||
#[inline(never)]
|
||||
unsafe fn validate_utf8_basic_avx2(
|
||||
input: &[u8],
|
||||
) -> core::result::Result<(), crate::basic::Utf8Error> {
|
||||
avx2::validate_utf8_basic(input)
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
not(feature = "std"),
|
||||
not(target_feature = "avx2"),
|
||||
target_feature = "sse4.2"
|
||||
))]
|
||||
pub(crate) unsafe fn validate_utf8_basic(
|
||||
input: &[u8],
|
||||
) -> core::result::Result<(), crate::basic::Utf8Error> {
|
||||
if input.len() < SIMD_CHUNK_SIZE {
|
||||
return super::validate_utf8_basic_fallback(input);
|
||||
}
|
||||
|
||||
validate_utf8_basic_sse42(input)
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
not(feature = "std"),
|
||||
not(target_feature = "avx2"),
|
||||
target_feature = "sse4.2"
|
||||
))]
|
||||
#[inline(never)]
|
||||
unsafe fn validate_utf8_basic_sse42(
|
||||
input: &[u8],
|
||||
) -> core::result::Result<(), crate::basic::Utf8Error> {
|
||||
sse42::validate_utf8_basic(input)
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
not(feature = "std"),
|
||||
not(target_feature = "avx2"),
|
||||
not(target_feature = "sse4.2")
|
||||
))]
|
||||
pub(crate) use super::validate_utf8_basic_fallback as validate_utf8_basic;
|
||||
|
||||
// validate_utf8_compat() std: implementation auto-selection
|
||||
|
||||
#[cfg(all(feature = "std", not(target_feature = "avx2")))]
|
||||
#[cfg(feature = "std")]
|
||||
#[inline]
|
||||
pub(crate) unsafe fn validate_utf8_compat(
|
||||
input: &[u8],
|
||||
) -> core::result::Result<(), crate::compat::Utf8Error> {
|
||||
use core::mem;
|
||||
use std::sync::atomic::{AtomicPtr, Ordering};
|
||||
|
||||
type FnRaw = *mut ();
|
||||
|
||||
static FN: AtomicPtr<()> = AtomicPtr::new(get_fastest as FnRaw);
|
||||
|
||||
unsafe fn get_fastest(input: &[u8]) -> core::result::Result<(), crate::compat::Utf8Error> {
|
||||
let fun = get_fastest_available_implementation_compat();
|
||||
FN.store(fun as FnRaw, Ordering::Relaxed);
|
||||
(fun)(input)
|
||||
}
|
||||
|
||||
if input.len() < SIMD_CHUNK_SIZE {
|
||||
return super::validate_utf8_compat_fallback(input);
|
||||
}
|
||||
|
||||
let fun = FN.load(Ordering::Relaxed);
|
||||
mem::transmute::<FnRaw, super::ValidateUtf8CompatFn>(fun)(input)
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "std", not(target_feature = "avx2")))]
|
||||
#[inline]
|
||||
fn get_fastest_available_implementation_compat() -> super::ValidateUtf8CompatFn {
|
||||
if std::is_x86_feature_detected!("avx2") {
|
||||
avx2::validate_utf8_compat
|
||||
} else if std::is_x86_feature_detected!("sse4.2") {
|
||||
sse42::validate_utf8_compat
|
||||
} else {
|
||||
super::validate_utf8_compat_fallback
|
||||
}
|
||||
}
|
||||
|
||||
// validate_utf8_basic() no-std: implementation selection by config
|
||||
|
||||
#[cfg(target_feature = "avx2")]
|
||||
pub(crate) unsafe fn validate_utf8_compat(
|
||||
input: &[u8],
|
||||
) -> core::result::Result<(), crate::compat::Utf8Error> {
|
||||
if input.len() < SIMD_CHUNK_SIZE {
|
||||
return super::validate_utf8_compat_fallback(input);
|
||||
}
|
||||
|
||||
validate_utf8_compat_avx2(input)
|
||||
}
|
||||
|
||||
#[cfg(target_feature = "avx2")]
|
||||
#[inline(never)]
|
||||
unsafe fn validate_utf8_compat_avx2(
|
||||
input: &[u8],
|
||||
) -> core::result::Result<(), crate::compat::Utf8Error> {
|
||||
avx2::validate_utf8_compat(input)
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
not(feature = "std"),
|
||||
not(target_feature = "avx2"),
|
||||
target_feature = "sse4.2"
|
||||
))]
|
||||
pub(crate) unsafe fn validate_utf8_compat(
|
||||
input: &[u8],
|
||||
) -> core::result::Result<(), crate::compat::Utf8Error> {
|
||||
if input.len() < SIMD_CHUNK_SIZE {
|
||||
return super::validate_utf8_compat_fallback(input);
|
||||
}
|
||||
|
||||
validate_utf8_compat_sse42(input)
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
not(feature = "std"),
|
||||
not(target_feature = "avx2"),
|
||||
target_feature = "sse4.2"
|
||||
))]
|
||||
#[inline(never)]
|
||||
pub(crate) unsafe fn validate_utf8_compat_sse42(
|
||||
input: &[u8],
|
||||
) -> core::result::Result<(), crate::compat::Utf8Error> {
|
||||
sse42::validate_utf8_compat(input)
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
not(feature = "std"),
|
||||
not(target_feature = "avx2"),
|
||||
not(target_feature = "sse4.2")
|
||||
))]
|
||||
pub(crate) use super::validate_utf8_compat_fallback as validate_utf8_compat;
|
||||
+245
@@ -0,0 +1,245 @@
|
||||
//! Contains the x86-64/x86 SSE4.2 UTF-8 validation implementation.
|
||||
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
|
||||
#[cfg(target_arch = "x86")]
|
||||
use core::arch::x86::{
|
||||
__m128i, _mm_alignr_epi8, _mm_and_si128, _mm_cmpgt_epi8, _mm_loadu_si128, _mm_movemask_epi8,
|
||||
_mm_or_si128, _mm_prefetch, _mm_set1_epi8, _mm_setr_epi8, _mm_setzero_si128, _mm_shuffle_epi8,
|
||||
_mm_srli_epi16, _mm_subs_epu8, _mm_testz_si128, _mm_xor_si128, _MM_HINT_T0,
|
||||
};
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
use core::arch::x86_64::{
|
||||
__m128i, _mm_alignr_epi8, _mm_and_si128, _mm_cmpgt_epi8, _mm_loadu_si128, _mm_movemask_epi8,
|
||||
_mm_or_si128, _mm_prefetch, _mm_set1_epi8, _mm_setr_epi8, _mm_setzero_si128, _mm_shuffle_epi8,
|
||||
_mm_srli_epi16, _mm_subs_epu8, _mm_testz_si128, _mm_xor_si128, _MM_HINT_T0,
|
||||
};
|
||||
|
||||
use crate::implementation::helpers::Utf8CheckAlgorithm;
|
||||
|
||||
// SSE 4.2 SIMD primitives
|
||||
|
||||
type SimdU8Value = crate::implementation::helpers::SimdU8Value<__m128i>;
|
||||
|
||||
impl SimdU8Value {
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn from_32_cut_off_leading(
|
||||
_v0: u8,
|
||||
_v1: u8,
|
||||
_v2: u8,
|
||||
_v3: u8,
|
||||
_v4: u8,
|
||||
_v5: u8,
|
||||
_v6: u8,
|
||||
_v7: u8,
|
||||
_v8: u8,
|
||||
_v9: u8,
|
||||
_v10: u8,
|
||||
_v11: u8,
|
||||
_v12: u8,
|
||||
_v13: u8,
|
||||
_v14: u8,
|
||||
_v15: u8,
|
||||
v16: u8,
|
||||
v17: u8,
|
||||
v18: u8,
|
||||
v19: u8,
|
||||
v20: u8,
|
||||
v21: u8,
|
||||
v22: u8,
|
||||
v23: u8,
|
||||
v24: u8,
|
||||
v25: u8,
|
||||
v26: u8,
|
||||
v27: u8,
|
||||
v28: u8,
|
||||
v29: u8,
|
||||
v30: u8,
|
||||
v31: u8,
|
||||
) -> Self {
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
Self::from(_mm_setr_epi8(
|
||||
v16 as i8, v17 as i8, v18 as i8, v19 as i8, v20 as i8, v21 as i8, v22 as i8, v23 as i8,
|
||||
v24 as i8, v25 as i8, v26 as i8, v27 as i8, v28 as i8, v29 as i8, v30 as i8, v31 as i8,
|
||||
))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn repeat_16(
|
||||
v0: u8,
|
||||
v1: u8,
|
||||
v2: u8,
|
||||
v3: u8,
|
||||
v4: u8,
|
||||
v5: u8,
|
||||
v6: u8,
|
||||
v7: u8,
|
||||
v8: u8,
|
||||
v9: u8,
|
||||
v10: u8,
|
||||
v11: u8,
|
||||
v12: u8,
|
||||
v13: u8,
|
||||
v14: u8,
|
||||
v15: u8,
|
||||
) -> Self {
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
Self::from(_mm_setr_epi8(
|
||||
v0 as i8, v1 as i8, v2 as i8, v3 as i8, v4 as i8, v5 as i8, v6 as i8, v7 as i8,
|
||||
v8 as i8, v9 as i8, v10 as i8, v11 as i8, v12 as i8, v13 as i8, v14 as i8, v15 as i8,
|
||||
))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn load_from(ptr: *const u8) -> Self {
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
Self::from(_mm_loadu_si128(ptr.cast::<__m128i>()))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn lookup_16(
|
||||
self,
|
||||
v0: u8,
|
||||
v1: u8,
|
||||
v2: u8,
|
||||
v3: u8,
|
||||
v4: u8,
|
||||
v5: u8,
|
||||
v6: u8,
|
||||
v7: u8,
|
||||
v8: u8,
|
||||
v9: u8,
|
||||
v10: u8,
|
||||
v11: u8,
|
||||
v12: u8,
|
||||
v13: u8,
|
||||
v14: u8,
|
||||
v15: u8,
|
||||
) -> Self {
|
||||
Self::from(_mm_shuffle_epi8(
|
||||
Self::repeat_16(
|
||||
v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15,
|
||||
)
|
||||
.0,
|
||||
self.0,
|
||||
))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn splat(val: u8) -> Self {
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
Self::from(_mm_set1_epi8(val as i8))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn splat0() -> Self {
|
||||
Self::from(_mm_setzero_si128())
|
||||
}
|
||||
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn or(self, b: Self) -> Self {
|
||||
Self::from(_mm_or_si128(self.0, b.0))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn and(self, b: Self) -> Self {
|
||||
Self::from(_mm_and_si128(self.0, b.0))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn xor(self, b: Self) -> Self {
|
||||
Self::from(_mm_xor_si128(self.0, b.0))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn saturating_sub(self, b: Self) -> Self {
|
||||
Self::from(_mm_subs_epu8(self.0, b.0))
|
||||
}
|
||||
|
||||
// ugly but shr<N> requires const generics
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn shr4(self) -> Self {
|
||||
Self::from(_mm_srli_epi16(self.0, 4)).and(Self::splat(0xFF >> 4))
|
||||
}
|
||||
|
||||
// ugly but prev<N> requires const generics
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn prev1(self, prev: Self) -> Self {
|
||||
Self::from(_mm_alignr_epi8(self.0, prev.0, 16 - 1))
|
||||
}
|
||||
|
||||
// ugly but prev<N> requires const generics
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn prev2(self, prev: Self) -> Self {
|
||||
Self::from(_mm_alignr_epi8(self.0, prev.0, 16 - 2))
|
||||
}
|
||||
|
||||
// ugly but prev<N> requires const generics
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn prev3(self, prev: Self) -> Self {
|
||||
Self::from(_mm_alignr_epi8(self.0, prev.0, 16 - 3))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn signed_gt(self, other: Self) -> Self {
|
||||
Self::from(_mm_cmpgt_epi8(self.0, other.0))
|
||||
}
|
||||
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn any_bit_set(self) -> bool {
|
||||
_mm_testz_si128(self.0, self.0) != 1
|
||||
}
|
||||
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn is_ascii(self) -> bool {
|
||||
_mm_movemask_epi8(self.0) == 0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<__m128i> for SimdU8Value {
|
||||
#[inline]
|
||||
fn from(val: __m128i) -> Self {
|
||||
Self(val)
|
||||
}
|
||||
}
|
||||
|
||||
impl Utf8CheckAlgorithm<SimdU8Value> {
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn must_be_2_3_continuation(prev2: SimdU8Value, prev3: SimdU8Value) -> SimdU8Value {
|
||||
let is_third_byte = prev2.saturating_sub(SimdU8Value::splat(0b1110_0000 - 1));
|
||||
let is_fourth_byte = prev3.saturating_sub(SimdU8Value::splat(0b1111_0000 - 1));
|
||||
|
||||
is_third_byte
|
||||
.or(is_fourth_byte)
|
||||
.signed_gt(SimdU8Value::splat0())
|
||||
}
|
||||
}
|
||||
|
||||
#[target_feature(enable = "sse4.2")]
|
||||
#[inline]
|
||||
unsafe fn simd_prefetch(ptr: *const u8) {
|
||||
_mm_prefetch(ptr.cast::<i8>(), _MM_HINT_T0);
|
||||
}
|
||||
|
||||
const PREFETCH: bool = false;
|
||||
use crate::implementation::helpers::TempSimdChunkA16 as TempSimdChunk;
|
||||
simd_input_128_bit!("sse4.2");
|
||||
algorithm_simd!("sse4.2");
|
||||
Reference in New Issue
Block a user