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
|
/* SPDX-License-Identifier: Apache-2.0
* Copyright(c) 2021 Cisco Systems, Inc.
*/
#ifndef included_vector_count_equal_h
#define included_vector_count_equal_h
#include <vppinfra/clib.h>
static_always_inline uword
clib_count_equal_u64 (u64 *data, uword max_count)
{
uword count;
u64 first;
if (max_count <= 1)
return max_count;
if (data[0] != data[1])
return 1;
count = 0;
first = data[0];
#if defined(CLIB_HAVE_VEC256)
u64x4 splat = u64x4_splat (first);
while (count + 3 < max_count)
{
u64 bmp;
bmp = u8x32_msb_mask ((u8x32) (u64x4_load_unaligned (data) == splat));
if (bmp != 0xffffffff)
{
count += count_trailing_zeros (~bmp) / 8;
return count;
}
data += 4;
count += 4;
}
#else
count += 2;
data += 2;
while (count + 3 < max_count && ((data[0] ^ first) | (data[1] ^ first) |
(data[2] ^ first) | (data[3] ^ first)) == 0)
{
data += 4;
count += 4;
}
#endif
while (count < max_count && (data[0] == first))
{
data += 1;
count += 1;
}
return count;
}
static_always_inline uword
clib_count_equal_u32 (u32 *data, uword max_count)
{
uword count;
u32 first;
if (max_count <= 1)
return max_count;
if (data[0] != data[1])
return 1;
count = 0;
first = data[0];
#if defined(CLIB_HAVE_VEC512)
u32x16 splat = u32x16_splat (first);
while (count + 15 < max_count)
{
u32 bmp;
bmp = u32x16_is_equal_mask (u32x16_load_unaligned (data), splat);
if (bmp != pow2_mask (16))
return count + count_trailing_zeros (~bmp);
data += 16;
count += 16;
}
if (count == max_count)
return count;
else
{
u32 mask = pow2_mask (max_count - count);
u32 bmp =
u32x16_is_equal_mask (u32x16_mask_load_zero (data, mask), splat) &
mask;
return count + count_trailing_zeros (~bmp);
}
#elif defined(CLIB_HAVE_VEC256)
u32x8 splat = u32x8_splat (first);
while (count + 7 < max_count)
{
u32 bmp;
#ifdef __AVX512F__
bmp = u32x8_is_equal_mask (u32x8_load_unaligned (data), splat);
if (bmp != pow2_mask (8))
return count + count_trailing_zeros (~bmp);
#else
bmp = u8x32_msb_mask ((u8x32) (u32x8_load_unaligned (data) == splat));
if (bmp != 0xffffffff)
return count + count_trailing_zeros (~bmp) / 4;
#endif
data += 8;
count += 8;
}
if (count == max_count)
return count;
#if defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
else
{
u32 mask = pow2_mask (max_count - count);
u32 bmp =
u32x8_is_equal_mask (u32x8_mask_load_zero (data, mask), splat) & mask;
return count + count_trailing_zeros (~bmp);
}
#endif
#elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK)
u32x4 splat = u32x4_splat (first);
while (count + 3 < max_count)
{
u64 bmp;
bmp = u8x16_msb_mask ((u8x16) (u32x4_load_unaligned (data) == splat));
if (bmp != pow2_mask (4 * 4))
{
count += count_trailing_zeros (~bmp) / 4;
return count;
}
data += 4;
count += 4;
}
#else
count += 2;
data += 2;
while (count + 3 < max_count && ((data[0] ^ first) | (data[1] ^ first) |
(data[2] ^ first) | (data[3] ^ first)) == 0)
{
data += 4;
count += 4;
}
#endif
while (count < max_count && (data[0] == first))
{
data += 1;
count += 1;
}
return count;
}
static_always_inline uword
clib_count_equal_u16 (u16 *data, uword max_count)
{
uword count;
u16 first;
if (max_count <= 1)
return max_count;
if (data[0] != data[1])
return 1;
count = 0;
first = data[0];
#if defined(CLIB_HAVE_VEC256)
u16x16 splat = u16x16_splat (first);
while (count + 15 < max_count)
{
u64 bmp;
bmp = u8x32_msb_mask ((u8x32) (u16x16_load_unaligned (data) == splat));
if (bmp != 0xffffffff)
{
count += count_trailing_zeros (~bmp) / 2;
return count;
}
data += 16;
count += 16;
}
#elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK)
u16x8 splat = u16x8_splat (first);
while (count + 7 < max_count)
{
u64 bmp;
bmp = u8x16_msb_mask ((u8x16) (u16x8_load_unaligned (data) == splat));
if (bmp != 0xffff)
{
count += count_trailing_zeros (~bmp) / 2;
return count;
}
data += 8;
count += 8;
}
#else
count += 2;
data += 2;
while (count + 3 < max_count && ((data[0] ^ first) | (data[1] ^ first) |
(data[2] ^ first) | (data[3] ^ first)) == 0)
{
data += 4;
count += 4;
}
#endif
while (count < max_count && (data[0] == first))
{
data += 1;
count += 1;
}
return count;
}
static_always_inline uword
clib_count_equal_u8 (u8 *data, uword max_count)
{
uword count;
u8 first;
if (max_count <= 1)
return max_count;
if (data[0] != data[1])
return 1;
count = 0;
first = data[0];
#if defined(CLIB_HAVE_VEC512)
u8x64 splat = u8x64_splat (first);
while (count + 63 < max_count)
{
u64 bmp;
bmp = u8x64_is_equal_mask (u8x64_load_unaligned (data), splat);
if (bmp != -1)
return count + count_trailing_zeros (~bmp);
data += 64;
count += 64;
}
if (count == max_count)
return count;
#if defined(CLIB_HAVE_VEC512_MASK_LOAD_STORE)
else
{
u64 mask = pow2_mask (max_count - count);
u64 bmp =
u8x64_is_equal_mask (u8x64_mask_load_zero (data, mask), splat) & mask;
return count + count_trailing_zeros (~bmp);
}
#endif
#elif defined(CLIB_HAVE_VEC256)
u8x32 splat = u8x32_splat (first);
while (count + 31 < max_count)
{
u64 bmp;
bmp = u8x32_msb_mask ((u8x32) (u8x32_load_unaligned (data) == splat));
if (bmp != 0xffffffff)
return count + count_trailing_zeros (~bmp);
data += 32;
count += 32;
}
if (count == max_count)
return count;
#if defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
else
{
u32 mask = pow2_mask (max_count - count);
u64 bmp =
u8x32_msb_mask (u8x32_mask_load_zero (data, mask) == splat) & mask;
return count + count_trailing_zeros (~bmp);
}
#endif
#elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK)
u8x16 splat = u8x16_splat (first);
while (count + 15 < max_count)
{
u64 bmp;
bmp = u8x16_msb_mask ((u8x16) (u8x16_load_unaligned (data) == splat));
if (bmp != 0xffff)
return count + count_trailing_zeros (~bmp);
data += 16;
count += 16;
}
#else
count += 2;
data += 2;
while (count + 3 < max_count && ((data[0] ^ first) | (data[1] ^ first) |
(data[2] ^ first) | (data[3] ^ first)) == 0)
{
data += 4;
count += 4;
}
#endif
while (count < max_count && (data[0] == first))
{
data += 1;
count += 1;
}
return count;
}
#endif
|