CARMA C++
bitTwiddling.h
Go to the documentation of this file.
1 #ifndef CARMA_UTIL_BITTWIDDLING_H
2 #define CARMA_UTIL_BITTWIDDLING_H
3 
6 
7 #include "carma/util/compileTimeCheck.h"
8 
9 
10 namespace carma {
11 namespace util {
12 
13 
14 template < int highestBit, int lowestBit >
15 unsigned char extractBitfield( unsigned char x );
16 
17 template < int highestBit, int lowestBit >
18 unsigned short extractBitfield( unsigned short x );
19 
20 template < int highestBit, int lowestBit >
21 unsigned int extractBitfield( unsigned int x );
22 
23 template < int highestBit, int lowestBit >
24 unsigned long extractBitfield( unsigned long x );
25 
26 template < int highestBit, int lowestBit >
27 unsigned long long extractBitfield( unsigned long long x );
28 
29 
30 // For now these ones are declared but the implementation should give a
31 // compile error on use because I am unclear on whether or not they should
32 // do sign extension
33 
34 template < int highestBit, int lowestBit >
35 char extractBitfield( char x );
36 
37 template < int highestBit, int lowestBit >
38 short extractBitfield( short x );
39 
40 template < int highestBit, int lowestBit >
41 int extractBitfield( int x );
42 
43 template < int highestBit, int lowestBit >
44 long extractBitfield( long x );
45 
46 template < int highestBit, int lowestBit >
47 long long extractBitfield( long long x );
48 
49 
50 // These are just wacky and I'm disallowing them at compile time
51 
52 template < int highestBit, int lowestBit >
53 float extractBitfield( float x );
54 
55 template < int highestBit, int lowestBit >
56 double extractBitfield( double x );
57 
58 template < int highestBit, int lowestBit >
59 long double extractBitfield( long double x );
60 
61 
62 namespace detail {
63 
64 
65 template < int highestBit, int lowestBit, typename T >
66 T extractUnsignedBitfieldImpl( T x );
67 
68 
69 } // namespace carma::util::detail
70 
71 
72 } // namespace carma::util
73 } // namespace carma
74 
75 
76 template < int highestBit,
77  int lowestBit,
78  typename T >
79 inline T
80 carma::util::detail::extractUnsignedBitfieldImpl( const T x )
81 {
82  compileTimeCheck< (highestBit >= 0) >();
83  compileTimeCheck< (highestBit < (8 * sizeof( T ))) >();
84 
85  compileTimeCheck< (lowestBit >= 0) >();
86  compileTimeCheck< (lowestBit < (8 * sizeof( T ))) >();
87 
88  compileTimeCheck< (highestBit >= lowestBit) >();
89 
90  const T hiMask = ~((~T(1)) << highestBit);
91  const T result = ((x & hiMask) >> lowestBit);
92 
93  return result;
94 }
95 
96 
97 template < int highestBit, int lowestBit >
98 inline unsigned char
99 carma::util::extractBitfield( const unsigned char x )
100 {
101  return detail::extractUnsignedBitfieldImpl< highestBit, lowestBit >( x );
102 }
103 
104 
105 template < int highestBit, int lowestBit >
106 inline unsigned short
107 carma::util::extractBitfield( const unsigned short x )
108 {
109  return detail::extractUnsignedBitfieldImpl< highestBit, lowestBit >( x );
110 }
111 
112 
113 template < int highestBit, int lowestBit >
114 inline unsigned int
115 carma::util::extractBitfield( const unsigned int x )
116 {
117  return detail::extractUnsignedBitfieldImpl< highestBit, lowestBit >( x );
118 }
119 
120 
121 template < int highestBit, int lowestBit >
122 inline unsigned long
123 carma::util::extractBitfield( const unsigned long x )
124 {
125  return detail::extractUnsignedBitfieldImpl< highestBit, lowestBit >( x );
126 }
127 
128 
129 template < int highestBit, int lowestBit >
130 inline unsigned long long
131 carma::util::extractBitfield( const unsigned long long x )
132 {
133  return detail::extractUnsignedBitfieldImpl< highestBit, lowestBit >( x );
134 }
135 
136 
137 template < int highestBit, int lowestBit >
138 inline char
139 carma::util::extractBitfield( const char x )
140 {
141  compileTimeCheck< false >();
142 
143  return 0;
144 }
145 
146 
147 template < int highestBit, int lowestBit >
148 inline short
149 carma::util::extractBitfield( const short x )
150 {
151  compileTimeCheck< false >();
152 
153  return 0;
154 }
155 
156 
157 template < int highestBit, int lowestBit >
158 inline int
159 carma::util::extractBitfield( const int x )
160 {
161  compileTimeCheck< false >();
162 
163  return 0;
164 }
165 
166 
167 template < int highestBit, int lowestBit >
168 inline long
169 carma::util::extractBitfield( const long x )
170 {
171  compileTimeCheck< false >();
172 
173  return 0;
174 }
175 
176 
177 template < int highestBit, int lowestBit >
178 inline long long
179 carma::util::extractBitfield( const long long x )
180 {
181  compileTimeCheck< false >();
182 
183  return 0;
184 }
185 
186 
187 template < int highestBit, int lowestBit >
188 inline float
189 carma::util::extractBitfield( const float x )
190 {
191  compileTimeCheck< false >();
192 
193  return 0;
194 }
195 
196 
197 template < int highestBit, int lowestBit >
198 inline double
199 carma::util::extractBitfield( const double x )
200 {
201  compileTimeCheck< false >();
202 
203  return 0;
204 }
205 
206 
207 template < int highestBit, int lowestBit >
208 inline long double
209 carma::util::extractBitfield( const long double x )
210 {
211  compileTimeCheck< false >();
212 
213  return 0;
214 }
215 
216 
217 #endif