blob: 15454ca5036bbe6db5fffa88261e3fee067aa1aa [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Damjan Marion7b90f662022-01-13 00:28:14 +010041static_always_inline uword
42clear_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
51static_always_inline uword
52get_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
61static_always_inline u8
62get_lowest_set_bit_index (uword x)
63{
64 return uword_bits > 32 ? __builtin_ctzll (x) : __builtin_ctz (x);
65}
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
67/* Population count from Hacker's Delight. */
Dave Barachc3799992016-08-15 11:12:27 -040068always_inline uword
69count_set_bits (uword x)
Ed Warnickecb9cada2015-12-08 15:45:58 -070070{
Damjan Marion23e310a2018-05-10 03:04:08 +020071#ifdef __POPCNT__
Damjan Marion7b90f662022-01-13 00:28:14 +010072 return uword_bits > 32 ? __builtin_popcountll (x) : __builtin_popcount (x);
Damjan Marion23e310a2018-05-10 03:04:08 +020073#else
Ed Warnickecb9cada2015-12-08 15:45:58 -070074#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
Dave Barachc3799992016-08-15 11:12:27 -0400100 return x & (2 * BITS (uword) - 1);
Damjan Marion23e310a2018-05-10 03:04:08 +0200101#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102}
103
Damjan Marion7b90f662022-01-13 00:28:14 +0100104#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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113/* Based on "Hacker's Delight" code from GLS. */
Dave Barachc3799992016-08-15 11:12:27 -0400114typedef struct
115{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 uword masks[1 + log2_uword_bits];
117} compress_main_t;
118
119always_inline void
120compress_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
146always_inline uword
147compress_bits (compress_main_t * cm, uword x)
148{
149 uword q, r;
150
151 r = x & cm->masks[0];
Dave Barachc3799992016-08-15 11:12:27 -0400152 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162#if uword_bits > 32
Dave Barachc3799992016-08-15 11:12:27 -0400163 q = r & cm->masks[6];
164 r ^= q ^ (q >> (uword) 32);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165#endif
166
167 return r;
168}
169
170always_inline uword
171rotate_left (uword x, uword i)
Dave Barachc3799992016-08-15 11:12:27 -0400172{
173 return (x << i) | (x >> (BITS (i) - i));
174}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
176always_inline uword
177rotate_right (uword x, uword i)
Dave Barachc3799992016-08-15 11:12:27 -0400178{
179 return (x >> i) | (x << (BITS (i) - i));
180}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181
182/* Returns snoob from Hacker's Delight. Next highest number
183 with same number of set bits. */
184always_inline uword
185next_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
Damjan Marion7b90f662022-01-13 00:28:14 +0100195#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)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199
Damjan Marionfdc874a2022-01-05 14:06:34 +0100200#else
Damjan Marion7b90f662022-01-13 00:28:14 +0100201#warning "already included"
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202#endif /* included_clib_bitops_h */
Dave Barachc3799992016-08-15 11:12:27 -0400203
204/*
205 * fd.io coding-style-patch-verification: ON
206 *
207 * Local Variables:
208 * eval: (c-set-style "gnu")
209 * End:
210 */