#[cfg(all(
feature = "simd",
target_arch = "aarch64",
not(target_feature = "neon")
))]
compile_error!(
"the `simd` feature needs NEON on AArch64; build with `--no-default-features` for a target without it"
);
use core::arch::aarch64::{
int16x4_t, int16x8_t, int32x4_t, int32x4x4_t, uint8x8_t, uint8x8x2_t, uint8x8x3_t, uint8x8x4_t,
uint8x16_t, uint8x16x3_t, uint8x16x4_t, uint16x8_t, uint16x8x4_t, uint32x2_t, uint32x4_t,
uint64x2x2_t, vcombine_u64, vget_high_u64, vget_low_u64, vreinterpretq_s32_u64,
vreinterpretq_u64_s32, vtrnq_s32,
};
#[target_feature(enable = "neon")]
#[must_use]
#[expect(
clippy::similar_names,
reason = "the C names row01, row23, row0h, row2l and the like"
)]
pub(crate) fn transpose4x4_neon(rows: int32x4x4_t) -> int32x4x4_t {
let mut row01 = uint64x2x2_t(vreinterpretq_u64_s32(rows.0), vreinterpretq_u64_s32(rows.1));
let mut row23 = uint64x2x2_t(vreinterpretq_u64_s32(rows.2), vreinterpretq_u64_s32(rows.3));
{
let row0h = vget_high_u64(row01.0);
let row2l = vget_low_u64(row23.0);
let row1h = vget_high_u64(row01.1);
let row3l = vget_low_u64(row23.1);
row01.0 = vcombine_u64(vget_low_u64(row01.0), row2l);
row23.0 = vcombine_u64(row0h, vget_high_u64(row23.0));
row01.1 = vcombine_u64(vget_low_u64(row01.1), row3l);
row23.1 = vcombine_u64(row1h, vget_high_u64(row23.1));
}
{
let out01 = vtrnq_s32(
vreinterpretq_s32_u64(row01.0),
vreinterpretq_s32_u64(row01.1),
);
let out23 = vtrnq_s32(
vreinterpretq_s32_u64(row23.0),
vreinterpretq_s32_u64(row23.1),
);
int32x4x4_t(out01.0, out01.1, out23.0, out23.1)
}
}
macro_rules! load {
($($name:ident: [$elem:ty; $count:literal] -> $vector:ty;)*) => {$(
#[doc = concat!("`", stringify!($name), "` on the first ", stringify!($count),
" elements of `src`, which must hold them.")]
#[inline]
#[must_use]
pub fn $name(src: &[$elem]) -> $vector {
let src = &src[..$count];
unsafe { core::arch::aarch64::$name(src.as_ptr()) }
}
)*};
}
macro_rules! store {
($($name:ident: [$elem:ty; $count:literal] <- $vector:ty;)*) => {$(
#[doc = concat!("`", stringify!($name), "` to the first ", stringify!($count),
" elements of `dst`, which must hold them.")]
#[inline]
pub fn $name(dst: &mut [$elem], value: $vector) {
let dst = &mut dst[..$count];
unsafe { core::arch::aarch64::$name(dst.as_mut_ptr(), value) }
}
)*};
}
load! {
vld1_u8: [u8; 8] -> uint8x8_t;
vld1q_u8: [u8; 16] -> uint8x16_t;
vld3_u8: [u8; 24] -> uint8x8x3_t;
vld4_u8: [u8; 32] -> uint8x8x4_t;
vld4q_u8: [u8; 64] -> uint8x16x4_t;
vld1_s16: [i16; 4] -> int16x4_t;
vld1q_s16: [i16; 8] -> int16x8_t;
vld1q_u16: [u16; 8] -> uint16x8_t;
vld4q_u16: [u16; 32] -> uint16x8x4_t;
vld1_u32: [u32; 2] -> uint32x2_t;
vld1q_u32: [u32; 4] -> uint32x4_t;
vld1q_s32: [i32; 4] -> int32x4_t;
}
store! {
vst1_u8: [u8; 8] <- uint8x8_t;
vst1q_u8: [u8; 16] <- uint8x16_t;
vst2_u8: [u8; 16] <- uint8x8x2_t;
vst3_u8: [u8; 24] <- uint8x8x3_t;
vst3q_u8: [u8; 48] <- uint8x16x3_t;
vst4_u8: [u8; 32] <- uint8x8x4_t;
vst4q_u8: [u8; 64] <- uint8x16x4_t;
vst1_s16: [i16; 4] <- int16x4_t;
vst1q_s16: [i16; 8] <- int16x8_t;
vst1q_u16: [u16; 8] <- uint16x8_t;
vst1_u32: [u32; 2] <- uint32x2_t;
vst1q_u32: [u32; 4] <- uint32x4_t;
vst1q_s32: [i32; 4] <- int32x4_t;
}
#[inline]
#[must_use]
pub fn vld4_u8_words(src: &[u32]) -> uint8x8x4_t {
let src = &src[..8];
unsafe { core::arch::aarch64::vld4_u8(src.as_ptr().cast::<u8>()) }
}
#[inline]
pub fn vst4_u8_words(dst: &mut [u32], value: uint8x8x4_t) {
let dst = &mut dst[..8];
unsafe { core::arch::aarch64::vst4_u8(dst.as_mut_ptr().cast::<u8>(), value) }
}
#[inline]
#[must_use]
pub fn vld4q_u8_words(src: &[u32]) -> uint8x16x4_t {
let src = &src[..16];
unsafe { core::arch::aarch64::vld4q_u8(src.as_ptr().cast::<u8>()) }
}
#[inline]
pub fn vst4q_u8_words(dst: &mut [u32], value: uint8x16x4_t) {
let dst = &mut dst[..16];
unsafe { core::arch::aarch64::vst4q_u8(dst.as_mut_ptr().cast::<u8>(), value) }
}
#[inline]
#[must_use]
pub fn load_u32_ne(src: &[u8]) -> u32 {
let src = &src[..4];
u32::from_ne_bytes([src[0], src[1], src[2], src[3]])
}
#[inline]
pub fn store_u32_ne(dst: &mut [u8], value: u32) {
dst[..4].copy_from_slice(&value.to_ne_bytes());
}
#[inline]
#[must_use]
pub fn vld1q_u8_words(src: &[u32]) -> core::arch::aarch64::uint8x16_t {
let src = &src[..4];
unsafe { core::arch::aarch64::vld1q_u8(src.as_ptr().cast::<u8>()) }
}
#[inline]
pub fn vst1q_u8_words(dst: &mut [u32], value: core::arch::aarch64::uint8x16_t) {
let dst = &mut dst[..4];
unsafe { core::arch::aarch64::vst1q_u8(dst.as_mut_ptr().cast::<u8>(), value) }
}
#[inline]
pub fn vst1q_s8_words(dst: &mut [u32], value: core::arch::aarch64::int8x16_t) {
let dst = &mut dst[..4];
unsafe { core::arch::aarch64::vst1q_s8(dst.as_mut_ptr().cast::<i8>(), value) }
}
load! {
vld1_u8_x2: [u8; 16] -> uint8x8x2_t;
vld1q_u8_x4: [u8; 64] -> uint8x16x4_t;
vld4_dup_u8: [u8; 4] -> uint8x8x4_t;
}
#[inline]
pub fn vst1_u8_halfwords(dst: &mut [i16], value: uint8x8_t) {
let dst = &mut dst[..4];
unsafe { core::arch::aarch64::vst1_u8(dst.as_mut_ptr().cast::<u8>(), value) }
}