vppinfra: add array mask func
[vpp.git] / src / vppinfra / vector / array_mask.h
1 /* 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
17 static_always_inline void
18 clib_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;
40 #if defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
41   if (n_elts)
42     {
43       u8 m = pow2_mask (n_elts);
44       u32x8_mask_store (u32x8_mask_load_zero (src + i, m) & mask8, src + i, m);
45     }
46   return;
47 #endif
48 #elif defined(CLIB_HAVE_VEC128)
49   u32x4 mask4 = u32x4_splat (mask);
50
51   for (i = 0; i + 4 <= n_elts; i += 4)
52     *((u32x4u *) (src + i)) &= mask4;
53   n_elts -= i;
54   switch (n_elts)
55     {
56     case 3:
57       src[2] &= mask;
58     case 2:
59       src[1] &= mask;
60     case 1:
61       src[0] &= mask;
62     case 0:
63     default:;
64     }
65   return;
66 #endif
67   while (n_elts > 0)
68     {
69       src[0] &= mask;
70       src++;
71       n_elts--;
72     }
73 }
74
75 #endif