33 constexpr
bool is_even(T num) {
return num % 2 == 0; }
40 return (num < 0) ? -(((-num) + 50) / 100) : (num + 50) / 100;
48 constexpr
int bounded_add(
int base,
int increment,
int max_sum,
int min_sum = 0)
51 return std::min(base + increment, std::max(base, max_sum));
53 return std::max(base + increment, std::min(base, min_sum));
62 constexpr T
modulo(T num,
int mod, T min = 0)
65 T
n = (num - min) % mod;
80 constexpr
int round_damage(
int base_damage,
int bonus,
int divisor) {
81 if (base_damage==0)
return 0;
82 const int rounding = divisor / 2 - (bonus < divisor || divisor==1 ? 0 : 1);
83 return std::max<int>(1, (base_damage * bonus + rounding) / divisor);
86 template<
typename Cmp>
87 bool in_ranges(
const Cmp
c,
const std::vector<std::pair<Cmp, Cmp>>& ranges)
89 return std::any_of(ranges.begin(), ranges.end(), [
c](
const std::pair<Cmp, Cmp>& range) {
90 return range.first <= c && c <= range.second;
107 return sizeof(T) * std::numeric_limits<unsigned char>::digits;
122 return sizeof(T) * std::numeric_limits<unsigned char>::digits;
147 const auto x =
static_cast<std::make_unsigned_t<N>
>(
n);
148 constexpr decltype(x) mask{1};
150 unsigned int count{0};
151 for(
int i = std::numeric_limits<decltype(mask)>::digits - 1;
i >= 0; --
i) {
152 if(x & (mask <<
i)) {
182 return count_leading_zeros<N>(~
n);
188 auto res = std::div(a,
b);
189 return 2 * res.rem >
b ? (res.quot + 1) : res.quot;
199 return static_cast<unsigned>((n1 * n2) >> 8);
209 return (n1 << 8) / n2;
constexpr int div100rounded(int num)
Guarantees portable results for division by 100; round half up, to the nearest integer.
constexpr int fixed_point_to_int(int32_t n)
If positive, just bit shift.
constexpr bool is_even(T num)
constexpr int bounded_add(int base, int increment, int max_sum, int min_sum=0)
Returns base + increment, but will not increase base above max_sum, nor decrease it below min_sum.
constexpr unsigned int count_leading_ones(N n)
Returns the quantity of leading 1 bits in n — i.e., the quantity of bits in n, minus the 1-based bit ...
constexpr int round_damage(int base_damage, int bonus, int divisor)
round (base_damage * bonus / divisor) to the closest integer, but up or down towards base_damage
constexpr bool is_odd(T num)
bool in_ranges(const Cmp c, const std::vector< std::pair< Cmp, Cmp >> &ranges)
constexpr unsigned fixed_point_multiply(int32_t n1, int32_t n2)
int rounded_division(int a, int b)
constexpr T modulo(T num, int mod, T min=0)
constexpr int32_t fixed_point_divide(int n1, int n2)
constexpr std::enable_if_t< std::is_integral_v< N >, unsigned int > count_leading_zeros(N n)
Returns the quantity of leading 0 bits in n — i.e., the quantity of bits in n, minus the 1-based bit ...
constexpr std::size_t bit_width()
Returns the size, in bits, of an instance of type T, providing a convenient and self-documenting name...
static map_location::direction n