sandbox
Loading...
Searching...
No Matches
fast_mod.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_UTILITY_FAST_MOD_HPP_
3#define LIBSBX_UTILITY_FAST_MOD_HPP_
4
5#include <concepts>
6#include <cmath>
7
8namespace sbx::utility {
9
21template<std::unsigned_integral Value, std::unsigned_integral Mod>
22constexpr auto fast_mod(const Value value, const Mod modulus) noexcept -> Value {
23 // return value - (value / modulus) * modulus;
24 return value < modulus ? value : value % static_cast<Value>(modulus);
25}
26
27template<std::floating_point Type>
28constexpr auto fast_mod(const Type value, const Type modulus) noexcept -> Type {
29 // return value - (value / modulus) * modulus;
30 return value < modulus ? value : std::fmod(value, modulus);
31}
32
33} // namespace sbx::utility
34
35#endif // LIBSBX_UTILITY_FAST_MOD_HPP_