-
Notifications
You must be signed in to change notification settings - Fork 1
Utilities
Teodor Đurić edited this page Dec 18, 2022
·
6 revisions
- EULER_NUMBER: f64 = 2.71828
pub fn modulo(num: u128, divisor: u128) -> usize
- This function requires
divisorto be a power of 2. - Takes a number
num, does some bit twiddling, and returns the remainder as ausize. - The computation exploits the fact that a remainder operation can be performed using only a bit-wise AND if the divisor is a power of 2. This is due to the fact that integer division (essentially repeated subtraction), will only affect bits bigger than the divisor i.e. bits at and to the left of the set bit in
divisorand what remains is the bits to the right of it. - The calculation of the remainder
rcan be described using the formula:$$\Huge{r = n \wedge(d-1) }$$ Animation of what the formula is actually doing:
pub fn closest_pow(n: u64) -> u64
- Takes a number
n, does some bit twiddling, and returnsc, the nearest power of 2 ton, such thatc >= n. - The return value,
c, is calculated by finding the most significant bit that is set to1, then a1is shifted into the spot left of that bit. In the case wherenis a power of 2, to avoid branching, the1is shifted one time less, resulting in the same number asn. - The calculation can be described using the formulae:
pub fn load_seeds(filename: &str) -> Option<Vec<u32>>
- Takes a filename and returns a vector of the seeds inside the file wrapped in an
Option
pub fn write_seeds(seeds: &Vec<u32>, filename: &str) -> ()
- Takes an immutable reference to a vector of seeds and writes them to a file determined by
filename.