Updated copyright
[libcds.git] / cds / algo / bitop.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
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.
18
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.
29 */
30
31 #ifndef CDSLIB_BITOP_H
32 #define CDSLIB_BITOP_H
33
34 /*
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
40
41     Editions:
42         2007.10.08    Maxim.Khiszinsky    Created
43 */
44
45 #include <cds/details/defs.h>
46 #include <cds/compiler/bitop.h>
47
48 namespace cds {
49     /// Bit operations
50     namespace bitop {
51
52         ///@cond none
53         namespace details {
54             template <int> struct BitOps;
55
56             // 32-bit bit ops
57             template <> struct BitOps<4> {
58                 typedef uint32_t        TUInt;
59
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 ) ;  }
66
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 ); }
69
70                 static TUInt    RandXorShift(TUInt x) { return bitop::platform::RandXorShift32(x); }
71             };
72
73             // 64-bit bit ops
74             template <> struct BitOps<8> {
75                 typedef uint64_t TUInt;
76
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 ) ;    }
83
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 ); }
86
87                 static TUInt    RandXorShift(TUInt x) { return bitop::platform::RandXorShift64(x); }
88             };
89         }    // namespace details
90         //@endcond
91
92
93         /// Get least significant bit (LSB) number (1..32/64), 0 if nArg == 0
94         template <typename T>
95         static inline int LSB( T nArg )
96         {
97             return details::BitOps< sizeof(T) >::LSB( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
98         }
99
100         /// Get least significant bit (LSB) number (0..31/63)
101         /**
102             Precondition: nArg != 0
103         */
104         template <typename T>
105         static inline int LSBnz( T nArg )
106         {
107             assert( nArg != 0 );
108             return details::BitOps< sizeof(T) >::LSBnz( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
109         }
110
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 )
114         {
115             return details::BitOps< sizeof(T) >::MSB( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
116         }
117
118         /// Get most significant bit (MSB) number (0..31/63)
119         /**
120             Precondition: nArg != 0
121         */
122         template <typename T>
123         static inline int MSBnz( T nArg )
124         {
125             assert( nArg != 0 );
126             return details::BitOps< sizeof(T) >::MSBnz( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
127         }
128
129         /// Get non-zero bit count of a word
130         template <typename T>
131         static inline int SBC( T nArg )
132         {
133             return details::BitOps< sizeof(T) >::SBC( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
134         }
135
136         /// Get zero bit count of a word
137         template <typename T>
138         static inline int ZBC( T nArg )
139         {
140             return details::BitOps< sizeof(T) >::ZBC( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
141         }
142
143         /// Reverse bit order of \p nArg
144         template <typename T>
145         static inline T RBO( T nArg )
146         {
147             return (T) details::BitOps< sizeof(T) >::RBO( (typename details::BitOps<sizeof(T)>::TUInt) nArg );
148         }
149
150         /// Complement bit \p nBit in \p nArg
151         template <typename T>
152         static inline bool complement( T& nArg, int nBit )
153         {
154             return details::BitOps< sizeof(T) >::complement( reinterpret_cast< typename details::BitOps<sizeof(T)>::TUInt& >( nArg ), nBit );
155         }
156
157         /// Simple random number generator
158         template <typename T>
159         static inline T RandXorShift( T x)
160         {
161             return (T) details::BitOps< sizeof(T) >::RandXorShift(x);
162         }
163
164     } // namespace bitop
165 } //namespace cds
166
167 #endif    // #ifndef CDSLIB_BITOP_H
168