Initial commit of vpp code.
[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 #ifdef CLIB_HAVE_VEC64
65 /* Signed 64 bit. */
66 typedef char i8x8 _vector_size (8);
67 typedef short i16x4 _vector_size (8);
68 typedef int i32x2 _vector_size (8);
69
70 /* Unsigned 64 bit. */
71 typedef unsigned char u8x8 _vector_size (8);
72 typedef unsigned short u16x4 _vector_size (8);
73 typedef unsigned int u32x2 _vector_size (8);
74
75 /* Floating point 64 bit. */
76 typedef float f32x2 _vector_size (8);
77 #endif /* CLIB_HAVE_VEC64 */
78
79 #ifdef CLIB_HAVE_VEC128
80 /* Signed 128 bit. */
81 typedef i8 i8x16 _vector_size (16);
82 typedef i16 i16x8 _vector_size (16);
83 typedef i32 i32x4 _vector_size (16);
84 typedef long long i64x2 _vector_size (16);
85
86 /* Unsigned 128 bit. */
87 typedef u8 u8x16 _vector_size (16);
88 typedef u16 u16x8 _vector_size (16);
89 typedef u32 u32x4 _vector_size (16);
90 typedef u64 u64x2 _vector_size (16);
91
92 typedef f32 f32x4 _vector_size (16);
93 typedef f64 f64x2 _vector_size (16);
94 #endif /* CLIB_HAVE_VEC128 */
95
96 /* Vector word sized types. */
97 #ifndef CLIB_VECTOR_WORD_BITS
98 # ifdef CLIB_HAVE_VEC128
99 #  define CLIB_VECTOR_WORD_BITS 128
100 # else
101 #  define CLIB_VECTOR_WORD_BITS 64
102 # endif
103 #endif /* CLIB_VECTOR_WORD_BITS */
104
105 /* Vector word sized types. */
106 #if CLIB_VECTOR_WORD_BITS == 128
107 typedef  i8  i8x _vector_size (16);
108 typedef i16 i16x _vector_size (16);
109 typedef i32 i32x _vector_size (16);
110 typedef i64 i64x _vector_size (16);
111 typedef  u8  u8x _vector_size (16);
112 typedef u16 u16x _vector_size (16);
113 typedef u32 u32x _vector_size (16);
114 typedef u64 u64x _vector_size (16);
115 #endif
116 #if CLIB_VECTOR_WORD_BITS == 64
117 typedef  i8  i8x _vector_size (8);
118 typedef i16 i16x _vector_size (8);
119 typedef i32 i32x _vector_size (8);
120 typedef i64 i64x _vector_size (8);
121 typedef  u8  u8x _vector_size (8);
122 typedef u16 u16x _vector_size (8);
123 typedef u32 u32x _vector_size (8);
124 typedef u64 u64x _vector_size (8);
125 #endif
126
127 #undef _vector_size
128
129 #define VECTOR_WORD_TYPE(t) t##x
130 #define VECTOR_WORD_TYPE_LEN(t) (sizeof (VECTOR_WORD_TYPE(t)) / sizeof (t))
131
132 /* Union types. */
133 #if (defined(CLIB_HAVE_VEC128) || defined(CLIB_HAVE_VEC64))
134
135 #define _(t)                                    \
136   typedef union {                               \
137     t##x as_##t##x;                             \
138     t as_##t[VECTOR_WORD_TYPE_LEN (t)]; \
139   } t##x##_union_t;
140
141 _ (u8);
142 _ (u16);
143 _ (u32);
144 _ (u64);
145 _ (i8);
146 _ (i16);
147 _ (i32);
148 _ (i64);
149
150 #undef _
151
152 #endif
153
154 #ifdef CLIB_HAVE_VEC64
155
156 #define _(t,n)                                  \
157   typedef union {                               \
158     t##x##n as_##t##x##n;                       \
159     t as_##t[n];                                \
160   } t##x##n##_union_t;                          \
161
162 _ (u8, 8);
163 _ (u16, 4);
164 _ (u32, 2);
165 _ (i8, 8);
166 _ (i16, 4);
167 _ (i32, 2);
168
169 #undef _
170
171 #endif
172
173 #ifdef CLIB_HAVE_VEC128
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, 16);
182 _ (u16, 8);
183 _ (u32, 4);
184 _ (u64, 2);
185 _ (i8, 16);
186 _ (i16, 8);
187 _ (i32, 4);
188 _ (i64, 2);
189 _ (f32, 4);
190 _ (f64, 2);
191
192 #undef _
193
194 #endif
195
196 /* When we don't have vector types, still define e.g. u32x4_union_t but as an array. */
197 #if !defined(CLIB_HAVE_VEC128) && !defined(CLIB_HAVE_VEC64)
198
199 #define _(t,n)                                  \
200   typedef union {                               \
201     t as_##t[n];                                \
202   } t##x##n##_union_t;                          \
203
204 _ (u8, 16);
205 _ (u16, 8);
206 _ (u32, 4);
207 _ (u64, 2);
208 _ (i8, 16);
209 _ (i16, 8);
210 _ (i32, 4);
211 _ (i64, 2);
212
213 #undef _
214
215 #endif
216
217 #if defined (__SSE2__) && __GNUC__ >= 4
218 #include <vppinfra/vector_sse2.h>
219 #endif
220
221 #if defined (__ALTIVEC__)
222 #include <vppinfra/vector_altivec.h>
223 #endif
224
225 #if defined (__IWMMXT__)
226 #include <vppinfra/vector_iwmmxt.h>
227 #endif
228
229 #if (defined(CLIB_HAVE_VEC128) || defined(CLIB_HAVE_VEC64))
230 #include <vppinfra/vector_funcs.h>
231 #endif
232
233 #endif /* included_clib_vector_h */