Skip to content

Commit

Permalink
feat: add wasm32 arch, implement check
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Sep 29, 2024
1 parent e6f49b5 commit ea86b6a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ jobs:
strategy:
fail-fast: false
matrix:
target: [aarch64-unknown-linux-gnu, i686-unknown-linux-gnu, x86_64-unknown-linux-gnu]
target:
- aarch64-unknown-linux-gnu
- i686-unknown-linux-gnu
- x86_64-unknown-linux-gnu
- wasm32-wasip1
rust: [nightly, stable, 1.64]
exclude:
- target: wasm32-wasip1
rust: 1.64
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
Expand Down
3 changes: 3 additions & 0 deletions src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ 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")] {
pub(crate) mod wasm32;
pub(crate) use wasm32 as imp;
} else {
pub(crate) use generic as imp;
}
Expand Down
40 changes: 40 additions & 0 deletions src/arch/wasm32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::generic;
use core::arch::wasm32::*;

pub(crate) const USE_CHECK_FN: bool = false;

pub(crate) use generic::{decode_checked, decode_unchecked, encode};

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

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

#[target_feature(enable = "simd128")]
unsafe fn check_simd128(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'));
let valid_digit = v128_and(ge0, le9);

let geua = u8x16_ge(chunk, u8x16_splat(b'A'));
let leuf = u8x16_le(chunk, u8x16_splat(b'F'));
let valid_upper = v128_and(geua, leuf);

let gela = u8x16_ge(chunk, u8x16_splat(b'a'));
let lelf = u8x16_le(chunk, u8x16_splat(b'f'));
let valid_lower = v128_and(gela, lelf);

let valid_letter = v128_or(valid_lower, valid_upper);
let valid = v128_or(valid_digit, valid_letter);
u8x16_all_true(valid)
})
}

0 comments on commit ea86b6a

Please sign in to comment.