Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/nix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ jobs:
auto-push: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
alert-threshold: '110%'
fail-on-alert: true
fail-threshold: '120%'
fail-threshold: '130%'
comment-on-alert: true
comment-always: ${{ github.event_name == 'pull_request' }}
benchmark-data-dir-path: dev/bench
Expand Down
2 changes: 1 addition & 1 deletion src/decoder/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub(crate) type Table = [Entry; 256];

#[inline(always)]
pub(crate) fn decode_helper<'a>(table: &Table, src: &'a [u8]) -> Cow<'a, str> {
if src.is_ascii() {
if crate::is_ascii(src) {
let s = unsafe { std::str::from_utf8_unchecked(src) };
return s.into();
}
Expand Down
2 changes: 1 addition & 1 deletion src/decoder/incomplete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub(crate) fn decode_helper<'a>(
fallback: Option<char>,
) -> Result<Cow<'a, str>, DecodeError> {
let fallback: Option<Entry> = fallback.map(Entry::from_char);
if bytes.is_ascii() {
if crate::is_ascii(bytes) {
let s = unsafe { std::str::from_utf8_unchecked(bytes) };
return Ok(s.into());
}
Expand Down
2 changes: 1 addition & 1 deletion src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub trait Encoder {
fallback: Option<u8>,
) -> Result<Cow<'a, [u8]>, EncodeError> {
let mut src = s.as_bytes();
if s.is_ascii() {
if crate::is_ascii_str(s) {
return Ok(src.into());
}
let len = s.chars().count();
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use thiserror::Error;
pub mod code_pages;
pub(crate) mod decoder;
mod encoder;
mod simd;
pub(crate) use encoder::Encoder;
pub(crate) use simd::{is_ascii, is_ascii_str};

#[derive(Error, Debug)]
#[error("Character in UTF-8 string has no mapping defined in code page")]
Expand Down
51 changes: 51 additions & 0 deletions src/simd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! SIMD-optimized ASCII detection.
//!
//! Works around LLVM codegen issues with `-C target-cpu=native` on AVX512 CPUs.

/// Check if all bytes are ASCII (< 128).
#[inline]
pub fn is_ascii(bytes: &[u8]) -> bool {
#[cfg(target_arch = "x86_64")]
if is_x86_feature_detected!("avx512bw") {
return unsafe { is_ascii_avx512(bytes) };
}
bytes.is_ascii()
}

/// Check if all characters in the string are ASCII.
#[inline]
pub fn is_ascii_str(s: &str) -> bool {
is_ascii(s.as_bytes())
}

#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx512bw")]
/// # Safety
/// Caller must ensure AVX512BW is available (via `is_x86_feature_detected!`).
unsafe fn is_ascii_avx512(bytes: &[u8]) -> bool {
use core::arch::x86_64::*;

let ptr = bytes.as_ptr();
let len = bytes.len();
let mut i = 0;

// Process 64-byte chunks
// SAFETY: Loop condition ensures i+64 <= len
while i + 64 <= len {
if _mm512_movepi8_mask(_mm512_loadu_si512(ptr.add(i).cast())) != 0 {
return false;
}
i += 64;
}

// Tail: masked load for remaining bytes
if i < len {
// SAFETY: Mask has only `len - i` bits set, so only valid bytes are loaded
let mask = (1u64 << (len - i)) - 1;
if _mm512_movepi8_mask(_mm512_maskz_loadu_epi8(mask, ptr.add(i).cast())) != 0 {
return false;
}
}

true
}
Loading