Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
integer_log2.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/math/integer_log2.hpp
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 
11 #ifndef TLX_MATH_INTEGER_LOG2_HEADER
12 #define TLX_MATH_INTEGER_LOG2_HEADER
13 
14 namespace tlx {
15 
16 //! \addtogroup tlx_math
17 //! \{
18 
19 /******************************************************************************/
20 // integer_log2_floor()
21 
22 //! calculate the log2 floor of an integer type (by repeated bit shifts)
23 template <typename IntegerType>
24 static inline unsigned integer_log2_floor_template(IntegerType i) {
25  unsigned p = 0;
26  while (i >= 65536) i >>= 16, p += 16;
27  while (i >= 256) i >>= 8, p += 8;
28  while (i >>= 1) ++p;
29  return p;
30 }
31 
32 /******************************************************************************/
33 // integer_log2_floor()
34 
35 //! calculate the log2 floor of an integer type (by repeated bit shifts)
36 unsigned integer_log2_floor(int i);
37 
38 //! calculate the log2 floor of an integer type (by repeated bit shifts)
39 unsigned integer_log2_floor(unsigned int i);
40 
41 //! calculate the log2 floor of an integer type (by repeated bit shifts)
42 unsigned integer_log2_floor(long i);
43 
44 //! calculate the log2 floor of an integer type (by repeated bit shifts)
45 unsigned integer_log2_floor(unsigned long i);
46 
47 //! calculate the log2 floor of an integer type (by repeated bit shifts)
48 unsigned integer_log2_floor(long long i);
49 
50 //! calculate the log2 floor of an integer type (by repeated bit shifts)
51 unsigned integer_log2_floor(unsigned long long i);
52 
53 /******************************************************************************/
54 // integer_log2_ceil()
55 
56 //! calculate the log2 ceiling of an integer type (by repeated bit shifts)
57 unsigned integer_log2_ceil(int i);
58 
59 //! calculate the log2 ceiling of an integer type (by repeated bit shifts)
60 unsigned integer_log2_ceil(unsigned int i);
61 
62 //! calculate the log2 ceiling of an integer type (by repeated bit shifts)
63 unsigned integer_log2_ceil(long i);
64 
65 //! calculate the log2 ceiling of an integer type (by repeated bit shifts)
66 unsigned integer_log2_ceil(unsigned long i);
67 
68 //! calculate the log2 ceiling of an integer type (by repeated bit shifts)
69 unsigned integer_log2_ceil(long long i);
70 
71 //! calculate the log2 ceiling of an integer type (by repeated bit shifts)
72 unsigned integer_log2_ceil(unsigned long long i);
73 
74 //! \}
75 
76 } // namespace tlx
77 
78 #endif // !TLX_MATH_INTEGER_LOG2_HEADER
79 
80 /******************************************************************************/
static unsigned integer_log2_floor_template(IntegerType i)
calculate the log2 floor of an integer type (by repeated bit shifts)
unsigned integer_log2_floor(int i)
calculate the log2 floor of an integer type (by repeated bit shifts)
unsigned integer_log2_ceil(int i)
calculate the log2 ceiling of an integer type (by repeated bit shifts)