Go to the documentation of this file.
21 static inline T
abs(
const T a)
23 return (a < (T)0) ? -a : a;
35 static inline T
Align(
const T x, uint n)
37 assert((n & (n - 1)) == 0 && n != 0);
39 return (T)((x + n) & ~((T)n));
55 static_assert(
sizeof(
size_t) ==
sizeof(
void *));
56 return reinterpret_cast<T *
>(
Align((
size_t)x, n));
77 static inline T
Clamp(
const T a,
const T min,
const T max)
80 if (a <= min)
return min;
81 if (a >= max)
return max;
100 static inline T
SoftClamp(
const T a,
const T min,
const T max)
103 using U = std::make_unsigned_t<T>;
104 return min - (U(min) - max) / 2;
106 if (a <= min)
return min;
107 if (a >= max)
return max;
127 static inline int Clamp(
const int a,
const int min,
const int max)
129 return Clamp<int>(a, min, max);
148 static inline uint
ClampU(
const uint a,
const uint min,
const uint max)
150 return Clamp<uint>(a, min, max);
169 return static_cast<int32
>(Clamp<int64>(a, INT32_MIN, INT32_MAX));
185 return static_cast<uint16
>(std::min(a,
static_cast<uint64
>(UINT16_MAX)));
195 template <
typename T>
196 static inline T
Delta(
const T a,
const T b)
198 return (a < b) ? b - a : a - b;
213 template <
typename T>
214 static inline bool IsInsideBS(
const T x,
const size_t base,
const size_t size)
216 return (
size_t)(x - base) < size;
229 template <
typename T>
230 static constexpr
inline bool IsInsideMM(
const T x,
const size_t min,
const size_t max) noexcept
232 return (
size_t)(x - min) < (max - min);
240 template <
typename T>
241 static inline void Swap(T &a, T &b)
267 return i * 101 >> 16;
282 return (a + b - 1) / b;
291 static inline uint
Ceil(uint a, uint b)
306 return (a +
static_cast<int>(b) / 2) /
static_cast<int>(b);
309 return (a - (
static_cast<int>(b) - 1) / 2) /
static_cast<int>(b);
321 const int _b =
static_cast<int>(b);
323 return (a + _b - 1) / _b;
326 return (a - _b + 1) / _b;
static constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
static int32 ClampToI32(const int64 a)
Reduce a signed 64-bit int to a signed 32-bit one.
static uint ToPercent8(uint i)
Converts a "fract" value 0..255 to "percent" value 0..100.
static uint ClampU(const uint a, const uint min, const uint max)
Clamp an unsigned integer between an interval.
int LeastCommonMultiple(int a, int b)
Compute least common multiple (lcm) of arguments a and b, the smallest integer value that is a multip...
static T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
static bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
static int DivAwayFromZero(int a, uint b)
Computes (a / b) rounded away from zero.
static int RoundDivSU(int a, uint b)
Computes round(a / b) for signed a and unsigned b.
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
uint32 IntSqrt(uint32 num)
Compute the integer square root.
static T abs(const T a)
Returns the absolute value of (scalar) variable.
static uint Ceil(uint a, uint b)
Computes ceil(a / b) * b for non-negative a and b.
static uint ToPercent16(uint i)
Converts a "fract" value 0..65535 to "percent" value 0..100.
static uint16 ClampToU16(const uint64 a)
Reduce an unsigned 64-bit int to an unsigned 16-bit one.
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
int GreatestCommonDivisor(int a, int b)
Compute greatest common divisor (gcd) of a and b.
static T SoftClamp(const T a, const T min, const T max)
Clamp a value between an interval.
static void Swap(T &a, T &b)
Type safe swap operation.
static T * AlignPtr(T *x, uint n)
Return the smallest multiple of n equal or greater than x Applies to pointers only.
static T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
int DivideApprox(int a, int b)
Deterministic approximate division.