1#pragma once
2
3#include <cstdint>
4
5#include "taichi/common/trait.h"
6
7namespace taichi {
8
9// Round up |a| to the closest multiple of |b|, works only for integers.
10template <typename T,
11 typename U,
12 typename = std::enable_if_t<std::is_convertible_v<U, T>>>
13T iroundup(T a, U b) {
14 static_assert(std::is_integral_v<T>, "LHS must be integral type");
15 static_assert(std::is_integral_v<U>, "RHS must be integral type");
16 return ((a + b - 1) / b) * b;
17}
18
19template <typename T>
20uint32_t log2int(T value) {
21 static_assert(std::is_integral_v<T>, "Must be integral type");
22
23 uint32_t ret = 0;
24 value >>= 1;
25 while (value) {
26 value >>= 1;
27 ret += 1;
28 }
29 return ret;
30}
31
32} // namespace taichi
33