Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
round_to_power_of_two.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/math/round_to_power_of_two.cpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2007-2017 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
12 
13 #include <cstddef>
14 
15 namespace tlx {
16 
17 template <typename Integral>
18 static inline Integral round_up_to_power_of_two_template(Integral n) {
19  --n;
20  for (size_t k = 1; k != 8 * sizeof(n); k <<= 1) {
21  n |= n >> k;
22  }
23  ++n;
24  return n;
25 }
26 
27 /******************************************************************************/
28 
31 }
32 
33 unsigned int round_up_to_power_of_two(unsigned int i) {
35 }
36 
39 }
40 
41 unsigned long round_up_to_power_of_two(unsigned long i) {
43 }
44 
45 long long round_up_to_power_of_two(long long i) {
47 }
48 
49 unsigned long long round_up_to_power_of_two(unsigned long long i) {
51 }
52 
53 /******************************************************************************/
54 
56  return round_up_to_power_of_two(i + 1) >> 1;
57 }
58 
59 unsigned int round_down_to_power_of_two(unsigned int i) {
60  return round_up_to_power_of_two(i + 1) >> 1;
61 }
62 
64  return round_up_to_power_of_two(i + 1) >> 1;
65 }
66 
67 unsigned long round_down_to_power_of_two(unsigned long i) {
68  return round_up_to_power_of_two(i + 1) >> 1;
69 }
70 
71 long long round_down_to_power_of_two(long long i) {
72  return round_up_to_power_of_two(i + 1) >> 1;
73 }
74 
75 unsigned long long round_down_to_power_of_two(unsigned long long i) {
76  return round_up_to_power_of_two(i + 1) >> 1;
77 }
78 
79 } // namespace tlx
80 
81 /******************************************************************************/
int round_down_to_power_of_two(int i)
does what it says: round down to next power of two
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)