FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
bitops.h
Go to the documentation of this file.
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. */
46 {
47 #if uword_bits == 64
48  const uword c1 = 0x5555555555555555;
49  const uword c2 = 0x3333333333333333;
50  const uword c3 = 0x0f0f0f0f0f0f0f0f;
51 #else
52  const uword c1 = 0x55555555;
53  const uword c2 = 0x33333333;
54  const uword c3 = 0x0f0f0f0f;
55 #endif
56 
57  /* Sum 1 bit at a time. */
58  x = x - ((x >> (uword) 1) & c1);
59 
60  /* 2 bits at a time. */
61  x = (x & c2) + ((x >> (uword) 2) & c2);
62 
63  /* 4 bits at a time. */
64  x = (x + (x >> (uword) 4)) & c3;
65 
66  /* 8, 16, 32 bits at a time. */
67  x = x + (x >> (uword) 8);
68  x = x + (x >> (uword) 16);
69 #if uword_bits == 64
70  x = x + (x >> (uword) 32);
71 #endif
72 
73  return x & (2 * BITS (uword) - 1);
74 }
75 
76 /* Based on "Hacker's Delight" code from GLS. */
77 typedef struct
78 {
79  uword masks[1 + log2_uword_bits];
81 
82 always_inline void
84 {
85  uword q, m, zm, n, i;
86 
87  m = ~mask;
88  zm = mask;
89 
90  cm->masks[0] = mask;
91  for (i = 0; i < log2_uword_bits; i++)
92  {
93  q = m;
94  m ^= m << 1;
95  m ^= m << 2;
96  m ^= m << 4;
97  m ^= m << 8;
98  m ^= m << 16;
99 #if uword_bits > 32
100  m ^= m << (uword) 32;
101 #endif
102  cm->masks[1 + i] = n = (m << 1) & zm;
103  m = q & ~m;
104  q = zm & n;
105  zm = zm ^ q ^ (q >> (1 << i));
106  }
107 }
108 
111 {
112  uword q, r;
113 
114  r = x & cm->masks[0];
115  q = r & cm->masks[1];
116  r ^= q ^ (q >> 1);
117  q = r & cm->masks[2];
118  r ^= q ^ (q >> 2);
119  q = r & cm->masks[3];
120  r ^= q ^ (q >> 4);
121  q = r & cm->masks[4];
122  r ^= q ^ (q >> 8);
123  q = r & cm->masks[5];
124  r ^= q ^ (q >> 16);
125 #if uword_bits > 32
126  q = r & cm->masks[6];
127  r ^= q ^ (q >> (uword) 32);
128 #endif
129 
130  return r;
131 }
132 
135 {
136  return (x << i) | (x >> (BITS (i) - i));
137 }
138 
141 {
142  return (x >> i) | (x << (BITS (i) - i));
143 }
144 
145 /* Returns snoob from Hacker's Delight. Next highest number
146  with same number of set bits. */
149 {
150  uword smallest, ripple, ones;
151  smallest = x & -x;
152  ripple = x + smallest;
153  ones = x ^ ripple;
154  ones = ones >> (2 + log2_first_set (x));
155  return ripple | ones;
156 }
157 
158 #define foreach_set_bit(var,mask,body) \
159 do { \
160  uword _foreach_set_bit_m_##var = (mask); \
161  uword _foreach_set_bit_f_##var; \
162  while (_foreach_set_bit_m_##var != 0) \
163  { \
164  _foreach_set_bit_f_##var = first_set (_foreach_set_bit_m_##var); \
165  _foreach_set_bit_m_##var ^= _foreach_set_bit_f_##var; \
166  (var) = min_log2 (_foreach_set_bit_f_##var); \
167  do { body; } while (0); \
168  } \
169 } while (0)
170 
171 #endif /* included_clib_bitops_h */
172 
173 /*
174  * fd.io coding-style-patch-verification: ON
175  *
176  * Local Variables:
177  * eval: (c-set-style "gnu")
178  * End:
179  */
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
static uword log2_first_set(uword x)
Definition: clib.h:290
#define log2_uword_bits
Definition: types.h:84
static void compress_init(compress_main_t *cm, uword mask)
Definition: bitops.h:83
#define always_inline
Definition: clib.h:84
static uword rotate_right(uword x, uword i)
Definition: bitops.h:140
u64 uword
Definition: types.h:112
static uword count_set_bits(uword x)
Definition: bitops.h:45
uword masks[1+log2_uword_bits]
Definition: bitops.h:79
#define BITS(x)
Definition: clib.h:58
static uword compress_bits(compress_main_t *cm, uword x)
Definition: bitops.h:110
static uword rotate_left(uword x, uword i)
Definition: bitops.h:134
static uword next_with_same_number_of_set_bits(uword x)
Definition: bitops.h:148