Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIMD implementations #10

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added src/arch/aarch64.rs
Empty file.
40 changes: 40 additions & 0 deletions src/arch/manual.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::Rgba;

#[allow(clippy::cast_lossless)]
pub fn _merge_impl(original: Rgba, other: Rgba) -> Rgba {
Cryptex-github marked this conversation as resolved.
Show resolved Hide resolved
// Optimize for common cases
if other.a == 255 {
return other;
} else if other.a == 0 {
return original;
}

let (base_r, base_g, base_b, base_a) = (
original.r as f32 / 255.,
original.g as f32 / 255.,
original.b as f32 / 255.,
original.a as f32 / 255.,
);

let (overlay_r, overlay_g, overlay_b, overlay_a) = (
other.r as f32 / 255.,
other.g as f32 / 255.,
other.b as f32 / 255.,
other.a as f32 / 255.,
);

let a_diff = 1. - overlay_a;
let a = a_diff.mul_add(base_a, overlay_a);

let a_ratio = a_diff * base_a;
let r = a_ratio.mul_add(base_r, overlay_a * overlay_r) / a;
let g = a_ratio.mul_add(base_g, overlay_a * overlay_g) / a;
let b = a_ratio.mul_add(base_b, overlay_a * overlay_b) / a;

Rgba {
r: (r * 255.) as u8,
g: (g * 255.) as u8,
b: (b * 255.) as u8,
a: (a * 255.) as u8,
}
}
17 changes: 17 additions & 0 deletions src/arch/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mod aarch64;
mod manual;
mod x86;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use std::is_x86_feature_detected;

pub fn merge_impl() -> unsafe fn(crate::Rgba, crate::Rgba) -> crate::Rgba {
Cryptex-github marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
if is_x86_feature_detected!("sse") && is_x86_feature_detected!("fma") {
return x86::_merge_impl;
}
}

manual::_merge_impl
}
76 changes: 76 additions & 0 deletions src/arch/x86.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;

Cryptex-github marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "sse")]
#[target_feature(enable = "fma")]
pub unsafe fn _merge_impl(original: crate::pixel::Rgba, other: crate::pixel::Rgba) -> crate::pixel::Rgba {
Cryptex-github marked this conversation as resolved.
Show resolved Hide resolved
// Optimize for common cases
Cryptex-github marked this conversation as resolved.
Show resolved Hide resolved
if other.a == 255 {
return other;
} else if other.a == 0 {
return original;
}

let mut base = [0_f32; 4];

_mm_store_ps(
base.as_mut_ptr(),
_mm_div_ps(_mm_setr_ps(original.r as f32, original.g as f32, original.b as f32, original.a as f32), _mm_set1_ps(255.)),
);

let [base_r, base_g, base_b, base_a] = base;

let mut overlay = [0_f32; 4];

_mm_store_ps(
overlay.as_mut_ptr(),
_mm_div_ps(_mm_setr_ps(other.r as f32, other.g as f32, other.b as f32, other.a as f32), _mm_set1_ps(255.)),
);

let [overlay_r, overlay_g, overlay_b, overlay_a] = overlay;

let a_diff = 1. - overlay_a;

let mut overlay_rgba = [0_f32; 4];

_mm_store_ps(
overlay_rgba.as_mut_ptr(),
_mm_mul_ps(
_mm_setr_ps(overlay_r, overlay_g, overlay_b, base_a),
_mm_setr_ps(overlay_a, overlay_a, overlay_a, a_diff),
),
);

let [overlay_r, overlay_g, overlay_b, a_ratio] = overlay_rgba;

let mut rgba = [0_f32; 4];

_mm_store_ps(
rgba.as_mut_ptr(),
_mm_fmadd_ps(
_mm_setr_ps(a_ratio, a_ratio, a_ratio, a_diff),
_mm_setr_ps(base_r, base_g, base_b, base_a),
_mm_setr_ps(overlay_r, overlay_g, overlay_b, overlay_a),
),
);

let [r, g, b, a] = rgba;

let mut res = [0_f32; 4];

_mm_store_ps(
res.as_mut_ptr(),
_mm_mul_ps(
_mm_div_ps(_mm_setr_ps(r, g, b, a), _mm_setr_ps(a, a, a, 1.)),
_mm_set1_ps(255.),
),
);

let [r, g, b, a] = res;

crate::pixel::Rgba { r: r as u8, g: g as u8, b: b as u8, a: a as u8 }
Cryptex-github marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ pub mod sequence;
#[cfg(feature = "text")]
pub mod text;

mod arch;
Cryptex-github marked this conversation as resolved.
Show resolved Hide resolved

macro_rules! inline_doc {
($($token:item)*) => {
$(#[doc(inline)] $token)*
Expand Down
39 changes: 2 additions & 37 deletions src/pixel.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Encloses pixel-related traits and pixel type implementations.

use crate::Error::DecodingError;
use crate::arch;
Cryptex-github marked this conversation as resolved.
Show resolved Hide resolved
use crate::{
encodings::ColorType,
image::OverlayMode,
Expand Down Expand Up @@ -888,44 +889,8 @@ impl Pixel for Rgba {
[self.r, self.g, self.b, self.a]
}

// TODO: SIMD could speed this up significantly
#[allow(clippy::cast_lossless)]
fn merge(self, other: Self) -> Self {
// Optimize for common cases
if other.a == 255 {
return other;
} else if other.a == 0 {
return self;
}

let (base_r, base_g, base_b, base_a) = (
self.r as f32 / 255.,
self.g as f32 / 255.,
self.b as f32 / 255.,
self.a as f32 / 255.,
);

let (overlay_r, overlay_g, overlay_b, overlay_a) = (
other.r as f32 / 255.,
other.g as f32 / 255.,
other.b as f32 / 255.,
other.a as f32 / 255.,
);

let a_diff = 1. - overlay_a;
let a = a_diff.mul_add(base_a, overlay_a);

let a_ratio = a_diff * base_a;
let r = a_ratio.mul_add(base_r, overlay_a * overlay_r) / a;
let g = a_ratio.mul_add(base_g, overlay_a * overlay_g) / a;
let b = a_ratio.mul_add(base_b, overlay_a * overlay_b) / a;

Self {
r: (r * 255.) as u8,
g: (g * 255.) as u8,
b: (b * 255.) as u8,
a: (a * 255.) as u8,
}
unsafe { arch::merge_impl()(self, other) }
}

#[allow(clippy::cast_lossless)]
Expand Down