vppinfra: AArch64 NEON implementation of clib_compare_u16_x64()
[vpp.git] / src / vppinfra / vector_funcs.h
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright(c) 2021 Cisco Systems, Inc.
3  */
4
5 #ifndef included_vector_funcs_h
6 #define included_vector_funcs_h
7 #include <vppinfra/clib.h>
8
9 /** \brief Compare 64 16-bit elemments with provied value and return bitmap
10
11     @param v value to compare elements with
12     @param a array of 64 u16 elements
13     @return u64 bitmap where each bit represents result of comparison
14 */
15
16 static_always_inline u64
17 clib_compare_u16_x64 (u16 v, u16 *a)
18 {
19   u64 mask = 0;
20 #if defined(CLIB_HAVE_VEC512)
21   u16x32 v32 = u16x32_splat (v);
22   u16x32u *av = (u16x32u *) a;
23   mask = ((u64) u16x32_is_equal_mask (av[0], v32) |
24           (u64) u16x32_is_equal_mask (av[1], v32) << 32);
25 #elif defined(CLIB_HAVE_VEC256)
26   u16x16 v16 = u16x16_splat (v);
27   u16x16u *av = (u16x16u *) a;
28   i8x32 x;
29
30   x = i16x16_pack (v16 == av[0], v16 == av[1]);
31   mask = i8x32_msb_mask ((i8x32) u64x4_permute (x, 0, 2, 1, 3));
32   x = i16x16_pack (v16 == av[2], v16 == av[3]);
33   mask |= (u64) i8x32_msb_mask ((i8x32) u64x4_permute (x, 0, 2, 1, 3)) << 32;
34 #elif defined(CLIB_HAVE_VEC128) && defined(__ARM_NEON)
35   u16x8 idx8 = u16x8_splat (v);
36   u16x8 m = { 1, 2, 4, 8, 16, 32, 64, 128 };
37   u16x8u *av = (u16x8u *) a;
38
39   /* compare each u16 elemment with idx8, result gives 0xffff in each element
40      of the resulting vector if comparison result is true.
41      Bitwise AND with m will give us one bit set for true result and offset
42      of that bit represend element index. Finally vaddvq_u16() gives us sum
43      of all elements of the vector which will give us u8 bitmap. */
44
45   mask = ((u64) vaddvq_u16 ((av[0] == idx8) & m) |
46           (u64) vaddvq_u16 ((av[1] == idx8) & m) << 8 |
47           (u64) vaddvq_u16 ((av[2] == idx8) & m) << 16 |
48           (u64) vaddvq_u16 ((av[3] == idx8) & m) << 24 |
49           (u64) vaddvq_u16 ((av[4] == idx8) & m) << 32 |
50           (u64) vaddvq_u16 ((av[5] == idx8) & m) << 40 |
51           (u64) vaddvq_u16 ((av[6] == idx8) & m) << 48 |
52           (u64) vaddvq_u16 ((av[7] == idx8) & m) << 56);
53 #elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK)
54   u16x8 idx8 = u16x8_splat (v);
55   u16x8u *av = (u16x8u *) a;
56   mask =
57     ((u64) i8x16_msb_mask (i16x8_pack (idx8 == av[0], idx8 == av[1])) |
58      (u64) i8x16_msb_mask (i16x8_pack (idx8 == av[2], idx8 == av[3])) << 16 |
59      (u64) i8x16_msb_mask (i16x8_pack (idx8 == av[4], idx8 == av[5])) << 32 |
60      (u64) i8x16_msb_mask (i16x8_pack (idx8 == av[6], idx8 == av[7])) << 48);
61 #else
62   for (int i = 0; i < 64; i++)
63     if (a[i] == v)
64       mask |= 1ULL << i;
65 #endif
66   return mask;
67 }
68
69 #endif