blob: fcff5e79d954b226a1f8719851b0fd6b530200d2 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Christophe Fontaine33e81952016-12-19 14:41:52 +010045#if defined (__MMX__) || defined (__IWMMXT__) || defined (__aarch64__)
Ed Warnickecb9cada2015-12-08 15:45:58 -070046#define CLIB_HAVE_VEC64
47#endif
48
Gabriel Ganneb81831d2017-12-05 17:33:37 +010049#if defined (__aarch64__) && defined(__ARM_NEON)
50#define CLIB_HAVE_VEC128
51#endif
52
Damjan Marion927b0712018-02-20 08:33:50 +010053#if defined (__SSE4_2__) && __GNUC__ >= 4
Ed Warnickecb9cada2015-12-08 15:45:58 -070054#define CLIB_HAVE_VEC128
55#endif
56
57#if defined (__ALTIVEC__)
58#define CLIB_HAVE_VEC128
59#endif
60
Damjan Marionc5766222018-04-16 00:18:34 +020061#if defined (__AVX2__)
Damjan Marionb79d8862017-11-10 20:20:32 +010062#define CLIB_HAVE_VEC256
Damjan Marionc5766222018-04-16 00:18:34 +020063#if defined (__clang__) && __clang_major__ < 4
64#undef CLIB_HAVE_VEC256
65#endif
Damjan Marionb79d8862017-11-10 20:20:32 +010066#endif
67
68#if defined (__AVX512F__)
69#define CLIB_HAVE_VEC512
70#endif
71
Ed Warnickecb9cada2015-12-08 15:45:58 -070072/* 128 implies 64 */
73#ifdef CLIB_HAVE_VEC128
74#define CLIB_HAVE_VEC64
75#endif
76
77#define _vector_size(n) __attribute__ ((vector_size (n)))
78
Damjan Marion6525c7f2018-02-20 12:34:40 +010079#define foreach_vec64i _(i,8,8) _(i,16,4) _(i,32,2)
80#define foreach_vec64u _(u,8,8) _(u,16,4) _(u,32,2)
81#define foreach_vec64f _(f,32,2)
82#define foreach_vec128i _(i,8,16) _(i,16,8) _(i,32,4) _(i,64,2)
83#define foreach_vec128u _(u,8,16) _(u,16,8) _(u,32,4) _(u,64,2)
84#define foreach_vec128f _(f,32,4) _(f,64,2)
85#define foreach_vec256i _(i,8,32) _(i,16,16) _(i,32,8) _(i,64,4)
86#define foreach_vec256u _(u,8,32) _(u,16,16) _(u,32,8) _(u,64,4)
87#define foreach_vec256f _(f,32,8) _(f,64,4)
88#define foreach_vec512i _(i,8,64) _(i,16,32) _(i,32,16) _(i,64,8)
89#define foreach_vec512u _(u,8,64) _(u,16,32) _(u,32,16) _(u,64,8)
90#define foreach_vec512f _(f,32,16) _(f,64,8)
Ed Warnickecb9cada2015-12-08 15:45:58 -070091
Damjan Marion6525c7f2018-02-20 12:34:40 +010092#if defined (CLIB_HAVE_VEC512)
93#define foreach_int_vec foreach_vec64i foreach_vec128i foreach_vec256i foreach_vec512i
94#define foreach_uint_vec foreach_vec64u foreach_vec128u foreach_vec256u foreach_vec512u
95#define foreach_float_vec foreach_vec64f foreach_vec128f foreach_vec256f foreach_vec512f
96#elif defined (CLIB_HAVE_VEC256)
97#define foreach_int_vec foreach_vec64i foreach_vec128i foreach_vec256i
98#define foreach_uint_vec foreach_vec64u foreach_vec128u foreach_vec256u
99#define foreach_float_vec foreach_vec64f foreach_vec128f foreach_vec256f
100#else
101#define foreach_int_vec foreach_vec64i foreach_vec128i
102#define foreach_uint_vec foreach_vec64u foreach_vec128u
103#define foreach_float_vec foreach_vec64f foreach_vec128f
104#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105
Damjan Marion6525c7f2018-02-20 12:34:40 +0100106#define foreach_vec foreach_int_vec foreach_uint_vec foreach_float_vec
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107
Damjan Marion6525c7f2018-02-20 12:34:40 +0100108/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
Damjan Marion6525c7f2018-02-20 12:34:40 +0100110/* Type Definitions */
111#define _(t,s,c) \
112typedef t##s t##s##x##c _vector_size (s/8*c); \
113typedef union { \
114 t##s##x##c as_##t##s##x##c; \
115 t##s as_##t##s[c]; \
116} t##s##x##c##_union_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117
Damjan Marion6525c7f2018-02-20 12:34:40 +0100118 foreach_vec64i foreach_vec64u foreach_vec64f
119 foreach_vec128i foreach_vec128u foreach_vec128f
120 foreach_vec256i foreach_vec256u foreach_vec256f
121 foreach_vec512i foreach_vec512u foreach_vec512f
122#undef _
Damjan Marionb79d8862017-11-10 20:20:32 +0100123
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124/* Vector word sized types. */
125#ifndef CLIB_VECTOR_WORD_BITS
Dave Barachc3799992016-08-15 11:12:27 -0400126#ifdef CLIB_HAVE_VEC128
127#define CLIB_VECTOR_WORD_BITS 128
128#else
129#define CLIB_VECTOR_WORD_BITS 64
130#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131#endif /* CLIB_VECTOR_WORD_BITS */
132
133/* Vector word sized types. */
134#if CLIB_VECTOR_WORD_BITS == 128
Dave Barachc3799992016-08-15 11:12:27 -0400135typedef i8 i8x _vector_size (16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136typedef i16 i16x _vector_size (16);
137typedef i32 i32x _vector_size (16);
138typedef i64 i64x _vector_size (16);
Dave Barachc3799992016-08-15 11:12:27 -0400139typedef u8 u8x _vector_size (16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140typedef u16 u16x _vector_size (16);
141typedef u32 u32x _vector_size (16);
142typedef u64 u64x _vector_size (16);
143#endif
144#if CLIB_VECTOR_WORD_BITS == 64
Dave Barachc3799992016-08-15 11:12:27 -0400145typedef i8 i8x _vector_size (8);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146typedef i16 i16x _vector_size (8);
147typedef i32 i32x _vector_size (8);
148typedef i64 i64x _vector_size (8);
Dave Barachc3799992016-08-15 11:12:27 -0400149typedef u8 u8x _vector_size (8);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150typedef u16 u16x _vector_size (8);
151typedef u32 u32x _vector_size (8);
152typedef u64 u64x _vector_size (8);
153#endif
154
155#undef _vector_size
156
157#define VECTOR_WORD_TYPE(t) t##x
158#define VECTOR_WORD_TYPE_LEN(t) (sizeof (VECTOR_WORD_TYPE(t)) / sizeof (t))
159
Damjan Marion6525c7f2018-02-20 12:34:40 +0100160/* this series of macros generate _is_equal, _is_greater, _is_zero, _add
161 and _sub inline funcitons for each vector type */
162#define _(t, s, c) \
163 static_always_inline t##s##x##c \
164t##s##x##c##_is_equal (t##s##x##c v1, t##s##x##c v2) \
165{ return (v1 == v2); } \
166 \
167static_always_inline t##s##x##c \
168t##s##x##c##_is_greater (t##s##x##c v1, t##s##x##c v2) \
169{ return (v1 > v2); } \
170 \
171static_always_inline t##s##x##c \
172t##s##x##c##_is_zero (t##s##x##c v1) \
173{ t##s##x##c z = {0}; return (v1 == z); } \
174 \
175static_always_inline t##s##x##c \
176t##s##x##c##_add (t##s##x##c v1, t##s##x##c v2) \
177{ return (v1 + v2); } \
178 \
179static_always_inline t##s##x##c \
180t##s##x##c##_sub (t##s##x##c v1, t##s##x##c v2) \
181{ return (v1 - v2); }
182 foreach_vec
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183#undef _
184
Damjan Marionc5766222018-04-16 00:18:34 +0200185/* this macro generate _splat inline functions for each scalar vector type */
Damjan Marion6525c7f2018-02-20 12:34:40 +0100186#define _(t, s, c) \
187 static_always_inline t##s##x##c \
188t##s##x##c##_splat (t##s x) \
189{ \
190 t##s##x##c r; \
191 int i; \
192 \
193 for (i = 0; i < c; i++) \
194 r[i] = x; \
195 \
196 return r; \
197}
Damjan Marionc5766222018-04-16 00:18:34 +0200198 foreach_vec128i foreach_vec128u
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199#undef _
200
Damjan Marion927b0712018-02-20 08:33:50 +0100201#if defined (__SSE4_2__) && __GNUC__ >= 4
202#include <vppinfra/vector_sse42.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203#endif
204
Damjan Marionc5766222018-04-16 00:18:34 +0200205#if defined (__AVX2__)
206#include <vppinfra/vector_avx2.h>
207#endif
208
209#if defined (__AVX512F__)
210#include <vppinfra/vector_avx512.h>
211#endif
212
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213#if defined (__ALTIVEC__)
214#include <vppinfra/vector_altivec.h>
215#endif
216
Christophe Fontaine33e81952016-12-19 14:41:52 +0100217#if defined (__aarch64__)
218#include <vppinfra/vector_neon.h>
219#endif
220
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221#if (defined(CLIB_HAVE_VEC128) || defined(CLIB_HAVE_VEC64))
222#include <vppinfra/vector_funcs.h>
223#endif
224
Damjan Marion6525c7f2018-02-20 12:34:40 +0100225/* *INDENT-ON* */
Dave Barachc3799992016-08-15 11:12:27 -0400226
Damjan Marion6525c7f2018-02-20 12:34:40 +0100227#endif /* included_clib_vector_h */
Dave Barachc3799992016-08-15 11:12:27 -0400228/*
229 * fd.io coding-style-patch-verification: ON
230 *
231 * Local Variables:
232 * eval: (c-set-style "gnu")
233 * End:
234 */