use crate::filter_weights::FilterWeights;
use std::arch::aarch64::*;
#[must_use]
#[inline]
#[target_feature(enable = "rdm")]
fn conv_horiz_1_u16(start_x: usize, src: &[u16], w0: int32x4_t, store: int32x4_t) -> int32x4_t {
unsafe {
const CN: usize = 1;
let src_ptr = src.get_unchecked((start_x * CN)..);
let px = vld1_lane_u16::<0>(src_ptr.as_ptr().cast(), vdup_n_u16(0));
let lo = vreinterpretq_s32_u32(vshll_n_u16::<6>(px));
vqrdmlahq_s32(store, lo, w0)
}
}
#[must_use]
#[inline]
#[target_feature(enable = "rdm")]
fn conv_horiz_2_u16(start_x: usize, src: &[u16], w0: int32x4_t, store: int32x4_t) -> int32x4_t {
unsafe {
const CN: usize = 1;
let src_ptr = src.get_unchecked((start_x * CN)..);
let px = vreinterpret_u16_u32(vld1_lane_u32::<0>(src_ptr.as_ptr().cast(), vdup_n_u32(0)));
vqrdmlahq_s32(store, vreinterpretq_s32_u32(vshll_n_u16::<6>(px)), w0)
}
}
#[must_use]
#[inline]
#[target_feature(enable = "rdm")]
fn conv_horiz_4_u16(
start_x: usize,
src: &[u16],
weights: int32x4_t,
store: int32x4_t,
) -> int32x4_t {
unsafe {
const CN: usize = 1;
let src_ptr = src.get_unchecked((start_x * CN)..);
let px = vld1_u16(src_ptr.as_ptr());
vqrdmlahq_s32(store, vreinterpretq_s32_u32(vshll_n_u16::<6>(px)), weights)
}
}
#[must_use]
#[inline]
#[target_feature(enable = "rdm")]
fn conv_horiz_8_u16(
start_x: usize,
src: &[u16],
weights: (int32x4_t, int32x4_t),
store: int32x4_t,
) -> int32x4_t {
unsafe {
const CN: usize = 1;
let src_ptr = src.get_unchecked((start_x * CN)..);
let pixels = vld1q_u16(src_ptr.as_ptr());
let acc = vqrdmlahq_s32(
store,
vreinterpretq_s32_u32(vshll_high_n_u16::<6>(pixels)),
weights.1,
);
vqrdmlahq_s32(
acc,
vreinterpretq_s32_u32(vshll_n_u16::<6>(vget_low_u16(pixels))),
weights.0,
)
}
}
pub(crate) fn convolve_horizontal_plane_neon_rows_4_hb_u16(
src: &[u16],
src_stride: usize,
dst: &mut [u16],
dst_stride: usize,
filter_weights: &FilterWeights<i32>,
bit_depth: u32,
) {
unsafe {
convolve_horizontal_plane_neon_rows_4_hb_impl(
src,
src_stride,
dst,
dst_stride,
filter_weights,
bit_depth,
)
}
}
#[target_feature(enable = "rdm")]
fn convolve_horizontal_plane_neon_rows_4_hb_impl(
src: &[u16],
src_stride: usize,
dst: &mut [u16],
dst_stride: usize,
filter_weights: &FilterWeights<i32>,
bit_depth: u32,
) {
unsafe {
let init = vld1q_s32([1i32 << 5, 0, 0, 0].as_ptr());
let v_max_colors = (1u32 << bit_depth) - 1;
let (row0_ref, rest) = dst.split_at_mut(dst_stride);
let (row1_ref, rest) = rest.split_at_mut(dst_stride);
let (row2_ref, row3_ref) = rest.split_at_mut(dst_stride);
let iter_row0 = row0_ref.iter_mut();
let iter_row1 = row1_ref.iter_mut();
let iter_row2 = row2_ref.iter_mut();
let iter_row3 = row3_ref.iter_mut();
for (((((chunk0, chunk1), chunk2), chunk3), &bounds), weights) in iter_row0
.zip(iter_row1)
.zip(iter_row2)
.zip(iter_row3)
.zip(filter_weights.bounds.iter())
.zip(
filter_weights
.weights
.chunks_exact(filter_weights.aligned_size),
)
{
let mut jx = 0usize;
let mut store_0 = init;
let mut store_1 = init;
let mut store_2 = init;
let mut store_3 = init;
let bounds_size = bounds.size;
let src0 = src;
let src1 = src0.get_unchecked(src_stride..);
let src2 = src1.get_unchecked(src_stride..);
let src3 = src2.get_unchecked(src_stride..);
while jx + 8 <= bounds_size {
let bounds_start = bounds.start + jx;
let w_ptr = weights.get_unchecked(jx..);
let weights_set = (
vld1q_s32(w_ptr.as_ptr()),
vld1q_s32(w_ptr.get_unchecked(4..).as_ptr()),
);
store_0 = conv_horiz_8_u16(bounds_start, src0, weights_set, store_0);
store_1 = conv_horiz_8_u16(bounds_start, src1, weights_set, store_1);
store_2 = conv_horiz_8_u16(bounds_start, src2, weights_set, store_2);
store_3 = conv_horiz_8_u16(bounds_start, src3, weights_set, store_3);
jx += 8;
}
while jx + 4 <= bounds_size {
let bounds_start = bounds.start + jx;
let w_ptr = weights.get_unchecked(jx..);
let weights = vld1q_s32(w_ptr.as_ptr());
store_0 = conv_horiz_4_u16(bounds_start, src0, weights, store_0);
store_1 = conv_horiz_4_u16(bounds_start, src1, weights, store_1);
store_2 = conv_horiz_4_u16(bounds_start, src2, weights, store_2);
store_3 = conv_horiz_4_u16(bounds_start, src3, weights, store_3);
jx += 4;
}
while jx + 2 <= bounds_size {
let w_ptr = weights.get_unchecked(jx..);
let bounds_start = bounds.start + jx;
let w0 = vcombine_s32(vld1_s32(w_ptr.as_ptr()), vdup_n_s32(0));
store_0 = conv_horiz_2_u16(bounds_start, src0, w0, store_0);
store_1 = conv_horiz_2_u16(bounds_start, src1, w0, store_1);
store_2 = conv_horiz_2_u16(bounds_start, src2, w0, store_2);
store_3 = conv_horiz_2_u16(bounds_start, src3, w0, store_3);
jx += 2;
}
while jx < bounds_size {
let w_ptr = weights.get_unchecked(jx..(jx + 1));
let bounds_start = bounds.start + jx;
let weight0 = vld1q_dup_s32(w_ptr.as_ptr());
store_0 = conv_horiz_1_u16(bounds_start, src0, weight0, store_0);
store_1 = conv_horiz_1_u16(bounds_start, src1, weight0, store_1);
store_2 = conv_horiz_1_u16(bounds_start, src2, weight0, store_2);
store_3 = conv_horiz_1_u16(bounds_start, src3, weight0, store_3);
jx += 1;
}
let packed = vpaddq_s32(vpaddq_s32(store_0, store_1), vpaddq_s32(store_2, store_3));
let mut saturated = vqshrun_n_s32::<6>(packed);
saturated = vmin_u16(saturated, vdup_n_u16(v_max_colors as u16));
vst1_lane_u16::<0>(chunk0, saturated);
vst1_lane_u16::<1>(chunk1, saturated);
vst1_lane_u16::<2>(chunk2, saturated);
vst1_lane_u16::<3>(chunk3, saturated);
}
}
}
pub(crate) fn convolve_horizontal_plane_neon_u16_hb_row(
src: &[u16],
dst: &mut [u16],
filter_weights: &FilterWeights<i32>,
bit_depth: u32,
) {
unsafe {
convolve_horizontal_plane_neon_u16_hb_impl(src, dst, filter_weights, bit_depth);
}
}
#[target_feature(enable = "rdm")]
fn convolve_horizontal_plane_neon_u16_hb_impl(
src: &[u16],
dst: &mut [u16],
filter_weights: &FilterWeights<i32>,
bit_depth: u32,
) {
unsafe {
let v_max_colors = (1u32 << bit_depth) - 1;
let init = vld1q_s32([1i32 << 5, 0, 0, 0].as_ptr());
for ((dst, bounds), weights) in dst.iter_mut().zip(filter_weights.bounds.iter()).zip(
filter_weights
.weights
.chunks_exact(filter_weights.aligned_size),
) {
let bounds_size = bounds.size;
let mut jx = 0usize;
let mut store = init;
while jx + 8 <= bounds_size {
let bounds_start = bounds.start + jx;
let w_ptr = weights.get_unchecked(jx..);
let weights_set = (
vld1q_s32(w_ptr.as_ptr()),
vld1q_s32(w_ptr.get_unchecked(4..).as_ptr()),
);
store = conv_horiz_8_u16(bounds_start, src, weights_set, store);
jx += 8;
}
while jx + 4 <= bounds_size {
let w_ptr = weights.get_unchecked(jx..);
let weights = vld1q_s32(w_ptr.as_ptr());
let bounds_start = bounds.start + jx;
store = conv_horiz_4_u16(bounds_start, src, weights, store);
jx += 4;
}
while jx + 2 <= bounds_size {
let w_ptr = weights.get_unchecked(jx..);
let bounds_start = bounds.start + jx;
let w0 = vcombine_s32(vld1_s32(w_ptr.as_ptr()), vdup_n_s32(0));
store = conv_horiz_2_u16(bounds_start, src, w0, store);
jx += 2;
}
while jx < bounds_size {
let w_ptr = weights.get_unchecked(jx..);
let weight0 = vld1q_dup_s32(w_ptr.as_ptr());
let bounds_start = bounds.start + jx;
store = conv_horiz_1_u16(bounds_start, src, weight0, store);
jx += 1;
}
let packed = vpaddq_s32(vpaddq_s32(store, vdupq_n_s32(0)), vdupq_n_s32(0));
let mut saturated = vqshrun_n_s32::<6>(packed);
saturated = vmin_u16(saturated, vdup_n_u16(v_max_colors as u16));
vst1_lane_u16::<0>(dst, saturated);
}
}
}