vppinfra: bitops cleanup
[vpp.git] / src / 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 static_always_inline uword
42 clear_lowest_set_bit (uword x)
43 {
44 #ifdef __BMI__
45   return uword_bits > 32 ? _blsr_u64 (x) : _blsr_u32 (x);
46 #else
47   return x & (x - 1);
48 #endif
49 }
50
51 static_always_inline uword
52 get_lowest_set_bit (uword x)
53 {
54 #ifdef __BMI__
55   return uword_bits > 32 ? _blsi_u64 (x) : _blsi_u32 (x);
56 #else
57   return x & -x;
58 #endif
59 }
60
61 static_always_inline u8
62 get_lowest_set_bit_index (uword x)
63 {
64   return uword_bits > 32 ? __builtin_ctzll (x) : __builtin_ctz (x);
65 }
66
67 /* Population count from Hacker's Delight. */
68 always_inline uword
69 count_set_bits (uword x)
70 {
71 #ifdef __POPCNT__
72   return uword_bits > 32 ? __builtin_popcountll (x) : __builtin_popcount (x);
73 #else
74 #if uword_bits == 64
75   const uword c1 = 0x5555555555555555;
76   const uword c2 = 0x3333333333333333;
77   const uword c3 = 0x0f0f0f0f0f0f0f0f;
78 #else
79   const uword c1 = 0x55555555;
80   const uword c2 = 0x33333333;
81   const uword c3 = 0x0f0f0f0f;
82 #endif
83
84   /* Sum 1 bit at a time. */
85   x = x - ((x >> (uword) 1) & c1);
86
87   /* 2 bits at a time. */
88   x = (x & c2) + ((x >> (uword) 2) & c2);
89
90   /* 4 bits at a time. */
91   x = (x + (x >> (uword) 4)) & c3;
92
93   /* 8, 16, 32 bits at a time. */
94   x = x + (x >> (uword) 8);
95   x = x + (x >> (uword) 16);
96 #if uword_bits == 64
97   x = x + (x >> (uword) 32);
98 #endif
99
100   return x & (2 * BITS (uword) - 1);
101 #endif
102 }
103
104 #if uword_bits == 64
105 #define count_leading_zeros(x) __builtin_clzll (x)
106 #else
107 #define count_leading_zeros(x) __builtin_clzll (x)
108 #endif
109
110 #define count_trailing_zeros(x) get_lowest_set_bit_index (x)
111 #define log2_first_set(x)       get_lowest_set_bit_index (x)
112
113 /* Based on "Hacker's Delight" code from GLS. */
114 typedef struct
115 {
116   uword masks[1 + log2_uword_bits];
117 } compress_main_t;
118
119 always_inline void
120 compress_init (compress_main_t * cm, uword mask)
121 {
122   uword q, m, zm, n, i;
123
124   m = ~mask;
125   zm = mask;
126
127   cm->masks[0] = mask;
128   for (i = 0; i < log2_uword_bits; i++)
129     {
130       q = m;
131       m ^= m << 1;
132       m ^= m << 2;
133       m ^= m << 4;
134       m ^= m << 8;
135       m ^= m << 16;
136 #if uword_bits > 32
137       m ^= m << (uword) 32;
138 #endif
139       cm->masks[1 + i] = n = (m << 1) & zm;
140       m = q & ~m;
141       q = zm & n;
142       zm = zm ^ q ^ (q >> (1 << i));
143     }
144 }
145
146 always_inline uword
147 compress_bits (compress_main_t * cm, uword x)
148 {
149   uword q, r;
150
151   r = x & cm->masks[0];
152   q = r & cm->masks[1];
153   r ^= q ^ (q >> 1);
154   q = r & cm->masks[2];
155   r ^= q ^ (q >> 2);
156   q = r & cm->masks[3];
157   r ^= q ^ (q >> 4);
158   q = r & cm->masks[4];
159   r ^= q ^ (q >> 8);
160   q = r & cm->masks[5];
161   r ^= q ^ (q >> 16);
162 #if uword_bits > 32
163   q = r & cm->masks[6];
164   r ^= q ^ (q >> (uword) 32);
165 #endif
166
167   return r;
168 }
169
170 always_inline uword
171 rotate_left (uword x, uword i)
172 {
173   return (x << i) | (x >> (BITS (i) - i));
174 }
175
176 always_inline uword
177 rotate_right (uword x, uword i)
178 {
179   return (x >> i) | (x << (BITS (i) - i));
180 }
181
182 /* Returns snoob from Hacker's Delight.  Next highest number
183    with same number of set bits. */
184 always_inline uword
185 next_with_same_number_of_set_bits (uword x)
186 {
187   uword smallest, ripple, ones;
188   smallest = x & -x;
189   ripple = x + smallest;
190   ones = x ^ ripple;
191   ones = ones >> (2 + log2_first_set (x));
192   return ripple | ones;
193 }
194
195 #define foreach_set_bit_index(i, v)                                           \
196   for (uword _tmp = (v) + 0 * (uword) (i = get_lowest_set_bit_index (v));     \
197        _tmp;                                                                  \
198        i = get_lowest_set_bit_index (_tmp = clear_lowest_set_bit (_tmp)))
199
200 #else
201 #warning "already included"
202 #endif /* included_clib_bitops_h */
203
204 /*
205  * fd.io coding-style-patch-verification: ON
206  *
207  * Local Variables:
208  * eval: (c-set-style "gnu")
209  * End:
210  */