7 Different bit algorithms:
8 LSB get least significant bit number
9 MSB get most significant bit number
10 bswap swap byte order of word
11 RBO reverse bit order of word
14 2007.10.08 Maxim.Khiszinsky Created
17 #include <cds/details/defs.h>
18 #include <cds/compiler/bitop.h>
26 template <int> struct BitOps;
29 template <> struct BitOps<4> {
30 typedef uint32_t TUInt;
32 static int MSB( TUInt x ) { return bitop::platform::msb32( x ); }
33 static int LSB( TUInt x ) { return bitop::platform::lsb32( x ); }
34 static int MSBnz( TUInt x ) { return bitop::platform::msb32nz( x ); }
35 static int LSBnz( TUInt x ) { return bitop::platform::lsb32nz( x ); }
36 static int SBC( TUInt x ) { return bitop::platform::sbc32( x ) ; }
37 static int ZBC( TUInt x ) { return bitop::platform::zbc32( x ) ; }
39 static TUInt RBO( TUInt x ) { return bitop::platform::rbo32( x ); }
40 static bool complement( TUInt& x, int nBit ) { return bitop::platform::complement32( &x, nBit ); }
42 static TUInt RandXorShift(TUInt x) { return bitop::platform::RandXorShift32(x); }
46 template <> struct BitOps<8> {
47 typedef atomic64u_unaligned TUInt;
49 static int MSB( TUInt x ) { return bitop::platform::msb64( x ); }
50 static int LSB( TUInt x ) { return bitop::platform::lsb64( x ); }
51 static int MSBnz( TUInt x ) { return bitop::platform::msb64nz( x ); }
52 static int LSBnz( TUInt x ) { return bitop::platform::lsb64nz( x ); }
53 static int SBC( TUInt x ) { return bitop::platform::sbc64( x ) ; }
54 static int ZBC( TUInt x ) { return bitop::platform::zbc64( x ) ; }
56 static TUInt RBO( TUInt x ) { return bitop::platform::rbo64( x ); }
57 static bool complement( TUInt& x, int nBit ) { return bitop::platform::complement64( &x, nBit ); }
59 static TUInt RandXorShift(TUInt x) { return bitop::platform::RandXorShift64(x); }
61 } // namespace details
65 /// Get least significant bit (LSB) number (1..32/64), 0 if nArg == 0
67 static inline int LSB( T nArg )
69 return details::BitOps< sizeof(T) >::LSB( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
72 /// Get least significant bit (LSB) number (0..31/63)
74 Precondition: nArg != 0
77 static inline int LSBnz( T nArg )
80 return details::BitOps< sizeof(T) >::LSBnz( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
83 /// Get most significant bit (MSB) number (1..32/64), 0 if nArg == 0
85 static inline int MSB( T nArg )
87 return details::BitOps< sizeof(T) >::MSB( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
90 /// Get most significant bit (MSB) number (0..31/63)
92 Precondition: nArg != 0
95 static inline int MSBnz( T nArg )
98 return details::BitOps< sizeof(T) >::MSBnz( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
101 /// Get non-zero bit count of a word
102 template <typename T>
103 static inline int SBC( T nArg )
105 return details::BitOps< sizeof(T) >::SBC( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
108 /// Get zero bit count of a word
109 template <typename T>
110 static inline int ZBC( T nArg )
112 return details::BitOps< sizeof(T) >::ZBC( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
115 /// Reverse bit order of \p nArg
116 template <typename T>
117 static inline T RBO( T nArg )
119 return (T) details::BitOps< sizeof(T) >::RBO( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
122 /// Complement bit \p nBit in \p nArg
123 template <typename T>
124 static inline bool complement( T& nArg, int nBit )
126 return details::BitOps< sizeof(T) >::complement( reinterpret_cast< typename details::BitOps<sizeof(T)>::TUInt& >( nArg ), nBit );
129 /// Simple random number generator
130 template <typename T>
131 static inline T RandXorShift( T x)
133 return (T) details::BitOps< sizeof(T) >::RandXorShift(x);
139 #endif // #ifndef CDSLIB_BITOP_H