blob: 6a6635b4c9314c16d04b641e44b882c14118c123 [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 (__aarch64__) && defined(__ARM_NEON) || defined (__i686__)
Gabriel Ganneb81831d2017-12-05 17:33:37 +010046#define CLIB_HAVE_VEC128
47#endif
48
Damjan Marion927b0712018-02-20 08:33:50 +010049#if defined (__SSE4_2__) && __GNUC__ >= 4
Ed Warnickecb9cada2015-12-08 15:45:58 -070050#define CLIB_HAVE_VEC128
51#endif
52
53#if defined (__ALTIVEC__)
54#define CLIB_HAVE_VEC128
55#endif
56
Damjan Marionc5766222018-04-16 00:18:34 +020057#if defined (__AVX2__)
Damjan Marionb79d8862017-11-10 20:20:32 +010058#define CLIB_HAVE_VEC256
Damjan Marionc5766222018-04-16 00:18:34 +020059#if defined (__clang__) && __clang_major__ < 4
60#undef CLIB_HAVE_VEC256
61#endif
Damjan Marionb79d8862017-11-10 20:20:32 +010062#endif
63
Damjan Marion162330f2020-04-29 21:28:15 +020064#if defined (__AVX512BITALG__)
Damjan Marionb79d8862017-11-10 20:20:32 +010065#define CLIB_HAVE_VEC512
66#endif
67
Ed Warnickecb9cada2015-12-08 15:45:58 -070068#define _vector_size(n) __attribute__ ((vector_size (n)))
Damjan Marion83049332019-09-25 00:25:36 +020069#define _vector_size_unaligned(n) __attribute__ ((vector_size (n), __aligned__ (1)))
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
Damjan Marion6525c7f2018-02-20 12:34:40 +010071#define foreach_vec64i _(i,8,8) _(i,16,4) _(i,32,2)
72#define foreach_vec64u _(u,8,8) _(u,16,4) _(u,32,2)
73#define foreach_vec64f _(f,32,2)
74#define foreach_vec128i _(i,8,16) _(i,16,8) _(i,32,4) _(i,64,2)
75#define foreach_vec128u _(u,8,16) _(u,16,8) _(u,32,4) _(u,64,2)
76#define foreach_vec128f _(f,32,4) _(f,64,2)
77#define foreach_vec256i _(i,8,32) _(i,16,16) _(i,32,8) _(i,64,4)
78#define foreach_vec256u _(u,8,32) _(u,16,16) _(u,32,8) _(u,64,4)
79#define foreach_vec256f _(f,32,8) _(f,64,4)
80#define foreach_vec512i _(i,8,64) _(i,16,32) _(i,32,16) _(i,64,8)
81#define foreach_vec512u _(u,8,64) _(u,16,32) _(u,32,16) _(u,64,8)
82#define foreach_vec512f _(f,32,16) _(f,64,8)
Ed Warnickecb9cada2015-12-08 15:45:58 -070083
Damjan Marion6525c7f2018-02-20 12:34:40 +010084#if defined (CLIB_HAVE_VEC512)
85#define foreach_int_vec foreach_vec64i foreach_vec128i foreach_vec256i foreach_vec512i
86#define foreach_uint_vec foreach_vec64u foreach_vec128u foreach_vec256u foreach_vec512u
87#define foreach_float_vec foreach_vec64f foreach_vec128f foreach_vec256f foreach_vec512f
88#elif defined (CLIB_HAVE_VEC256)
89#define foreach_int_vec foreach_vec64i foreach_vec128i foreach_vec256i
90#define foreach_uint_vec foreach_vec64u foreach_vec128u foreach_vec256u
91#define foreach_float_vec foreach_vec64f foreach_vec128f foreach_vec256f
92#else
93#define foreach_int_vec foreach_vec64i foreach_vec128i
94#define foreach_uint_vec foreach_vec64u foreach_vec128u
95#define foreach_float_vec foreach_vec64f foreach_vec128f
96#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
Damjan Marion6525c7f2018-02-20 12:34:40 +010098#define foreach_vec foreach_int_vec foreach_uint_vec foreach_float_vec
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
Damjan Marion6525c7f2018-02-20 12:34:40 +0100100/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101
Damjan Marion6525c7f2018-02-20 12:34:40 +0100102/* Type Definitions */
103#define _(t,s,c) \
104typedef t##s t##s##x##c _vector_size (s/8*c); \
Damjan Marion83049332019-09-25 00:25:36 +0200105typedef t##s t##s##x##c##u _vector_size_unaligned (s/8*c); \
Damjan Marion6525c7f2018-02-20 12:34:40 +0100106typedef union { \
107 t##s##x##c as_##t##s##x##c; \
108 t##s as_##t##s[c]; \
109} t##s##x##c##_union_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
Damjan Marion6525c7f2018-02-20 12:34:40 +0100111 foreach_vec64i foreach_vec64u foreach_vec64f
112 foreach_vec128i foreach_vec128u foreach_vec128f
113 foreach_vec256i foreach_vec256u foreach_vec256f
114 foreach_vec512i foreach_vec512u foreach_vec512f
115#undef _
Damjan Marionb79d8862017-11-10 20:20:32 +0100116
Damjan Marion5c00ec22020-07-16 19:19:29 +0200117/* universal inlines */
118#define _(t, s, c) \
119static_always_inline t##s##x##c \
120t##s##x##c##_zero () \
121{ return (t##s##x##c) {}; } \
122
123foreach_vec
124#undef _
125
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126#undef _vector_size
127
128#define VECTOR_WORD_TYPE(t) t##x
129#define VECTOR_WORD_TYPE_LEN(t) (sizeof (VECTOR_WORD_TYPE(t)) / sizeof (t))
130
Damjan Marion927b0712018-02-20 08:33:50 +0100131#if defined (__SSE4_2__) && __GNUC__ >= 4
132#include <vppinfra/vector_sse42.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133#endif
134
Damjan Marionc5766222018-04-16 00:18:34 +0200135#if defined (__AVX2__)
136#include <vppinfra/vector_avx2.h>
137#endif
138
Damjan Marion09aeee62021-04-20 21:28:45 +0200139#if defined(__AVX512F__)
Damjan Marionc5766222018-04-16 00:18:34 +0200140#include <vppinfra/vector_avx512.h>
141#endif
142
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143#if defined (__ALTIVEC__)
144#include <vppinfra/vector_altivec.h>
145#endif
146
Christophe Fontaine33e81952016-12-19 14:41:52 +0100147#if defined (__aarch64__)
148#include <vppinfra/vector_neon.h>
149#endif
150
Damjan Mariona52e1662018-05-19 00:04:23 +0200151/* this macro generate _splat inline functions for each scalar vector type */
152#ifndef CLIB_VEC128_SPLAT_DEFINED
153#define _(t, s, c) \
154 static_always_inline t##s##x##c \
155t##s##x##c##_splat (t##s x) \
156{ \
157 t##s##x##c r; \
158 int i; \
159 \
160 for (i = 0; i < c; i++) \
161 r[i] = x; \
162 \
163 return r; \
164}
165 foreach_vec128i foreach_vec128u
166#undef _
167#endif
168
Damjan Marion6525c7f2018-02-20 12:34:40 +0100169/* *INDENT-ON* */
Dave Barachc3799992016-08-15 11:12:27 -0400170
Damjan Marion6525c7f2018-02-20 12:34:40 +0100171#endif /* included_clib_vector_h */
Dave Barachc3799992016-08-15 11:12:27 -0400172/*
173 * fd.io coding-style-patch-verification: ON
174 *
175 * Local Variables:
176 * eval: (c-set-style "gnu")
177 * End:
178 */