1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
/* SPDX-License-Identifier: Apache-2.0
* Copyright(c) 2021 Cisco Systems, Inc.
*/
#ifndef included_vector_mask_compare_h
#define included_vector_mask_compare_h
#include <vppinfra/clib.h>
#include <vppinfra/memcpy.h>
static_always_inline u64
clib_mask_compare_u16_x64 (u16 v, u16 *a)
{
u64 mask = 0;
#if defined(CLIB_HAVE_VEC512)
u16x32 v32 = u16x32_splat (v);
u16x32u *av = (u16x32u *) a;
mask = ((u64) u16x32_is_equal_mask (av[0], v32) |
(u64) u16x32_is_equal_mask (av[1], v32) << 32);
#elif defined(CLIB_HAVE_VEC256)
u16x16 v16 = u16x16_splat (v);
u16x16u *av = (u16x16u *) a;
i8x32 x;
x = i8x32_pack (v16 == av[0], v16 == av[1]);
mask = i8x32_msb_mask ((i8x32) u64x4_permute (x, 0, 2, 1, 3));
x = i8x32_pack (v16 == av[2], v16 == av[3]);
mask |= (u64) i8x32_msb_mask ((i8x32) u64x4_permute (x, 0, 2, 1, 3)) << 32;
#elif defined(CLIB_HAVE_VEC128) && defined(__ARM_NEON)
u16x8 v8 = u16x8_splat (v);
u16x8 m = { 1, 2, 4, 8, 16, 32, 64, 128 };
u16x8u *av = (u16x8u *) a;
/* compare each u16 elemment with v8, result gives 0xffff in each element
of the resulting vector if comparison result is true.
Bitwise AND with m will give us one bit set for true result and offset
of that bit represend element index. Finally vaddvq_u16() gives us sum
of all elements of the vector which will give us u8 bitmap. */
for (int i = 0; i < 8; i++)
mask |= (u64) vaddvq_u16 ((av[i] == v8) & m) << (i * 8);
#elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK)
u16x8 v8 = u16x8_splat (v);
u16x8u *av = (u16x8u *) a;
mask = ((u64) i8x16_msb_mask (i8x16_pack (v8 == av[0], v8 == av[1])) |
(u64) i8x16_msb_mask (i8x16_pack (v8 == av[2], v8 == av[3])) << 16 |
(u64) i8x16_msb_mask (i8x16_pack (v8 == av[4], v8 == av[5])) << 32 |
(u64) i8x16_msb_mask (i8x16_pack (v8 == av[6], v8 == av[7])) << 48);
#else
for (int i = 0; i < 64; i++)
if (a[i] == v)
mask |= 1ULL << i;
#endif
return mask;
}
static_always_inline u64
clib_mask_compare_u16_x64_n (u16 v, u16 *a, u32 n_elts)
{
u64 mask = 0;
CLIB_UNUSED (u64 data_mask) = pow2_mask (n_elts);
#if defined(CLIB_HAVE_VEC512)
u16x32 v32 = u16x32_splat (v);
u16x32u *av = (u16x32u *) a;
mask = ((u64) u16x32_is_equal_mask (
u16x32_mask_load_zero (&av[0], data_mask), v32) |
(u64) u16x32_is_equal_mask (
u16x32_mask_load_zero (&av[1], data_mask >> 32), v32)
<< 32);
#elif defined(CLIB_HAVE_VEC256) && defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
u16x16 v16 = u16x16_splat (v);
u16x16u *av = (u16x16u *) a;
i8x32 x;
x = i8x32_pack (v16 == u16x16_mask_load_zero (&av[0], data_mask),
v16 == u16x16_mask_load_zero (&av[1], data_mask >> 16));
mask = i8x32_msb_mask ((i8x32) u64x4_permute (x, 0, 2, 1, 3));
x = i8x32_pack (v16 == u16x16_mask_load_zero (&av[2], data_mask >> 32),
v16 == u16x16_mask_load_zero (&av[3], data_mask >> 48));
mask |= (u64) i8x32_msb_mask ((i8x32) u64x4_permute (x, 0, 2, 1, 3)) << 32;
#else
for (int i = 0; i < n_elts; i++)
if (a[i] == v)
mask |= 1ULL << i;
#endif
return mask;
}
/** \brief Compare 16-bit elemments with provied value and return bitmap
@param v value to compare elements with
@param a array of u16 elements
@param mask array of u64 where reuslting mask will be stored
@param n_elts number of elements in the array
@return none
*/
static_always_inline void
clib_mask_compare_u16 (u16 v, u16 *a, u64 *mask, u32 n_elts)
{
while (n_elts >= 64)
{
mask++[0] = clib_mask_compare_u16_x64 (v, a);
n_elts -= 64;
a += 64;
}
if (PREDICT_TRUE (n_elts == 0))
return;
mask[0] = clib_mask_compare_u16_x64_n (v, a, n_elts) & pow2_mask (n_elts);
}
static_always_inline u64
clib_mask_compare_u32_x64 (u32 v, u32 *a)
{
u64 mask = 0;
#if defined(CLIB_HAVE_VEC512)
u32x16 v16 = u32x16_splat (v);
u32x16u *av = (u32x16u *) a;
mask = ((u64) u32x16_is_equal_mask (av[0], v16) |
(u64) u32x16_is_equal_mask (av[1], v16) << 16 |
(u64) u32x16_is_equal_mask (av[2], v16) << 32 |
(u64) u32x16_is_equal_mask (av[3], v16) << 48);
#elif defined(CLIB_HAVE_VEC256)
u32x8 v8 = u32x8_splat (v);
u32x8u *av = (u32x8u *) a;
u32x8 m = { 0, 4, 1, 5, 2, 6, 3, 7 };
i8x32 c;
c = i8x32_pack (i16x16_pack ((i32x8) (v8 == av[0]), (i32x8) (v8 == av[1])),
i16x16_pack ((i32x8) (v8 == av[2]), (i32x8) (v8 == av[3])));
mask = i8x32_msb_mask ((i8x32) u32x8_permute ((u32x8) c, m));
c = i8x32_pack (i16x16_pack ((i32x8) (v8 == av[4]), (i32x8) (v8 == av[5])),
i16x16_pack ((i32x8) (v8 == av[6]), (i32x8) (v8 == av[7])));
mask |= (u64) i8x32_msb_mask ((i8x32) u32x8_permute ((u32x8) c, m)) << 32;
#elif defined(CLIB_HAVE_VEC128) && defined(__ARM_NEON)
u32x4 v4 = u32x4_splat (v);
u32x4 m = { 1, 2, 4, 8 };
u32x4u *av = (u32x4u *) a;
/* compare each u32 elemment with v4, result gives -1 in each element
of the resulting vector if comparison result is true.
Bitwise AND with m will give us one bit set for true result and offset
of that bit represend element index. Finally vaddvq_u32() gives us sum
of all elements of the vector which will give us u8 bitmap. */
for (int i = 0; i < 16; i++)
mask |= (u64) vaddvq_u32 ((av[i] == v4) & m) << (i * 4);
#elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK)
u32x4 v4 = u32x4_splat (v);
u32x4u *av = (u32x4u *) a;
for (int i = 0; i < 4; i++)
{
i16x8 p1 = i16x8_pack (v4 == av[0], v4 == av[1]);
i16x8 p2 = i16x8_pack (v4 == av[2], v4 == av[3]);
mask |= (u64) i8x16_msb_mask (i8x16_pack (p1, p2)) << (i * 16);
av += 4;
}
#else
for (int i = 0; i < 64; i++)
if (a[i] == v)
mask |= 1ULL << i;
#endif
return mask;
}
static_always_inline u64
clib_mask_compare_u32_x64_n (u32 v, u32 *a, u32 n_elts)
{
u64 mask = 0;
CLIB_UNUSED (u64 data_mask) = pow2_mask (n_elts);
#if defined(CLIB_HAVE_VEC512)
u32x16 v16 = u32x16_splat (v);
u32x16u *av = (u32x16u *) a;
mask = ((u64) u32x16_is_equal_mask (
u32x16_mask_load_zero (&av[0], data_mask), v16) |
(u64) u32x16_is_equal_mask (
u32x16_mask_load_zero (&av[1], data_mask >> 16), v16)
<< 16 |
(u64) u32x16_is_equal_mask (
u32x16_mask_load_zero (&av[2], data_mask >> 32), v16)
<< 32 |
(u64) u32x16_is_equal_mask (
u32x16_mask_load_zero (&av[3], data_mask >> 48), v16)
<< 48);
#elif defined(CLIB_HAVE_VEC256) && defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
u32x8 v8 = u32x8_splat (v);
u32x8u *av = (u32x8u *) a;
u32x8 m = { 0, 4, 1, 5, 2, 6, 3, 7 };
i8x32 c;
c = i8x32_pack (
i16x16_pack (
(i32x8) (v8 == u32x8_mask_load_zero (&av[0], data_mask)),
(i32x8) (v8 == u32x8_mask_load_zero (&av[1], data_mask >> 8))),
i16x16_pack (
(i32x8) (v8 == u32x8_mask_load_zero (&av[2], data_mask >> 16)),
(i32x8) (v8 == u32x8_mask_load_zero (&av[3], data_mask >> 24))));
mask = i8x32_msb_mask ((i8x32) u32x8_permute ((u32x8) c, m));
c = i8x32_pack (
i16x16_pack (
(i32x8) (v8 == u32x8_mask_load_zero (&av[4], data_mask >> 32)),
(i32x8) (v8 == u32x8_mask_load_zero (&av[5], data_mask >> 40))),
i16x16_pack (
(i32x8) (v8 == u32x8_mask_load_zero (&av[6], data_mask >> 48)),
(i32x8) (v8 == u32x8_mask_load_zero (&av[7], data_mask >> 56))));
mask |= (u64) i8x32_msb_mask ((i8x32) u32x8_permute ((u32x8) c, m)) << 32;
mask |= (u64) i8x32_msb_mask ((i8x32) u32x8_permute ((u32x8) c, m)) << 32;
#else
for (int i = 0; i < n_elts; i++)
if (a[i] == v)
mask |= 1ULL << i;
#endif
return mask;
}
/** \brief Compare 32-bit elemments with provied value and return bitmap
@param v value to compare elements with
@param a array of u32 elements
@param mask array of u64 where reuslting mask will be stored
@param n_elts number of elements in the array
@return none
*/
static_always_inline void
clib_mask_compare_u32 (u32 v, u32 *a, u64 *bitmap, u32 n_elts)
{
while (n_elts >= 64)
{
bitmap++[0] = clib_mask_compare_u32_x64 (v, a);
n_elts -= 64;
a += 64;
}
if (PREDICT_TRUE (n_elts == 0))
return;
bitmap[0] = clib_mask_compare_u32_x64_n (v, a, n_elts) & pow2_mask (n_elts);
}
static_always_inline u64
clib_mask_compare_u64_x64 (u64 v, u64 *a)
{
u64 mask = 0;
#if defined(CLIB_HAVE_VEC512)
u64x8 v8 = u64x8_splat (v);
u64x8u *av = (u64x8u *) a;
mask = ((u64) u64x8_is_equal_mask (av[0], v8) |
(u64) u64x8_is_equal_mask (av[1], v8) << 8 |
(u64) u64x8_is_equal_mask (av[2], v8) << 16 |
(u64) u64x8_is_equal_mask (av[3], v8) << 24 |
(u64) u64x8_is_equal_mask (av[4], v8) << 32 |
(u64) u64x8_is_equal_mask (av[5], v8) << 40 |
(u64) u64x8_is_equal_mask (av[6], v8) << 48 |
(u64) u64x8_is_equal_mask (av[7], v8) << 56);
#elif defined(CLIB_HAVE_VEC256) && defined(__BMI2__)
u64x4 v4 = u64x4_splat (v);
u64x4u *av = (u64x4u *) a;
for (int i = 0; i < 16; i += 2)
{
u64 l = u8x32_msb_mask (v4 == av[i]);
u64 h = u8x32_msb_mask (v4 == av[i + 1]);
mask |= _pext_u64 (l | h << 32, 0x0101010101010101) << (i * 4);
}
#else
for (int i = 0; i < 64; i++)
if (a[i] == v)
mask |= 1ULL << i;
#endif
return mask;
}
static_always_inline u64
clib_mask_compare_u64_x64_n (u64 v, u64 *a, u32 n_elts)
{
u64 mask = 0;
CLIB_UNUSED (u64 data_mask) = pow2_mask (n_elts);
#if defined(CLIB_HAVE_VEC512)
u64x8 v8 = u64x8_splat (v);
u64x8u *av = (u64x8u *) a;
mask =
((u64) u64x8_is_equal_mask (u64x8_mask_load_zero (&av[0], data_mask), v8) |
(u64) u64x8_is_equal_mask (u64x8_mask_load_zero (&av[1], data_mask >> 8),
v8)
<< 8 |
(u64) u64x8_is_equal_mask (u64x8_mask_load_zero (&av[2], data_mask >> 16),
v8)
<< 16 |
(u64) u64x8_is_equal_mask (u64x8_mask_load_zero (&av[3], data_mask >> 24),
v8)
<< 24 |
(u64) u64x8_is_equal_mask (u64x8_mask_load_zero (&av[4], data_mask >> 32),
v8)
<< 32 |
(u64) u64x8_is_equal_mask (u64x8_mask_load_zero (&av[5], data_mask >> 40),
v8)
<< 40 |
(u64) u64x8_is_equal_mask (u64x8_mask_load_zero (&av[6], data_mask >> 48),
v8)
<< 48 |
(u64) u64x8_is_equal_mask (u64x8_mask_load_zero (&av[7], data_mask >> 56),
v8)
<< 56);
#elif defined(CLIB_HAVE_VEC256) && defined(__BMI2__) && \
defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
u64x4 v4 = u64x4_splat (v);
u64x4u *av = (u64x4u *) a;
for (int i = 0; i < 16; i += 2)
{
u64 l = u8x32_msb_mask (v4 == u64x4_mask_load_zero (&av[i], data_mask));
u64 h = u8x32_msb_mask (
v4 == u64x4_mask_load_zero (&av[i + 1], data_mask >> 4));
mask |= _pext_u64 (l | h << 32, 0x0101010101010101) << (i * 4);
data_mask >>= 8;
}
#else
for (int i = 0; i < n_elts; i++)
if (a[i] == v)
mask |= 1ULL << i;
#endif
return mask;
}
/** \brief Compare 64-bit elemments with provied value and return bitmap
@param v value to compare elements with
@param a array of u64 elements
@param mask array of u64 where reuslting mask will be stored
@param n_elts number of elements in the array
@return none
*/
static_always_inline void
clib_mask_compare_u64 (u64 v, u64 *a, u64 *bitmap, u32 n_elts)
{
while (n_elts >= 64)
{
bitmap++[0] = clib_mask_compare_u64_x64 (v, a);
n_elts -= 64;
a += 64;
}
if (PREDICT_TRUE (n_elts == 0))
return;
bitmap[0] = clib_mask_compare_u64_x64_n (v, a, n_elts) & pow2_mask (n_elts);
}
#endif
|