Description
fastrange is a small C and C++ library that maps a machine word to an integer in a range faster than the modulo operator. Mapping a value to a range, such as turning a hash into a bucket number, is a common step in hashing and probabilistic algorithms. The standard approach is a modulo reduction, which relies on an integer division and can be slow even on recent processors. fastrange replaces that division with a multiplication followed by a shift, and the README says it keeps the results almost equally likely across the range. The library is a single portable header file that you can drop into a C or C++ project. All of its functions are static inline, so there is no library to link against, and including the header from several translation units is fine. It needs a C99 or later compiler or a C++ compiler, and the README says it is known to work with GCC and Clang. The README also reports that the idea sped up some operations in TensorFlow by 10 to 20 percent.…
Highlights
- Single header file with no linking needed
- Faster alternative to modulo reduction
- Works with C99 and C++ compilers
- CMake use through FetchContent, submodule or install
- Static inline functions
Ratings & Reviews
No ratings yet
Be the first to rate Fastrange.
People also visit
Competitive Programming RS
Algorithm Snippets for Competitive Programming in Rust
Fastrange FAQ
What is fastrange?
A header-only C and C++ library that maps a value into a range faster than the modulo operator. It is useful in hashing and probabilistic algorithms.
How do I add it to a project?
Copy the single header file into your project, or use it with CMake through FetchContent, a git submodule or an installed copy. The README covers each option.
Which compiler do I need?
A C99 or better compiler, or a C++ compiler. The README says the header is known to work with GCC and Clang.
Does it need to be linked?
No. All the functions are static inline, so there is no library to link against.
Is it open source?
The source is public on GitHub at lemire/fastrange. The README cites Daniel Lemire's related paper on fast random integer generation.
What are the best Fastrange alternatives?
Popular alternatives to Fastrange include Frozen, Hash Wasm and Re2. See all Fastrange alternatives




