pub(crate) mod common;
mod specialized;
use crate::core::dictionary::Dictionary;
use crate::simd::variants::{identify_base32_variant, identify_base64_variant};
use specialized::base16::identify_hex_variant;
use std::sync::OnceLock;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)]
pub enum SimdLevel {
Avx2,
Ssse3,
None,
}
#[allow(dead_code)]
static SIMD_LEVEL: OnceLock<SimdLevel> = OnceLock::new();
#[inline]
#[allow(dead_code)]
fn detect_simd_level() -> SimdLevel {
#[cfg(target_arch = "x86_64")]
{
if is_x86_feature_detected!("avx2") {
SimdLevel::Avx2
} else if is_x86_feature_detected!("ssse3") {
SimdLevel::Ssse3
} else {
SimdLevel::None
}
}
#[cfg(not(target_arch = "x86_64"))]
{
SimdLevel::None
}
}
#[inline]
#[allow(dead_code)]
pub fn simd_level() -> SimdLevel {
*SIMD_LEVEL.get_or_init(detect_simd_level)
}
#[inline]
#[allow(dead_code)]
pub fn has_avx2() -> bool {
matches!(simd_level(), SimdLevel::Avx2)
}
#[inline]
#[allow(dead_code)]
pub fn has_ssse3() -> bool {
matches!(simd_level(), SimdLevel::Ssse3 | SimdLevel::Avx2)
}
#[cfg(target_arch = "x86_64")]
pub fn encode_base64_simd(data: &[u8], dictionary: &Dictionary) -> Option<String> {
if dictionary.base() != 64 {
return None;
}
let variant = identify_base64_variant(dictionary)?;
if !is_x86_feature_detected!("ssse3") {
return None;
}
specialized::base64::encode(data, dictionary, variant)
}
#[cfg(target_arch = "x86_64")]
pub fn decode_base64_simd(encoded: &str, dictionary: &Dictionary) -> Option<Vec<u8>> {
if dictionary.base() != 64 {
return None;
}
let variant = identify_base64_variant(dictionary)?;
if !is_x86_feature_detected!("ssse3") {
return None;
}
if encoded.len() < 16 {
return None;
}
specialized::base64::decode(encoded, variant)
}
#[cfg(target_arch = "x86_64")]
pub fn encode_base16_simd(data: &[u8], dictionary: &Dictionary) -> Option<String> {
if dictionary.base() != 16 {
return None;
}
let variant = identify_hex_variant(dictionary)?;
if !is_x86_feature_detected!("ssse3") {
return None;
}
specialized::base16::encode(data, dictionary, variant)
}
#[cfg(target_arch = "x86_64")]
pub fn decode_base16_simd(encoded: &str, dictionary: &Dictionary) -> Option<Vec<u8>> {
if dictionary.base() != 16 {
return None;
}
let variant = identify_hex_variant(dictionary)?;
if !is_x86_feature_detected!("ssse3") {
return None;
}
if encoded.len() < 32 {
return None;
}
specialized::base16::decode(encoded, variant)
}
#[cfg(target_arch = "x86_64")]
pub fn encode_base256_simd(data: &[u8], dictionary: &Dictionary) -> Option<String> {
if dictionary.base() != 256 {
return None;
}
if !is_x86_feature_detected!("ssse3") {
return None;
}
specialized::base256::encode(data, dictionary)
}
#[cfg(target_arch = "x86_64")]
pub fn decode_base256_simd(encoded: &str, dictionary: &Dictionary) -> Option<Vec<u8>> {
if dictionary.base() != 256 {
return None;
}
if !is_x86_feature_detected!("ssse3") {
return None;
}
if encoded.len() < 16 {
return None;
}
specialized::base256::decode(encoded, dictionary)
}
#[cfg(target_arch = "x86_64")]
#[allow(dead_code)]
pub fn encode_base32_simd(data: &[u8], dictionary: &Dictionary) -> Option<String> {
if dictionary.base() != 32 {
return None;
}
let variant = identify_base32_variant(dictionary)?;
if !is_x86_feature_detected!("ssse3") {
return None;
}
specialized::base32::encode(data, dictionary, variant)
}
#[cfg(target_arch = "x86_64")]
#[allow(dead_code)]
pub fn decode_base32_simd(encoded: &str, dictionary: &Dictionary) -> Option<Vec<u8>> {
if dictionary.base() != 32 {
return None;
}
let variant = identify_base32_variant(dictionary)?;
if !is_x86_feature_detected!("ssse3") {
return None;
}
if encoded.len() < 16 {
return None;
}
specialized::base32::decode(encoded, variant)
}