blob: 778ed3e638f0c07f07d9fc2608914d7041b98fe0 [file] [log] [blame]
Mohsin Kazmi0ec7dad2021-07-15 10:34:36 +00001/* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2021 Cisco Systems, Inc.
3 */
4
5#ifndef included_vector_array_mask_h
6#define included_vector_array_mask_h
7#include <vppinfra/clib.h>
8
9/** \brief Mask array of 32-bit elemments
10
11 @param src source array of u32 elements
12 @param mask use to mask the values of source array
13 @param n_elts number of elements in the source array
14 @return masked values are return in source array
15*/
16
17static_always_inline void
18clib_array_mask_u32 (u32 *src, u32 mask, u32 n_elts)
19{
20 u32 i;
21#if defined(CLIB_HAVE_VEC512)
22 u32x16 mask16 = u32x16_splat (mask);
23
24 for (i = 0; i + 16 <= n_elts; i += 16)
25 *((u32x16u *) (src + i)) &= mask16;
26 n_elts -= i;
27 if (n_elts)
28 {
29 u16 m = pow2_mask (n_elts);
30 u32x16_mask_store (u32x16_mask_load_zero (src + i, m) & mask16, src + i,
31 m);
32 }
33 return;
34#elif defined(CLIB_HAVE_VEC256)
35 u32x8 mask8 = u32x8_splat (mask);
36
37 for (i = 0; i + 8 <= n_elts; i += 8)
38 *((u32x8u *) (src + i)) &= mask8;
39 n_elts -= i;
Mohsin Kazmi992d9962021-08-03 17:56:05 +000040 src += i;
Mohsin Kazmi0ec7dad2021-07-15 10:34:36 +000041#if defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
42 if (n_elts)
43 {
44 u8 m = pow2_mask (n_elts);
Mohsin Kazmi992d9962021-08-03 17:56:05 +000045 u32x8_mask_store (u32x8_mask_load_zero (src, m) & mask8, src, m);
Mohsin Kazmi0ec7dad2021-07-15 10:34:36 +000046 }
47 return;
48#endif
49#elif defined(CLIB_HAVE_VEC128)
50 u32x4 mask4 = u32x4_splat (mask);
51
52 for (i = 0; i + 4 <= n_elts; i += 4)
53 *((u32x4u *) (src + i)) &= mask4;
54 n_elts -= i;
Mohsin Kazmi992d9962021-08-03 17:56:05 +000055 src += i;
Mohsin Kazmi0ec7dad2021-07-15 10:34:36 +000056 switch (n_elts)
57 {
58 case 3:
59 src[2] &= mask;
60 case 2:
61 src[1] &= mask;
62 case 1:
63 src[0] &= mask;
64 case 0:
65 default:;
66 }
67 return;
68#endif
69 while (n_elts > 0)
70 {
71 src[0] &= mask;
72 src++;
73 n_elts--;
74 }
75}
76
77#endif