Compare commits

..

2 Commits

Author SHA1 Message Date
96abbe3a6e Ignore Cargo.lock 2025-01-14 14:22:40 -05:00
53da38aa68 Fix bug and make testing more reliable 2025-01-14 14:22:07 -05:00
3 changed files with 5 additions and 7 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/target
Cargo.lock

View File

@ -5,5 +5,3 @@ edition = "2021"
[dependencies]
[dev-dependencies]
rayon = "1.10.0"

View File

@ -1,6 +1,7 @@
/// It is not actually necissary or even recommended to use this code.
/// The compiler should always optimize division with constant denominators for you.
/// This code is just to help understand how the compilers optimization works.
#[inline(always)]
pub fn div10(num: u32) -> (u32, u32) {
// Calculate num / 10
// Note: 0.10 = 3435973837 / (1<<35)
@ -9,7 +10,7 @@ pub fn div10(num: u32) -> (u32, u32) {
// Calculate quot * 10
// Note: 10x = 2x + 4(2x)
let quot2 = quot << 1;
let quot8 = quot2 << 3;
let quot8 = quot2 << 2;
let quot10 = quot2 + quot8;
// Calculate the remainder
@ -22,14 +23,12 @@ pub fn div10(num: u32) -> (u32, u32) {
#[cfg(test)]
mod tests {
use super::*;
use rayon::prelude::*;
#[test]
/// This will take a REALLY long time
fn brute_check() {
(0..=u32::MAX).into_par_iter().for_each(|i| {
for i in 0..=u32::MAX {
let ans = (i / 10, i % 10);
assert_eq!(ans, div10(i));
});
}
}
}