-
Notifications
You must be signed in to change notification settings - Fork 50
Improve quality of f32/f64 generation. #103
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
+118
−46
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
16c465a
Test f32/f64 generation.
reinerp 606d2c0
Improve quality of f32/f64 generation.
reinerp 25a58a1
Apply clippy suggestions
reinerp 0c328ca
Add some more commentary.
reinerp 5b7946f
cargo fmt
reinerp 9ae0608
Remove use of f32::exp2().
reinerp d571112
Add #[inline]
reinerp 7c3b83b
Switch from u64(..) to gen_u64().
reinerp d994a10
Merge branch 'master' into master
taiki-e 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,59 +77,72 @@ fn u128() { | |
|
|
||
| #[test] | ||
| fn f32() { | ||
| for _ in 0..1000 { | ||
| let result = fastrand::f32(); | ||
| assert!((0.0..1.0).contains(&result)); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn f64() { | ||
| for _ in 0..1000 { | ||
| let result = fastrand::f64(); | ||
| assert!((0.0..1.0).contains(&result)); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn digit() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I accidentally removed several unrelated tests when resolving conflicts. Filed #110 to re-add them. |
||
| for base in 1..36 { | ||
| let result = fastrand::digit(base); | ||
| assert!(result.is_ascii_digit() || result.is_ascii_lowercase()); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn global_rng_choice() { | ||
| let items = [1, 4, 9, 5, 2, 3, 6, 7, 8, 0]; | ||
|
|
||
| for item in &items { | ||
| while fastrand::choice(&items).unwrap() != item {} | ||
| let mut r = fastrand::Rng::with_seed(0); | ||
| let tiny = (-24.0f32).exp2(); | ||
| let mut count_tiny_nonzero = 0; | ||
| let mut count_top_half = 0; | ||
| for _ in 0..100_000_000 { | ||
| let x = r.f32(); | ||
| assert!((0.0..1.0).contains(&x)); | ||
| if x > 0.0 && x < tiny { | ||
| count_tiny_nonzero += 1; | ||
| } else if x > 0.5 { | ||
| count_top_half += 1; | ||
| } | ||
| } | ||
| assert!(count_top_half >= 49_000_000); | ||
| assert!(count_tiny_nonzero > 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn global_rng_alphabetic() { | ||
| for _ in 0..1000 { | ||
| let result = fastrand::alphabetic(); | ||
| assert!(result.is_ascii_alphabetic()) | ||
| fn f32_inclusive() { | ||
| let mut r = fastrand::Rng::with_seed(0); | ||
| let tiny = (-24.0f32).exp2(); | ||
| let mut count_top_half = 0; | ||
| let mut count_tiny_nonzero = 0; | ||
| let mut count_one = 0; | ||
| for _ in 0..100_000_000 { | ||
| let x = r.f32_inclusive(); | ||
| assert!((0.0..=1.0).contains(&x)); | ||
| if x == 1.0 { | ||
| count_one += 1; | ||
| } else if x > 0.5 { | ||
| count_top_half += 1; | ||
| } else if x > 0.0 && x < tiny { | ||
| count_tiny_nonzero += 1; | ||
| } | ||
| } | ||
| assert!(count_top_half >= 49_000_000); | ||
| assert!(count_one > 0); | ||
| assert!(count_tiny_nonzero > 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn global_rng_lowercase() { | ||
| for _ in 0..1000 { | ||
| let result = fastrand::lowercase(); | ||
| assert!(result.is_ascii_lowercase()) | ||
| fn f64() { | ||
| let mut r = fastrand::Rng::with_seed(0); | ||
| let mut count_top_half = 0; | ||
| for _ in 0..100_000_000 { | ||
| let x = r.f64(); | ||
| assert!((0.0..1.0).contains(&x)); | ||
| if x > 0.5 { | ||
| count_top_half += 1; | ||
| } | ||
| } | ||
| assert!(count_top_half >= 49_000_000); | ||
| } | ||
|
|
||
| #[test] | ||
| fn global_rng_uppercase() { | ||
| for _ in 0..1000 { | ||
| let result = fastrand::uppercase(); | ||
| assert!(result.is_ascii_uppercase()) | ||
| fn f64_inclusive() { | ||
| let mut r = fastrand::Rng::with_seed(0); | ||
| let mut count_top_half = 0; | ||
| for _ in 0..100_000_000 { | ||
| let x = r.f64_inclusive(); | ||
| assert!((0.0..=1.0).contains(&x)); | ||
| if x > 0.5 { | ||
| count_top_half += 1; | ||
| } | ||
| } | ||
| assert!(count_top_half >= 49_000_000); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
||
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.
Was the perf of this measured? Seems like adding a branch and a loop to
f32might have performance implications I'm not comfortable with releasing.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.
I didn't measure at the time since there was no existing benchmark; I only looked at assembly.
I've now added a benchmark in #112 and measured the results on AArch64 (M2 Pro) and x86-64 (Zen4). I get:
So the change is always a win if you switch to f32_inclusive(), and it's a win on x86-64 (but a loss on AArch64) if you stay with f32().
Note that the branch is almost-always taken (probability
1 - 2^-23) so the branch predictor will predict this ~perfectly.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.
(These numbers are the benchmark numbers, divided by 10_000, since the benchmark itself generates 10_000 f32s.)