add CLIB_HAVE_VEC128 with NEON intrinsics (VPP-1127)
[vpp.git] / src / vppinfra / vector_neon.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef included_vector_neon_h
17 #define included_vector_neon_h
18 #include <arm_neon.h>
19
20 /* Splats. */
21
22 #define u8x16_splat(i) vdupq_n_u8(i)
23 #define u16x8_splat(i) vdupq_n_u16(i)
24 #define i16x8_splat(i) vdupq_n_s16(i)
25 #define u32x4_splat(i) vdupq_n_u32(i)
26 #define i32x4_splat(i) vdupq_n_s32(i)
27
28 /* Arithmetic */
29 #define u16x8_add(a,b) vaddq_u16(a,b)
30 #define i16x8_add(a,b) vaddq_s16(a,b)
31 #define u16x8_sub_saturate(a,b) vsubq_u16(a,b)
32 #define i16x8_sub_saturate(a,b) vsubq_s16(a,b)
33
34
35 /* Compare operations. */
36 #define u8x16_is_equal(a,b) vceqq_u8(a,b)
37 #define i8x16_is_equal(a,b) vceqq_s8(a,b)
38 #define u16x8_is_equal(a,b) vceqq_u16(a,b)
39 #define i16x8_is_equal(a,b) vceqq_i16(a,b)
40 #define u32x4_is_equal(a,b) vceqq_u32(a,b)
41 #define i32x4_is_equal(a,b) vceqq_s32(a,b)
42 #define i8x16_is_greater(a,b) vcgtq_s8(a,b)
43 #define i16x8_is_greater(a,b) vcgtq_u8(a,b)
44 #define i32x4_is_greater(a,b) vcgtq_s32(a,b)
45
46 always_inline u8x16
47 u8x16_is_zero (u8x16 x)
48 {
49   u8x16 zero = { 0 };
50   return u8x16_is_equal (x, zero);
51 }
52
53 always_inline u16x8
54 u16x8_is_zero (u16x8 x)
55 {
56   u16x8 zero = { 0 };
57   return u16x8_is_equal (x, zero);
58 }
59
60 always_inline u32x4
61 u32x4_is_zero (u32x4 x)
62 {
63   u32x4 zero = { 0 };
64   return u32x4_is_equal (x, zero);
65 }
66
67 /* Converts all ones/zeros compare mask to bitmap. */
68 always_inline u32
69 u8x16_compare_byte_mask (u8x16 x)
70 {
71   static int8_t const __attribute__ ((aligned (16))) xr[8] =
72   {
73   -7, -6, -5, -4, -3, -2, -1, 0};
74   uint8x8_t mask_and = vdup_n_u8 (0x80);
75   int8x8_t mask_shift = vld1_s8 (xr);
76
77   uint8x8_t lo = vget_low_u8 (x);
78   uint8x8_t hi = vget_high_u8 (x);
79
80   lo = vand_u8 (lo, mask_and);
81   lo = vshl_u8 (lo, mask_shift);
82
83   hi = vand_u8 (hi, mask_and);
84   hi = vshl_u8 (hi, mask_shift);
85
86   lo = vpadd_u8 (lo, lo);
87   lo = vpadd_u8 (lo, lo);
88   lo = vpadd_u8 (lo, lo);
89
90   hi = vpadd_u8 (hi, hi);
91   hi = vpadd_u8 (hi, hi);
92   hi = vpadd_u8 (hi, hi);
93
94   return ((hi[0] << 8) | (lo[0] & 0xff));
95 }
96
97 always_inline u32
98 u16x8_zero_byte_mask (u16x8 input)
99 {
100   u8x16 vall_one = vdupq_n_u8 (0x0);
101   u8x16 res_values = { 0x01, 0x02, 0x04, 0x08,
102     0x10, 0x20, 0x40, 0x80,
103     0x01, 0x02, 0x04, 0x08,
104     0x10, 0x20, 0x40, 0x80
105   };
106
107   /* input --> [0x80, 0x40, 0x01, 0xf0, ... ] */
108   u8x16 test_result =
109     vreinterpretq_u8_u16 (vceqq_u16 (input, vreinterpretq_u16_u8 (vall_one)));
110   u8x16 before_merge = vminq_u8 (test_result, res_values);
111   /*before_merge--> [0x80, 0x00, 0x00, 0x10, ... ] */
112   /* u8x16 --> [a,b,c,d, e,f,g,h, i,j,k,l, m,n,o,p] */
113   /* pair add until we have 2 uint64_t  */
114   u16x8 merge1 = vpaddlq_u8 (before_merge);
115   /* u16x8-->  [a+b,c+d, e+f,g+h, i+j,k+l, m+n,o+p] */
116   u32x4 merge2 = vpaddlq_u16 (merge1);
117   /* u32x4-->  [a+b+c+d, e+f+g+h, i+j+k+l, m+n+o+p] */
118   u64x2 merge3 = vpaddlq_u32 (merge2);
119   /* u64x2-->  [a+b+c+d+e+f+g+h,  i+j+k+l+m+n+o+p]  */
120   return (u32) (vgetq_lane_u64 (merge3, 1) << 8) + vgetq_lane_u64 (merge3, 0);
121 }
122
123 #endif /* included_vector_neon_h */
124
125 /*
126  * fd.io coding-style-patch-verification: ON
127  *
128  * Local Variables:
129  * eval: (c-set-style "gnu")
130  * End:
131  */