blob: 2b02d9eb3013acb48cb95859dafd24142c6ceff4 [file] [log] [blame]
Damjan Marionef0bac72021-04-22 18:08:28 +02001/* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2021 Cisco Systems, Inc.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004
5#ifndef included_vector_funcs_h
6#define included_vector_funcs_h
Damjan Marionef0bac72021-04-22 18:08:28 +02007#include <vppinfra/clib.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -07008
Damjan Marionef0bac72021-04-22 18:08:28 +02009/** \brief Compare 64 16-bit elemments with provied value and return bitmap
Ed Warnickecb9cada2015-12-08 15:45:58 -070010
Damjan Marionef0bac72021-04-22 18:08:28 +020011 @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
16static_always_inline u64
17clib_compare_u16_x64 (u16 v, u16 *a)
18{
19 u64 mask = 0;
Damjan Marion3295ddf2021-04-28 19:31:22 +020020#if defined(CLIB_HAVE_VEC512)
Damjan Marionef0bac72021-04-22 18:08:28 +020021 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;
Damjan Marion3295ddf2021-04-28 19:31:22 +020034#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);
Damjan Marionef0bac72021-04-22 18:08:28 +020053#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;
Ed Warnickecb9cada2015-12-08 15:45:58 -070065#endif
Damjan Marionef0bac72021-04-22 18:08:28 +020066 return mask;
67}
Ed Warnickecb9cada2015-12-08 15:45:58 -070068
Ed Warnickecb9cada2015-12-08 15:45:58 -070069#endif