Fixed cppcheck style warnings
[libcds.git] / cds / details / bitop_generic.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-2016
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_DETAILS_BITOP_GENERIC_H
32 #define CDSLIB_DETAILS_BITOP_GENERIC_H
33
34 #include <cstdlib>     // rand()
35
36 namespace cds {
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 )
41         {
42             return (x & ( x - 1 )) == 0    && x;
43         }
44 #endif
45
46 #ifndef cds_bitop_isPow2_64_DEFINED
47         static inline bool isPow2_64( uint64_t x )
48         {
49             return (x & ( x - 1 )) == 0 && x;
50         }
51 #endif
52
53         //***************************************************
54         // Most significant bit number (1..N)
55         // Return 0 if x == 0
56         //
57 #ifndef cds_bitop_msb32_DEFINED
58         // Return number (1..32) of most significant bit
59         // Return 0 if x == 0
60         // Source: Linux kernel
61         static inline int msb32( uint32_t x )
62         {
63             int r = 32;
64
65             if (!x)
66                 return 0;
67             if (!(x & 0xffff0000u)) {
68                 x <<= 16;
69                 r -= 16;
70             }
71             if (!(x & 0xff000000u)) {
72                 x <<= 8;
73                 r -= 8;
74             }
75             if (!(x & 0xf0000000u)) {
76                 x <<= 4;
77                 r -= 4;
78             }
79             if (!(x & 0xc0000000u)) {
80                 x <<= 2;
81                 r -= 2;
82             }
83             if (!(x & 0x80000000u)) {
84                 //x <<= 1;
85                 r -= 1;
86             }
87             return r;
88         }
89 #endif
90
91 #ifndef cds_bitop_msb32nz_DEFINED
92         static inline int msb32nz( uint32_t x )
93         {
94             return msb32( x ) - 1;
95         }
96 #endif
97
98 #ifndef cds_bitop_msb64_DEFINED
99         static inline int msb64( uint64_t x )
100         {
101             uint32_t h = (uint32_t) (x >> 32);
102             if ( h )
103                 return msb32( h ) + 32;
104             return msb32( (uint32_t) x );
105         }
106 #endif
107
108 #ifndef cds_bitop_msb64nz_DEFINED
109         static inline int msb64nz( uint64_t x )
110         {
111             return msb64( x ) - 1;
112         }
113 #endif
114
115         //***************************************************
116         // Least significant bit number (1..N)
117         // Return 0 if x == 0
118         //
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 )
124         {
125             int r = 1;
126
127             if (!x)
128                 return 0;
129             if (!(x & 0xffff)) {
130                 x >>= 16;
131                 r += 16;
132             }
133             if (!(x & 0xff)) {
134                 x >>= 8;
135                 r += 8;
136             }
137             if (!(x & 0xf)) {
138                 x >>= 4;
139                 r += 4;
140             }
141             if (!(x & 3)) {
142                 x >>= 2;
143                 r += 2;
144             }
145             if (!(x & 1)) {
146                 //x >>= 1;
147                 r += 1;
148             }
149             return r;
150         }
151 #endif
152
153 #ifndef cds_bitop_lsb32nz_DEFINED
154         static inline int lsb32nz( uint32_t x )
155         {
156             return lsb32( x ) - 1;
157         }
158 #endif
159
160 #ifndef cds_bitop_lsb64_DEFINED
161         static inline int lsb64( uint64_t x )
162         {
163             if ( !x )
164                 return 0;
165             if ( x & 0xffffffffu )
166                 return lsb32( (uint32_t) x );
167             return lsb32( (uint32_t) (x >> 32) ) + 32;
168         }
169 #endif
170
171 #ifndef cds_bitop_lsb64nz_DEFINED
172         static inline int lsb64nz( uint64_t x )
173         {
174             return lsb64( x ) - 1;
175         }
176 #endif
177
178         //******************************************************
179         // Reverse bit order
180         //******************************************************
181 #ifndef cds_bitop_rbo32_DEFINED
182         static inline uint32_t rbo32( uint32_t x )
183         {
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);
188             // swap nibbles ...
189             x = ((x >> 4) & 0x0F0F0F0F) | ((x & 0x0F0F0F0F) << 4);
190             // swap bytes
191             x = ((x >> 8) & 0x00FF00FF) | ((x & 0x00FF00FF) << 8);
192             // swap 2-byte long pairs
193             return ( x >> 16 ) | ( x << 16 );
194         }
195 #endif
196
197 #ifndef cds_bitop_rbo64_DEFINED
198         static inline uint64_t rbo64( uint64_t x )
199         {
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) )));
202         }
203 #endif
204
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 )
210         {
211 #        ifdef cds_beans_zbc32_DEFINED
212             return 32 - zbc32( x );
213 #        else
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;
218 #        endif
219         }
220 #endif
221
222 #ifndef cds_bitop_sbc64_DEFINED
223         static inline int sbc64( uint64_t x )
224         {
225 #        ifdef cds_beans_zbc64_DEFINED
226             return 64 - zbc64( x );
227 #        else
228             return sbc32( (uint32_t) (x >> 32) ) + sbc32( (uint32_t) x );
229 #        endif
230         }
231 #endif
232
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 )
238         {
239             return 32 - sbc32( x );
240         }
241 #endif
242
243 #ifndef cds_bitop_zbc64_DEFINED
244         static inline int zbc64( uint64_t x )
245         {
246             return 64 - sbc64( x );
247         }
248 #endif
249
250         // Bit complement
251 #ifndef cds_bitop_complement32_DEFINED
252         static inline bool complement32( uint32_t * pArg, unsigned int nBit )
253         {
254             assert( pArg );
255             uint32_t nVal = *pArg & (1 << nBit);
256             *pArg ^= 1 << nBit;
257             return nVal != 0;
258         }
259 #endif
260
261 #ifndef cds_bitop_complement64_DEFINED
262         static inline bool complement64( uint64_t * pArg, unsigned int nBit )
263         {
264             assert( pArg );
265             uint64_t nVal = *pArg & (uint64_t(1) << nBit);
266             *pArg ^= uint64_t(1) << nBit;
267             return nVal != 0;
268         }
269 #endif
270
271         /*
272             Simple random number generator
273             Source:
274                 [2003] George Marsaglia "Xorshift RNGs"
275         */
276         static inline uint32_t RandXorShift32(uint32_t x)
277         {
278             //static uint32_t xRandom = 2463534242UL    ;    //rand() | 0x0100    ;    // must be nonzero
279             //uint32_t x = xRandom;
280             if ( !x )
281                 x = (( std::rand() + 1) << 16 ) + std::rand() + 1;
282             x ^= x << 13;
283             x ^= x >> 15;
284             return x ^= x << 5;
285         }
286
287         static inline uint64_t RandXorShift64(uint64_t x)
288         {
289             //static uint64_t xRandom = 88172645463325252LL;
290             //uint64_t x = xRandom;
291             if ( !x )
292                 x = 88172645463325252LL;
293             x ^= x << 13;
294             x ^= x >> 7;
295             return x ^= x << 17;
296         }
297     }}    // namespace bitop::platform
298 } // namespace cds
299
300 #endif    // CDSLIB_DETAILS_BITOP_GENERIC_H