use crate::filter_weights::FilterWeights;
use crate::neon::utils::{
prefer_vfmaq_f32, prefer_vfmaq_lane_f32, prefer_vfmaq_laneq_f32, xvld1q_f32_x2, xvld1q_u16_x2,
xvld1q_u16_x4,
};
use core::f16;
use std::arch::aarch64::*;
#[must_use]
#[inline(always)]
fn conv_horiz_rgba_8_f16(
start_x: usize,
src: &[f16],
set1: float32x4_t,
set2: float32x4_t,
store: float32x4_t,
) -> float32x4_t {
unsafe {
const CN: usize = 4;
let src_ptr = src.get_unchecked(start_x * CN..).as_ptr();
let rgb_pixel = xvld1q_u16_x4(src_ptr.cast());
let mut acc = prefer_vfmaq_laneq_f32::<0>(
store,
vcvt_f32_f16(vreinterpret_f16_u16(vget_low_u16(rgb_pixel.0))),
set1,
);
acc = prefer_vfmaq_laneq_f32::<1>(
acc,
vcvt_high_f32_f16(vreinterpretq_f16_u16(rgb_pixel.0)),
set1,
);
acc = prefer_vfmaq_laneq_f32::<2>(
acc,
vcvt_f32_f16(vreinterpret_f16_u16(vget_low_u16(rgb_pixel.1))),
set1,
);
acc = prefer_vfmaq_laneq_f32::<3>(
acc,
vcvt_high_f32_f16(vreinterpretq_f16_u16(rgb_pixel.1)),
set1,
);
acc = prefer_vfmaq_laneq_f32::<0>(
acc,
vcvt_f32_f16(vreinterpret_f16_u16(vget_low_u16(rgb_pixel.2))),
set2,
);
acc = prefer_vfmaq_laneq_f32::<1>(
acc,
vcvt_high_f32_f16(vreinterpretq_f16_u16(rgb_pixel.2)),
set2,
);
acc = prefer_vfmaq_laneq_f32::<2>(
acc,
vcvt_f32_f16(vreinterpret_f16_u16(vget_low_u16(rgb_pixel.3))),
set2,
);
prefer_vfmaq_laneq_f32::<3>(
acc,
vcvt_high_f32_f16(vreinterpretq_f16_u16(rgb_pixel.3)),
set2,
)
}
}
#[must_use]
#[inline(always)]
fn conv_horiz_rgba_4_f16(
start_x: usize,
src: &[f16],
set1: float32x4_t,
store: float32x4_t,
) -> float32x4_t {
unsafe {
const CN: usize = 4;
let src_ptr = src.get_unchecked(start_x * CN..).as_ptr();
let rgb_pixel = xvld1q_u16_x2(src_ptr.cast());
let acc = prefer_vfmaq_laneq_f32::<0>(
store,
vcvt_f32_f16(vreinterpret_f16_u16(vget_low_u16(rgb_pixel.0))),
set1,
);
let acc = prefer_vfmaq_laneq_f32::<1>(
acc,
vcvt_f32_f16(vreinterpret_f16_u16(vget_high_u16(rgb_pixel.0))),
set1,
);
let acc = prefer_vfmaq_laneq_f32::<2>(
acc,
vcvt_f32_f16(vreinterpret_f16_u16(vget_low_u16(rgb_pixel.1))),
set1,
);
prefer_vfmaq_laneq_f32::<3>(
acc,
vcvt_f32_f16(vreinterpret_f16_u16(vget_high_u16(rgb_pixel.0))),
set1,
)
}
}
#[must_use]
#[inline(always)]
fn conv_horiz_rgba_2_f32(
start_x: usize,
src: &[f16],
set: float32x2_t,
store: float32x4_t,
) -> float32x4_t {
unsafe {
const CN: usize = 4;
let src_ptr = src.get_unchecked(start_x * CN..).as_ptr();
let rgb_pixel = vld1q_u16(src_ptr.cast());
let acc = prefer_vfmaq_lane_f32::<0>(
store,
vcvt_f32_f16(vreinterpret_f16_u16(vget_low_u16(rgb_pixel))),
set,
);
prefer_vfmaq_lane_f32::<1>(
acc,
vcvt_f32_f16(vreinterpret_f16_u16(vget_high_u16(rgb_pixel))),
set,
)
}
}
#[must_use]
#[inline(always)]
fn conv_horiz_rgba_1_f16(
start_x: usize,
src: &[f16],
set: float32x4_t,
store: float32x4_t,
) -> float32x4_t {
unsafe {
const CN: usize = 4;
let src_ptr = src.get_unchecked(start_x * CN..).as_ptr();
let rgb_pixel = vld1_u16(src_ptr.cast());
prefer_vfmaq_f32(store, vcvt_f32_f16(vreinterpret_f16_u16(rgb_pixel)), set)
}
}
pub(crate) fn convolve_horizontal_rgba_neon_row_one_f16(
src: &[f16],
dst: &mut [f16],
filter_weights: &FilterWeights<f32>,
_: u32,
) {
unsafe {
const CN: usize = 4;
for ((dst, bounds), weights) in dst
.as_chunks_mut::<CN>()
.0
.iter_mut()
.zip(filter_weights.bounds.iter())
.zip(
filter_weights
.weights
.chunks_exact(filter_weights.aligned_size),
)
{
let mut jx = 0usize;
let mut store = vdupq_n_f32(0f32);
while jx + 4 <= bounds.size {
let bounds_start = bounds.start + jx;
let w_s = weights.get_unchecked(jx);
let read_weights = vld1q_f32(w_s);
store = conv_horiz_rgba_4_f16(bounds_start, src, read_weights, store);
jx += 4;
}
while jx + 2 <= bounds.size {
let bounds_start = bounds.start + jx;
let w_s = weights.get_unchecked(jx);
let read_weights = vld1_f32(w_s);
store = conv_horiz_rgba_2_f32(bounds_start, src, read_weights, store);
jx += 2;
}
while jx <= bounds.size {
let bounds_start = bounds.start + jx;
let w_s = weights.get_unchecked(jx);
let weight0 = vld1q_dup_f32(w_s);
store = conv_horiz_rgba_1_f16(bounds_start, src, weight0, store);
jx += 1;
}
vst1_u16(
dst.as_mut_ptr().cast(),
vreinterpret_u16_f16(vcvt_f16_f32(store)),
);
}
}
}
pub(crate) fn convolve_horizontal_rgba_neon_rows_4_f16(
src: &[f16],
src_stride: usize,
dst: &mut [f16],
dst_stride: usize,
filter_weights: &FilterWeights<f32>,
_: u32,
) {
unsafe {
const CN: usize = 4;
let zeros = vdupq_n_f32(0f32);
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.as_chunks_mut::<CN>().0;
let iter_row1 = row1_ref.as_chunks_mut::<CN>().0;
let iter_row2 = row2_ref.as_chunks_mut::<CN>().0;
let iter_row3 = row3_ref.as_chunks_mut::<CN>().0;
for (((((chunk0, chunk1), chunk2), chunk3), &bounds), weights) in iter_row0
.iter_mut()
.zip(iter_row1.iter_mut())
.zip(iter_row2.iter_mut())
.zip(iter_row3.iter_mut())
.zip(filter_weights.bounds.iter())
.zip(
filter_weights
.weights
.chunks_exact(filter_weights.aligned_size),
)
{
let mut jx = 0usize;
let mut store_0 = zeros;
let mut store_1 = zeros;
let mut store_2 = zeros;
let mut store_3 = zeros;
while jx + 8 <= bounds.size {
let w_s = weights.get_unchecked(jx);
let read_weights = xvld1q_f32_x2(w_s);
let bounds_start = bounds.start + jx;
store_0 = conv_horiz_rgba_8_f16(
bounds_start,
src,
read_weights.0,
read_weights.1,
store_0,
);
let s_ptr_1 = src.get_unchecked(src_stride..);
store_1 = conv_horiz_rgba_8_f16(
bounds_start,
s_ptr_1,
read_weights.0,
read_weights.1,
store_1,
);
let s_ptr2 = src.get_unchecked(src_stride * 2..);
store_2 = conv_horiz_rgba_8_f16(
bounds_start,
s_ptr2,
read_weights.0,
read_weights.1,
store_2,
);
let s_ptr3 = src.get_unchecked(src_stride * 3..);
store_3 = conv_horiz_rgba_8_f16(
bounds_start,
s_ptr3,
read_weights.0,
read_weights.1,
store_3,
);
jx += 8;
}
while jx + 4 <= bounds.size {
let w_s = weights.get_unchecked(jx);
let read_weights = vld1q_f32(w_s);
let bounds_start = bounds.start + jx;
store_0 = conv_horiz_rgba_4_f16(bounds_start, src, read_weights, store_0);
let s_ptr_1 = src.get_unchecked(src_stride..);
store_1 = conv_horiz_rgba_4_f16(bounds_start, s_ptr_1, read_weights, store_1);
let s_ptr2 = src.get_unchecked(src_stride * 2..);
store_2 = conv_horiz_rgba_4_f16(bounds_start, s_ptr2, read_weights, store_2);
let s_ptr3 = src.get_unchecked(src_stride * 3..);
store_3 = conv_horiz_rgba_4_f16(bounds_start, s_ptr3, read_weights, store_3);
jx += 4;
}
while jx + 2 <= bounds.size {
let w_s = weights.get_unchecked(jx);
let read_weights = vld1_f32(w_s);
let bounds_start = bounds.start + jx;
store_0 = conv_horiz_rgba_2_f32(bounds_start, src, read_weights, store_0);
let ptr_1 = src.get_unchecked(src_stride..);
store_1 = conv_horiz_rgba_2_f32(bounds_start, ptr_1, read_weights, store_1);
let ptr_2 = src.get_unchecked(src_stride * 2..);
store_2 = conv_horiz_rgba_2_f32(bounds_start, ptr_2, read_weights, store_2);
let ptr_3 = src.get_unchecked(src_stride * 3..);
store_3 = conv_horiz_rgba_2_f32(bounds_start, ptr_3, read_weights, store_3);
jx += 2;
}
while jx < bounds.size {
let w_s = weights.get_unchecked(jx);
let weight0 = vld1q_dup_f32(w_s);
let bounds_start = bounds.start + jx;
store_0 = conv_horiz_rgba_1_f16(bounds_start, src, weight0, store_0);
let ptr_1 = src.get_unchecked(src_stride..);
store_1 = conv_horiz_rgba_1_f16(bounds_start, ptr_1, weight0, store_1);
let ptr_2 = src.get_unchecked(src_stride * 2..);
store_2 = conv_horiz_rgba_1_f16(bounds_start, ptr_2, weight0, store_2);
let ptr_3 = src.get_unchecked(src_stride * 3..);
store_3 = conv_horiz_rgba_1_f16(bounds_start, ptr_3, weight0, store_3);
jx += 1;
}
vst1_u16(
chunk0.as_mut_ptr().cast(),
vreinterpret_u16_f16(vcvt_f16_f32(store_0)),
);
vst1_u16(
chunk1.as_mut_ptr().cast(),
vreinterpret_u16_f16(vcvt_f16_f32(store_1)),
);
vst1_u16(
chunk2.as_mut_ptr().cast(),
vreinterpret_u16_f16(vcvt_f16_f32(store_2)),
);
vst1_u16(
chunk3.as_mut_ptr().cast(),
vreinterpret_u16_f16(vcvt_f16_f32(store_3)),
);
}
}
}