-
Notifications
You must be signed in to change notification settings - Fork 11
SIMD implementations #10
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
Draft
asze17
wants to merge
5
commits into
main
Choose a base branch
from
feature/simd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
02b968b
Implement x86 specific merge impl
asze17 432c9a2
Resolve requested changes
asze17 a4f3bdd
Merge branch 'main' of github.com:jay3332/ril into feature/simd
asze17 3ccae17
SIMD implementation of invert
asze17 bc3f458
Add a cfg flag to detect if rustc is nightly
asze17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| use rustc_version::{version, version_meta, Channel}; | ||
|
|
||
| fn main() { | ||
| assert!(version().unwrap().major >= 1); | ||
|
|
||
| match version_meta().unwrap().channel { | ||
| Channel::Nightly => println!("cargo:rustc-cfg=RUSTC_IS_NIGHTLY"), | ||
| _ => {} | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| use crate::Rgba; | ||
|
|
||
| pub fn _merge_impl(base: Rgba, other: Rgba) -> Rgba { | ||
| let (base_r, base_g, base_b, base_a) = ( | ||
| base.r as f32 / 255., | ||
| base.g as f32 / 255., | ||
| base.b as f32 / 255., | ||
| base.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, | ||
| } | ||
| } | ||
|
|
||
| #[inline] | ||
| pub fn _invert_impl(base: Rgba) -> Rgba { | ||
| Rgba { | ||
| r: !base.r, | ||
| g: !base.g, | ||
| b: !base.b, | ||
| a: !base.a, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #![allow(clippy::cast_lossless)] | ||
| #![allow(clippy::wildcard_imports)] | ||
| #![allow(dead_code)] | ||
|
|
||
| mod aarch64; | ||
| mod manual; | ||
| mod x86; | ||
|
|
||
| use crate::Rgba; | ||
|
|
||
| #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
| use std::is_x86_feature_detected; | ||
|
|
||
| #[inline] | ||
| pub fn merge_impl(base: Rgba, other: Rgba) -> Rgba { | ||
| // Optimize for common cases | ||
| if other.a == 255 { | ||
| return other; | ||
| } else if other.a == 0 { | ||
| return base; | ||
| } | ||
|
|
||
| #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
| if is_x86_feature_detected!("sse") && is_x86_feature_detected!("fma") { | ||
| unsafe { | ||
| return x86::_merge_impl(base, other); | ||
| } | ||
| } | ||
|
|
||
| manual::_merge_impl(base, other) | ||
| } | ||
|
|
||
| #[inline] | ||
| pub fn invert_impl(base: Rgba) -> Rgba { | ||
| #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
| if is_x86_feature_detected!("sse") { | ||
| unsafe { | ||
| return x86::_invert_impl(base); | ||
| } | ||
| } | ||
|
|
||
| manual::_invert_impl(base) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| #[cfg(target_arch = "x86")] | ||
| use std::arch::x86::*; | ||
| #[cfg(target_arch = "x86_64")] | ||
| use std::arch::x86_64::*; | ||
|
|
||
| use crate::Rgba; | ||
|
|
||
| const ONES: f32 = unsafe { std::mem::transmute(0xff_ff_ff_ff_u32) }; | ||
|
|
||
| #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
| #[target_feature(enable = "sse")] | ||
| #[target_feature(enable = "fma")] | ||
| pub unsafe fn _merge_impl(base: Rgba, other: Rgba) -> Rgba { | ||
| let mut base_rgba = [0_f32; 4]; | ||
| let mut overlay = [0_f32; 4]; | ||
| let mut overlay_rgba = [0_f32; 4]; | ||
| let mut rgba = [0_f32; 4]; | ||
| let mut res = [0_f32; 4]; | ||
|
|
||
| _mm_store_ps( | ||
| base_rgba.as_mut_ptr(), | ||
| _mm_div_ps( | ||
| _mm_setr_ps(base.r as f32, base.g as f32, base.b as f32, base.a as f32), | ||
| _mm_set1_ps(255.), | ||
| ), | ||
| ); | ||
|
|
||
| let [base_r, base_g, base_b, base_a] = base_rgba; | ||
| _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; | ||
|
|
||
| _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; | ||
|
|
||
| _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; | ||
|
|
||
| _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; | ||
|
|
||
| Rgba { | ||
| r: r as u8, | ||
| g: g as u8, | ||
| b: b as u8, | ||
| a: a as u8, | ||
| } | ||
| } | ||
|
|
||
| #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
| #[target_feature(enable = "sse")] | ||
| pub unsafe fn _invert_impl(base: Rgba) -> Rgba { | ||
| let mut res = [0_f32; 4]; | ||
|
|
||
| _mm_store_ps( | ||
| res.as_mut_ptr(), | ||
| _mm_xor_ps( | ||
| _mm_setr_ps(base.r as f32, base.g as f32, base.b as f32, base.a as f32), | ||
| _mm_set1_ps(ONES), | ||
| ), | ||
| ); | ||
|
|
||
| let [r, g, b, a] = res; | ||
|
|
||
| Rgba { | ||
| r: r as u8, | ||
| g: g as u8, | ||
| b: b as u8, | ||
| a: a as u8, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -261,6 +261,7 @@ | |
| clippy::doc_markdown | ||
| )] | ||
|
|
||
| mod arch; | ||
| pub mod draw; | ||
| pub mod encode; | ||
| pub mod encodings; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.