58fc808abccd98c00462117d033cdecf55998509
[vpp.git] / vppinfra / vppinfra / vector.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   Copyright (c) 2005 Eliot Dresselhaus
17
18   Permission is hereby granted, free of charge, to any person obtaining
19   a copy of this software and associated documentation files (the
20   "Software"), to deal in the Software without restriction, including
21   without limitation the rights to use, copy, modify, merge, publish,
22   distribute, sublicense, and/or sell copies of the Software, and to
23   permit persons to whom the Software is furnished to do so, subject to
24   the following conditions:
25
26   The above copyright notice and this permission notice shall be
27   included in all copies or substantial portions of the Software.
28
29   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37
38 #ifndef included_clib_vector_h
39 #define included_clib_vector_h
40
41 #include <vppinfra/clib.h>
42
43 /* Vector types. */
44
45 #if defined (__MMX__) || defined (__IWMMXT__)
46 #define CLIB_HAVE_VEC64
47 #endif
48
49 #if defined (__SSE2__) && __GNUC__ >= 4
50 #define CLIB_HAVE_VEC128
51 #endif
52
53 #if defined (__ALTIVEC__)
54 #define CLIB_HAVE_VEC128
55 #endif
56
57 /* 128 implies 64 */
58 #ifdef CLIB_HAVE_VEC128
59 #define CLIB_HAVE_VEC64
60 #endif
61
62 #define _vector_size(n) __attribute__ ((vector_size (n)))
63
64 #if defined (__aarch64__) || defined (__arm__)
65 typedef unsigned int u32x4 _vector_size (16);
66 #endif
67
68 #ifdef CLIB_HAVE_VEC64
69 /* Signed 64 bit. */
70 typedef char i8x8 _vector_size (8);
71 typedef short i16x4 _vector_size (8);
72 typedef int i32x2 _vector_size (8);
73
74 /* Unsigned 64 bit. */
75 typedef unsigned char u8x8 _vector_size (8);
76 typedef unsigned short u16x4 _vector_size (8);
77 typedef unsigned int u32x2 _vector_size (8);
78
79 /* Floating point 64 bit. */
80 typedef float f32x2 _vector_size (8);
81 #endif /* CLIB_HAVE_VEC64 */
82
83 #ifdef CLIB_HAVE_VEC128
84 /* Signed 128 bit. */
85 typedef i8 i8x16 _vector_size (16);
86 typedef i16 i16x8 _vector_size (16);
87 typedef i32 i32x4 _vector_size (16);
88 typedef long long i64x2 _vector_size (16);
89
90 /* Unsigned 128 bit. */
91 typedef u8 u8x16 _vector_size (16);
92 typedef u16 u16x8 _vector_size (16);
93 typedef u32 u32x4 _vector_size (16);
94 typedef u64 u64x2 _vector_size (16);
95
96 typedef f32 f32x4 _vector_size (16);
97 typedef f64 f64x2 _vector_size (16);
98
99 /* Signed 256 bit. */
100 typedef i8 i8x32 _vector_size (32);
101 typedef i16 i16x16 _vector_size (32);
102 typedef i32 i32x8 _vector_size (32);
103 typedef long long i64x4 _vector_size (32);
104
105 /* Unsigned 256 bit. */
106 typedef u8 u8x32 _vector_size (32);
107 typedef u16 u16x16 _vector_size (32);
108 typedef u32 u32x8 _vector_size (32);
109 typedef u64 u64x4 _vector_size (32);
110
111 typedef f32 f32x8 _vector_size (32);
112 typedef f64 f64x4 _vector_size (32);
113 #endif /* CLIB_HAVE_VEC128 */
114
115 /* Vector word sized types. */
116 #ifndef CLIB_VECTOR_WORD_BITS
117 #ifdef CLIB_HAVE_VEC128
118 #define CLIB_VECTOR_WORD_BITS 128
119 #else
120 #define CLIB_VECTOR_WORD_BITS 64
121 #endif
122 #endif /* CLIB_VECTOR_WORD_BITS */
123
124 /* Vector word sized types. */
125 #if CLIB_VECTOR_WORD_BITS == 128
126 typedef i8 i8x _vector_size (16);
127 typedef i16 i16x _vector_size (16);
128 typedef i32 i32x _vector_size (16);
129 typedef i64 i64x _vector_size (16);
130 typedef u8 u8x _vector_size (16);
131 typedef u16 u16x _vector_size (16);
132 typedef u32 u32x _vector_size (16);
133 typedef u64 u64x _vector_size (16);
134 #endif
135 #if CLIB_VECTOR_WORD_BITS == 64
136 typedef i8 i8x _vector_size (8);
137 typedef i16 i16x _vector_size (8);
138 typedef i32 i32x _vector_size (8);
139 typedef i64 i64x _vector_size (8);
140 typedef u8 u8x _vector_size (8);
141 typedef u16 u16x _vector_size (8);
142 typedef u32 u32x _vector_size (8);
143 typedef u64 u64x _vector_size (8);
144 #endif
145
146 #undef _vector_size
147
148 #define VECTOR_WORD_TYPE(t) t##x
149 #define VECTOR_WORD_TYPE_LEN(t) (sizeof (VECTOR_WORD_TYPE(t)) / sizeof (t))
150
151 /* Union types. */
152 #if (defined(CLIB_HAVE_VEC128) || defined(CLIB_HAVE_VEC64))
153
154 #define _(t)                                    \
155   typedef union {                               \
156     t##x as_##t##x;                             \
157     t as_##t[VECTOR_WORD_TYPE_LEN (t)]; \
158   } t##x##_union_t;
159
160 _(u8);
161 _(u16);
162 _(u32);
163 _(u64);
164 _(i8);
165 _(i16);
166 _(i32);
167 _(i64);
168
169 #undef _
170
171 #endif
172
173 #ifdef CLIB_HAVE_VEC64
174
175 #define _(t,n)                                  \
176   typedef union {                               \
177     t##x##n as_##t##x##n;                       \
178     t as_##t[n];                                \
179   } t##x##n##_union_t;                          \
180
181 _(u8, 8);
182 _(u16, 4);
183 _(u32, 2);
184 _(i8, 8);
185 _(i16, 4);
186 _(i32, 2);
187
188 #undef _
189
190 #endif
191
192 #ifdef CLIB_HAVE_VEC128
193
194 #define _(t,n)                                  \
195   typedef union {                               \
196     t##x##n as_##t##x##n;                       \
197     t as_##t[n];                                \
198   } t##x##n##_union_t;                          \
199
200 _(u8, 16);
201 _(u16, 8);
202 _(u32, 4);
203 _(u64, 2);
204 _(i8, 16);
205 _(i16, 8);
206 _(i32, 4);
207 _(i64, 2);
208 _(f32, 4);
209 _(f64, 2);
210
211 #undef _
212
213 #endif
214
215 /* When we don't have vector types, still define e.g. u32x4_union_t but as an array. */
216 #if !defined(CLIB_HAVE_VEC128) && !defined(CLIB_HAVE_VEC64)
217
218 #define _(t,n)                                  \
219   typedef union {                               \
220     t as_##t[n];                                \
221   } t##x##n##_union_t;                          \
222
223 _(u8, 16);
224 _(u16, 8);
225 _(u32, 4);
226 _(u64, 2);
227 _(i8, 16);
228 _(i16, 8);
229 _(i32, 4);
230 _(i64, 2);
231
232 #undef _
233
234 #endif
235
236 #if defined (__SSE2__) && __GNUC__ >= 4
237 #include <vppinfra/vector_sse2.h>
238 #endif
239
240 #if defined (__ALTIVEC__)
241 #include <vppinfra/vector_altivec.h>
242 #endif
243
244 #if defined (__IWMMXT__)
245 #include <vppinfra/vector_iwmmxt.h>
246 #endif
247
248 #if (defined(CLIB_HAVE_VEC128) || defined(CLIB_HAVE_VEC64))
249 #include <vppinfra/vector_funcs.h>
250 #endif
251
252 #endif /* included_clib_vector_h */
253
254 /*
255  * fd.io coding-style-patch-verification: ON
256  *
257  * Local Variables:
258  * eval: (c-set-style "gnu")
259  * End:
260  */