28#ifndef MYSQL_MATH_MATH_H_
29#define MYSQL_MATH_MATH_H_
50template <typename T, std::enable_if_t<std::is_integral<T>::value &&
51 std::is_unsigned<T>::value,
53constexpr T
add_bounded(
const T x,
const T y,
const T maximum) {
54 if (y >= maximum || maximum - y < x)
return maximum;
78 typename T,
typename T2,
79 std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value &&
80 std::is_arithmetic<T2>::value,
84 if (y > 1 &&
static_cast<T
>(maximum / y) < x)
return maximum;
85 return static_cast<T
>(x * y);
89template <typename T, std::enable_if_t<std::is_integral<T>::value &&
90 std::is_unsigned<T>::value,
93 return (x + y - 1) / y;
constexpr T add_bounded(const T x, const T y, const T maximum)
Return x+y, limited to the given maximum.
Definition: math.h:53
constexpr T multiply_bounded(const T x, const T2 y, const T maximum)
Return x*y, limited to the given maximum.
Definition: math.h:82
constexpr T ceil_div(const T x, const T y)
Return ceil(x / y), where x and y are unsigned integer types.
Definition: math.h:92