-
Notifications
You must be signed in to change notification settings - Fork 1
hypotenuse function for f32, f64 #15
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a343307
hypotenuse function for f32, f64
skewballfox 11db444
use sqrt intrinsic for fastmath, implemented Hypot for Neon
skewballfox b1e9599
added hypot test for Neon, Axx512
skewballfox a1a2e27
revert sqrt change, fallback to direct calculation if no_std for now
skewballfox c1b88c7
added export_hypot
skewballfox 5ba40f7
safe api implemented
skewballfox 7479b04
fixed issue with hypot test, fixed clippy warnings
skewballfox 4085561
added export hypot test for avx2fma, subnormal floats
skewballfox c8fca55
apply changes from code review
skewballfox 511e1da
apply changes from code review
skewballfox c1120d8
apply changes from code review
skewballfox dbd00fc
changed name of numeric trait to MiscFloatOps
skewballfox 446c6b6
fixed issue with "out of order" arguments to neon fma functions
skewballfox 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,212 @@ | ||
| //! Common arithmetic operations | ||
| //! | ||
| //! I.e. Add, Sub, Mul, Div... | ||
| use crate::buffer::WriteOnlyBuffer; | ||
| use crate::danger::core_simd_api::Hypot; | ||
| use crate::danger::{generic_hypot_vertical, SimdRegister}; | ||
| use crate::math::{AutoMath, Math, Numeric}; | ||
| use crate::mem_loader::{IntoMemLoader, MemLoader}; | ||
|
|
||
| macro_rules! define_hypot_impls { | ||
| ( | ||
| $hypot_name:ident, | ||
| $imp:ident $(,)? | ||
| $(target_features = $($feat:expr $(,)?)+)? | ||
| ) => { | ||
| #[inline] | ||
| $(#[target_feature($(enable = $feat, )*)])* | ||
| #[doc = include_str!("../export_docs/arithmetic_hypot_vertical.md")] | ||
| $( | ||
|
|
||
| #[doc = concat!("- ", $("**`+", $feat, "`** ", )*)] | ||
| #[doc = "CPU features are available at runtime. Running on hardware _without_ this feature available will cause immediate UB."] | ||
| )* | ||
| pub unsafe fn $hypot_name<T, B1, B2, B3>( | ||
| a: B1, | ||
| b: B2, | ||
| result: &mut [B3], | ||
| ) | ||
| where | ||
| T: Copy, | ||
| B1: IntoMemLoader<T>, | ||
| B1::Loader: MemLoader<Value = T>, | ||
| B2: IntoMemLoader<T>, | ||
| B2::Loader: MemLoader<Value = T>, | ||
| crate::danger::$imp: SimdRegister<T>+ Hypot<T>, | ||
| AutoMath: Math<T> + Numeric<T>, | ||
| for<'a> &'a mut [B3]: WriteOnlyBuffer<Item = T>, | ||
| { | ||
| generic_hypot_vertical::<T, crate::danger::$imp, AutoMath, B1, B2, B3>( | ||
| a, | ||
| b, | ||
| result, | ||
| ) | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| define_hypot_impls!(generic_fallback_hypot_vertical, Fallback,); | ||
| #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
| define_hypot_impls!(generic_avx2_hypot_vertical, Avx2, target_features = "avx2"); | ||
| #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
| define_hypot_impls!( | ||
| generic_avx2fma_hypot_vertical, | ||
| Avx2Fma, | ||
| target_features = "avx2", | ||
| "fma" | ||
| ); | ||
| #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "nightly"))] | ||
| define_hypot_impls!( | ||
| generic_avx512_hypot_vertical, | ||
| Avx512, | ||
| target_features = "avx512f", | ||
| "avx512bw" | ||
| ); | ||
| #[cfg(target_arch = "aarch64")] | ||
| define_hypot_impls!(generic_neon_hypot_vertical, Neon, target_features = "neon"); | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use num_traits::{Float, FloatConst}; | ||
|
|
||
| use super::*; | ||
|
|
||
| macro_rules! define_inner_test { | ||
| ($variant:ident, op = $op:ident, ty = $t:ident) => { | ||
| paste::paste! { | ||
| #[test] | ||
| fn [< $variant _ $op _value_ $t >]() { | ||
| let (l1, _) = crate::test_utils::get_sample_vectors::<$t>(533); | ||
|
|
||
| let mut result = vec![$t::default(); 533]; | ||
| unsafe { [< $variant _ $op _vertical >](&l1, 2 as $t, &mut result) }; | ||
|
|
||
| let expected = l1.iter() | ||
| .copied() | ||
| .map(|v| AutoMath::$op(v, 2 as $t)) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| for ((initial, expected), actual) in l1.iter().zip(&expected).zip(&result) { | ||
| let ulps_diff = get_diff_ulps(*expected, *actual); | ||
| assert!( | ||
| ulps_diff.abs() <= 1, | ||
| "result differs by more than 1 ULP:\n initial inputs: {}, 2\n expected: {} actual: {}\nulps diff: {}", | ||
| initial, expected, actual, ulps_diff | ||
| ); | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| #[test] | ||
| fn [< $variant _ $op _vector_ $t >]() { | ||
| let (l1, l2) = crate::test_utils::get_sample_vectors::<$t>(533); | ||
|
|
||
| let mut result = vec![$t::default(); 533]; | ||
| unsafe { [< $variant _ $op _vertical >](&l1, &l2, &mut result) }; | ||
|
|
||
| let expected = l1.iter() | ||
| .copied() | ||
| .zip(l2.iter().copied()) | ||
| .map(|(a, b)| AutoMath::$op(a, b)) | ||
| .collect::<Vec<_>>(); | ||
| for ((initial, expected), actual) in l1.iter().zip(&expected).zip(&result) { | ||
| let ulps_diff = get_diff_ulps(*expected, *actual); | ||
| assert!( | ||
| ulps_diff.abs() <= 1, | ||
| "result differs by more than 1 ULP:\n initial inputs: {}, 2\n expected: {} actual: {}\nulps diff: {}", | ||
| initial, expected, actual, ulps_diff | ||
| ); | ||
| } | ||
| } | ||
| #[test] | ||
| fn [< $variant _ $op _subnormal_value_ $t >]() { | ||
| let (l1, _) = crate::test_utils::get_subnormal_sample_vectors::<$t>(533); | ||
|
|
||
| let mut result = vec![$t::default(); 533]; | ||
| unsafe { [< $variant _ $op _vertical >](&l1, 2 as $t, &mut result) }; | ||
|
|
||
| let expected = l1.iter() | ||
| .copied() | ||
| .map(|v| AutoMath::$op(v, 2 as $t)) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| for ((initial, expected), actual) in l1.iter().zip(&expected).zip(&result) { | ||
| let ulps_diff = get_diff_ulps(*expected, *actual); | ||
| assert!( | ||
| ulps_diff.abs() <= 1, | ||
| "result differs by more than 1 ULP:\n initial inputs: {}, 2\n expected: {} actual: {}\nulps diff: {}", | ||
| initial, expected, actual, ulps_diff | ||
| ); | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| #[test] | ||
| fn [< $variant _ $op _subnormal_vector_ $t >]() { | ||
| let (l1, l2) = crate::test_utils::get_subnormal_sample_vectors::<$t>(533); | ||
|
|
||
| let mut result = vec![$t::default(); 533]; | ||
| unsafe { [< $variant _ $op _vertical >](&l1, &l2, &mut result) }; | ||
|
|
||
| let expected = l1.iter() | ||
| .copied() | ||
| .zip(l2.iter().copied()) | ||
| .map(|(a, b)| AutoMath::$op(a, b)) | ||
| .collect::<Vec<_>>(); | ||
| for ((initial, expected), actual) in l1.iter().zip(&expected).zip(&result) { | ||
| let ulps_diff = get_diff_ulps(*expected, *actual); | ||
| assert!( | ||
| ulps_diff.abs() <= 1, | ||
| "result differs by more than 1 ULP:\n initial inputs: {}, 2\n expected: {} actual: {}\nulps diff: {}", | ||
| initial, expected, actual, ulps_diff | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| fn get_diff_ulps<T>(a: T, b: T) -> i64 | ||
| where | ||
| T: Float + FloatConst, | ||
| { | ||
| let (a_mant, a_exp, a_sign) = a.integer_decode(); | ||
| let (b_mant, b_exp, b_sign) = b.integer_decode(); | ||
| assert!(a_sign == b_sign); | ||
| assert!(a_exp == b_exp); | ||
| a_mant as i64 - b_mant as i64 | ||
| } | ||
|
|
||
| macro_rules! define_numeric_test { | ||
| ($variant:ident, types = $($t:ident $(,)?)+) => { | ||
| $( | ||
| define_inner_test!($variant, op = hypot, ty = $t); | ||
|
|
||
| )* | ||
| }; | ||
| } | ||
|
|
||
| define_numeric_test!(generic_fallback, types = f32, f64,); | ||
| #[cfg(all( | ||
| any(target_arch = "x86", target_arch = "x86_64"), | ||
| target_feature = "avx2" | ||
| ))] | ||
| define_numeric_test!(generic_avx2, types = f32, f64,); | ||
| #[cfg(all( | ||
| any(target_arch = "x86", target_arch = "x86_64"), | ||
| target_feature = "avx2", | ||
| target_feature = "fma" | ||
| ))] | ||
| define_numeric_test!(generic_avx2fma, types = f32, f64,); | ||
| #[cfg(all( | ||
| any(target_arch = "x86", target_arch = "x86_64"), | ||
| feature = "nightly", | ||
| target_feature = "avx512f" | ||
| ))] | ||
| define_numeric_test!(generic_avx512, types = f32, f64,); | ||
| #[cfg(target_arch = "aarch64")] | ||
skewballfox marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| define_numeric_test!(generic_neon, types = f32, f64,); | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, please add docs for this function as well, and just add a note under
# safetythat it depends on the safety requirements ofSimdRegister<T>.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean for the docs of
op_hypotcorrect?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, technically it depends on both but I guess if you satisfy
op_hypotyou satisfy the register requirements anyway.