26 static inline T
max(
const T a,
const T b)
28 return (a >= b) ? a : b;
42 static inline T
min(
const T a,
const T b)
44 return (a < b) ? a : b;
56 static inline int min(
const int a,
const int b)
58 return min<int>(a, b);
70 static inline uint
minu(
const uint a,
const uint b)
72 return min<uint>(a, b);
83 static inline T
abs(
const T a)
85 return (a < (T)0) ? -a : a;
97 static inline T
Align(
const T x, uint n)
99 assert((n & (n - 1)) == 0 && n != 0);
101 return (T)((x + n) & ~((T)n));
114 template <
typename T>
117 assert_compile(
sizeof(
size_t) ==
sizeof(
void *));
118 return (T *)
Align((
size_t)x, n);
138 template <
typename T>
142 if (a <= min)
return min;
143 if (a >= max)
return max;
163 static inline int Clamp(
const int a,
const int min,
const int max)
165 return Clamp<int>(a,
min,
max);
184 static inline uint
ClampU(
const uint a,
const uint
min,
const uint
max)
186 return Clamp<uint>(a,
min,
max);
205 return (int32)Clamp<int64>(a, INT32_MIN, INT32_MAX);
221 return (uint16)min<uint64>(a, (uint64)UINT16_MAX);
231 template <
typename T>
232 static inline T
Delta(
const T a,
const T b)
234 return (a < b) ? b - a : a - b;
249 template <
typename T>
250 static inline bool IsInsideBS(
const T x,
const uint base,
const uint size)
252 return (uint)(x - base) < size;
265 template <
typename T>
268 return (uint)(x -
min) < (max - min);
276 template <
typename T>
277 static inline void Swap(T &a, T &b)
303 return i * 101 >> 16;
318 return (a + b - 1) / b;
327 static inline uint
Ceil(uint a, uint b)
342 return (a + (
int)b / 2) / (int)b;
345 return (a - ((
int)b - 1) / 2) / (
int)b;
static void Swap(T &a, T &b)
Type safe swap operation.
static uint minu(const uint a, const uint b)
Returns the minimum of two unsigned integers.
static bool IsInsideMM(const T x, const uint min, const uint max)
Checks if a value is in an interval.
int DivideApprox(int a, int b)
Deterministic approximate division.
static bool IsInsideBS(const T x, const uint base, const uint size)
Checks if a value is between a window started at some base point.
static T max(const T a, const T b)
Returns the maximum of two values.
static int RoundDivSU(int a, uint b)
Computes round(a / b) for signed a and unsigned b.
static uint ClampU(const uint a, const uint min, const uint max)
Clamp an unsigned integer between an interval.
uint32 IntSqrt(uint32 num)
Compute the integer square root.
static T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
static T * AlignPtr(T *x, uint n)
Return the smallest multiple of n equal or greater than x Applies to pointers only.
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 min(const T a, const T b)
Returns the minimum of two values.
static T Clamp(const T a, const T min, const T max)
Clamp a value between 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 T abs(const T a)
Returns the absolute value of (scalar) variable.
static uint ToPercent16(uint i)
Converts a "fract" value 0..65535 to "percent" value 0..100.
static uint Ceil(uint a, uint b)
Computes ceil(a / b) * b for non-negative a and b.
static T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
static uint16 ClampToU16(const uint64 a)
Reduce an unsigned 64-bit int to an unsigned 16-bit one.
int GreatestCommonDivisor(int a, int b)
Compute greatest common divisor (gcd) of a and b.