2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CDSLIB_DETAILS_BITOP_GENERIC_H
32 #define CDSLIB_DETAILS_BITOP_GENERIC_H
34 #include <cstdlib> // rand()
37 namespace bitop { namespace platform {
38 // Return true if x = 2 ** k, k >= 0
39 #ifndef cds_bitop_isPow2_32_DEFINED
40 static inline bool isPow2_32( uint32_t x )
42 return (x & ( x - 1 )) == 0 && x;
46 #ifndef cds_bitop_isPow2_64_DEFINED
47 static inline bool isPow2_64( uint64_t x )
49 return (x & ( x - 1 )) == 0 && x;
53 //***************************************************
54 // Most significant bit number (1..N)
57 #ifndef cds_bitop_msb32_DEFINED
58 // Return number (1..32) of most significant bit
60 // Source: Linux kernel
61 static inline int msb32( uint32_t x )
67 if (!(x & 0xffff0000u)) {
71 if (!(x & 0xff000000u)) {
75 if (!(x & 0xf0000000u)) {
79 if (!(x & 0xc0000000u)) {
83 if (!(x & 0x80000000u)) {
91 #ifndef cds_bitop_msb32nz_DEFINED
92 static inline int msb32nz( uint32_t x )
94 return msb32( x ) - 1;
98 #ifndef cds_bitop_msb64_DEFINED
99 static inline int msb64( uint64_t x )
101 uint32_t h = (uint32_t) (x >> 32);
103 return msb32( h ) + 32;
104 return msb32( (uint32_t) x );
108 #ifndef cds_bitop_msb64nz_DEFINED
109 static inline int msb64nz( uint64_t x )
111 return msb64( x ) - 1;
115 //***************************************************
116 // Least significant bit number (1..N)
117 // Return 0 if x == 0
119 #ifndef cds_bitop_lsb32_DEFINED
120 // Return number (1..32) of least significant bit
121 // Return 0 if x == 0
122 // Source: Linux kernel
123 static inline int lsb32( uint32_t x )
153 #ifndef cds_bitop_lsb32nz_DEFINED
154 static inline int lsb32nz( uint32_t x )
156 return lsb32( x ) - 1;
160 #ifndef cds_bitop_lsb64_DEFINED
161 static inline int lsb64( uint64_t x )
165 if ( x & 0xffffffffu )
166 return lsb32( (uint32_t) x );
167 return lsb32( (uint32_t) (x >> 32) ) + 32;
171 #ifndef cds_bitop_lsb64nz_DEFINED
172 static inline int lsb64nz( uint64_t x )
174 return lsb64( x ) - 1;
178 //******************************************************
180 //******************************************************
181 #ifndef cds_bitop_rbo32_DEFINED
182 static inline uint32_t rbo32( uint32_t x )
184 // swap odd and even bits
185 x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1);
186 // swap consecutive pairs
187 x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2);
189 x = ((x >> 4) & 0x0F0F0F0F) | ((x & 0x0F0F0F0F) << 4);
191 x = ((x >> 8) & 0x00FF00FF) | ((x & 0x00FF00FF) << 8);
192 // swap 2-byte long pairs
193 return ( x >> 16 ) | ( x << 16 );
197 #ifndef cds_bitop_rbo64_DEFINED
198 static inline uint64_t rbo64( uint64_t x )
200 // Low 32bit Hight 32bit
201 return ( static_cast<uint64_t>(rbo32( (uint32_t) x )) << 32 ) | ( static_cast<uint64_t>( rbo32( (uint32_t) (x >> 32) )));
205 //******************************************************
206 // Set bit count. Return count of non-zero bits in word
207 //******************************************************
208 #ifndef cds_bitop_sbc32_DEFINED
209 static inline int sbc32( uint32_t x )
211 # ifdef cds_beans_zbc32_DEFINED
212 return 32 - zbc32( x );
214 // Algorithm from Sean Eron Anderson's great collection
215 x = x - ((x >> 1) & 0x55555555);
216 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
217 return (((x + (x >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
222 #ifndef cds_bitop_sbc64_DEFINED
223 static inline int sbc64( uint64_t x )
225 # ifdef cds_beans_zbc64_DEFINED
226 return 64 - zbc64( x );
228 return sbc32( (uint32_t) (x >> 32) ) + sbc32( (uint32_t) x );
233 //******************************************************
234 // Zero bit count. Return count of zero bits in word
235 //******************************************************
236 #ifndef cds_bitop_zbc32_DEFINED
237 static inline int zbc32( uint32_t x )
239 return 32 - sbc32( x );
243 #ifndef cds_bitop_zbc64_DEFINED
244 static inline int zbc64( uint64_t x )
246 return 64 - sbc64( x );
251 #ifndef cds_bitop_complement32_DEFINED
252 static inline bool complement32( uint32_t * pArg, unsigned int nBit )
255 uint32_t nVal = *pArg & (1 << nBit);
261 #ifndef cds_bitop_complement64_DEFINED
262 static inline bool complement64( uint64_t * pArg, unsigned int nBit )
265 uint64_t nVal = *pArg & (uint64_t(1) << nBit);
266 *pArg ^= uint64_t(1) << nBit;
272 Simple random number generator
274 [2003] George Marsaglia "Xorshift RNGs"
276 static inline uint32_t RandXorShift32(uint32_t x)
278 //static uint32_t xRandom = 2463534242UL ; //rand() | 0x0100 ; // must be nonzero
279 //uint32_t x = xRandom;
281 x = (( std::rand() + 1) << 16 ) + std::rand() + 1;
287 static inline uint64_t RandXorShift64(uint64_t x)
289 //static uint64_t xRandom = 88172645463325252LL;
290 //uint64_t x = xRandom;
292 x = 88172645463325252LL;
297 }} // namespace bitop::platform
300 #endif // CDSLIB_DETAILS_BITOP_GENERIC_H