Utilities#
2026-03-13
4 min read time
RAFT contains numerous utility functions and primitives that are easily usable. This page provides C++ API references for the publicly-exposed utility functions.
Integer Arithmetic#
#include <raft/util/integer_utils.hpp>
namespace raft
-
template<typename From, typename To>
constexpr bool is_narrowing_v = is_narrowing<From, To>::value# Check whether the numeric conversion is narrowing
-
template<typename S>
inline constexpr S round_up_safe(
)# Finds the smallest integer not less than
number_to_roundand moduloSis zero. This function assumes thatnumber_to_roundis non-negative andmodulusis positive.
-
template<typename S>
inline S round_down_safe(
)# Finds the largest integer not greater than
number_to_roundand moduloSis zero. This function assumes thatnumber_to_roundis non-negative andmodulusis positive.
-
template<typename S, typename T>
inline constexpr S div_rounding_up_unsafe(
) noexcept# Divides the left-hand-side by the right-hand-side, rounding up to an integral multiple of the right-hand-side, e.g. (9,5) -> 2 , (10,5) -> 2, (11,5) -> 3.
Note
sensitive to overflow, i.e. if dividend > std::numeric_limits<S>::max() - divisor, the result will be incorrect
- Parameters:
dividend – the number to divide
divisor – the number by which to divide
- Returns:
The least integer multiple of divisor which is greater than or equal to the non-integral division dividend/divisor.
-
template<typename I>
inline constexpr auto div_rounding_up_safe(
) noexcept -> std::enable_if_t<std::is_integral<I>::value, I># Divides the left-hand-side by the right-hand-side, rounding up to an integral multiple of the right-hand-side, e.g. (9,5) -> 2 , (10,5) -> 2, (11,5) -> 3.
Note
will not overflow, and may or may not be slower than the intuitive approach of using (dividend + divisor - 1) / divisor
- Parameters:
dividend – the number to divide
divisor – the number of by which to divide
- Returns:
The least integer multiple of divisor which is greater than or equal to the non-integral division dividend/divisor.
-
template<typename I>
inline constexpr auto is_a_power_of_two( - I val
-
template<typename T>
inline constexpr auto bound_by_power_of_two( - T x
Given an integer
x, return suchythatx <= yandis_a_power_of_two(y). If suchydoes not exist inT, return zero.
-
template<typename T>
inline constexpr auto absolute_value( - T val
Return the absolute value of a number.
This calls
std::abs()which performs equivalent:(value < 0) ? -value : value.This was created to prevent compile errors calling
std::abs()with unsigned integers. An example compile error appears as follows:error: more than one instance of overloaded function "std::abs" matches the argument list: function "abs(int)" function "std::abs(long)" function "std::abs(long long)" function "std::abs(double)" function "std::abs(float)" function "std::abs(long double)" argument types are: (uint64_t)Not all cases could be if-ed out using std::is_signed<T>::value and satisfy the compiler.
- Parameters:
val – Numeric value can be either integer or float type.
- Returns:
Absolute value if value type is signed.
- inline _RAFT_HOST_DEVICE void wmul_64bit (uint64_t &res_hi, uint64_t &res_lo, uint64_t a, uint64_t b)
Wide multiplication of two unsigned 64-bit integers