Initial commit of vpp code.
[vpp.git] / vppinfra / vppinfra / bitops.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16   Copyright (c) 2005 Eliot Dresselhaus
17
18   Permission is hereby granted, free of charge, to any person obtaining
19   a copy of this software and associated documentation files (the
20   "Software"), to deal in the Software without restriction, including
21   without limitation the rights to use, copy, modify, merge, publish,
22   distribute, sublicense, and/or sell copies of the Software, and to
23   permit persons to whom the Software is furnished to do so, subject to
24   the following conditions:
25
26   The above copyright notice and this permission notice shall be
27   included in all copies or substantial portions of the Software.
28
29   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37
38 #ifndef included_clib_bitops_h
39 #define included_clib_bitops_h
40
41 #include <vppinfra/clib.h>
42
43 /* Population count from Hacker's Delight. */
44 always_inline uword count_set_bits (uword x)
45 {
46 #if uword_bits == 64
47   const uword c1 = 0x5555555555555555;
48   const uword c2 = 0x3333333333333333;
49   const uword c3 = 0x0f0f0f0f0f0f0f0f;
50 #else
51   const uword c1 = 0x55555555;
52   const uword c2 = 0x33333333;
53   const uword c3 = 0x0f0f0f0f;
54 #endif
55
56   /* Sum 1 bit at a time. */
57   x = x - ((x >> (uword) 1) & c1);
58
59   /* 2 bits at a time. */
60   x = (x & c2) + ((x >> (uword) 2) & c2);
61
62   /* 4 bits at a time. */
63   x = (x + (x >> (uword) 4)) & c3;
64
65   /* 8, 16, 32 bits at a time. */
66   x = x + (x >> (uword) 8);
67   x = x + (x >> (uword) 16);
68 #if uword_bits == 64
69   x = x + (x >> (uword) 32);
70 #endif
71
72   return x & (2*BITS (uword) - 1);
73 }
74
75 /* Based on "Hacker's Delight" code from GLS. */
76 typedef struct {
77   uword masks[1 + log2_uword_bits];
78 } compress_main_t;
79
80 always_inline void
81 compress_init (compress_main_t * cm, uword mask)
82 {
83   uword q, m, zm, n, i;
84
85   m = ~mask;
86   zm = mask;
87
88   cm->masks[0] = mask;
89   for (i = 0; i < log2_uword_bits; i++)
90     {
91       q = m;
92       m ^= m << 1;
93       m ^= m << 2;
94       m ^= m << 4;
95       m ^= m << 8;
96       m ^= m << 16;
97 #if uword_bits > 32
98       m ^= m << (uword) 32;
99 #endif
100       cm->masks[1 + i] = n = (m << 1) & zm;
101       m = q & ~m;
102       q = zm & n;
103       zm = zm ^ q ^ (q >> (1 << i));
104     }
105 }
106
107 always_inline uword
108 compress_bits (compress_main_t * cm, uword x)
109 {
110   uword q, r;
111
112   r = x & cm->masks[0];
113   q = r & cm->masks[1]; r ^= q ^ (q >> 1);
114   q = r & cm->masks[2]; r ^= q ^ (q >> 2);
115   q = r & cm->masks[3]; r ^= q ^ (q >> 4);
116   q = r & cm->masks[4]; r ^= q ^ (q >> 8);
117   q = r & cm->masks[5]; r ^= q ^ (q >> 16);
118 #if uword_bits > 32
119   q = r & cm->masks[6]; r ^= q ^ (q >> (uword) 32);
120 #endif
121
122   return r;
123 }
124
125 always_inline uword
126 rotate_left (uword x, uword i)
127 { return (x << i) | (x >> (BITS (i) - i)); }
128
129 always_inline uword
130 rotate_right (uword x, uword i)
131 { return (x >> i) | (x << (BITS (i) - i)); }
132
133 /* Returns snoob from Hacker's Delight.  Next highest number
134    with same number of set bits. */
135 always_inline uword
136 next_with_same_number_of_set_bits (uword x)
137 {
138   uword smallest, ripple, ones;
139   smallest = x & -x;
140   ripple = x + smallest;
141   ones = x ^ ripple;
142   ones = ones >> (2 + log2_first_set (x));
143   return ripple | ones;
144 }
145
146 #define foreach_set_bit(var,mask,body)                                  \
147 do {                                                                    \
148   uword _foreach_set_bit_m_##var = (mask);                              \
149   uword _foreach_set_bit_f_##var;                                       \
150   while (_foreach_set_bit_m_##var != 0)                                 \
151     {                                                                   \
152       _foreach_set_bit_f_##var = first_set (_foreach_set_bit_m_##var);  \
153       _foreach_set_bit_m_##var ^= _foreach_set_bit_f_##var;             \
154       (var) = min_log2 (_foreach_set_bit_f_##var);                      \
155       do { body; } while (0);                                           \
156     }                                                                   \
157 } while (0)
158
159 #endif /* included_clib_bitops_h */