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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 Copyright (c) 2005 Eliot Dresselhaus
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:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
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.
38 #ifndef included_clib_bitops_h
39 #define included_clib_bitops_h
41 #define SET_BIT(i) (1 << i)
42 #define GET_BIT(n, i) (n >> i) & 1U
44 static_always_inline uword
45 clear_lowest_set_bit (uword x)
48 return uword_bits > 32 ? _blsr_u64 (x) : _blsr_u32 (x);
54 static_always_inline uword
55 get_lowest_set_bit (uword x)
58 return uword_bits > 32 ? _blsi_u64 (x) : _blsi_u32 (x);
64 static_always_inline u8
65 get_lowest_set_bit_index (uword x)
67 return uword_bits > 32 ? __builtin_ctzll (x) : __builtin_ctz (x);
70 /* Population count from Hacker's Delight. */
72 count_set_bits (uword x)
75 return uword_bits > 32 ? __builtin_popcountll (x) : __builtin_popcount (x);
78 const uword c1 = 0x5555555555555555;
79 const uword c2 = 0x3333333333333333;
80 const uword c3 = 0x0f0f0f0f0f0f0f0f;
82 const uword c1 = 0x55555555;
83 const uword c2 = 0x33333333;
84 const uword c3 = 0x0f0f0f0f;
87 /* Sum 1 bit at a time. */
88 x = x - ((x >> (uword) 1) & c1);
90 /* 2 bits at a time. */
91 x = (x & c2) + ((x >> (uword) 2) & c2);
93 /* 4 bits at a time. */
94 x = (x + (x >> (uword) 4)) & c3;
96 /* 8, 16, 32 bits at a time. */
97 x = x + (x >> (uword) 8);
98 x = x + (x >> (uword) 16);
100 x = x + (x >> (uword) 32);
103 return x & (2 * BITS (uword) - 1);
108 #define count_leading_zeros(x) __builtin_clzll (x)
110 #define count_leading_zeros(x) __builtin_clzll (x)
113 #define count_trailing_zeros(x) get_lowest_set_bit_index (x)
114 #define log2_first_set(x) get_lowest_set_bit_index (x)
116 /* Based on "Hacker's Delight" code from GLS. */
119 uword masks[1 + log2_uword_bits];
123 compress_init (compress_main_t * cm, uword mask)
125 uword q, m, zm, n, i;
131 for (i = 0; i < log2_uword_bits; i++)
140 m ^= m << (uword) 32;
142 cm->masks[1 + i] = n = (m << 1) & zm;
145 zm = zm ^ q ^ (q >> (1 << i));
150 compress_bits (compress_main_t * cm, uword x)
154 r = x & cm->masks[0];
155 q = r & cm->masks[1];
157 q = r & cm->masks[2];
159 q = r & cm->masks[3];
161 q = r & cm->masks[4];
163 q = r & cm->masks[5];
166 q = r & cm->masks[6];
167 r ^= q ^ (q >> (uword) 32);
174 rotate_left (uword x, uword i)
176 return (x << i) | (x >> (BITS (i) - i));
180 rotate_right (uword x, uword i)
182 return (x >> i) | (x << (BITS (i) - i));
185 /* Returns snoob from Hacker's Delight. Next highest number
186 with same number of set bits. */
188 next_with_same_number_of_set_bits (uword x)
190 uword smallest, ripple, ones;
192 ripple = x + smallest;
194 ones = ones >> (2 + log2_first_set (x));
195 return ripple | ones;
198 #define foreach_set_bit_index(i, v) \
199 for (uword _tmp = (v) + 0 * (uword) (i = get_lowest_set_bit_index (v)); \
201 i = get_lowest_set_bit_index (_tmp = clear_lowest_set_bit (_tmp)))
203 static_always_inline uword
204 uword_bitmap_count_set_bits (uword *bmp, uword n_uwords)
208 count += count_set_bits (bmp++[0]);
212 static_always_inline uword
213 uword_bitmap_is_bit_set (uword *bmp, uword bit_index)
215 bmp += bit_index / uword_bits;
216 bit_index %= uword_bits;
217 return (bmp[0] >> bit_index) & 1;
220 static_always_inline void
221 uword_bitmap_set_bits_at_index (uword *bmp, uword bit_index, uword n_bits)
223 bmp += bit_index / uword_bits;
224 bit_index %= uword_bits;
225 uword max_bits = uword_bits - bit_index;
227 if (n_bits < max_bits)
229 bmp[0] |= pow2_mask (n_bits) << bit_index;
233 bmp++[0] |= pow2_mask (max_bits) << bit_index;
236 for (; n_bits >= uword_bits; bmp++, n_bits -= uword_bits)
240 bmp[0] |= pow2_mask (n_bits);
243 static_always_inline void
244 uword_bitmap_clear_bits_at_index (uword *bmp, uword bit_index, uword n_bits)
246 bmp += bit_index / uword_bits;
247 bit_index %= uword_bits;
248 uword max_bits = uword_bits - bit_index;
250 if (n_bits < max_bits)
252 bmp[0] &= ~(pow2_mask (n_bits) << bit_index);
256 bmp++[0] &= ~(pow2_mask (max_bits) << bit_index);
259 for (; n_bits >= uword_bits; bmp++, n_bits -= uword_bits)
263 bmp[0] &= ~pow2_mask (n_bits);
266 static_always_inline int
267 uword_bitmap_find_first_set (uword *bmp)
273 return (b - bmp) * uword_bits + get_lowest_set_bit_index (b[0]);
276 static_always_inline u32
277 bit_extract_u32 (u32 v, u32 mask)
280 return _pext_u32 (v, mask);
287 u32 lowest_mask_bit = get_lowest_set_bit (mask);
288 mask ^= lowest_mask_bit;
289 rv |= (v & lowest_mask_bit) ? bit : 0;
297 static_always_inline u64
298 bit_extract_u64 (u64 v, u64 mask)
301 return _pext_u64 (v, mask);
308 u64 lowest_mask_bit = get_lowest_set_bit (mask);
309 mask ^= lowest_mask_bit;
310 rv |= (v & lowest_mask_bit) ? bit : 0;
318 static_always_inline void
319 u64_bit_set (u64 *p, u8 bit_index, u8 is_one)
322 val &= ~(1ULL << bit_index);
323 val |= 1ULL << bit_index;
327 static_always_inline void
328 u32_bit_set (u32 *p, u8 bit_index, u8 is_one)
331 val &= ~(1U << bit_index);
332 val |= 1U << bit_index;
336 static_always_inline int
337 u64_is_bit_set (u64 v, u8 bit_index)
339 return (v & 1ULL << bit_index) != 0;
342 static_always_inline int
343 u32_is_bit_set (u32 v, u8 bit_index)
345 return (v & 1U << bit_index) != 0;
349 #warning "already included"
350 #endif /* included_clib_bitops_h */
353 * fd.io coding-style-patch-verification: ON
356 * eval: (c-set-style "gnu")