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_BITOP_H
32 #define CDSLIB_BITOP_H
35 Different bit algorithms:
36 LSB get least significant bit number
37 MSB get most significant bit number
38 bswap swap byte order of word
39 RBO reverse bit order of word
42 2007.10.08 Maxim.Khiszinsky Created
45 #include <cds/details/defs.h>
46 #include <cds/compiler/bitop.h>
54 template <int> struct BitOps;
57 template <> struct BitOps<4> {
58 typedef uint32_t TUInt;
60 static int MSB( TUInt x ) { return bitop::platform::msb32( x ); }
61 static int LSB( TUInt x ) { return bitop::platform::lsb32( x ); }
62 static int MSBnz( TUInt x ) { return bitop::platform::msb32nz( x ); }
63 static int LSBnz( TUInt x ) { return bitop::platform::lsb32nz( x ); }
64 static int SBC( TUInt x ) { return bitop::platform::sbc32( x ) ; }
65 static int ZBC( TUInt x ) { return bitop::platform::zbc32( x ) ; }
67 static TUInt RBO( TUInt x ) { return bitop::platform::rbo32( x ); }
68 static bool complement( TUInt& x, int nBit ) { return bitop::platform::complement32( &x, nBit ); }
70 static TUInt RandXorShift(TUInt x) { return bitop::platform::RandXorShift32(x); }
74 template <> struct BitOps<8> {
75 typedef uint64_t TUInt;
77 static int MSB( TUInt x ) { return bitop::platform::msb64( x ); }
78 static int LSB( TUInt x ) { return bitop::platform::lsb64( x ); }
79 static int MSBnz( TUInt x ) { return bitop::platform::msb64nz( x ); }
80 static int LSBnz( TUInt x ) { return bitop::platform::lsb64nz( x ); }
81 static int SBC( TUInt x ) { return bitop::platform::sbc64( x ) ; }
82 static int ZBC( TUInt x ) { return bitop::platform::zbc64( x ) ; }
84 static TUInt RBO( TUInt x ) { return bitop::platform::rbo64( x ); }
85 static bool complement( TUInt& x, int nBit ) { return bitop::platform::complement64( &x, nBit ); }
87 static TUInt RandXorShift(TUInt x) { return bitop::platform::RandXorShift64(x); }
89 } // namespace details
93 /// Get least significant bit (LSB) number (1..32/64), 0 if nArg == 0
95 static inline int LSB( T nArg )
97 return details::BitOps< sizeof(T) >::LSB( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
100 /// Get least significant bit (LSB) number (0..31/63)
102 Precondition: nArg != 0
104 template <typename T>
105 static inline int LSBnz( T nArg )
108 return details::BitOps< sizeof(T) >::LSBnz( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
111 /// Get most significant bit (MSB) number (1..32/64), 0 if nArg == 0
112 template <typename T>
113 static inline int MSB( T nArg )
115 return details::BitOps< sizeof(T) >::MSB( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
118 /// Get most significant bit (MSB) number (0..31/63)
120 Precondition: nArg != 0
122 template <typename T>
123 static inline int MSBnz( T nArg )
126 return details::BitOps< sizeof(T) >::MSBnz( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
129 /// Get non-zero bit count of a word
130 template <typename T>
131 static inline int SBC( T nArg )
133 return details::BitOps< sizeof(T) >::SBC( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
136 /// Get zero bit count of a word
137 template <typename T>
138 static inline int ZBC( T nArg )
140 return details::BitOps< sizeof(T) >::ZBC( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
143 /// Reverse bit order of \p nArg
144 template <typename T>
145 static inline T RBO( T nArg )
147 return (T) details::BitOps< sizeof(T) >::RBO( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
150 /// Complement bit \p nBit in \p nArg
151 template <typename T>
152 static inline bool complement( T& nArg, int nBit )
154 return details::BitOps< sizeof(T) >::complement( reinterpret_cast< typename details::BitOps<sizeof(T)>::TUInt& >( nArg ), nBit );
157 /// Simple random number generator
158 template <typename T>
159 static inline T RandXorShift( T x)
161 return (T) details::BitOps< sizeof(T) >::RandXorShift(x);
167 #endif // #ifndef CDSLIB_BITOP_H