-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
portable-simd
as a implementation backend
- Loading branch information
Showing
4 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use crate::default; | ||
use core::simd::{u8x16, Swizzle}; | ||
use core::slice; | ||
|
||
const CHUNK_SIZE: usize = core::mem::size_of::<u8x16>(); | ||
|
||
/// Hex encoding function using [`std::simd`][core::simd]. | ||
/// | ||
/// # Safety | ||
/// | ||
/// `output` must be a valid pointer to at least `2 * input.len()` bytes. | ||
pub(super) unsafe fn encode<const UPPER: bool>(input: &[u8], output: *mut u8) { | ||
let mut i = 0; | ||
let (prefix, chunks, suffix) = input.as_simd::<CHUNK_SIZE>(); | ||
|
||
default::encode::<UPPER>(prefix, output); | ||
i += prefix.len(); | ||
|
||
let hex_table = u8x16::from_array(*crate::get_chars_table::<UPPER>()); | ||
for &chunk in chunks { | ||
// Load input bytes and mask to nibbles. | ||
let mut lo = chunk & u8x16::splat(15); | ||
let mut hi = chunk >> u8x16::splat(4); | ||
|
||
// Lookup the corresponding ASCII hex digit for each nibble. | ||
lo = hex_table.swizzle_dyn(lo); | ||
hi = hex_table.swizzle_dyn(hi); | ||
|
||
// Interleave the nibbles ([hi[0], lo[0], hi[1], lo[1], ...]). | ||
let (hex_lo, hex_hi) = u8x16::interleave(hi, lo); | ||
|
||
// Store result into the output buffer. | ||
hex_lo.copy_to_slice(slice::from_raw_parts_mut(output.add(i), CHUNK_SIZE)); | ||
i += CHUNK_SIZE; | ||
hex_hi.copy_to_slice(slice::from_raw_parts_mut(output.add(i), CHUNK_SIZE)); | ||
i += CHUNK_SIZE; | ||
} | ||
|
||
default::encode::<UPPER>(suffix, output.add(i)); | ||
} | ||
|
||
pub(super) use default::decode; | ||
|
||
struct HexCharsTable<const UPPER: bool>; | ||
|
||
impl<const UPPER: bool> Swizzle<16, 16> for HexCharsTable<UPPER> { | ||
const INDEX: [usize; 16] = [0; 16]; | ||
} |