Changed current relation from BasicBlock to BasicBlockImpl, and Function
[jitcs.git] / src / jitcs_int_power2funcs.h
blob855b891c393ced6d7550efbf9615c6e59cacb0a4
1 #ifndef _JITCS_INT_POWER2FUNCS_H_
2 #define _JITCS_INT_POWER2FUNCS_H_
4 #include "jitcs_base.h"
6 namespace jitcs {
8 // compile time functions
9 template <size_t t>
10 struct IsPowerOf2OrZeroCT { static const bool Val = (t & (t - 1)) == 0; };
11 template <size_t t>
12 struct IsPowerOf2CT { static const bool Val = t != 0 && IsPowerOf2OrZeroCT<t>::Val; };
14 template <size_t t>
15 struct Log2CT { static const size_t Val = t > 1 ? (Log2CT<(t>>1)>::Val + 1) : 0; };
16 template <>
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);
25 // runtime functions
26 inline bool IsPowerOf2OrZero(size_t t) { return (t & (t - 1)) == 0; }
27 inline bool IsPowerOf2(size_t t) { return t != 0 && IsPowerOf2OrZero(t); }
29 template <size_t u>
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);
34 template <size_t u>
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
46 #endif
47 // _JITCS_INT_POWER2FUNCS_H_