main
hpp 28 lines 785 Bytes
Raw
1 #pragma once
2
3 #include <climits>
4 #include <iostream>
5
6 inline constexpr uint32_t next_pow_2(uint32_t const num) {
7 if (num <= 1) return num;
8 return 1 << (CHAR_BIT * sizeof(num) - __builtin_clz(num - 1));
9 }
10
11 template <typename A, typename B>
12 static inline constexpr auto div_ceil(A a, B b) {
13 return (a + b - 1) / b;
14 }
15
16 // Round a down to the next multiple of b. The caller is responsible for making
17 // sure that b is non-zero
18 template <typename T>
19 inline constexpr T round_to_previous_multiple_of(T a, T b) {
20 return a % b == 0 ? a : (a / b) * b;
21 }
22
23 // Round a up to the next multiple of b. The caller is responsible for making
24 // sure that b is non-zero
25 template <typename T>
26 inline constexpr T round_to_next_multiple_of(T a, T b) {
27 return a % b == 0 ? a : ((a / b) + 1) * b;
28 }