Mohsin Kazmi | 0ec7dad | 2021-07-15 10:34:36 +0000 | [diff] [blame] | 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; |
Mohsin Kazmi | 992d996 | 2021-08-03 17:56:05 +0000 | [diff] [blame] | 40 | src += i; |
Mohsin Kazmi | 0ec7dad | 2021-07-15 10:34:36 +0000 | [diff] [blame] | 41 | #if defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE) |
| 42 | if (n_elts) |
| 43 | { |
| 44 | u8 m = pow2_mask (n_elts); |
Mohsin Kazmi | 992d996 | 2021-08-03 17:56:05 +0000 | [diff] [blame] | 45 | u32x8_mask_store (u32x8_mask_load_zero (src, m) & mask8, src, m); |
Mohsin Kazmi | 0ec7dad | 2021-07-15 10:34:36 +0000 | [diff] [blame] | 46 | } |
| 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 Kazmi | 992d996 | 2021-08-03 17:56:05 +0000 | [diff] [blame] | 55 | src += i; |
Mohsin Kazmi | 0ec7dad | 2021-07-15 10:34:36 +0000 | [diff] [blame] | 56 | 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 |