@@ -17,10 +17,42 @@ namespace __esimd {
17
17
// / Constant in number of bytes.
18
18
enum { BYTE = 1 , WORD = 2 , DWORD = 4 , QWORD = 8 , OWORD = 16 , GRF = 32 };
19
19
20
- // / Compute the next power of 2 at compile time.
21
- static ESIMD_INLINE constexpr unsigned int getNextPowerOf2 (unsigned int n,
22
- unsigned int k = 1 ) {
23
- return (k >= n) ? k : getNextPowerOf2 (n, k * 2 );
20
+ // / Compute next power of 2 of a constexpr with guaranteed compile-time
21
+ // / evaluation.
22
+ template <unsigned int N, unsigned int K, bool K_gt_eq_N> struct NextPowerOf2 ;
23
+
24
+ template <unsigned int N, unsigned int K> struct NextPowerOf2 <N, K, true > {
25
+ static constexpr unsigned int get () { return K; }
26
+ };
27
+
28
+ template <unsigned int N, unsigned int K> struct NextPowerOf2 <N, K, false > {
29
+ static constexpr unsigned int get () {
30
+ return NextPowerOf2<N, K * 2 , K * 2 >= N>::get ();
31
+ }
32
+ };
33
+
34
+ template <unsigned int N> unsigned int getNextPowerOf2 () {
35
+ return NextPowerOf2<N, 1 , (1 >= N)>::get ();
36
+ }
37
+
38
+ template <> unsigned int getNextPowerOf2<0 >() { return 0 ; }
39
+
40
+ // / Compute binary logarithm of a constexpr with guaranteed compile-time
41
+ // / evaluation.
42
+ template <unsigned int N, bool N_gt_1> struct Log2 ;
43
+
44
+ template <unsigned int N> struct Log2 <N, false > {
45
+ static constexpr unsigned int get () { return 0 ; }
46
+ };
47
+
48
+ template <unsigned int N> struct Log2 <N, true > {
49
+ static constexpr unsigned int get () {
50
+ return 1 + Log2<(N >> 1 ), ((N >> 1 ) > 1 )>::get ();
51
+ }
52
+ };
53
+
54
+ template <unsigned int N> constexpr unsigned int log2 () {
55
+ return Log2<N, (N > 1 )>::get ();
24
56
}
25
57
26
58
// / Check if a given 32 bit positive integer is a power of 2 at compile time.
@@ -33,10 +65,6 @@ static ESIMD_INLINE constexpr bool isPowerOf2(unsigned int n,
33
65
return (n & (n - 1 )) == 0 && n <= limit;
34
66
}
35
67
36
- static ESIMD_INLINE constexpr unsigned log2 (unsigned n) {
37
- return (n > 1 ) ? 1 + log2 (n >> 1 ) : 0 ;
38
- }
39
-
40
68
} // namespace __esimd
41
69
42
70
__SYCL_INLINE_NAMESPACE (cl) {
0 commit comments