round_mult
A tiny library to round a number up or down to a multiplier.
Usage
The library has two functions:
round_mult::[up] (see note below)round_mult::[down]
They both take a value and a multiplier and round the value down or up to the multiplier respectively.
[!NOTE] As of Rust 1.73.0, integer primitives have a built-in
next_multiple_ofmethod.
Multiplier
There are two kind of multipliers:
- [
NonZeroPow2] When the multiplier is a power of two, it can be calculated faster. Prefer it when possible. std::num::NonZeroU_for any multiplier value.
Example
E.g.
use NonZeroUsize;
use NonZeroPow2;
assert_eq!;
// These two are semantically equivalent:
assert_eq!;
// but NonZeroPow2 (the first parameter) is faster.
// However, it can't be used when the multiplier isn't a power of two.
// In that case use a NonZeroU_ type:
assert_eq!;
assert_eq!;
Example: SIMD
The main motivation for this library is SIMD processing. Specifically when the length of data isn't a multiple of the SIMD lanes count, which means you will have a remainder of data to process without SIMD.
use NonZeroPow2;