use generic_array::typenum::U4;
use super::arch;
use crate::register::{
CastRegister, ConcatRegister, ExtendRegister, IndexableRegister, NumericRegister, Register, Storage,
array::ArrayRegister, reduced::ReducedRegister,
};
macro_rules! sat_clamp_narrow16 {
($from:ty, $fe:ty, $ie:ty) => {
#[inline(always)]
fn saturating_cast_from(value: Storage<$from>) -> Storage<Self> {
let lo = <$from as Register>::splat(<$ie>::MIN as $fe);
let hi = <$from as Register>::splat(<$ie>::MAX as $fe);
let clamped = <$from as NumericRegister>::min(<$from as NumericRegister>::max(value, lo), hi);
<Self as CastRegister<$from>>::cast_from(clamped)
}
};
}
pub type I16x4V3 = ReducedRegister<super::I16x8V3, U4>;
pub type U16x4V3 = ReducedRegister<super::U16x8V3, U4>;
#[thermite_macros::inline_always]
impl ConcatRegister<ArrayRegister<i16, 2>> for I16x4V3 {
fn concat(lo: Storage<ArrayRegister<i16, 2>>, hi: Storage<ArrayRegister<i16, 2>>) -> Storage<Self> {
ReducedRegister::new(unsafe { arch::_mm_setr_epi16(lo.0[0], lo.0[1], hi.0[0], hi.0[1], 0, 0, 0, 0) })
}
fn split(value: Storage<Self>) -> (Storage<ArrayRegister<i16, 2>>, Storage<ArrayRegister<i16, 2>>) {
let mut arr = [0i16; 8];
unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
(ArrayRegister([arr[0], arr[1]]), ArrayRegister([arr[2], arr[3]]))
}
}
#[thermite_macros::inline_always]
impl ExtendRegister<ArrayRegister<i16, 2>> for I16x4V3 {
fn extend(value: Storage<ArrayRegister<i16, 2>>) -> Storage<Self> {
ReducedRegister::new(unsafe { arch::_mm_setr_epi16(value.0[0], value.0[1], 0, 0, 0, 0, 0, 0) })
}
fn narrow(value: Storage<Self>) -> Storage<ArrayRegister<i16, 2>> {
let mut arr = [0i16; 8];
unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
ArrayRegister([arr[0], arr[1]])
}
}
#[thermite_macros::inline_always]
impl ConcatRegister<ArrayRegister<u16, 2>> for U16x4V3 {
fn concat(lo: Storage<ArrayRegister<u16, 2>>, hi: Storage<ArrayRegister<u16, 2>>) -> Storage<Self> {
ReducedRegister::new(unsafe {
arch::_mm_setr_epi16(
lo.0[0] as i16,
lo.0[1] as i16,
hi.0[0] as i16,
hi.0[1] as i16,
0,
0,
0,
0,
)
})
}
fn split(value: Storage<Self>) -> (Storage<ArrayRegister<u16, 2>>, Storage<ArrayRegister<u16, 2>>) {
let mut arr = [0u16; 8];
unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
(ArrayRegister([arr[0], arr[1]]), ArrayRegister([arr[2], arr[3]]))
}
}
#[thermite_macros::inline_always]
impl ExtendRegister<ArrayRegister<u16, 2>> for U16x4V3 {
fn extend(value: Storage<ArrayRegister<u16, 2>>) -> Storage<Self> {
ReducedRegister::new(unsafe { arch::_mm_setr_epi16(value.0[0] as i16, value.0[1] as i16, 0, 0, 0, 0, 0, 0) })
}
fn narrow(value: Storage<Self>) -> Storage<ArrayRegister<u16, 2>> {
let mut arr = [0u16; 8];
unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
ArrayRegister([arr[0], arr[1]])
}
}
#[thermite_macros::inline_always]
impl ConcatRegister<I16x4V3> for super::I16x8V3 {
fn concat(lo: Storage<I16x4V3>, hi: Storage<I16x4V3>) -> Storage<Self> {
unsafe { arch::_mm_unpacklo_epi64(lo.0, hi.0) }
}
fn split(value: Storage<Self>) -> (Storage<I16x4V3>, Storage<I16x4V3>) {
(
ReducedRegister::new(value),
ReducedRegister::new(unsafe { arch::_mm_unpackhi_epi64(value, value) }),
)
}
}
#[thermite_macros::inline_always]
impl ConcatRegister<U16x4V3> for super::U16x8V3 {
fn concat(lo: Storage<U16x4V3>, hi: Storage<U16x4V3>) -> Storage<Self> {
unsafe { arch::_mm_unpacklo_epi64(lo.0, hi.0) }
}
fn split(value: Storage<Self>) -> (Storage<U16x4V3>, Storage<U16x4V3>) {
(
ReducedRegister::new(value),
ReducedRegister::new(unsafe { arch::_mm_unpackhi_epi64(value, value) }),
)
}
}
#[inline(always)]
fn bool_to_i16_mask(b: bool) -> i16 {
if b { !0 } else { 0 }
}
macro_rules! impl_bool_concat {
($ty:ty) => {
#[thermite_macros::inline_always]
impl ConcatRegister<ArrayRegister<bool, 2>> for $ty {
fn concat(lo: Storage<ArrayRegister<bool, 2>>, hi: Storage<ArrayRegister<bool, 2>>) -> Storage<Self> {
ReducedRegister::new(unsafe {
arch::_mm_setr_epi16(
bool_to_i16_mask(lo.0[0]),
bool_to_i16_mask(lo.0[1]),
bool_to_i16_mask(hi.0[0]),
bool_to_i16_mask(hi.0[1]),
0,
0,
0,
0,
)
})
}
fn split(value: Storage<Self>) -> (Storage<ArrayRegister<bool, 2>>, Storage<ArrayRegister<bool, 2>>) {
let mut arr = [0i16; 8];
unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
(
ArrayRegister([arr[0] != 0, arr[1] != 0]),
ArrayRegister([arr[2] != 0, arr[3] != 0]),
)
}
}
#[thermite_macros::inline_always]
impl ExtendRegister<ArrayRegister<bool, 2>> for $ty {
fn extend(value: Storage<ArrayRegister<bool, 2>>) -> Storage<Self> {
ReducedRegister::new(unsafe {
arch::_mm_setr_epi16(
bool_to_i16_mask(value.0[0]),
bool_to_i16_mask(value.0[1]),
0,
0,
0,
0,
0,
0,
)
})
}
fn narrow(value: Storage<Self>) -> Storage<ArrayRegister<bool, 2>> {
let mut arr = [0i16; 8];
unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
ArrayRegister([arr[0] != 0, arr[1] != 0])
}
}
};
}
impl_bool_concat!(I16x4V3);
impl_bool_concat!(U16x4V3);
#[thermite_macros::inline_always]
impl CastRegister<I16x4V3> for super::I32x4V3 {
fn cast_from(value: Storage<I16x4V3>) -> Storage<Self> {
unsafe { arch::_mm_cvtepi16_epi32(value.0) }
}
}
#[thermite_macros::inline_always]
impl CastRegister<U16x4V3> for super::U32x4V3 {
fn cast_from(value: Storage<U16x4V3>) -> Storage<Self> {
unsafe { arch::_mm_cvtepu16_epi32(value.0) }
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::I32x4V3> for I16x4V3 {
fn cast_from(value: Storage<super::I32x4V3>) -> Storage<Self> {
ReducedRegister::new(unsafe {
arch::_mm_shuffle_epi8(
value,
arch::_mm_setr_epi8(0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1),
)
})
}
fn saturating_cast_from(value: Storage<super::I32x4V3>) -> Storage<Self> {
ReducedRegister::new(unsafe { arch::_mm_packs_epi32(value, value) })
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::U32x4V3> for U16x4V3 {
fn cast_from(value: Storage<super::U32x4V3>) -> Storage<Self> {
ReducedRegister::new(unsafe {
arch::_mm_shuffle_epi8(
value,
arch::_mm_setr_epi8(0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1),
)
})
}
fn saturating_cast_from(value: Storage<super::U32x4V3>) -> Storage<Self> {
let clamped = unsafe { arch::_mm_min_epu32(value, arch::_mm_set1_epi32(0xFFFF)) };
ReducedRegister::new(unsafe { arch::_mm_packus_epi32(clamped, clamped) })
}
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<i16, 2>> for super::half::I32x2V3 {
fn cast_from(value: Storage<ArrayRegister<i16, 2>>) -> Storage<Self> {
ReducedRegister::new(unsafe { arch::_mm_setr_epi32(value.0[0] as i32, value.0[1] as i32, 0, 0) })
}
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<u16, 2>> for super::half::U32x2V3 {
fn cast_from(value: Storage<ArrayRegister<u16, 2>>) -> Storage<Self> {
ReducedRegister::new(unsafe { arch::_mm_setr_epi32(value.0[0] as i32, value.0[1] as i32, 0, 0) })
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::half::I32x2V3> for ArrayRegister<i16, 2> {
fn cast_from(value: Storage<super::half::I32x2V3>) -> Storage<Self> {
let mut arr = [0i32; 4];
unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
ArrayRegister([arr[0] as i16, arr[1] as i16])
}
sat_clamp_narrow16!(super::half::I32x2V3, i32, i16);
}
#[thermite_macros::inline_always]
impl CastRegister<super::half::U32x2V3> for ArrayRegister<u16, 2> {
fn cast_from(value: Storage<super::half::U32x2V3>) -> Storage<Self> {
let mut arr = [0u32; 4];
unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
ArrayRegister([arr[0] as u16, arr[1] as u16])
}
sat_clamp_narrow16!(super::half::U32x2V3, u32, u16);
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<i16, 2>> for super::I64x2V3 {
fn cast_from(value: Storage<ArrayRegister<i16, 2>>) -> Storage<Self> {
unsafe { arch::_mm_set_epi64x(value.0[1] as i64, value.0[0] as i64) }
}
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<u16, 2>> for super::U64x2V3 {
fn cast_from(value: Storage<ArrayRegister<u16, 2>>) -> Storage<Self> {
unsafe { arch::_mm_set_epi64x(value.0[1] as i64, value.0[0] as i64) }
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::I64x2V3> for ArrayRegister<i16, 2> {
fn cast_from(value: Storage<super::I64x2V3>) -> Storage<Self> {
let mut arr = [0i64; 2];
unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value) };
ArrayRegister([arr[0] as i16, arr[1] as i16])
}
sat_clamp_narrow16!(super::I64x2V3, i64, i16);
}
#[thermite_macros::inline_always]
impl CastRegister<super::U64x2V3> for ArrayRegister<u16, 2> {
fn cast_from(value: Storage<super::U64x2V3>) -> Storage<Self> {
let mut arr = [0u64; 2];
unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value) };
ArrayRegister([arr[0] as u16, arr[1] as u16])
}
sat_clamp_narrow16!(super::U64x2V3, u64, u16);
}
#[thermite_macros::inline_always]
impl CastRegister<I16x4V3> for super::I64x4V3 {
fn cast_from(value: Storage<I16x4V3>) -> Storage<Self> {
unsafe { arch::_mm256_cvtepi16_epi64(value.0) }
}
}
#[thermite_macros::inline_always]
impl CastRegister<U16x4V3> for super::U64x4V3 {
fn cast_from(value: Storage<U16x4V3>) -> Storage<Self> {
unsafe { arch::_mm256_cvtepu16_epi64(value.0) }
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::I64x4V3> for I16x4V3 {
fn cast_from(value: Storage<super::I64x4V3>) -> Storage<Self> {
ReducedRegister::new(unsafe { arch::_mm256_cvtepi64_epi16x_v3(value) })
}
sat_clamp_narrow16!(super::I64x4V3, i64, i16);
}
#[thermite_macros::inline_always]
impl CastRegister<super::U64x4V3> for U16x4V3 {
fn cast_from(value: Storage<super::U64x4V3>) -> Storage<Self> {
ReducedRegister::new(unsafe { arch::_mm256_cvtepi64_epi16x_v3(value) })
}
sat_clamp_narrow16!(super::U64x4V3, u64, u16);
}
#[thermite_macros::inline_always]
impl CastRegister<super::I16x8V3> for ArrayRegister<super::I64x4V3, 2> {
fn cast_from(value: Storage<super::I16x8V3>) -> Storage<Self> {
unsafe {
ArrayRegister([
arch::_mm256_cvtepi16_epi64(value), arch::_mm256_cvtepi16_epi64(arch::_mm_srli_si128(value, 8)), ])
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::U16x8V3> for ArrayRegister<super::U64x4V3, 2> {
fn cast_from(value: Storage<super::U16x8V3>) -> Storage<Self> {
unsafe {
ArrayRegister([
arch::_mm256_cvtepu16_epi64(value),
arch::_mm256_cvtepu16_epi64(arch::_mm_srli_si128(value, 8)),
])
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::I16x16V3> for ArrayRegister<super::I64x4V3, 4> {
fn cast_from(value: Storage<super::I16x16V3>) -> Storage<Self> {
unsafe {
let lo = arch::_mm256_castsi256_si128(value); let hi = arch::_mm256_extracti128_si256(value, 1); ArrayRegister([
arch::_mm256_cvtepi16_epi64(lo),
arch::_mm256_cvtepi16_epi64(arch::_mm_srli_si128(lo, 8)),
arch::_mm256_cvtepi16_epi64(hi),
arch::_mm256_cvtepi16_epi64(arch::_mm_srli_si128(hi, 8)),
])
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::U16x16V3> for ArrayRegister<super::U64x4V3, 4> {
fn cast_from(value: Storage<super::U16x16V3>) -> Storage<Self> {
unsafe {
let lo = arch::_mm256_castsi256_si128(value);
let hi = arch::_mm256_extracti128_si256(value, 1);
ArrayRegister([
arch::_mm256_cvtepu16_epi64(lo),
arch::_mm256_cvtepu16_epi64(arch::_mm_srli_si128(lo, 8)),
arch::_mm256_cvtepu16_epi64(hi),
arch::_mm256_cvtepu16_epi64(arch::_mm_srli_si128(hi, 8)),
])
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<i16, 2>> for super::half::F32x2V3 {
fn cast_from(value: Storage<ArrayRegister<i16, 2>>) -> Storage<Self> {
ReducedRegister::new(unsafe {
let widened = arch::_mm_setr_epi32(value.0[0] as i32, value.0[1] as i32, 0, 0);
arch::_mm_cvtepi32_ps(widened)
})
}
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<u16, 2>> for super::half::F32x2V3 {
fn cast_from(value: Storage<ArrayRegister<u16, 2>>) -> Storage<Self> {
ReducedRegister::new(unsafe {
let widened = arch::_mm_setr_epi32(value.0[0] as i32, value.0[1] as i32, 0, 0);
arch::_mm_cvtepi32_ps(widened)
})
}
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<i16, 2>> for super::F64x2V3 {
fn cast_from(value: Storage<ArrayRegister<i16, 2>>) -> Storage<Self> {
unsafe {
let widened = arch::_mm_setr_epi32(value.0[0] as i32, value.0[1] as i32, 0, 0);
arch::_mm_cvtepi32_pd(widened)
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<u16, 2>> for super::F64x2V3 {
fn cast_from(value: Storage<ArrayRegister<u16, 2>>) -> Storage<Self> {
unsafe {
let widened = arch::_mm_setr_epi32(value.0[0] as i32, value.0[1] as i32, 0, 0);
arch::_mm_cvtepi32_pd(widened)
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<I16x4V3> for super::F32x4V3 {
fn cast_from(value: Storage<I16x4V3>) -> Storage<Self> {
unsafe {
let widened = arch::_mm_cvtepi16_epi32(value.0);
arch::_mm_cvtepi32_ps(widened)
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<U16x4V3> for super::F32x4V3 {
fn cast_from(value: Storage<U16x4V3>) -> Storage<Self> {
unsafe {
let widened = arch::_mm_cvtepu16_epi32(value.0);
arch::_mm_cvtepi32_ps(widened)
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<I16x4V3> for super::F64x4V3 {
fn cast_from(value: Storage<I16x4V3>) -> Storage<Self> {
unsafe {
let widened = arch::_mm_cvtepi16_epi32(value.0); arch::_mm256_cvtepi32_pd(widened) }
}
}
#[thermite_macros::inline_always]
impl CastRegister<U16x4V3> for super::F64x4V3 {
fn cast_from(value: Storage<U16x4V3>) -> Storage<Self> {
unsafe {
let widened = arch::_mm_cvtepu16_epi32(value.0);
arch::_mm256_cvtepi32_pd(widened)
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::I16x8V3> for super::F32x8V3 {
fn cast_from(value: Storage<super::I16x8V3>) -> Storage<Self> {
unsafe {
let widened = arch::_mm256_cvtepi16_epi32(value); arch::_mm256_cvtepi32_ps(widened)
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::U16x8V3> for super::F32x8V3 {
fn cast_from(value: Storage<super::U16x8V3>) -> Storage<Self> {
unsafe {
let widened = arch::_mm256_cvtepu16_epi32(value);
arch::_mm256_cvtepi32_ps(widened)
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::I16x8V3> for ArrayRegister<super::F64x4V3, 2> {
fn cast_from(value: Storage<super::I16x8V3>) -> Storage<Self> {
unsafe {
ArrayRegister([
arch::_mm256_cvtepi32_pd(arch::_mm_cvtepi16_epi32(value)), arch::_mm256_cvtepi32_pd(arch::_mm_cvtepi16_epi32(arch::_mm_srli_si128(value, 8))), ])
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::U16x8V3> for ArrayRegister<super::F64x4V3, 2> {
fn cast_from(value: Storage<super::U16x8V3>) -> Storage<Self> {
unsafe {
ArrayRegister([
arch::_mm256_cvtepi32_pd(arch::_mm_cvtepu16_epi32(value)),
arch::_mm256_cvtepi32_pd(arch::_mm_cvtepu16_epi32(arch::_mm_srli_si128(value, 8))),
])
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::I16x16V3> for ArrayRegister<super::F32x8V3, 2> {
fn cast_from(value: Storage<super::I16x16V3>) -> Storage<Self> {
unsafe {
let lo = arch::_mm256_castsi256_si128(value); let hi = arch::_mm256_extracti128_si256(value, 1); ArrayRegister([
arch::_mm256_cvtepi32_ps(arch::_mm256_cvtepi16_epi32(lo)),
arch::_mm256_cvtepi32_ps(arch::_mm256_cvtepi16_epi32(hi)),
])
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::U16x16V3> for ArrayRegister<super::F32x8V3, 2> {
fn cast_from(value: Storage<super::U16x16V3>) -> Storage<Self> {
unsafe {
let lo = arch::_mm256_castsi256_si128(value);
let hi = arch::_mm256_extracti128_si256(value, 1);
ArrayRegister([
arch::_mm256_cvtepi32_ps(arch::_mm256_cvtepu16_epi32(lo)),
arch::_mm256_cvtepi32_ps(arch::_mm256_cvtepu16_epi32(hi)),
])
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::I16x16V3> for ArrayRegister<super::F64x4V3, 4> {
fn cast_from(value: Storage<super::I16x16V3>) -> Storage<Self> {
unsafe {
let lo = arch::_mm256_castsi256_si128(value); let hi = arch::_mm256_extracti128_si256(value, 1); ArrayRegister([
arch::_mm256_cvtepi32_pd(arch::_mm_cvtepi16_epi32(lo)),
arch::_mm256_cvtepi32_pd(arch::_mm_cvtepi16_epi32(arch::_mm_srli_si128(lo, 8))),
arch::_mm256_cvtepi32_pd(arch::_mm_cvtepi16_epi32(hi)),
arch::_mm256_cvtepi32_pd(arch::_mm_cvtepi16_epi32(arch::_mm_srli_si128(hi, 8))),
])
}
}
}
#[thermite_macros::inline_always]
impl CastRegister<super::U16x16V3> for ArrayRegister<super::F64x4V3, 4> {
fn cast_from(value: Storage<super::U16x16V3>) -> Storage<Self> {
unsafe {
let lo = arch::_mm256_castsi256_si128(value);
let hi = arch::_mm256_extracti128_si256(value, 1);
ArrayRegister([
arch::_mm256_cvtepi32_pd(arch::_mm_cvtepu16_epi32(lo)),
arch::_mm256_cvtepi32_pd(arch::_mm_cvtepu16_epi32(arch::_mm_srli_si128(lo, 8))),
arch::_mm256_cvtepi32_pd(arch::_mm_cvtepu16_epi32(hi)),
arch::_mm256_cvtepi32_pd(arch::_mm_cvtepu16_epi32(arch::_mm_srli_si128(hi, 8))),
])
}
}
}
impl IndexableRegister<<super::super::X86V3 as crate::simd::Simd>::u32x4> for I16x4V3 {}
impl IndexableRegister<<super::super::X86V3 as crate::simd::Simd>::u32x4> for U16x4V3 {}
impl IndexableRegister<<super::super::X86V3 as crate::simd::Simd>::u64x4> for I16x4V3 {}
impl IndexableRegister<<super::super::X86V3 as crate::simd::Simd>::u64x4> for U16x4V3 {}