Skip to content

Commit

Permalink
fix: require target_feature = "simd128" on wasm32 (#18)
Browse files Browse the repository at this point in the history
* fix: require `target_feature = "simd128"` on `wasm32`

Fixes #17.

* chore: clippy
  • Loading branch information
DaniPopes authored Nov 21, 2024
1 parent ce406b5 commit aa535d6
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ cfg_if::cfg_if! {
} else if #[cfg(target_arch = "aarch64")] {
pub(crate) mod aarch64;
pub(crate) use aarch64 as imp;
} else if #[cfg(target_arch = "wasm32")] {
// See https://github.com/DaniPopes/const-hex/issues/17
} else if #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))] {
pub(crate) mod wasm32;
pub(crate) use wasm32 as imp;
} else {
Expand Down
23 changes: 2 additions & 21 deletions src/arch/wasm32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,9 @@ use core::arch::wasm32::*;

pub(crate) const USE_CHECK_FN: bool = true;

#[inline(always)]
fn has_simd128() -> bool {
cfg!(target_feature = "simd128")
}

#[inline]
pub(crate) unsafe fn encode<const UPPER: bool>(input: &[u8], output: *mut u8) {
if !has_simd128() {
return generic::encode::<UPPER>(input, output);
}
encode_simd128::<UPPER>(input, output)
}

#[target_feature(enable = "simd128")]
unsafe fn encode_simd128<const UPPER: bool>(input: &[u8], output: *mut u8) {
pub(crate) unsafe fn encode<const UPPER: bool>(input: &[u8], output: *mut u8) {
// Load table.
let hex_table = v128_load(get_chars_table::<UPPER>().as_ptr().cast());

Expand Down Expand Up @@ -61,15 +49,8 @@ unsafe fn encode_simd128<const UPPER: bool>(input: &[u8], output: *mut u8) {
}

#[inline]
pub(crate) fn check(input: &[u8]) -> bool {
if !has_simd128() {
return generic::check(input);
}
check_simd128(input)
}

#[target_feature(enable = "simd128")]
fn check_simd128(input: &[u8]) -> bool {
pub(crate) fn check(input: &[u8]) -> bool {
generic::check_unaligned_chunks(input, |chunk: v128| {
let ge0 = u8x16_ge(chunk, u8x16_splat(b'0'));
let le9 = u8x16_le(chunk, u8x16_splat(b'9'));
Expand Down
1 change: 1 addition & 0 deletions src/arch/x86.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(unsafe_op_in_unsafe_fn)]
#![allow(unexpected_cfgs)]

use super::generic;
use crate::get_chars_table;
Expand Down
2 changes: 1 addition & 1 deletion src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ where
{
struct HexStrVisitor<T>(PhantomData<T>);

impl<'de, T> Visitor<'de> for HexStrVisitor<T>
impl<T> Visitor<'_> for HexStrVisitor<T>
where
T: FromHex,
<T as FromHex>::Error: fmt::Display,
Expand Down

0 comments on commit aa535d6

Please sign in to comment.