blob: 17ad49ffb46adc25c8ab14a24f4524e8719f28e8 [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
41#include <vppinfra/clib.h>
42
43/* Population count from Hacker's Delight. */
Dave Barachc3799992016-08-15 11:12:27 -040044always_inline uword
45count_set_bits (uword x)
Ed Warnickecb9cada2015-12-08 15:45:58 -070046{
Damjan Marion23e310a2018-05-10 03:04:08 +020047#ifdef __POPCNT__
48#if uword_bits == 64
49 return __builtin_popcountll (x);
50#else
51 return __builtin_popcount (x);
52#endif
53#else
Ed Warnickecb9cada2015-12-08 15:45:58 -070054#if uword_bits == 64
55 const uword c1 = 0x5555555555555555;
56 const uword c2 = 0x3333333333333333;
57 const uword c3 = 0x0f0f0f0f0f0f0f0f;
58#else
59 const uword c1 = 0x55555555;
60 const uword c2 = 0x33333333;
61 const uword c3 = 0x0f0f0f0f;
62#endif
63
64 /* Sum 1 bit at a time. */
65 x = x - ((x >> (uword) 1) & c1);
66
67 /* 2 bits at a time. */
68 x = (x & c2) + ((x >> (uword) 2) & c2);
69
70 /* 4 bits at a time. */
71 x = (x + (x >> (uword) 4)) & c3;
72
73 /* 8, 16, 32 bits at a time. */
74 x = x + (x >> (uword) 8);
75 x = x + (x >> (uword) 16);
76#if uword_bits == 64
77 x = x + (x >> (uword) 32);
78#endif
79
Dave Barachc3799992016-08-15 11:12:27 -040080 return x & (2 * BITS (uword) - 1);
Damjan Marion23e310a2018-05-10 03:04:08 +020081#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -070082}
83
84/* Based on "Hacker's Delight" code from GLS. */
Dave Barachc3799992016-08-15 11:12:27 -040085typedef struct
86{
Ed Warnickecb9cada2015-12-08 15:45:58 -070087 uword masks[1 + log2_uword_bits];
88} compress_main_t;
89
90always_inline void
91compress_init (compress_main_t * cm, uword mask)
92{
93 uword q, m, zm, n, i;
94
95 m = ~mask;
96 zm = mask;
97
98 cm->masks[0] = mask;
99 for (i = 0; i < log2_uword_bits; i++)
100 {
101 q = m;
102 m ^= m << 1;
103 m ^= m << 2;
104 m ^= m << 4;
105 m ^= m << 8;
106 m ^= m << 16;
107#if uword_bits > 32
108 m ^= m << (uword) 32;
109#endif
110 cm->masks[1 + i] = n = (m << 1) & zm;
111 m = q & ~m;
112 q = zm & n;
113 zm = zm ^ q ^ (q >> (1 << i));
114 }
115}
116
117always_inline uword
118compress_bits (compress_main_t * cm, uword x)
119{
120 uword q, r;
121
122 r = x & cm->masks[0];
Dave Barachc3799992016-08-15 11:12:27 -0400123 q = r & cm->masks[1];
124 r ^= q ^ (q >> 1);
125 q = r & cm->masks[2];
126 r ^= q ^ (q >> 2);
127 q = r & cm->masks[3];
128 r ^= q ^ (q >> 4);
129 q = r & cm->masks[4];
130 r ^= q ^ (q >> 8);
131 q = r & cm->masks[5];
132 r ^= q ^ (q >> 16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133#if uword_bits > 32
Dave Barachc3799992016-08-15 11:12:27 -0400134 q = r & cm->masks[6];
135 r ^= q ^ (q >> (uword) 32);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136#endif
137
138 return r;
139}
140
141always_inline uword
142rotate_left (uword x, uword i)
Dave Barachc3799992016-08-15 11:12:27 -0400143{
144 return (x << i) | (x >> (BITS (i) - i));
145}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
147always_inline uword
148rotate_right (uword x, uword i)
Dave Barachc3799992016-08-15 11:12:27 -0400149{
150 return (x >> i) | (x << (BITS (i) - i));
151}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
153/* Returns snoob from Hacker's Delight. Next highest number
154 with same number of set bits. */
155always_inline uword
156next_with_same_number_of_set_bits (uword x)
157{
158 uword smallest, ripple, ones;
159 smallest = x & -x;
160 ripple = x + smallest;
161 ones = x ^ ripple;
162 ones = ones >> (2 + log2_first_set (x));
163 return ripple | ones;
164}
165
166#define foreach_set_bit(var,mask,body) \
167do { \
168 uword _foreach_set_bit_m_##var = (mask); \
169 uword _foreach_set_bit_f_##var; \
170 while (_foreach_set_bit_m_##var != 0) \
171 { \
172 _foreach_set_bit_f_##var = first_set (_foreach_set_bit_m_##var); \
173 _foreach_set_bit_m_##var ^= _foreach_set_bit_f_##var; \
174 (var) = min_log2 (_foreach_set_bit_f_##var); \
175 do { body; } while (0); \
176 } \
177} while (0)
178
179#endif /* included_clib_bitops_h */
Dave Barachc3799992016-08-15 11:12:27 -0400180
181/*
182 * fd.io coding-style-patch-verification: ON
183 *
184 * Local Variables:
185 * eval: (c-set-style "gnu")
186 * End:
187 */