blob: 2b84cc24869b41a3a7b536d04d14f26ec4224b00 [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
Dave Barach9466c452018-08-24 17:21:14 -040045#if defined (__MMX__) || defined (__IWMMXT__) || defined (__aarch64__) \
46 || defined (__i686__)
Ed Warnickecb9cada2015-12-08 15:45:58 -070047#define CLIB_HAVE_VEC64
48#endif
49
Dave Barach9466c452018-08-24 17:21:14 -040050#if defined (__aarch64__) && defined(__ARM_NEON) || defined (__i686__)
Gabriel Ganneb81831d2017-12-05 17:33:37 +010051#define CLIB_HAVE_VEC128
52#endif
53
Damjan Marion927b0712018-02-20 08:33:50 +010054#if defined (__SSE4_2__) && __GNUC__ >= 4
Ed Warnickecb9cada2015-12-08 15:45:58 -070055#define CLIB_HAVE_VEC128
56#endif
57
58#if defined (__ALTIVEC__)
59#define CLIB_HAVE_VEC128
60#endif
61
Damjan Marionc5766222018-04-16 00:18:34 +020062#if defined (__AVX2__)
Damjan Marionb79d8862017-11-10 20:20:32 +010063#define CLIB_HAVE_VEC256
Damjan Marionc5766222018-04-16 00:18:34 +020064#if defined (__clang__) && __clang_major__ < 4
65#undef CLIB_HAVE_VEC256
66#endif
Damjan Marionb79d8862017-11-10 20:20:32 +010067#endif
68
69#if defined (__AVX512F__)
70#define CLIB_HAVE_VEC512
71#endif
72
Ed Warnickecb9cada2015-12-08 15:45:58 -070073/* 128 implies 64 */
74#ifdef CLIB_HAVE_VEC128
75#define CLIB_HAVE_VEC64
76#endif
77
78#define _vector_size(n) __attribute__ ((vector_size (n)))
79
Damjan Marion6525c7f2018-02-20 12:34:40 +010080#define foreach_vec64i _(i,8,8) _(i,16,4) _(i,32,2)
81#define foreach_vec64u _(u,8,8) _(u,16,4) _(u,32,2)
82#define foreach_vec64f _(f,32,2)
83#define foreach_vec128i _(i,8,16) _(i,16,8) _(i,32,4) _(i,64,2)
84#define foreach_vec128u _(u,8,16) _(u,16,8) _(u,32,4) _(u,64,2)
85#define foreach_vec128f _(f,32,4) _(f,64,2)
86#define foreach_vec256i _(i,8,32) _(i,16,16) _(i,32,8) _(i,64,4)
87#define foreach_vec256u _(u,8,32) _(u,16,16) _(u,32,8) _(u,64,4)
88#define foreach_vec256f _(f,32,8) _(f,64,4)
89#define foreach_vec512i _(i,8,64) _(i,16,32) _(i,32,16) _(i,64,8)
90#define foreach_vec512u _(u,8,64) _(u,16,32) _(u,32,16) _(u,64,8)
91#define foreach_vec512f _(f,32,16) _(f,64,8)
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
Damjan Marion6525c7f2018-02-20 12:34:40 +010093#if defined (CLIB_HAVE_VEC512)
94#define foreach_int_vec foreach_vec64i foreach_vec128i foreach_vec256i foreach_vec512i
95#define foreach_uint_vec foreach_vec64u foreach_vec128u foreach_vec256u foreach_vec512u
96#define foreach_float_vec foreach_vec64f foreach_vec128f foreach_vec256f foreach_vec512f
97#elif defined (CLIB_HAVE_VEC256)
98#define foreach_int_vec foreach_vec64i foreach_vec128i foreach_vec256i
99#define foreach_uint_vec foreach_vec64u foreach_vec128u foreach_vec256u
100#define foreach_float_vec foreach_vec64f foreach_vec128f foreach_vec256f
101#else
102#define foreach_int_vec foreach_vec64i foreach_vec128i
103#define foreach_uint_vec foreach_vec64u foreach_vec128u
104#define foreach_float_vec foreach_vec64f foreach_vec128f
105#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
Damjan Marion6525c7f2018-02-20 12:34:40 +0100107#define foreach_vec foreach_int_vec foreach_uint_vec foreach_float_vec
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
Damjan Marion6525c7f2018-02-20 12:34:40 +0100109/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
Damjan Marion6525c7f2018-02-20 12:34:40 +0100111/* Type Definitions */
112#define _(t,s,c) \
113typedef t##s t##s##x##c _vector_size (s/8*c); \
114typedef union { \
115 t##s##x##c as_##t##s##x##c; \
116 t##s as_##t##s[c]; \
117} t##s##x##c##_union_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
Damjan Marion6525c7f2018-02-20 12:34:40 +0100119 foreach_vec64i foreach_vec64u foreach_vec64f
120 foreach_vec128i foreach_vec128u foreach_vec128f
121 foreach_vec256i foreach_vec256u foreach_vec256f
122 foreach_vec512i foreach_vec512u foreach_vec512f
123#undef _
Damjan Marionb79d8862017-11-10 20:20:32 +0100124
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125/* Vector word sized types. */
126#ifndef CLIB_VECTOR_WORD_BITS
Dave Barachc3799992016-08-15 11:12:27 -0400127#ifdef CLIB_HAVE_VEC128
128#define CLIB_VECTOR_WORD_BITS 128
129#else
130#define CLIB_VECTOR_WORD_BITS 64
131#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132#endif /* CLIB_VECTOR_WORD_BITS */
133
134/* Vector word sized types. */
135#if CLIB_VECTOR_WORD_BITS == 128
Dave Barachc3799992016-08-15 11:12:27 -0400136typedef i8 i8x _vector_size (16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137typedef i16 i16x _vector_size (16);
138typedef i32 i32x _vector_size (16);
139typedef i64 i64x _vector_size (16);
Dave Barachc3799992016-08-15 11:12:27 -0400140typedef u8 u8x _vector_size (16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141typedef u16 u16x _vector_size (16);
142typedef u32 u32x _vector_size (16);
143typedef u64 u64x _vector_size (16);
144#endif
145#if CLIB_VECTOR_WORD_BITS == 64
Dave Barachc3799992016-08-15 11:12:27 -0400146typedef i8 i8x _vector_size (8);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147typedef i16 i16x _vector_size (8);
148typedef i32 i32x _vector_size (8);
149typedef i64 i64x _vector_size (8);
Dave Barachc3799992016-08-15 11:12:27 -0400150typedef u8 u8x _vector_size (8);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151typedef u16 u16x _vector_size (8);
152typedef u32 u32x _vector_size (8);
153typedef u64 u64x _vector_size (8);
154#endif
155
156#undef _vector_size
157
158#define VECTOR_WORD_TYPE(t) t##x
159#define VECTOR_WORD_TYPE_LEN(t) (sizeof (VECTOR_WORD_TYPE(t)) / sizeof (t))
160
Damjan Marion927b0712018-02-20 08:33:50 +0100161#if defined (__SSE4_2__) && __GNUC__ >= 4
162#include <vppinfra/vector_sse42.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163#endif
164
Damjan Marionc5766222018-04-16 00:18:34 +0200165#if defined (__AVX2__)
166#include <vppinfra/vector_avx2.h>
167#endif
168
169#if defined (__AVX512F__)
170#include <vppinfra/vector_avx512.h>
171#endif
172
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173#if defined (__ALTIVEC__)
174#include <vppinfra/vector_altivec.h>
175#endif
176
Christophe Fontaine33e81952016-12-19 14:41:52 +0100177#if defined (__aarch64__)
178#include <vppinfra/vector_neon.h>
179#endif
180
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181#if (defined(CLIB_HAVE_VEC128) || defined(CLIB_HAVE_VEC64))
182#include <vppinfra/vector_funcs.h>
183#endif
184
Damjan Mariona52e1662018-05-19 00:04:23 +0200185/* this macro generate _splat inline functions for each scalar vector type */
186#ifndef CLIB_VEC128_SPLAT_DEFINED
187#define _(t, s, c) \
188 static_always_inline t##s##x##c \
189t##s##x##c##_splat (t##s x) \
190{ \
191 t##s##x##c r; \
192 int i; \
193 \
194 for (i = 0; i < c; i++) \
195 r[i] = x; \
196 \
197 return r; \
198}
199 foreach_vec128i foreach_vec128u
200#undef _
201#endif
202
Damjan Marion6525c7f2018-02-20 12:34:40 +0100203/* *INDENT-ON* */
Dave Barachc3799992016-08-15 11:12:27 -0400204
Damjan Marion6525c7f2018-02-20 12:34:40 +0100205#endif /* included_clib_vector_h */
Dave Barachc3799992016-08-15 11:12:27 -0400206/*
207 * fd.io coding-style-patch-verification: ON
208 *
209 * Local Variables:
210 * eval: (c-set-style "gnu")
211 * End:
212 */