FD.io VPP  v18.11-rc0-18-g2a3fb1a
Vector Packet Processing
sparse_vec.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_sparse_vec_h
39 #define included_sparse_vec_h
40 
41 #include <vppinfra/vec.h>
42 #include <vppinfra/bitops.h>
43 
44 /* Sparsely indexed vectors. Basic idea taken from Hacker's delight.
45  Eliot added ranges. */
46 typedef struct
47 {
48  /* Bitmap one for each sparse index. */
50 
51  /* member_counts[i] = total number of members with j < i. */
53 
54 #define SPARSE_VEC_IS_RANGE (1 << 0)
55 #define SPARSE_VEC_IS_VALID_RANGE (1 << 1)
58 
61 {
62  return vec_header (v, sizeof (sparse_vec_header_t));
63 }
64 
65 /* Index 0 is always used to mark indices that are not valid in
66  sparse vector. For example, you look up V[0x1234] and 0x1234 is not
67  known you'll get 0 back as an index. */
68 #define SPARSE_VEC_INVALID_INDEX (0)
69 
70 always_inline void *
71 sparse_vec_new (uword elt_bytes, uword sparse_index_bits)
72 {
73  void *v;
75  word n;
76 
77  ASSERT (sparse_index_bits <= 16);
78 
79  v = _vec_resize ((void *) 0,
80  /* length increment */ 8,
81  /* data bytes */ 8 * elt_bytes,
82  /* header bytes */ sizeof (h[0]),
83  /* data align */ 0);
84 
85  /* Make space for invalid entry (entry 0). */
86  _vec_len (v) = 1;
87 
88  h = sparse_vec_header (v);
89 
90  n = sparse_index_bits - min_log2 (BITS (uword));
91  if (n < 0)
92  n = 0;
93  n = 1ULL << n;
95  vec_resize (h->member_counts, n);
96 
97  return v;
98 }
99 
102  uword sparse_index,
103  uword maybe_range, u32 * insert)
104 {
106  uword i, b, d, w;
107  u8 is_member;
108 
109  h = sparse_vec_header (v);
110  i = sparse_index / BITS (h->is_member_bitmap[0]);
111  b = sparse_index % BITS (h->is_member_bitmap[0]);
112 
113  ASSERT (i < vec_len (h->is_member_bitmap));
114  ASSERT (i < vec_len (h->member_counts));
115 
116  w = h->is_member_bitmap[i];
117 
118  if (PREDICT_TRUE (maybe_range == 0 && insert == 0 &&
119  count_trailing_zeros (w) == b))
120  return h->member_counts[i] + 1;
121 
122  d = h->member_counts[i] + count_set_bits (w & ((1ULL << b) - 1));
123  is_member = (w & (1ULL << b)) != 0;
124 
125  if (maybe_range)
126  {
127  u8 r = h->range_flags[d];
128  u8 is_range, is_valid_range;
129 
130  is_range = maybe_range & (r & SPARSE_VEC_IS_RANGE);
131  is_valid_range = (r & SPARSE_VEC_IS_VALID_RANGE) != 0;
132 
133  is_member = is_range ? is_valid_range : is_member;
134  }
135 
136  if (insert)
137  {
138  *insert = !is_member;
139  if (!is_member)
140  {
141  uword j;
142  w |= 1ULL << b;
143  h->is_member_bitmap[i] = w;
144  for (j = i + 1; j < vec_len (h->member_counts); j++)
145  h->member_counts[j] += 1;
146  }
147 
148  return 1 + d;
149  }
150 
151  d = is_member ? d : 0;
152 
153  return is_member + d;
154 }
155 
157 sparse_vec_index (void *v, uword sparse_index)
158 {
159  return sparse_vec_index_internal (v, sparse_index,
160  /* maybe range */ 0,
161  /* insert? */ 0);
162 }
163 
164 always_inline void
166  u32 si0, u32 si1, u32 * i0_return, u32 * i1_return)
167 {
169  uword b0, b1, w0, w1, v0, v1;
170  u32 i0, i1, d0, d1;
171  u8 is_member0, is_member1;
172 
173  h = sparse_vec_header (v);
174 
175  i0 = si0 / BITS (h->is_member_bitmap[0]);
176  i1 = si1 / BITS (h->is_member_bitmap[0]);
177 
178  b0 = si0 % BITS (h->is_member_bitmap[0]);
179  b1 = si1 % BITS (h->is_member_bitmap[0]);
180 
181  ASSERT (i0 < vec_len (h->is_member_bitmap));
182  ASSERT (i1 < vec_len (h->is_member_bitmap));
183 
184  ASSERT (i0 < vec_len (h->member_counts));
185  ASSERT (i1 < vec_len (h->member_counts));
186 
187  w0 = h->is_member_bitmap[i0];
188  w1 = h->is_member_bitmap[i1];
189 
190  if (PREDICT_TRUE ((count_trailing_zeros (w0) == b0) +
191  (count_trailing_zeros (w1) == b1) == 2))
192  {
193  *i0_return = h->member_counts[i0] + 1;
194  *i1_return = h->member_counts[i1] + 1;
195  return;
196  }
197 
198  v0 = w0 & ((1ULL << b0) - 1);
199  v1 = w1 & ((1ULL << b1) - 1);
200 
201  /* Speculate that masks will have zero or one bits set. */
202  d0 = h->member_counts[i0] + (v0 != 0);
203  d1 = h->member_counts[i1] + (v1 != 0);
204 
205  /* Validate speculation. */
206  if (PREDICT_FALSE (!is_pow2 (v0) || !is_pow2 (v1)))
207  {
208  d0 += count_set_bits (v0) - (v0 != 0);
209  d1 += count_set_bits (v1) - (v1 != 0);
210  }
211 
212  is_member0 = (w0 & (1ULL << b0)) != 0;
213  is_member1 = (w1 & (1ULL << b1)) != 0;
214 
215  d0 = is_member0 ? d0 : 0;
216  d1 = is_member1 ? d1 : 0;
217 
218  *i0_return = is_member0 + d0;
219  *i1_return = is_member1 + d1;
220 }
221 
222 #define sparse_vec_free(v) vec_free(v)
223 
224 #define sparse_vec_elt_at_index(v,i) \
225  vec_elt_at_index ((v), sparse_vec_index ((v), (i)))
226 
227 #define sparse_vec_validate(v,i) \
228 ({ \
229  uword _i; \
230  u32 _insert; \
231  \
232  if (! (v)) \
233  (v) = sparse_vec_new (sizeof ((v)[0]), BITS (u16)); \
234  \
235  _i = sparse_vec_index_internal ((v), (i), \
236  /* maybe range */ 0, \
237  /* insert? */ &_insert); \
238  if (_insert) \
239  vec_insert_ha ((v), 1, _i, \
240  /* header size */ sizeof (sparse_vec_header_t), \
241  /* align */ 0); \
242  \
243  /* Invalid index is 0. */ \
244  ASSERT (_i > 0); \
245  \
246  (v) + _i; \
247 })
248 
249 #endif /* included_sparse_vec_h */
250 
251 /*
252  * fd.io coding-style-patch-verification: ON
253  *
254  * Local Variables:
255  * eval: (c-set-style "gnu")
256  * End:
257  */
#define SPARSE_VEC_IS_RANGE
Definition: sparse_vec.h:54
#define PREDICT_TRUE(x)
Definition: clib.h:106
int i
unsigned char u8
Definition: types.h:56
#define count_trailing_zeros(x)
Definition: clib.h:133
static uword min_log2(uword x)
Definition: clib.h:138
i64 word
Definition: types.h:111
#define always_inline
Definition: clib.h:92
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:240
unsigned int u32
Definition: types.h:88
static void sparse_vec_index2(void *v, u32 si0, u32 si1, u32 *i0_return, u32 *i1_return)
Definition: sparse_vec.h:165
#define v
Definition: acl.c:491
unsigned short u16
Definition: types.h:57
#define PREDICT_FALSE(x)
Definition: clib.h:105
static sparse_vec_header_t * sparse_vec_header(void *v)
Definition: sparse_vec.h:60
static uword sparse_vec_index(void *v, uword sparse_index)
Definition: sparse_vec.h:157
#define ASSERT(truth)
static uword is_pow2(uword x)
Definition: clib.h:229
static uword sparse_vec_index_internal(void *v, uword sparse_index, uword maybe_range, u32 *insert)
Definition: sparse_vec.h:101
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
#define SPARSE_VEC_IS_VALID_RANGE
Definition: sparse_vec.h:55
static void * vec_header(void *v, uword header_bytes)
Find a user vector header.
Definition: vec_bootstrap.h:92
static uword count_set_bits(uword x)
Definition: bitops.h:45
uword * is_member_bitmap
Definition: sparse_vec.h:49
static void * sparse_vec_new(uword elt_bytes, uword sparse_index_bits)
Definition: sparse_vec.h:71
#define BITS(x)
Definition: clib.h:58
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".