banan-os/BAN/include/BAN/Math.h

53 lines
701 B
C
Raw Normal View History

2022-12-30 19:52:16 +02:00
#pragma once
#include <BAN/Traits.h>
#include <stdint.h>
2023-01-10 19:11:27 +02:00
namespace BAN::Math
2022-12-30 19:52:16 +02:00
{
template<typename T>
inline constexpr T min(T a, T b)
2022-12-30 19:52:16 +02:00
{
return a < b ? a : b;
}
template<typename T>
inline constexpr T max(T a, T b)
2022-12-30 19:52:16 +02:00
{
return a > b ? a : b;
}
template<typename T>
inline constexpr T clamp(T x, T min, T max)
2022-12-30 19:52:16 +02:00
{
return x < min ? min : x > max ? max : x;
}
template<integral T>
inline constexpr T gcd(T a, T b)
2023-01-10 19:11:27 +02:00
{
T t;
while (b)
{
t = b;
b = a % b;
a = t;
}
return a;
}
template<integral T>
inline constexpr T lcm(T a, T b)
2023-01-10 19:11:27 +02:00
{
return a / gcd(a, b) * b;
}
template<integral T>
inline constexpr T div_round_up(T a, T b)
{
return (a + b - 1) / b;
}
2022-12-30 19:52:16 +02:00
}