Compare commits

..

No commits in common. "96abbe3a6efbc2ea5cb9a34ff8896556aab9a44d" and "8ff474860849b4fbb462b77346283260232ee097" have entirely different histories.

3 changed files with 7 additions and 5 deletions

1
.gitignore vendored
View File

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

View File

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

View File

@ -1,7 +1,6 @@
/// 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)
@ -10,7 +9,7 @@ pub fn div10(num: u32) -> (u32, u32) {
// Calculate quot * 10
// Note: 10x = 2x + 4(2x)
let quot2 = quot << 1;
let quot8 = quot2 << 2;
let quot8 = quot2 << 3;
let quot10 = quot2 + quot8;
// Calculate the remainder
@ -23,12 +22,14 @@ 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() {
for i in 0..=u32::MAX {
(0..=u32::MAX).into_par_iter().for_each(|i| {
let ans = (i / 10, i % 10);
assert_eq!(ans, div10(i));
}
});
}
}