1 #ifndef _JITCS_INT_POWER2FUNCS_H_
2 #define _JITCS_INT_POWER2FUNCS_H_
4 #include "jitcs_base.h"
8 // compile time functions
10 struct IsPowerOf2OrZeroCT
{ static const bool Val
= (t
& (t
- 1)) == 0; };
12 struct IsPowerOf2CT
{ static const bool Val
= t
!= 0 && IsPowerOf2OrZeroCT
<t
>::Val
; };
15 struct Log2CT
{ static const size_t Val
= t
> 1 ? (Log2CT
<(t
>>1)>::Val
+ 1) : 0; };
17 struct Log2CT
<0> { static const size_t Val
= 0; };
19 template <size_t t
, size_t u
>
20 struct RoundUpToPowerOf2CT
{
21 static_assert(IsPowerOf2CT
<u
>::Val
, "expected power of 2");
22 static const size_t Val
= (t
+ u
- 1) & (~u
+ 1);
26 inline bool IsPowerOf2OrZero(size_t t
) { return (t
& (t
- 1)) == 0; }
27 inline bool IsPowerOf2(size_t t
) { return t
!= 0 && IsPowerOf2OrZero(t
); }
30 inline size_t RoundUpToPowerOf2(size_t t
) {
31 static_assert(IsPowerOf2CT
<u
>::Val
, "expected power of 2");
32 return (t
+ u
- 1) & (~u
+ 1);
35 inline size_t DivRoundedUpByPowerOf2(size_t t
) {
36 static_assert(IsPowerOf2CT
<u
>::Val
, "expected power of 2");
37 return (t
+ u
- 1) / u
;
40 inline size_t RoundUpToPowerOf2(size_t t
, size_t u
) {
41 assert(IsPowerOf2(u
));
42 return (t
+ u
- 1) & (~u
+ 1);
45 } // end of namespace jitcs
47 // _JITCS_INT_POWER2FUNCS_H_