cheshirekow  v0.1.0
powersOfTwo.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
37 #ifndef MPBLOCKS_CUDA_POWERS_OF_TWO_H
38 #define MPBLOCKS_CUDA_POWERS_OF_TWO_H
39 
40 
41 #include <climits>
42 
43 
44 namespace mpblocks {
45 namespace cuda {
46 
47 
48 
49 
51 
70 template <typename T>
71 inline bool isPow2(T x)
72 {
73  return ((x&(x-1))==0);
74 }
75 
76 
77 
78 
80 
101 template <typename T>
102 inline T nextPow2( T x )
103 {
104  if (x == 0)
105  return 0;
106  --x;
107  for(unsigned int i=1; i <= sizeof(T)*CHAR_BIT; i++)
108  x |= x >> i;
109  return ++x;
110 }
111 
112 
113 
114 
116 
136 template <typename T>
137 inline T prevPow2( T x )
138 {
139  if(x == 0)
140  return 0;
141 
142  unsigned int i=0;
143  for(;x > 0; x >>= 1)
144  i++;
145  return 1 << (i-1);
146 }
147 
148 
149 
150 
152 
181 template <typename T>
182 inline T intPow( T x, T p)
183 {
184  if (p == 0) return 1;
185  if (p == 1) return x;
186  return x * intPow<T>(x, p-1);
187 }
188 
189 
190 
191 
193 
196 template <typename T>
197 inline T twoPow( T x )
198 {
199  return 0x01 << x;
200 }
201 
202 
203 
204 
206 
213 template <typename T>
214 inline T log2( T x )
215 {
216  if(!x)
217  {
218  return 0;
219  }
220 
221  else
222  {
223  T log2x = 0;
224  while( (x & 1) == 0 )
225  {
226  x >>= 1;
227  log2x++;
228  }
229  return log2x;
230  }
231 }
232 
233 
234 
235 
237 
240 template <typename T>
241 inline T times2( const T& x )
242 {
243  return x << 1;
244 }
245 
246 
247 
248 
250 
253 template <typename T>
254 inline T divideBy2( const T& x )
255 {
256  return x >> 1;
257 }
258 
259 
260 
261 
263 
269 template <typename T>
270 inline T intDivideRoundUp(T x, T y)
271 {
272  return (x+y-1)/y;
273 }
274 
275 
276 
277 
279 
282 template <typename T>
283 inline T isOdd(T x)
284 {
285  return ((x & 0x01) == 0x01);
286 }
287 
288 
289 
291 
294 template <typename T>
295 inline T isEven(T x)
296 {
297  return ((x & 0x01) == 0x00);
298 }
299 
300 
301 
302 
304 
307 template <unsigned int I, typename T>
308 inline T getBit(T x)
309 {
310  return x & (0x01 << I);
311 }
312 
313 
314 
317 
320 template <typename T>
321 inline T dividePow2(T x, T y)
322 {
323  return twoPow(log2(x) - log2(y));
324 }
325 
326 
327 } // namespace cuda
328 } // namespace mpblocks
329 
330 
331 #endif //#ifndef POWERS_OF_TWO_H
T log2(T x)
if x is a power of two then it returns the log of x with base 2
Definition: powersOfTwo.h:214
T getBit(T x)
returns the value of the specified bit
Definition: powersOfTwo.h:308
T intPow(T x, T p)
returns x to the power of p
Definition: powersOfTwo.h:182
T times2(const T &x)
returns x*2 (using bit shift)
Definition: powersOfTwo.h:241
T twoPow(T x)
returns 2 to the power of x (2^x)
Definition: powersOfTwo.h:197
T intDivideRoundUp(T x, T y)
integer divide with round up
Definition: powersOfTwo.h:270
T isOdd(T x)
returns true if the number is odd
Definition: powersOfTwo.h:283
bool isPow2(T x)
returns true if the parameter is an exact power of two
Definition: powersOfTwo.h:71
T dividePow2(T x, T y)
returns x/y if x and y are both powers of two, otherwise the result is undefined
Definition: powersOfTwo.h:321
T prevPow2(T x)
returns the largest power of two that is not greater than x
Definition: powersOfTwo.h:137
T isEven(T x)
returns true if the number is even
Definition: powersOfTwo.h:295
T divideBy2(const T &x)
returns x/2 (using bit shift), if x is odd then the result is floor(x/2)
Definition: powersOfTwo.h:254
T nextPow2(T x)
returns the smallest power of two that is not less than x
Definition: powersOfTwo.h:102