tlx
round_to_power_of_two.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/math/round_to_power_of_two.hpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2007-2018 Timo Bingmann <tb@panthema.net>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
11 #ifndef TLX_MATH_ROUND_TO_POWER_OF_TWO_HEADER
12 #define TLX_MATH_ROUND_TO_POWER_OF_TWO_HEADER
13 
14 #include <cstddef>
15 
16 namespace tlx {
17 
18 //! \addtogroup tlx_math
19 //! \{
20 
21 /******************************************************************************/
22 // round_up_to_power_of_two()
23 
24 template <typename Integral>
25 static inline Integral round_up_to_power_of_two_template(Integral n) {
26  --n;
27  for (size_t k = 1; k != 8 * sizeof(n); k <<= 1) {
28  n |= n >> k;
29  }
30  ++n;
31  return n;
32 }
33 
34 /******************************************************************************/
35 // round_up_to_power_of_two()
36 
37 //! does what it says: round up to next power of two
38 static inline int round_up_to_power_of_two(int i) {
40 }
41 
42 //! does what it says: round up to next power of two
43 static inline unsigned int round_up_to_power_of_two(unsigned int i) {
45 }
46 
47 //! does what it says: round up to next power of two
48 static inline long round_up_to_power_of_two(long i) {
50 }
51 
52 //! does what it says: round up to next power of two
53 static inline unsigned long round_up_to_power_of_two(unsigned long i) {
55 }
56 
57 //! does what it says: round up to next power of two
58 static inline long long round_up_to_power_of_two(long long i) {
60 }
61 
62 //! does what it says: round up to next power of two
63 static inline
64 unsigned long long round_up_to_power_of_two(unsigned long long i) {
66 }
67 
68 /******************************************************************************/
69 // round_down_to_power_of_two()
70 
71 //! does what it says: round down to next power of two
72 static inline int round_down_to_power_of_two(int i) {
73  return round_up_to_power_of_two(i + 1) >> 1;
74 }
75 
76 //! does what it says: round down to next power of two
77 static inline unsigned int round_down_to_power_of_two(unsigned int i) {
78  return round_up_to_power_of_two(i + 1) >> 1;
79 }
80 
81 //! does what it says: round down to next power of two
82 static inline long round_down_to_power_of_two(long i) {
83  return round_up_to_power_of_two(i + 1) >> 1;
84 }
85 
86 //! does what it says: round down to next power of two
87 static inline unsigned long round_down_to_power_of_two(unsigned long i) {
88  return round_up_to_power_of_two(i + 1) >> 1;
89 }
90 
91 //! does what it says: round down to next power of two
92 static inline long long round_down_to_power_of_two(long long i) {
93  return round_up_to_power_of_two(i + 1) >> 1;
94 }
95 
96 //! does what it says: round down to next power of two
97 static inline
98 unsigned long long round_down_to_power_of_two(unsigned long long i) {
99  return round_up_to_power_of_two(i + 1) >> 1;
100 }
101 
102 //! \}
103 
104 } // namespace tlx
105 
106 #endif // !TLX_MATH_ROUND_TO_POWER_OF_TWO_HEADER
107 
108 /******************************************************************************/
static int round_down_to_power_of_two(int i)
does what it says: round down to next power of two
static int round_up_to_power_of_two(int i)
does what it says: round up to next power of two
static Integral round_up_to_power_of_two_template(Integral n)