- Added support of optimisation with help of NEON SIMD for convolution of U8x4 images.

- Added optimisation for processing `U8x4` images by `MulDiv` with helps of `NEON SIMD` instructions.
This commit is contained in:
Kirill Kuzminykh
2022-10-19 00:25:14 +04:00
parent 29f9f1f508
commit ff2837d704
18 changed files with 822 additions and 16 deletions
+3
View File
@@ -25,6 +25,9 @@
- Added generic trait `IntoPixelComponent<Out: PixelComponent>`.
- Added generic structure `Pixel` for create all types of pixels.
- Added full support of optimisation with help of `SSE4.1` for convolution of `U8x3` images.
- Added support of optimisation with help of `NEON SIMD` for convolution of `U8x4` images.
- Added optimisation for processing `U8x4` images by `MulDiv` with
helps of `NEON SIMD` instructions.
### Example application
+12 -12
View File
@@ -10,18 +10,18 @@ Rust library for fast image resizing with using of SIMD instructions.
Supported pixel formats and available optimisations:
| Format | Description | Native Rust | SSE4.1 | AVX2 |
|:------:|:--------------------------------------------------------------|:-----------:|:-------:|:----:|
| U8 | One `u8` component per pixel (e.g. L) | + | partial | + |
| U8x2 | Two `u8` components per pixel (e.g. LA) | + | + | + |
| U8x3 | Three `u8` components per pixel (e.g. RGB) | + | + | + |
| U8x4 | Four `u8` components per pixel (e.g. RGBA, RGBx, CMYK) | + | + | + |
| U16 | One `u16` components per pixel (e.g. L16) | + | + | + |
| U16x2 | Two `u16` components per pixel (e.g. LA16) | + | + | + |
| U16x3 | Three `u16` components per pixel (e.g. RGB16) | + | + | + |
| U16x4 | Four `u16` components per pixel (e.g. RGBA16, RGBx16, CMYK16) | + | + | + |
| I32 | One `i32` component per pixel | + | - | - |
| F32 | One `f32` component per pixel | + | - | - |
| Format | Description | Native Rust | SSE4.1 | AVX2 | Neon |
|:------:|:--------------------------------------------------------------|:-----------:|:-------:|:----:|:----:|
| U8 | One `u8` component per pixel (e.g. L) | + | partial | + | - |
| U8x2 | Two `u8` components per pixel (e.g. LA) | + | + | + | - |
| U8x3 | Three `u8` components per pixel (e.g. RGB) | + | + | + | - |
| U8x4 | Four `u8` components per pixel (e.g. RGBA, RGBx, CMYK) | + | + | + | + |
| U16 | One `u16` components per pixel (e.g. L16) | + | + | + | - |
| U16x2 | Two `u16` components per pixel (e.g. LA16) | + | + | + | - |
| U16x3 | Three `u16` components per pixel (e.g. RGB16) | + | + | + | - |
| U16x4 | Four `u16` components per pixel (e.g. RGBA16, RGBx16, CMYK16) | + | + | + | - |
| I32 | One `i32` component per pixel | + | - | - | - |
| F32 | One `f32` component per pixel | + | - | - | - |
## Colorspace
+4
View File
@@ -98,6 +98,10 @@ fn bench_alpha(bench: &mut Bench) {
cpu_extensions.push(CpuExtensions::Sse4_1);
cpu_extensions.push(CpuExtensions::Avx2);
}
#[cfg(target_arch = "aarch64")]
{
cpu_extensions.push(CpuExtensions::Neon);
}
for pixel_type in pixel_types {
for &extensions in cpu_extensions.iter() {
println!("Mul {:?} {:?}", pixel_type, extensions);
+4
View File
@@ -60,6 +60,10 @@ pub fn bench_downscale_rgba(bench: &mut Bench) {
cpu_ext_and_name.push((CpuExtensions::Sse4_1, "sse4.1"));
cpu_ext_and_name.push((CpuExtensions::Avx2, "avx2"));
}
#[cfg(target_arch = "aarch64")]
{
cpu_ext_and_name.push((CpuExtensions::Neon, "neon"));
}
for (cpu_ext, ext_name) in cpu_ext_and_name {
for alg_name in alg_names {
let resize_alg = match alg_name {
+10
View File
@@ -7,6 +7,8 @@ use super::AlphaMulDiv;
#[cfg(target_arch = "x86_64")]
mod avx2;
mod native;
#[cfg(target_arch = "aarch64")]
mod neon;
#[cfg(target_arch = "x86_64")]
mod sse4;
@@ -21,6 +23,8 @@ impl AlphaMulDiv for U8x4 {
CpuExtensions::Avx2 => unsafe { avx2::multiply_alpha(src_image, dst_image) },
#[cfg(target_arch = "x86_64")]
CpuExtensions::Sse4_1 => unsafe { sse4::multiply_alpha(src_image, dst_image) },
#[cfg(target_arch = "aarch64")]
CpuExtensions::Neon => unsafe { neon::multiply_alpha(src_image, dst_image) },
_ => native::multiply_alpha(src_image, dst_image),
}
}
@@ -31,6 +35,8 @@ impl AlphaMulDiv for U8x4 {
CpuExtensions::Avx2 => unsafe { avx2::multiply_alpha_inplace(image) },
#[cfg(target_arch = "x86_64")]
CpuExtensions::Sse4_1 => unsafe { sse4::multiply_alpha_inplace(image) },
#[cfg(target_arch = "aarch64")]
CpuExtensions::Neon => unsafe { neon::multiply_alpha_inplace(image) },
_ => native::multiply_alpha_inplace(image),
}
}
@@ -45,6 +51,8 @@ impl AlphaMulDiv for U8x4 {
CpuExtensions::Avx2 => unsafe { avx2::divide_alpha(src_image, dst_image) },
#[cfg(target_arch = "x86_64")]
CpuExtensions::Sse4_1 => unsafe { sse4::divide_alpha(src_image, dst_image) },
#[cfg(target_arch = "aarch64")]
CpuExtensions::Neon => unsafe { neon::divide_alpha(src_image, dst_image) },
_ => native::divide_alpha(src_image, dst_image),
}
}
@@ -55,6 +63,8 @@ impl AlphaMulDiv for U8x4 {
CpuExtensions::Avx2 => unsafe { avx2::divide_alpha_inplace(image) },
#[cfg(target_arch = "x86_64")]
CpuExtensions::Sse4_1 => unsafe { sse4::divide_alpha_inplace(image) },
#[cfg(target_arch = "aarch64")]
CpuExtensions::Neon => unsafe { neon::divide_alpha_inplace(image) },
_ => native::divide_alpha_inplace(image),
}
}
+157
View File
@@ -0,0 +1,157 @@
use crate::neon_utils;
use crate::pixels::U8x4;
use crate::{ImageView, ImageViewMut};
use std::arch::aarch64::*;
use std::intrinsics::transmute;
use super::native;
#[target_feature(enable = "neon")]
pub(crate) unsafe fn multiply_alpha(
src_image: &ImageView<U8x4>,
dst_image: &mut ImageViewMut<U8x4>,
) {
let src_rows = src_image.iter_rows(0);
let dst_rows = dst_image.iter_rows_mut();
for (src_row, dst_row) in src_rows.zip(dst_rows) {
multiply_alpha_row(src_row, dst_row);
}
}
#[target_feature(enable = "neon")]
pub(crate) unsafe fn multiply_alpha_inplace(image: &mut ImageViewMut<U8x4>) {
for dst_row in image.iter_rows_mut() {
let src_row = std::slice::from_raw_parts(dst_row.as_ptr(), dst_row.len());
multiply_alpha_row(src_row, dst_row);
}
}
#[inline]
#[target_feature(enable = "neon")]
unsafe fn multiply_alpha_row(src_row: &[U8x4], dst_row: &mut [U8x4]) {
let zero = vdupq_n_u8(0);
// let half = vdupq_n_u16(128);
const MAX_A: u32 = 0xff000000u32;
let max_alpha = vreinterpretq_u8_u32(vdupq_n_u32(MAX_A));
static FACTOR_MASK_DATA: [u8; 16] = [3, 3, 3, 3, 7, 7, 7, 7, 11, 11, 11, 11, 15, 15, 15, 15];
let factor_mask = vld1q_u8(FACTOR_MASK_DATA.as_ptr());
let src_chunks = src_row.chunks_exact(4);
let src_remainder = src_chunks.remainder();
let mut dst_chunks = dst_row.chunks_exact_mut(4);
for (src, dst) in src_chunks.zip(&mut dst_chunks) {
let src_pixels = neon_utils::load_u8x16(src, 0);
let factor_pixels = vqtbl1q_u8(src_pixels, factor_mask);
let factor_pixels = vorrq_u8(factor_pixels, max_alpha);
let pix1 = vreinterpretq_u16_u8(vzip1q_u8(src_pixels, zero));
let factors = vreinterpretq_u16_u8(vzip1q_u8(factor_pixels, zero));
let pix1 = vmulq_u16(pix1, factors);
let pix1 = vaddq_u16(pix1, vrshrq_n_u16::<8>(pix1));
let pix1 = vrshrq_n_u16::<8>(pix1);
let pix2 = vreinterpretq_u16_u8(vzip2q_u8(src_pixels, zero));
let factors = vreinterpretq_u16_u8(vzip2q_u8(factor_pixels, zero));
let pix2 = vmulq_u16(pix2, factors);
let pix2 = vaddq_u16(pix2, vrshrq_n_u16::<8>(pix2));
let pix2 = vrshrq_n_u16::<8>(pix2);
let dst_pixels = vcombine_u8(vqmovn_u16(pix1), vqmovn_u16(pix2));
let dst_ptr = dst.as_mut_ptr() as *mut u128;
vstrq_p128(dst_ptr, transmute(dst_pixels));
}
if !src_remainder.is_empty() {
let dst_reminder = dst_chunks.into_remainder();
native::multiply_alpha_row(src_remainder, dst_reminder);
}
}
// Divide
#[target_feature(enable = "neon")]
pub(crate) unsafe fn divide_alpha(src_image: &ImageView<U8x4>, dst_image: &mut ImageViewMut<U8x4>) {
let src_rows = src_image.iter_rows(0);
let dst_rows = dst_image.iter_rows_mut();
for (src_row, dst_row) in src_rows.zip(dst_rows) {
divide_alpha_row(src_row, dst_row);
}
}
#[target_feature(enable = "neon")]
pub(crate) unsafe fn divide_alpha_inplace(image: &mut ImageViewMut<U8x4>) {
for dst_row in image.iter_rows_mut() {
let src_row = std::slice::from_raw_parts(dst_row.as_ptr(), dst_row.len());
divide_alpha_row(src_row, dst_row);
}
}
#[target_feature(enable = "neon")]
pub(crate) unsafe fn divide_alpha_row(src_row: &[U8x4], dst_row: &mut [U8x4]) {
let src_chunks = src_row.chunks_exact(4);
let src_remainder = src_chunks.remainder();
let mut dst_chunks = dst_row.chunks_exact_mut(4);
for (src, dst) in src_chunks.zip(&mut dst_chunks) {
divide_alpha_four_pixels(src, dst);
}
if !src_remainder.is_empty() {
let dst_reminder = dst_chunks.into_remainder();
let mut src_pixels = [U8x4::new(0); 4];
src_pixels
.iter_mut()
.zip(src_remainder)
.for_each(|(d, s)| *d = *s);
let mut dst_pixels = [U8x4::new(0); 4];
divide_alpha_four_pixels(src_pixels.as_slice(), dst_pixels.as_mut_slice());
dst_pixels
.iter()
.zip(dst_reminder)
.for_each(|(s, d)| *d = *s);
}
}
#[target_feature(enable = "neon")]
#[inline]
unsafe fn divide_alpha_four_pixels(src: &[U8x4], dst: &mut [U8x4]) {
let zero = vdupq_n_u8(0);
let alpha_mask = vreinterpretq_u8_u32(vdupq_n_u32(0xff000000u32));
static SHUFFLE1_DATA: [u8; 16] = [0, 1, 0, 1, 0, 1, 0, 1, 4, 5, 4, 5, 4, 5, 4, 5];
let shuffle1 = vld1q_u8(SHUFFLE1_DATA.as_ptr());
static SHUFFLE2_DATA: [u8; 16] = [8, 9, 8, 9, 8, 9, 8, 9, 12, 13, 12, 13, 12, 13, 12, 13];
let shuffle2 = vld1q_u8(SHUFFLE2_DATA.as_ptr());
let alpha_scale = vdupq_n_f32(255.0 * 256.0);
let src_pixels = neon_utils::load_u8x16(src, 0);
let alpha_u32 = vshrq_n_u32::<24>(vreinterpretq_u32_u8(src_pixels));
let zero_alpha_mask = vceqzq_u32(alpha_u32);
let alpha_f32 = vcvtq_f32_u32(vorrq_u32(alpha_u32, zero_alpha_mask));
let scaled_alpha_f32 = vdivq_f32(alpha_scale, alpha_f32);
let scaled_alpha_u32 = vcvtnq_u32_f32(scaled_alpha_f32);
let mma0 = vreinterpretq_u16_u8(vqtbl1q_u8(vreinterpretq_u8_u32(scaled_alpha_u32), shuffle1));
let mma1 = vreinterpretq_u16_u8(vqtbl1q_u8(vreinterpretq_u8_u32(scaled_alpha_u32), shuffle2));
let pix0 = vreinterpretq_u16_u8(vzip1q_u8(zero, src_pixels));
let pix1 = vreinterpretq_u16_u8(vzip2q_u8(zero, src_pixels));
let pix0 = neon_utils::mulhi_u16x8(pix0, mma0);
let pix1 = neon_utils::mulhi_u16x8(pix1, mma1);
let alpha = vandq_u8(src_pixels, alpha_mask);
let rgb = vcombine_u8(vmovn_u16(pix0), vmovn_u16(pix1));
let dst_pixels = vbslq_u8(alpha_mask, alpha, rgb);
let dst_ptr = dst.as_mut_ptr() as *mut u128;
vstrq_p128(dst_ptr, transmute(dst_pixels));
}
-1
View File
@@ -129,7 +129,6 @@ unsafe fn divide_alpha_four_pixels(src: *const U8x4, dst: *mut U8x4) {
let alpha_f32 = _mm_cvtepi32_ps(_mm_srli_epi32::<24>(src_pixels));
let scaled_alpha_f32 = _mm_div_ps(alpha_scale, alpha_f32);
// let scaled_alpha_f32 = _mm_mul_ps(alpha_scale, _mm_rcp_ps(alpha_f32));
let scaled_alpha_i32 = _mm_cvtps_epi32(scaled_alpha_f32);
let mma0 = _mm_shuffle_epi8(scaled_alpha_i32, shuffle1);
let mma1 = _mm_shuffle_epi8(scaled_alpha_i32, shuffle2);
+1 -2
View File
@@ -2,7 +2,7 @@ macro_rules! constify_imm8 {
($imm8:expr, $expand:ident) => {
#[allow(overflowing_literals)]
match ($imm8) & 0b0011_1111 {
0 => $expand!(0),
0 => {}
1 => $expand!(1),
2 => $expand!(2),
3 => $expand!(3),
@@ -33,7 +33,6 @@ macro_rules! constify_imm8 {
29 => $expand!(29),
30 => $expand!(30),
31 => $expand!(31),
32 => $expand!(32),
_ => unreachable!(),
}
};
+4
View File
@@ -8,6 +8,8 @@ use super::{Coefficients, Convolution};
#[cfg(target_arch = "x86_64")]
mod avx2;
mod native;
#[cfg(target_arch = "aarch64")]
mod neon;
#[cfg(target_arch = "x86_64")]
mod sse4;
@@ -24,6 +26,8 @@ impl Convolution for U8x4 {
CpuExtensions::Avx2 => avx2::horiz_convolution(src_image, dst_image, offset, coeffs),
#[cfg(target_arch = "x86_64")]
CpuExtensions::Sse4_1 => sse4::horiz_convolution(src_image, dst_image, offset, coeffs),
#[cfg(target_arch = "aarch64")]
CpuExtensions::Neon => neon::horiz_convolution(src_image, dst_image, offset, coeffs),
_ => native::horiz_convolution(src_image, dst_image, offset, coeffs),
}
}
+308
View File
@@ -0,0 +1,308 @@
use std::arch::aarch64::*;
use crate::convolution::{optimisations, Coefficients};
use crate::image_view::{FourRows, FourRowsMut};
use crate::neon_utils;
use crate::pixels::U8x4;
use crate::{ImageView, ImageViewMut};
#[inline]
pub(crate) fn horiz_convolution(
src_image: &ImageView<U8x4>,
dst_image: &mut ImageViewMut<U8x4>,
offset: u32,
coeffs: Coefficients,
) {
let normalizer = optimisations::Normalizer16::new(coeffs);
let precision = normalizer.precision();
let coefficients_chunks = normalizer.normalized_chunks();
let dst_height = dst_image.height().get();
let src_iter = src_image.iter_4_rows(offset, dst_height + offset);
let dst_iter = dst_image.iter_4_rows_mut();
for (src_rows, dst_rows) in src_iter.zip(dst_iter) {
unsafe {
horiz_convolution_8u4x(src_rows, dst_rows, &coefficients_chunks, precision);
}
}
let mut yy = dst_height - dst_height % 4;
while yy < dst_height {
unsafe {
horiz_convolution_8u(
src_image.get_row(yy + offset).unwrap(),
dst_image.get_row_mut(yy).unwrap(),
&coefficients_chunks,
precision,
);
}
yy += 1;
}
}
/// For safety, it is necessary to ensure the following conditions:
/// - length of all rows in src_rows must be equal
/// - length of all rows in dst_rows must be equal
/// - coefficients_chunks.len() == dst_rows.0.len()
/// - max(chunk.start + chunk.values.len() for chunk in coefficients_chunks) <= src_row.0.len()
/// - precision <= MAX_COEFS_PRECISION
#[target_feature(enable = "neon")]
unsafe fn horiz_convolution_8u4x(
src_rows: FourRows<U8x4>,
dst_rows: FourRowsMut<U8x4>,
coefficients_chunks: &[optimisations::CoefficientsI16Chunk],
precision: u8,
) {
let (s_row0, s_row1, s_row2, s_row3) = src_rows;
let s_rows = [s_row0, s_row1, s_row2, s_row3];
let (d_row0, d_row1, d_row2, d_row3) = dst_rows;
let d_rows = [d_row0, d_row1, d_row2, d_row3];
let initial = vdupq_n_s32(1 << (precision - 1));
let zero_u8x16 = vdupq_n_u8(0);
let zero_u8x8 = vdup_n_u8(0);
for (dst_x, coeffs_chunk) in coefficients_chunks.iter().enumerate() {
let mut x: usize = coeffs_chunk.start as usize;
let mut sss_a = [initial; 4];
let mut coeffs = coeffs_chunk.values;
let coeffs_by_8 = coeffs.chunks_exact(8);
coeffs = coeffs_by_8.remainder();
for k in coeffs_by_8 {
let coeffs_i16x8 = neon_utils::load_i16x8(k, 0);
let coeff0 = vdup_laneq_s16::<0>(coeffs_i16x8);
let coeff1 = vdup_laneq_s16::<1>(coeffs_i16x8);
let coeff2 = vdup_laneq_s16::<2>(coeffs_i16x8);
let coeff3 = vdup_laneq_s16::<3>(coeffs_i16x8);
let coeff4 = vdup_laneq_s16::<4>(coeffs_i16x8);
let coeff5 = vdup_laneq_s16::<5>(coeffs_i16x8);
let coeff6 = vdup_laneq_s16::<6>(coeffs_i16x8);
let coeff7 = vdup_laneq_s16::<7>(coeffs_i16x8);
for i in 0..4 {
let source = neon_utils::load_u8x16(s_rows[i], x);
let mut sss = sss_a[i];
let source_i16 = vreinterpretq_s16_u8(vzip1q_u8(source, zero_u8x16));
let pix = vget_low_s16(source_i16);
sss = vmlal_s16(sss, pix, coeff0);
let pix = vget_high_s16(source_i16);
sss = vmlal_s16(sss, pix, coeff1);
let source_i16 = vreinterpretq_s16_u8(vzip2q_u8(source, zero_u8x16));
let pix = vget_low_s16(source_i16);
sss = vmlal_s16(sss, pix, coeff2);
let pix = vget_high_s16(source_i16);
sss = vmlal_s16(sss, pix, coeff3);
let source = neon_utils::load_u8x16(s_rows[i], x + 4);
let source_i16 = vreinterpretq_s16_u8(vzip1q_u8(source, zero_u8x16));
let pix = vget_low_s16(source_i16);
sss = vmlal_s16(sss, pix, coeff4);
let pix = vget_high_s16(source_i16);
sss = vmlal_s16(sss, pix, coeff5);
let source_i16 = vreinterpretq_s16_u8(vzip2q_u8(source, zero_u8x16));
let pix = vget_low_s16(source_i16);
sss = vmlal_s16(sss, pix, coeff6);
let pix = vget_high_s16(source_i16);
sss = vmlal_s16(sss, pix, coeff7);
sss_a[i] = sss;
}
x += 8;
}
let coeffs_by_4 = coeffs.chunks_exact(4);
coeffs = coeffs_by_4.remainder();
for k in coeffs_by_4 {
let coeffs_i16x4 = neon_utils::load_i16x4(k, 0);
let coeff0 = vdup_lane_s16::<0>(coeffs_i16x4);
let coeff1 = vdup_lane_s16::<1>(coeffs_i16x4);
let coeff2 = vdup_lane_s16::<2>(coeffs_i16x4);
let coeff3 = vdup_lane_s16::<3>(coeffs_i16x4);
for i in 0..4 {
let source = neon_utils::load_u8x16(s_rows[i], x);
let mut sss = sss_a[i];
let source_i16 = vreinterpretq_s16_u8(vzip1q_u8(source, zero_u8x16));
let pix = vget_low_s16(source_i16);
sss = vmlal_s16(sss, pix, coeff0);
let pix = vget_high_s16(source_i16);
sss = vmlal_s16(sss, pix, coeff1);
let source_i16 = vreinterpretq_s16_u8(vzip2q_u8(source, zero_u8x16));
let pix = vget_low_s16(source_i16);
sss = vmlal_s16(sss, pix, coeff2);
let pix = vget_high_s16(source_i16);
sss = vmlal_s16(sss, pix, coeff3);
sss_a[i] = sss;
}
x += 4;
}
let coeffs_by_2 = coeffs.chunks_exact(2);
coeffs = coeffs_by_2.remainder();
for k in coeffs_by_2 {
let coeff0 = vdup_n_s16(k[0]);
let coeff1 = vdup_n_s16(k[1]);
for i in 0..4 {
let source = neon_utils::load_u8x8(s_rows[i], x);
let mut sss = sss_a[i];
let pix = vreinterpret_s16_u8(vzip1_u8(source, zero_u8x8));
sss = vmlal_s16(sss, pix, coeff0);
let pix = vreinterpret_s16_u8(vzip2_u8(source, zero_u8x8));
sss = vmlal_s16(sss, pix, coeff1);
sss_a[i] = sss;
}
x += 2
}
if let Some(&k) = coeffs.first() {
let coeff = vdup_n_s16(k);
for i in 0..4 {
let source = neon_utils::load_u8x4(s_rows[i], x);
let pix = vreinterpret_s16_u8(vzip1_u8(source, zero_u8x8));
sss_a[i] = vmlal_s16(sss_a[i], pix, coeff);
}
}
macro_rules! call {
($imm8:expr) => {{
sss_a[0] = vshrq_n_s32::<$imm8>(sss_a[0]);
sss_a[1] = vshrq_n_s32::<$imm8>(sss_a[1]);
sss_a[2] = vshrq_n_s32::<$imm8>(sss_a[2]);
sss_a[3] = vshrq_n_s32::<$imm8>(sss_a[3]);
}};
}
constify_imm8!(precision, call);
for i in 0..4 {
let s = vqmovun_s16(vcombine_s16(vqmovn_s32(sss_a[i]), vdup_n_s16(0)));
let s = vreinterpret_u32_u8(s);
d_rows[i].get_unchecked_mut(dst_x).0 = vget_lane_u32::<0>(s);
}
}
}
/// For safety, it is necessary to ensure the following conditions:
/// - bounds.len() == dst_row.len()
/// - coefficients_chunks.len() == dst_row.len()
/// - max(chunk.start + chunk.values.len() for chunk in coefficients_chunks) <= src_row.len()
/// - precision <= MAX_COEFS_PRECISION
#[target_feature(enable = "neon")]
unsafe fn horiz_convolution_8u(
src_row: &[U8x4],
dst_row: &mut [U8x4],
coefficients_chunks: &[optimisations::CoefficientsI16Chunk],
precision: u8,
) {
let initial = vdupq_n_s32(1 << (precision - 1));
let zero_u8x16 = vdupq_n_u8(0);
let zero_u8x8 = vdup_n_u8(0);
for (dst_x, &coeffs_chunk) in coefficients_chunks.iter().enumerate() {
let mut x: usize = coeffs_chunk.start as usize;
let mut sss = initial;
let mut coeffs = coeffs_chunk.values;
let coeffs_by_8 = coeffs.chunks_exact(8);
coeffs = coeffs_by_8.remainder();
for k in coeffs_by_8 {
let coeffs_i16x8 = neon_utils::load_i16x8(k, 0);
let source = neon_utils::load_u8x16(src_row, x);
let source_i16 = vreinterpretq_s16_u8(vzip1q_u8(source, zero_u8x16));
let pix = vget_low_s16(source_i16);
sss = vmlal_s16(sss, pix, vdup_laneq_s16::<0>(coeffs_i16x8));
let pix = vget_high_s16(source_i16);
sss = vmlal_s16(sss, pix, vdup_laneq_s16::<1>(coeffs_i16x8));
let source_i16 = vreinterpretq_s16_u8(vzip2q_u8(source, zero_u8x16));
let pix = vget_low_s16(source_i16);
sss = vmlal_s16(sss, pix, vdup_laneq_s16::<2>(coeffs_i16x8));
let pix = vget_high_s16(source_i16);
sss = vmlal_s16(sss, pix, vdup_laneq_s16::<3>(coeffs_i16x8));
let source = neon_utils::load_u8x16(src_row, x + 4);
let source_i16 = vreinterpretq_s16_u8(vzip1q_u8(source, zero_u8x16));
let pix = vget_low_s16(source_i16);
sss = vmlal_s16(sss, pix, vdup_laneq_s16::<4>(coeffs_i16x8));
let pix = vget_high_s16(source_i16);
sss = vmlal_s16(sss, pix, vdup_laneq_s16::<5>(coeffs_i16x8));
let source_i16 = vreinterpretq_s16_u8(vzip2q_u8(source, zero_u8x16));
let pix = vget_low_s16(source_i16);
sss = vmlal_s16(sss, pix, vdup_laneq_s16::<6>(coeffs_i16x8));
let pix = vget_high_s16(source_i16);
sss = vmlal_s16(sss, pix, vdup_laneq_s16::<7>(coeffs_i16x8));
x += 8;
}
let coeffs_by_4 = coeffs.chunks_exact(4);
coeffs = coeffs_by_4.remainder();
for k in coeffs_by_4 {
let coeffs_i16x4 = neon_utils::load_i16x4(k, 0);
let source = neon_utils::load_u8x16(src_row, x);
let source_i16 = vreinterpretq_s16_u8(vzip1q_u8(source, zero_u8x16));
let pix = vget_low_s16(source_i16);
sss = vmlal_s16(sss, pix, vdup_lane_s16::<0>(coeffs_i16x4));
let pix = vget_high_s16(source_i16);
sss = vmlal_s16(sss, pix, vdup_lane_s16::<1>(coeffs_i16x4));
let source_i16 = vreinterpretq_s16_u8(vzip2q_u8(source, zero_u8x16));
let pix = vget_low_s16(source_i16);
sss = vmlal_s16(sss, pix, vdup_lane_s16::<2>(coeffs_i16x4));
let pix = vget_high_s16(source_i16);
sss = vmlal_s16(sss, pix, vdup_lane_s16::<3>(coeffs_i16x4));
x += 4;
}
let coeffs_by_2 = coeffs.chunks_exact(2);
coeffs = coeffs_by_2.remainder();
for k in coeffs_by_2 {
let source = neon_utils::load_u8x8(src_row, x);
let pix = vreinterpret_s16_u8(vzip1_u8(source, zero_u8x8));
sss = vmlal_s16(sss, pix, vdup_n_s16(k[0]));
let pix = vreinterpret_s16_u8(vzip2_u8(source, zero_u8x8));
sss = vmlal_s16(sss, pix, vdup_n_s16(k[1]));
x += 2
}
if let Some(&k) = coeffs.first() {
let source = neon_utils::load_u8x4(src_row, x);
let pix = vreinterpret_s16_u8(vzip1_u8(source, zero_u8x8));
sss = vmlal_s16(sss, pix, vdup_n_s16(k));
}
macro_rules! call {
($imm8:expr) => {{
sss = vshrq_n_s32::<$imm8>(sss);
}};
}
constify_imm8!(precision, call);
let s = vqmovun_s16(vcombine_s16(vqmovn_s32(sss), vdup_n_s16(0)));
let s = vreinterpret_u32_u8(s);
dst_row.get_unchecked_mut(dst_x).0 = vget_lane_u32::<0>(s);
}
}
+4
View File
@@ -6,6 +6,8 @@ use crate::{ImageView, ImageViewMut};
#[cfg(target_arch = "x86_64")]
pub(crate) mod avx2;
pub(crate) mod native;
#[cfg(target_arch = "aarch64")]
mod neon;
#[cfg(target_arch = "x86_64")]
pub(crate) mod sse4;
@@ -20,6 +22,8 @@ pub(crate) fn vert_convolution_u8<T: PixelExt<Component = u8>>(
CpuExtensions::Avx2 => avx2::vert_convolution(src_image, dst_image, coeffs),
#[cfg(target_arch = "x86_64")]
CpuExtensions::Sse4_1 => sse4::vert_convolution(src_image, dst_image, coeffs),
#[cfg(target_arch = "aarch64")]
CpuExtensions::Neon => neon::vert_convolution(src_image, dst_image, coeffs),
_ => native::vert_convolution(src_image, dst_image, coeffs),
}
}
+185
View File
@@ -0,0 +1,185 @@
use std::arch::aarch64::*;
use std::mem::transmute;
use crate::convolution::{optimisations, Coefficients};
use crate::neon_utils;
use crate::pixels::PixelExt;
use crate::{ImageView, ImageViewMut};
pub(crate) fn vert_convolution<T: PixelExt<Component = u8>>(
src_image: &ImageView<T>,
dst_image: &mut ImageViewMut<T>,
coeffs: Coefficients,
) {
// Check safety conditions
debug_assert_eq!(src_image.width(), dst_image.width());
debug_assert_eq!(coeffs.bounds.len(), dst_image.height().get() as usize);
let normalizer = optimisations::Normalizer16::new(coeffs);
let coefficients_chunks = normalizer.normalized_chunks();
let precision = normalizer.precision();
let initial = 1 << (precision - 1);
let mut tmp_dst = vec![0i32; dst_image.width().get() as usize * T::count_of_components()];
let tmp_buf = tmp_dst.as_mut_slice();
let dst_rows = dst_image.iter_rows_mut();
for (dst_row, coeffs_chunk) in dst_rows.zip(coefficients_chunks) {
tmp_buf.fill(initial);
unsafe {
vert_convolution_into_one_row_i32(src_image, tmp_buf, coeffs_chunk);
let dst_comp = T::components_mut(dst_row);
macro_rules! call {
($imm8:expr) => {{
store_tmp_buf_into_dst_row::<$imm8>(tmp_buf, dst_comp, &normalizer);
}};
}
constify_imm8!(precision as i32, call);
}
}
}
#[target_feature(enable = "neon")]
unsafe fn vert_convolution_into_one_row_i32<T: PixelExt<Component = u8>>(
src_img: &ImageView<T>,
dst_buf: &mut [i32],
coeffs_chunk: optimisations::CoefficientsI16Chunk,
) {
let width = dst_buf.len();
let y_start = coeffs_chunk.start;
let coeffs = coeffs_chunk.values;
let zero_u8x16 = vdupq_n_u8(0);
let zero_u8x8 = vdup_n_u8(0);
for (s_row, &coeff) in src_img.iter_rows(y_start).zip(coeffs) {
let components = T::components(s_row);
let coeff_i16x4 = vdup_n_s16(coeff);
let mut x: usize = 0;
while x < width.saturating_sub(63) {
let source = neon_utils::load_u8x16x4(components, x);
for s in [source.0, source.1, source.2, source.3] {
let mut accum = neon_utils::load_i32x4x4(dst_buf, x);
let pix = vreinterpretq_s16_u8(vzip1q_u8(s, zero_u8x16));
accum.0 = vmlal_s16(accum.0, vget_low_s16(pix), coeff_i16x4);
accum.1 = vmlal_s16(accum.1, vget_high_s16(pix), coeff_i16x4);
let pix = vreinterpretq_s16_u8(vzip2q_u8(s, zero_u8x16));
accum.2 = vmlal_s16(accum.2, vget_low_s16(pix), coeff_i16x4);
accum.3 = vmlal_s16(accum.3, vget_high_s16(pix), coeff_i16x4);
neon_utils::store_i32x4x4(dst_buf, x, accum);
x += 16;
}
}
if x < width.saturating_sub(31) {
let source = neon_utils::load_u8x16x2(components, x);
for s in [source.0, source.1] {
let mut accum = neon_utils::load_i32x4x4(dst_buf, x);
let pix = vreinterpretq_s16_u8(vzip1q_u8(s, zero_u8x16));
accum.0 = vmlal_s16(accum.0, vget_low_s16(pix), coeff_i16x4);
accum.1 = vmlal_s16(accum.1, vget_high_s16(pix), coeff_i16x4);
let pix = vreinterpretq_s16_u8(vzip2q_u8(s, zero_u8x16));
accum.2 = vmlal_s16(accum.2, vget_low_s16(pix), coeff_i16x4);
accum.3 = vmlal_s16(accum.3, vget_high_s16(pix), coeff_i16x4);
neon_utils::store_i32x4x4(dst_buf, x, accum);
x += 16;
}
}
if x < width.saturating_sub(15) {
let s = neon_utils::load_u8x16(components, x);
let mut accum = neon_utils::load_i32x4x4(dst_buf, x);
let pix = vreinterpretq_s16_u8(vzip1q_u8(s, zero_u8x16));
accum.0 = vmlal_s16(accum.0, vget_low_s16(pix), coeff_i16x4);
accum.1 = vmlal_s16(accum.1, vget_high_s16(pix), coeff_i16x4);
let pix = vreinterpretq_s16_u8(vzip2q_u8(s, zero_u8x16));
accum.2 = vmlal_s16(accum.2, vget_low_s16(pix), coeff_i16x4);
accum.3 = vmlal_s16(accum.3, vget_high_s16(pix), coeff_i16x4);
neon_utils::store_i32x4x4(dst_buf, x, accum);
x += 16;
}
if x < width.saturating_sub(7) {
let s = vcombine_u8(neon_utils::load_u8x8(components, x), zero_u8x8);
let mut accum = neon_utils::load_i32x4x2(dst_buf, x);
let pix = vreinterpretq_s16_u8(vzip1q_u8(s, zero_u8x16));
accum.0 = vmlal_s16(accum.0, vget_low_s16(pix), coeff_i16x4);
accum.1 = vmlal_s16(accum.1, vget_high_s16(pix), coeff_i16x4);
neon_utils::store_i32x4x2(dst_buf, x, accum);
x += 8;
}
if x < width.saturating_sub(3) {
let s = neon_utils::create_u8x16_from_one_u32(components, x);
let mut accum = neon_utils::load_i32x4(dst_buf, x);
let pix = vreinterpretq_s16_u8(vzip1q_u8(s, zero_u8x16));
accum = vmlal_s16(accum, vget_low_s16(pix), coeff_i16x4);
neon_utils::store_i32x4(dst_buf, x, accum);
x += 4
}
let coeff = coeff as i32;
let tmp_tail = dst_buf.iter_mut().skip(x);
let comp_tail = components.iter().skip(x);
for (accum, &comp) in tmp_tail.zip(comp_tail) {
*accum += coeff * comp as i32;
}
}
}
#[target_feature(enable = "neon")]
unsafe fn store_tmp_buf_into_dst_row<const IMM: i32>(
mut src_buf: &[i32],
dst_buf: &mut [u8],
normalizer: &optimisations::Normalizer16,
) {
let mut dst_chunks_16 = dst_buf.chunks_exact_mut(16);
let src_chunks_16 = src_buf.chunks_exact(16);
src_buf = src_chunks_16.remainder();
for (dst_chunk, src_chunk) in dst_chunks_16.by_ref().zip(src_chunks_16) {
let mut accum = neon_utils::load_i32x4x4(src_chunk, 0);
accum.0 = vshrq_n_s32::<IMM>(accum.0);
accum.1 = vshrq_n_s32::<IMM>(accum.1);
accum.2 = vshrq_n_s32::<IMM>(accum.2);
accum.3 = vshrq_n_s32::<IMM>(accum.3);
let sss0_i16 = vcombine_s16(vqmovn_s32(accum.0), vqmovn_s32(accum.1));
let sss1_i16 = vcombine_s16(vqmovn_s32(accum.2), vqmovn_s32(accum.3));
let sss_u8 = vcombine_u8(vqmovun_s16(sss0_i16), vqmovun_s16(sss1_i16));
let dst_ptr = dst_chunk.as_mut_ptr() as *mut u128;
vstrq_p128(dst_ptr, transmute(sss_u8));
}
let mut dst_chunks_8 = dst_chunks_16.into_remainder().chunks_exact_mut(8);
let src_chunks_8 = src_buf.chunks_exact(8);
src_buf = src_chunks_8.remainder();
for (dst_chunk, src_chunk) in dst_chunks_8.by_ref().zip(src_chunks_8) {
let mut accum = neon_utils::load_i32x4x2(src_chunk, 0);
accum.0 = vshrq_n_s32::<IMM>(accum.0);
accum.1 = vshrq_n_s32::<IMM>(accum.1);
let sss_i16 = vcombine_s16(vqmovn_s32(accum.0), vqmovn_s32(accum.1));
let sss_u8 = vcombine_u8(vqmovun_s16(sss_i16), vqmovun_s16(sss_i16));
let res = vdupd_laneq_u64::<0>(vreinterpretq_u64_u8(sss_u8));
let dst_ptr = dst_chunk.as_mut_ptr() as *mut u64;
*dst_ptr = res;
}
let mut dst_chunks_4 = dst_chunks_8.into_remainder().chunks_exact_mut(4);
let src_chunks_4 = src_buf.chunks_exact(4);
src_buf = src_chunks_4.remainder();
for (dst_chunk, src_chunk) in dst_chunks_4.by_ref().zip(src_chunks_4) {
let mut accum = neon_utils::load_i32x4(src_chunk, 0);
accum = vshrq_n_s32::<IMM>(accum);
let sss_i16 = vcombine_s16(vqmovn_s32(accum), vqmovn_s32(accum));
let sss_u8 = vcombine_u8(vqmovun_s16(sss_i16), vqmovun_s16(sss_i16));
let res = vdups_laneq_u32::<0>(vreinterpretq_u32_u8(sss_u8));
let dst_ptr = dst_chunk.as_mut_ptr() as *mut u32;
*dst_ptr = res;
}
let dst_chunk = dst_chunks_4.into_remainder();
for (dst, &src) in dst_chunk.iter_mut().zip(src_buf) {
*dst = normalizer.clip(src);
}
}
+2
View File
@@ -21,6 +21,8 @@ mod errors;
mod image;
mod image_view;
mod mul_div;
#[cfg(target_arch = "aarch64")]
mod neon_utils;
pub mod pixels;
mod resizer;
#[cfg(target_arch = "x86_64")]
+93
View File
@@ -0,0 +1,93 @@
use std::arch::aarch64::*;
#[inline(always)]
pub unsafe fn load_u8x4<T>(buf: &[T], index: usize) -> uint8x8_t {
let ptr = buf.get_unchecked(index..).as_ptr() as *const u32;
vcreate_u8(*ptr as u64)
}
#[inline(always)]
pub unsafe fn load_u8x8<T>(buf: &[T], index: usize) -> uint8x8_t {
vld1_u8(buf.get_unchecked(index..).as_ptr() as *const u8)
}
#[inline(always)]
pub unsafe fn load_u8x16<T>(buf: &[T], index: usize) -> uint8x16_t {
vld1q_u8(buf.get_unchecked(index..).as_ptr() as *const u8)
}
#[inline(always)]
pub unsafe fn load_u8x16x2<T>(buf: &[T], index: usize) -> uint8x16x2_t {
vld1q_u8_x2(buf.get_unchecked(index..).as_ptr() as *const u8)
}
#[inline(always)]
pub unsafe fn load_u8x16x4<T>(buf: &[T], index: usize) -> uint8x16x4_t {
vld1q_u8_x4(buf.get_unchecked(index..).as_ptr() as *const u8)
}
#[inline(always)]
pub unsafe fn load_i32x4<T>(buf: &[T], index: usize) -> int32x4_t {
vld1q_s32(buf.get_unchecked(index..).as_ptr() as *const i32)
}
#[inline(always)]
pub unsafe fn store_i32x4<T>(buf: &mut [T], index: usize, v: int32x4_t) {
vst1q_s32(buf.get_unchecked_mut(index..).as_mut_ptr() as *mut i32, v);
}
#[inline(always)]
pub unsafe fn load_i32x4x2<T>(buf: &[T], index: usize) -> int32x4x2_t {
vld1q_s32_x2(buf.get_unchecked(index..).as_ptr() as *const i32)
}
#[inline(always)]
pub unsafe fn store_i32x4x2<T>(buf: &mut [T], index: usize, v: int32x4x2_t) {
vst1q_s32_x2(buf.get_unchecked_mut(index..).as_mut_ptr() as *mut i32, v);
}
#[inline(always)]
pub unsafe fn load_i32x4x4<T>(buf: &[T], index: usize) -> int32x4x4_t {
vld1q_s32_x4(buf.get_unchecked(index..).as_ptr() as *const i32)
}
#[inline(always)]
pub unsafe fn store_i32x4x4<T>(buf: &mut [T], index: usize, v: int32x4x4_t) {
vst1q_s32_x4(buf.get_unchecked_mut(index..).as_mut_ptr() as *mut i32, v);
}
#[inline(always)]
pub unsafe fn load_i16x4<T>(buf: &[T], index: usize) -> int16x4_t {
vld1_s16(buf.get_unchecked(index..).as_ptr() as *const i16)
}
#[inline(always)]
pub unsafe fn load_i16x8<T>(buf: &[T], index: usize) -> int16x8_t {
vld1q_s16(buf.get_unchecked(index..).as_ptr() as *const i16)
}
/// Moves 32-bit integer from `buf` to the least significant 32 bits of an uint8x16_t object,
/// zero extending the upper bits.
/// ```plain
/// r0 := a
/// r1 := 0x0
/// r2 := 0x0
/// r3 := 0x0
/// ```
#[inline(always)]
pub unsafe fn create_u8x16_from_one_u32<T>(buf: &[T], index: usize) -> uint8x16_t {
let ptr = buf.get_unchecked(index..).as_ptr() as *const u32;
vreinterpretq_u8_u32(vsetq_lane_u32::<0>(*ptr, vdupq_n_u32(0u32)))
}
/// Multiply the packed unsigned 16-bit integers in a and b, producing
/// intermediate 32-bit integers, and store the high 16 bits of the intermediate
/// integers in dst.
#[inline(always)]
pub unsafe fn mulhi_u16x8(a: uint16x8_t, b: uint16x8_t) -> uint16x8_t {
let a3210 = vget_low_u16(a);
let b3210 = vget_low_u16(b);
let ab3210 = vmull_u16(a3210, b3210);
let ab7654 = vmull_high_u16(a, b);
vuzp2q_u16(vreinterpretq_u16_u32(ab3210), vreinterpretq_u16_u32(ab7654))
}
+13 -1
View File
@@ -14,6 +14,8 @@ pub enum CpuExtensions {
Sse4_1,
#[cfg(target_arch = "x86_64")]
Avx2,
#[cfg(target_arch = "aarch64")]
Neon,
}
impl Default for CpuExtensions {
@@ -28,7 +30,17 @@ impl Default for CpuExtensions {
}
}
#[cfg(not(target_arch = "x86_64"))]
#[cfg(target_arch = "aarch64")]
fn default() -> Self {
use std::arch::is_aarch64_feature_detected;
if is_aarch64_feature_detected!("neon") {
Self::Neon
} else {
Self::None
}
}
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
fn default() -> Self {
Self::None
}
+2
View File
@@ -266,5 +266,7 @@ pub fn cpu_ext_into_str(cpu_extensions: CpuExtensions) -> &'static str {
CpuExtensions::Sse4_1 => "sse41",
#[cfg(target_arch = "x86_64")]
CpuExtensions::Avx2 => "avx2",
#[cfg(target_arch = "aarch64")]
CpuExtensions::Neon => "neon",
}
}
+16
View File
@@ -124,6 +124,14 @@ mod multiply_alpha_u8x4 {
}
}
#[cfg(target_arch = "aarch64")]
#[test]
fn neon_test() {
for (s, r) in SRC_PIXELS.into_iter().zip(RES_PIXELS) {
mul_div_alpha_test(Oper::Mul, s, r, CpuExtensions::Neon);
}
}
#[test]
fn native_test() {
for (s, r) in SRC_PIXELS.into_iter().zip(RES_PIXELS) {
@@ -305,6 +313,14 @@ mod divide_alpha_u8x4 {
}
}
#[cfg(target_arch = "aarch64")]
#[test]
fn neon_test() {
for (s, r) in SRC_PIXELS.into_iter().zip(RES_PIXELS) {
mul_div_alpha_test(OPER, s, r, CpuExtensions::Neon);
}
}
#[test]
fn native_test() {
for (s, r) in SRC_PIXELS.into_iter().zip(RES_PIXELS) {
+4
View File
@@ -315,6 +315,10 @@ fn downscale_u8x4() {
cpu_extensions_vec.push(CpuExtensions::Sse4_1);
cpu_extensions_vec.push(CpuExtensions::Avx2);
}
#[cfg(target_arch = "aarch64")]
{
cpu_extensions_vec.push(CpuExtensions::Neon);
}
for cpu_extensions in cpu_extensions_vec {
let buffer =
downscale_test::<P>(ResizeAlg::Convolution(FilterType::Lanczos3), cpu_extensions);