use crate::filter_op_declare::{Arena, MorthOpFilterFlat2DRow};
use crate::flat_se::AnalyzedSe;
use crate::morph_base::MorphNativeOp;
use crate::op_type::MorphOp;
use crate::unsafe_slice::UnsafeSlice;
use crate::ImageSize;
#[cfg(target_arch = "aarch64")]
use std::arch::aarch64::*;
#[cfg(target_arch = "arm")]
use std::arch::arm::*;
#[derive(Clone)]
pub(crate) struct MorphOpFilterNeon2DRowU16<const OP_TYPE: u8> {}
impl<const OP_TYPE: u8> Default for MorphOpFilterNeon2DRowU16<OP_TYPE> {
fn default() -> Self {
MorphOpFilterNeon2DRowU16 {}
}
}
#[inline(always)]
unsafe fn xvld1q_u16_x4(a: *const u16) -> uint16x8x4_t {
let v0 = vld1q_u16(a);
let v1 = vld1q_u16(a.add(8));
let v2 = vld1q_u16(a.add(16));
let v3 = vld1q_u16(a.add(24));
uint16x8x4_t(v0, v1, v2, v3)
}
#[inline(always)]
unsafe fn xvld1q_u16_x2(a: *const u16) -> uint16x8x2_t {
let v0 = vld1q_u16(a);
let v1 = vld1q_u16(a.add(8));
uint16x8x2_t(v0, v1)
}
#[inline(always)]
unsafe fn xvst1q_u16_x4(a: *mut u16, b: uint16x8x4_t) {
vst1q_u16(a, b.0);
vst1q_u16(a.add(8), b.1);
vst1q_u16(a.add(16), b.2);
vst1q_u16(a.add(24), b.3);
}
#[inline(always)]
unsafe fn xvst1q_u16_x2(a: *mut u16, b: uint16x8x2_t) {
vst1q_u16(a, b.0);
vst1q_u16(a.add(8), b.1);
}
impl<T, const OP_TYPE: u8> MorthOpFilterFlat2DRow<T> for MorphOpFilterNeon2DRowU16<OP_TYPE>
where
T: Copy + 'static,
{
unsafe fn dispatch_row(
&self,
arena: &Arena<T>,
dst: &UnsafeSlice<T>,
image_size: ImageSize,
analyzed_se: AnalyzedSe,
y: usize,
) {
let width = image_size.width;
let op_type: MorphOp = OP_TYPE.into();
let stride = width * arena.components;
let decision = match op_type {
MorphOp::Dilate => vmaxq_u16,
MorphOp::Erode => vminq_u16,
};
let decision_half = match op_type {
MorphOp::Dilate => vmax_u16,
MorphOp::Erode => vmin_u16,
};
let src: &Vec<u16> = std::mem::transmute(&arena.arena);
let dst: &UnsafeSlice<u16> = std::mem::transmute(dst);
let dx = arena.pad_w as i32;
let dy = arena.pad_h as i32;
let total_width = width * arena.components;
let arena_stride = arena.width * arena.components;
let offsets = analyzed_se
.left_front
.element_offsets
.iter()
.map(|&x| {
src.get_unchecked(
((x.y + dy + y as i32) as usize * arena_stride + (x.x + dx) as usize)..,
)
})
.collect::<Vec<_>>();
let off0 = offsets.get_unchecked(0);
let length = analyzed_se.left_front.element_offsets.len();
let mut cx = 0usize;
while cx + 32 < total_width {
let mut rows = xvld1q_u16_x4((*off0.get_unchecked(cx..)).as_ptr());
for i in 1..length {
let new_rows =
xvld1q_u16_x4((*offsets.get_unchecked(i)).get_unchecked(cx..).as_ptr());
rows.0 = decision(rows.0, new_rows.0);
rows.1 = decision(rows.1, new_rows.1);
rows.2 = decision(rows.2, new_rows.2);
rows.3 = decision(rows.3, new_rows.3);
}
xvst1q_u16_x4(dst.slice.as_ptr().add(y * stride + cx) as *mut u16, rows);
cx += 32;
}
while cx + 16 < total_width {
let mut rows = xvld1q_u16_x2((*off0.get_unchecked(cx..)).as_ptr());
for i in 1..length {
let new_rows =
xvld1q_u16_x2((*offsets.get_unchecked(i)).get_unchecked(cx..).as_ptr());
rows.0 = decision(rows.0, new_rows.0);
rows.1 = decision(rows.1, new_rows.1);
}
xvst1q_u16_x2(dst.slice.as_ptr().add(y * stride + cx) as *mut u16, rows);
cx += 16;
}
while cx + 8 < total_width {
let mut rows = vld1q_u16((*off0.get_unchecked(cx..)).as_ptr());
for i in 1..length {
let new_row = vld1q_u16((*offsets.get_unchecked(i)).get_unchecked(cx..).as_ptr());
rows = decision(rows, new_row);
}
vst1q_u16(dst.slice.as_ptr().add(y * stride + cx) as *mut u16, rows);
cx += 8;
}
while cx + 4 < total_width {
let mut rows = vld1_u16((*off0.get_unchecked(cx..)).as_ptr());
for i in 1..length {
let new_row = vld1_u16((*offsets.get_unchecked(i)).get_unchecked(cx..).as_ptr());
rows = decision_half(rows, new_row);
}
vst1_u16(dst.slice.as_ptr().add(y * stride + cx) as *mut u16, rows);
cx += 4;
}
for x in cx..total_width {
let mut k0 = *(*off0).get_unchecked(x);
for i in 1..length {
k0 = k0.op::<OP_TYPE>(*(*offsets.get_unchecked(i)).get_unchecked(x));
}
dst.write(y * stride + x, k0);
}
}
}