use generic_array::{
GenericArray,
sequence::GenericSequence,
typenum::{self, Unsigned},
};
use crate::{
isa::InstructionSet,
register::{
BitshiftRegister, BitwiseRegister, CoreRegister, ExtendRegister, IntegerRegister, InterleaveRegister,
MaskElement, MaskRegister, NumericRegister, PartialOrdRegister, Register, Storage, UnsignedIntegerRegister,
ZeroUpper, empty_reg, reg,
},
};
use super::arch;
#[cfg_attr(not(feature = "document_registers"), doc(hidden))]
#[derive(Debug, Clone, Copy, Hash)]
pub struct U8x32V3;
#[thermite_macros::inline_always]
impl CoreRegister for U8x32V3 {
type NativeIsa = crate::backend::x86_v3::X86V3;
type Lanes = typenum::U32;
type Storage = arch::__m256i;
type Mask = Self;
const IS_EMULATED: bool = false;
const EMPTY: Storage<Self> = empty_reg::<Self>();
const HAS_EQUAL_SIZE_MASK: bool = true;
fn blendv(mask: Storage<Self::Mask>, lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_blendv_epi8(lhs, rhs, mask) }
}
fn zz(mask: Storage<Self::Mask>, value: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_and_si256(value, mask) }
}
fn nz(mask: Storage<Self::Mask>, value: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_andnot_si256(mask, value) }
}
fn zeroupper_z<Z: ZeroUpper>(value: Storage<Self>) -> Storage<Self> {
super::I8x32V3::zeroupper_z::<Z>(value) }
fn from_mask(mask: Storage<Self::Mask>) -> Storage<Self> {
mask
}
}
#[thermite_macros::inline_always]
impl ExtendRegister<u8> for U8x32V3 {
fn extend(value: Storage<u8>) -> Storage<Self> {
let mut arr = [0u8; 32];
arr[0] = value;
unsafe { arch::_mm256_loadu_si256(arr.as_ptr() as *const _) }
}
fn narrow(value: Storage<Self>) -> Storage<u8> {
Self::as_slice(&value)[0]
}
}
#[thermite_macros::inline_always]
impl MaskRegister for U8x32V3 {
const FALSY: Storage<Self> = reg::<Self, 32>([0; 32]);
const TRUTHY: Storage<Self> = reg::<Self, 32>([!0; 32]);
fn set(mut mask: Storage<Self::Mask>, lane: usize, value: bool) -> Storage<Self> {
Self::as_mut_slice(&mut mask)[lane] = if value { MaskElement::TRUTHY } else { MaskElement::FALSY };
mask
}
fn test(mask: Storage<Self::Mask>, lane: usize) -> bool {
Self::as_slice(&mask)[lane].to_bool()
}
fn all(value: Storage<Self>) -> bool {
unsafe { arch::_mm256_movemask_epi8(value) as u32 == 0xFFFF_FFFF }
}
fn any(value: Storage<Self>) -> bool {
unsafe { arch::_mm256_movemask_epi8(value) != 0 }
}
fn none(value: Storage<Self>) -> bool {
unsafe { arch::_mm256_movemask_epi8(value) == 0 }
}
fn from_native_bitmask(bitmask: u64) -> Storage<Self> {
unsafe { arch::_mm256_movm_epi8x_v3(bitmask) }
}
fn native_bitmask(value: Storage<Self>) -> Option<u64> {
Some(unsafe { (arch::_mm256_movemask_epi8(value) as u32) as u64 })
}
#[cfg(feature = "bitvec")]
fn fill_bitmask(value: Storage<Self>, view: &mut bitvec::slice::BitSlice<u32>) {
let mask = Self::native_bitmask(value).unwrap() as u32;
let mask = bitvec::slice::BitSlice::from_slice(core::slice::from_ref(&mask));
view.copy_from_bitslice(&mask[..Self::Lanes::USIZE]);
}
}
#[rustfmt::skip] #[thermite_macros::inline_always]
impl BitwiseRegister for U8x32V3 {
fn bitxor(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_xor_si256(lhs, rhs) }
}
fn bitand(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_and_si256(lhs, rhs) }
}
fn bitandnot(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_andnot_si256(lhs, rhs) }
}
fn bitor(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_or_si256(lhs, rhs) }
}
fn not(value: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_xor_si256(value, arch::_mm256_set1_epi8(-1)) }
}
}
#[thermite_macros::inline_always]
impl Register for U8x32V3 {
type Element = u8;
type Signed = super::I8x32V3;
type Unsigned = super::U8x32V3;
fn into_mask(value: Storage<Self>) -> Storage<Self::Mask> {
Self::ne(value, Self::ZERO)
}
fn into_mask_unchecked(value: Storage<Self>) -> Storage<Self::Mask> {
value
}
fn msb_to_mask(value: Storage<Self>) -> Storage<Self::Mask> {
unsafe { arch::_mm256_cmpgt_epi8(arch::_mm256_setzero_si256(), value) }
}
fn new(value: GenericArray<Self::Element, Self::Lanes>) -> Storage<Self> {
unsafe { arch::_mm256_loadu_si256(value.as_ptr() as *const _) }
}
fn single(value: Self::Element) -> Storage<Self> {
let mut arr = [0u8; 32];
arr[0] = value;
unsafe { arch::_mm256_loadu_si256(arr.as_ptr() as *const _) }
}
impl_native_extract!(@epi8x32);
fn splat(value: Self::Element) -> Storage<Self> {
unsafe { arch::_mm256_set1_epi8(value as i8) }
}
unsafe fn load(ptr: *const Self::Element) -> Storage<Self> {
unsafe { arch::_mm256_load_si256(ptr as *const _) }
}
unsafe fn load_unaligned(ptr: *const Self::Element) -> Storage<Self> {
unsafe { arch::_mm256_loadu_si256(ptr as *const _) }
}
unsafe fn store(ptr: *mut Self::Element, value: Storage<Self>) {
unsafe { arch::_mm256_store_si256(ptr as *mut _, value) }
}
unsafe fn store_unaligned(ptr: *mut Self::Element, value: Storage<Self>) {
unsafe { arch::_mm256_storeu_si256(ptr as *mut _, value) }
}
unsafe fn load_stream(ptr: *const Self::Element) -> Storage<Self> {
unsafe { arch::_mm256_stream_load_si256(ptr as _) }
}
unsafe fn store_stream(ptr: *mut Self::Element, value: Storage<Self>) {
unsafe { arch::_mm256_stream_si256(ptr as _, value) }
}
fn reverse(value: Storage<Self>) -> Storage<Self> {
unsafe {
let rev = arch::_mm256_shuffle_epi8(
value,
arch::_mm256_setr_epi8(
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, ),
);
arch::_mm256_permute2x128_si256(rev, rev, 0x01)
}
}
fn swap_bytes(value: Storage<Self>) -> Storage<Self> {
value
}
const HAS_PERMUTEV: bool = <super::I8x32V3 as Register>::HAS_PERMUTEV;
fn permutev(value: Storage<Self>, idxs: GenericArray<u32, Self::Lanes>) -> Storage<Self> {
super::I8x32V3::permutev(value, idxs)
}
compress_via_wide!();
const HAS_NATIVE_ALIGN: bool = true;
fn align<const OFFSET: usize>(a: Storage<Self>, b: Storage<Self>) -> Storage<Self> {
super::I8x32V3::align::<OFFSET>(a, b)
}
}
#[thermite_macros::inline_always]
impl InterleaveRegister for U8x32V3 {
fn interleave(a: Storage<Self>, b: Storage<Self>) -> (Storage<Self>, Storage<Self>) {
super::I8x32V3::interleave(a, b)
}
fn deinterleave(a: Storage<Self>, b: Storage<Self>) -> (Storage<Self>, Storage<Self>) {
super::I8x32V3::deinterleave(a, b)
}
}
#[rustfmt::skip] #[thermite_macros::inline_always]
impl BitshiftRegister for U8x32V3 {
const HAS_TRUE_SHIFTV: bool = false;
const HAS_WIDE_BYTE_SHIFTS: bool = true;
fn bshli<const IMM8: i32>(value: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_bslli_epi128(value, IMM8) }
}
fn bshri<const IMM8: i32>(value: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_bsrli_epi128(value, IMM8) }
}
fn shl(value: Storage<Self>, shift: u32) -> Storage<Self> {
unsafe { arch::_mm256_sll_epi8x_v3(value, shift) }
}
fn shr(value: Storage<Self>, shift: u32) -> Storage<Self> {
unsafe { arch::_mm256_srl_epi8x_v3(value, shift) }
}
fn shli<const IMM8: i32>(value: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_slli_epi8x_v3::<IMM8>(value) }
}
fn shri<const IMM8: i32>(value: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_srli_epi8x_v3::<IMM8>(value) }
}
}
#[thermite_macros::inline_always]
impl PartialOrdRegister for U8x32V3 {
fn gt(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_cmpgt_epu8x_v3(lhs, rhs) }
}
fn lt(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_cmpgt_epu8x_v3(rhs, lhs) }
}
fn eq(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_cmpeq_epi8(lhs, rhs) }
}
}
#[thermite_macros::inline_always]
impl NumericRegister for U8x32V3 {
const ZERO: Storage<Self> = reg::<Self, 32>([0; 32]);
const ONE: Storage<Self> = reg::<Self, 32>([1; 32]);
const TWO: Storage<Self> = reg::<Self, 32>([2; 32]);
const MIN: Storage<Self> = reg::<Self, 32>([u8::MIN; 32]);
const MAX: Storage<Self> = reg::<Self, 32>([u8::MAX; 32]);
fn min_element(value: Storage<Self>) -> Self::Element {
_mm256_reduce_epi8_v3!(value; _mm_min_epu8) as u8
}
fn max_element(value: Storage<Self>) -> Self::Element {
_mm256_reduce_epi8_v3!(value; _mm_max_epu8) as u8
}
fn sum_elements(value: Storage<Self>) -> Self::Element {
_mm256_reduce_epi8_v3!(value; _mm_add_epi8) as u8
}
fn prod_elements(value: Storage<Self>) -> Self::Element {
Self::as_slice(&value).iter().copied().fold(1u8, u8::wrapping_mul)
}
fn offset() -> Storage<Self> {
Self::splat(<Self::Lanes as typenum::Unsigned>::U8)
}
fn indexed() -> Storage<Self> {
Self::new(GenericArray::generate(|i| i as u8))
}
fn add(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_add_epi8(lhs, rhs) }
}
fn sub(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_sub_epi8(lhs, rhs) }
}
fn add_c(mask: Storage<Self::Mask>, lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_add_epi8(lhs, arch::_mm256_and_si256(rhs, mask)) }
}
fn sub_c(mask: Storage<Self::Mask>, lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_sub_epi8(lhs, arch::_mm256_and_si256(rhs, mask)) }
}
fn mul(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_mullo_epi8x_v3(lhs, rhs) }
}
fn div(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
Self::zip(lhs, rhs, |a, b| if b == 0 { 0 } else { a / b })
}
fn rem(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
Self::zip(lhs, rhs, |a, b| if b == 0 { 0 } else { a % b })
}
fn min(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_min_epu8(lhs, rhs) }
}
fn max(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_max_epu8(lhs, rhs) }
}
}
#[thermite_macros::inline_always]
impl IntegerRegister for U8x32V3 {
fn mulhi(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_mulhi_epu8x_v3(lhs, rhs) }
}
fn mullo(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_mullo_epi8x_v3(lhs, rhs) }
}
fn saturating_add(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_adds_epu8(lhs, rhs) }
}
fn saturating_sub(lhs: Storage<Self>, rhs: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_subs_epu8(lhs, rhs) }
}
fn div_branched(value: Storage<Self>, divider: crate::Divider<Self::Element>) -> Storage<Self> {
arch::div_epu::<Self>(value, divider.multiplier(), divider.shift())
}
fn div_branchfree(value: Storage<Self>, divider: crate::BranchfreeDivider<Self::Element>) -> Storage<Self> {
arch::div_epu_bf::<Self>(value, divider.multiplier(), divider.shift())
}
fn divv_branchfree(value: Storage<Self>, dividers: crate::divider::vector::VectorDivider<Self>) -> Storage<Self> {
arch::divv_epu_bf::<Self>(value, dividers.multipliers.0, dividers.shifts.0)
}
const HAS_HARDWARE_POPCNT: bool = false;
fn count_ones(value: Storage<Self>) -> Storage<Self> {
unsafe { arch::_mm256_popcnt_epi8x_v3(value) }
}
fn leading_zeros(value: Storage<Self>) -> Storage<Self> {
Self::sub(Self::splat(8), Self::ilog2p1(value))
}
fn trailing_zeros(value: Storage<Self>) -> Storage<Self> {
super::I8x32V3::trailing_zeros(value)
}
}
#[thermite_macros::inline_always]
impl UnsignedIntegerRegister for U8x32V3 {}