Repository navigation
add ppcf128 library API
#164021
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
Open
folkertdev
wants to merge
5
commits into
rust-lang:main
Choose a base branch
from
folkertdev:ppcf128-library-api
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.
Open
add ppcf128 library API
#164021
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e8e2d61
add `ppcf128` API
folkertdev ee0b258
doc updates
folkertdev 366b6b5
ignore doc tests on non-powerpc targets
folkertdev e967f98
add representations table
folkertdev 7f937e4
remove to_bits and fix the doc prelude macro
folkertdev 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
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,172 @@ | ||
| #[cfg(target_arch = "powerpc")] | ||
| use core::arch::powerpc::ppcf128; | ||
| #[cfg(target_arch = "powerpc64")] | ||
| use core::arch::powerpc64::ppcf128; | ||
| use std::assert_matches; | ||
|
|
||
| const _: () = assert!(size_of::<ppcf128>() == 16); | ||
| const _: () = assert!(align_of::<ppcf128>() == 16); | ||
|
|
||
| #[test] | ||
| fn constants() { | ||
| assert_eq!( | ||
| ppcf128::MIN.to_le_bytes(), | ||
| [ | ||
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff, // | ||
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xfc, | ||
| ] | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| ppcf128::MAX.to_le_bytes(), | ||
| [ | ||
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0x7f, // | ||
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0x7c, | ||
| ] | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| ppcf128::NAN.to_le_bytes(), | ||
| [ | ||
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x7f, // | ||
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
| ] | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| ppcf128::INFINITY.to_le_bytes(), | ||
| [ | ||
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x7f, // | ||
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
| ] | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| ppcf128::NEG_INFINITY.to_le_bytes(), | ||
| [ | ||
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff, // | ||
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn impls() { | ||
| const ONE: ppcf128 = ppcf128::from_components(1.0, 0.0); | ||
| const NEGATIVE_ONE: ppcf128 = ppcf128::from_components(-1.0, 0.0); | ||
| const TWO: ppcf128 = ppcf128::from_components(2.0, 0.0); | ||
|
|
||
| assert_eq!(ONE, ONE); | ||
| assert_ne!(ONE, TWO); | ||
|
|
||
| assert!(ONE <= ONE); | ||
| assert!(ONE < TWO); | ||
| assert!(TWO > ONE); | ||
| assert!(TWO >= TWO); | ||
|
|
||
| assert!(NEGATIVE_ONE <= NEGATIVE_ONE); | ||
| assert!(NEGATIVE_ONE < ONE); | ||
| assert!(ONE > NEGATIVE_ONE); | ||
| assert!(ONE >= ONE); | ||
|
|
||
| const MAX: ppcf128 = ppcf128::MAX; | ||
| const ALMOST_MAX: ppcf128 = { | ||
| let (large, small) = MAX.to_components(); | ||
| ppcf128::from_components(large, small.next_down()) | ||
| }; | ||
|
|
||
| assert!(ALMOST_MAX <= ALMOST_MAX); | ||
| assert!(ALMOST_MAX < MAX); | ||
| assert!(MAX > ALMOST_MAX); | ||
| assert!(MAX >= MAX); | ||
|
|
||
| assert!(ONE < ppcf128::INFINITY); | ||
| assert!(ONE > ppcf128::NEG_INFINITY); | ||
|
|
||
| assert_ne!(ONE, ppcf128::NAN); | ||
| assert_ne!(ppcf128::NAN, ppcf128::NAN); | ||
|
|
||
| assert_eq!(ppcf128::default().to_components(), (0.0, 0.0)); | ||
| } | ||
|
|
||
| macro_rules! assert_eq_normalized { | ||
| (($large:expr, $small:expr) ==> ($expected_large:expr, $expected_small:expr)) => {{ | ||
| let (large, small) = ppcf128::from_components($large, $small).to_components(); | ||
| let (expected_large, expected_small): (f64, f64) = ($expected_large, $expected_small); | ||
|
|
||
| if expected_large.is_nan() { | ||
| assert!(large.is_nan(), "expected NaN, got {large}"); | ||
| } else { | ||
| assert_eq!(large, expected_large); | ||
| } | ||
|
|
||
| if expected_small.is_nan() { | ||
| assert!(small.is_nan(), "expected NaN, got {small}"); | ||
| } else { | ||
| assert_eq!(small, expected_small); | ||
| } | ||
| }}; | ||
| } | ||
|
|
||
| #[test] | ||
| fn normalize() { | ||
| // Already normalized. | ||
| assert_eq_normalized!((1.0, 0.0) ==> (1.0, 0.0)); | ||
| assert_eq_normalized!((-1.0, 0.0) ==> (-1.0, 0.0)); | ||
|
|
||
| // The value is normalized. | ||
| assert_eq_normalized!((1.0, 0.5) ==> (1.5, 0.0)); | ||
| assert_eq_normalized!((-1.0, 0.5) ==> (-0.5, 0.0)); | ||
| assert_eq_normalized!((0.0, 1.0) ==> (1.0, 0.0)); | ||
| assert_eq_normalized!((0.0, -1.0) ==> (-1.0, 0.0)); | ||
|
|
||
| let large = 2.0f64.powi(53); | ||
| assert_eq_normalized!((large, -(large - 1.0)) ==> (1.0, 0.0)); | ||
|
|
||
| // Exact cancellation. | ||
| assert_eq_normalized!((1.0, -1.0) ==> (0.0, 0.0)); | ||
| assert_eq_normalized!((-1.0, 1.0) ==> (0.0, 0.0)); | ||
|
|
||
| // A component too small to affect the high part remains in the low part. | ||
| let half_ulp = f64::EPSILON / 2.0; | ||
| assert_eq_normalized!((1.0, half_ulp) ==> (1.0, half_ulp)); | ||
| assert_eq_normalized!((-1.0, -half_ulp) ==> (-1.0, -half_ulp)); | ||
|
|
||
| assert_eq_normalized!((1.0, -half_ulp) ==> (1.0 - half_ulp, 0.0)); | ||
| assert_eq_normalized!((-1.0, half_ulp) ==> (-1.0 + half_ulp, 0.0)); | ||
|
|
||
| // Rounding the sum produces a compensating low component. | ||
| let three_quarters_ulp = 3.0 * f64::EPSILON / 4.0; | ||
| assert_eq_normalized!( | ||
| (1.0, three_quarters_ulp) ==> | ||
| (1.0 + f64::EPSILON, -f64::EPSILON / 4.0) | ||
| ); | ||
|
|
||
| // Normalization works across a large difference in exponent. | ||
| let large = 2.0f64.powi(100); | ||
| assert_eq_normalized!((1.0, large) ==> (large, 1.0)); | ||
|
|
||
| // Infinity with a zero low component is already normalized. | ||
| assert_eq_normalized!((f64::INFINITY, 0.0) ==> (f64::INFINITY, 0.0)); | ||
| assert_eq_normalized!((f64::NEG_INFINITY, 0.0) ==> (f64::NEG_INFINITY, 0.0)); | ||
|
|
||
| // The small component of an infinity is normalized to zero. | ||
| assert_eq_normalized!((f64::INFINITY, 42.0) ==> (f64::INFINITY, 0.0)); | ||
| assert_eq_normalized!((f64::NEG_INFINITY, 42.0) ==> (f64::NEG_INFINITY, 0.0)); | ||
|
|
||
| // A finite high component combined with infinity normalizes to infinity. | ||
| assert_eq_normalized!((1.0, f64::INFINITY) ==> (f64::INFINITY, 0.0)); | ||
| assert_eq_normalized!((1.0, f64::NEG_INFINITY) ==> (f64::NEG_INFINITY, 0.0)); | ||
|
|
||
| // Opposite infinities produce NaN. | ||
| assert_eq_normalized!( | ||
| (f64::INFINITY, f64::NEG_INFINITY) ==> | ||
| (f64::NAN, 0.0) | ||
| ); | ||
|
|
||
| assert_eq_normalized!((1.0, f64::NAN) ==> (f64::NAN, 0.0)); | ||
| assert_eq_normalized!((f64::NAN, 1.0) ==> (f64::NAN, 1.0)); | ||
|
|
||
| assert_matches!(ppcf128::checked_from_components(1.0, 0.0), Some(_)); | ||
| assert_matches!(ppcf128::checked_from_components(0.0, 1.0), None); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.