blob: 49bc2976b3ee23429a2967465b2564dcf53bbf67 [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
Damjan Marion56f54af2021-10-12 20:30:02 +020068#define _vector_size(n) __attribute__ ((vector_size (n), __may_alias__))
69#define _vector_size_unaligned(n) \
70 __attribute__ ((vector_size (n), __aligned__ (1), __may_alias__))
Ed Warnickecb9cada2015-12-08 15:45:58 -070071
Damjan Marion6525c7f2018-02-20 12:34:40 +010072#define foreach_vec64i _(i,8,8) _(i,16,4) _(i,32,2)
73#define foreach_vec64u _(u,8,8) _(u,16,4) _(u,32,2)
74#define foreach_vec64f _(f,32,2)
75#define foreach_vec128i _(i,8,16) _(i,16,8) _(i,32,4) _(i,64,2)
76#define foreach_vec128u _(u,8,16) _(u,16,8) _(u,32,4) _(u,64,2)
77#define foreach_vec128f _(f,32,4) _(f,64,2)
78#define foreach_vec256i _(i,8,32) _(i,16,16) _(i,32,8) _(i,64,4)
79#define foreach_vec256u _(u,8,32) _(u,16,16) _(u,32,8) _(u,64,4)
80#define foreach_vec256f _(f,32,8) _(f,64,4)
81#define foreach_vec512i _(i,8,64) _(i,16,32) _(i,32,16) _(i,64,8)
82#define foreach_vec512u _(u,8,64) _(u,16,32) _(u,32,16) _(u,64,8)
83#define foreach_vec512f _(f,32,16) _(f,64,8)
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
Damjan Marion6525c7f2018-02-20 12:34:40 +010085#if defined (CLIB_HAVE_VEC512)
86#define foreach_int_vec foreach_vec64i foreach_vec128i foreach_vec256i foreach_vec512i
87#define foreach_uint_vec foreach_vec64u foreach_vec128u foreach_vec256u foreach_vec512u
88#define foreach_float_vec foreach_vec64f foreach_vec128f foreach_vec256f foreach_vec512f
89#elif defined (CLIB_HAVE_VEC256)
90#define foreach_int_vec foreach_vec64i foreach_vec128i foreach_vec256i
91#define foreach_uint_vec foreach_vec64u foreach_vec128u foreach_vec256u
92#define foreach_float_vec foreach_vec64f foreach_vec128f foreach_vec256f
93#else
94#define foreach_int_vec foreach_vec64i foreach_vec128i
95#define foreach_uint_vec foreach_vec64u foreach_vec128u
96#define foreach_float_vec foreach_vec64f foreach_vec128f
97#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
Damjan Marion6525c7f2018-02-20 12:34:40 +010099#define foreach_vec foreach_int_vec foreach_uint_vec foreach_float_vec
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
Damjan Marion6525c7f2018-02-20 12:34:40 +0100101/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
Damjan Marion6525c7f2018-02-20 12:34:40 +0100103/* Type Definitions */
104#define _(t,s,c) \
105typedef t##s t##s##x##c _vector_size (s/8*c); \
Damjan Marion83049332019-09-25 00:25:36 +0200106typedef t##s t##s##x##c##u _vector_size_unaligned (s/8*c); \
Damjan Marion6525c7f2018-02-20 12:34:40 +0100107typedef union { \
108 t##s##x##c as_##t##s##x##c; \
109 t##s as_##t##s[c]; \
110} t##s##x##c##_union_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111
Damjan Marion6525c7f2018-02-20 12:34:40 +0100112 foreach_vec64i foreach_vec64u foreach_vec64f
113 foreach_vec128i foreach_vec128u foreach_vec128f
114 foreach_vec256i foreach_vec256u foreach_vec256f
115 foreach_vec512i foreach_vec512u foreach_vec512f
116#undef _
Damjan Marionb79d8862017-11-10 20:20:32 +0100117
Damjan Marion5c00ec22020-07-16 19:19:29 +0200118/* universal inlines */
119#define _(t, s, c) \
120static_always_inline t##s##x##c \
121t##s##x##c##_zero () \
122{ return (t##s##x##c) {}; } \
123
124foreach_vec
125#undef _
126
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127#undef _vector_size
128
Damjan Marionefd6de82021-12-02 13:02:38 +0100129 /* _shuffle and _shuffle2 */
130#if defined(__GNUC__) && !defined(__clang__)
131#define __builtin_shufflevector(v1, v2, ...) \
132 __builtin_shuffle ((v1), (v2), (__typeof__ (v1)){ __VA_ARGS__ })
133#endif
134
135#define u8x16_shuffle(v1, ...) \
136 (u8x16) __builtin_shufflevector ((u8x16) (v1), (u8x16) (v1), __VA_ARGS__)
137#define u8x32_shuffle(v1, ...) \
138 (u8x32) __builtin_shufflevector ((u8x32) (v1), (u8x32) (v1), __VA_ARGS__)
139#define u8x64_shuffle(v1, ...) \
140 (u8x64) __builtin_shufflevector ((u8x64) (v1), (u8x64) (v1), __VA_ARGS__)
141
142#define u16x8_shuffle(v1, ...) \
143 (u16x8) __builtin_shufflevector ((u16x8) (v1), (u16x8) (v1), __VA_ARGS__)
144#define u16x16_shuffle(v1, ...) \
145 (u16x16) __builtin_shufflevector ((u16x16) (v1), (u16x16) (v1), __VA_ARGS__)
146#define u16x32_shuffle(v1, ...) \
147 (u16u32) __builtin_shufflevector ((u16x32) (v1), (u16x32) (v1), __VA_ARGS__);
148
149#define u32x4_shuffle(v1, ...) \
150 (u32x4) __builtin_shufflevector ((u32x4) (v1), (u32x4) (v1), __VA_ARGS__)
151#define u32x8_shuffle(v1, ...) \
152 (u32x8) __builtin_shufflevector ((u32x8) (v1), (u32x8) (v1), __VA_ARGS__)
153#define u32x16_shuffle(v1, ...) \
154 (u32x16) __builtin_shufflevector ((u32x16) (v1), (u32x16) (v1), __VA_ARGS__)
155
156#define u64x2_shuffle(v1, ...) \
157 (u64x2) __builtin_shufflevector ((u64x2) (v1), (u64x2) (v1), __VA_ARGS__)
158#define u64x4_shuffle(v1, ...) \
159 (u64x4) __builtin_shufflevector ((u64x4) (v1), (u64x4) (v1), __VA_ARGS__)
160#define u64x8_shuffle(v1, ...) \
161 (u64x8) __builtin_shufflevector ((u64x8) (v1), (u64x8) (v1), __VA_ARGS__)
162
163#define u8x16_shuffle2(v1, v2, ...) \
164 (u8x16) __builtin_shufflevector ((u8x16) (v1), (u8x16) (v2), __VA_ARGS__)
165#define u8x32_shuffle2(v1, v2, ...) \
166 (u8x32) __builtin_shufflevector ((u8x32) (v1), (u8x32) (v2), __VA_ARGS__)
167#define u8x64_shuffle2(v1, v2, ...) \
168 (u8x64) __builtin_shufflevector ((u8x64) (v1), (u8x64) (v2), __VA_ARGS__)
169
170#define u16x8_shuffle2(v1, v2, ...) \
171 (u16x8) __builtin_shufflevector ((u16x8) (v1), (u16x8) (v2), __VA_ARGS__)
172#define u16x16_shuffle2(v1, v2, ...) \
173 (u16x16) __builtin_shufflevector ((u16x16) (v1), (u16x16) (v2), __VA_ARGS__)
174#define u16x32_shuffle2(v1, v2, ...) \
175 (u16u32) __builtin_shufflevector ((u16x32) (v1), (u16x32) (v2), __VA_ARGS__);
176
177#define u32x4_shuffle2(v1, v2, ...) \
178 (u32x4) __builtin_shufflevector ((u32x4) (v1), (u32x4) (v2), __VA_ARGS__)
179#define u32x8_shuffle2(v1, v2, ...) \
180 (u32x8) __builtin_shufflevector ((u32x8) (v1), (u32x8) (v2), __VA_ARGS__)
181#define u32x16_shuffle2(v1, v2, ...) \
182 (u32x16) __builtin_shufflevector ((u32x16) (v1), (u32x16) (v2), __VA_ARGS__)
183
184#define u64x2_shuffle2(v1, v2, ...) \
185 (u64x2) __builtin_shufflevector ((u64x2) (v1), (u64x2) (v2), __VA_ARGS__)
186#define u64x4_shuffle2(v1, v2, ...) \
187 (u64x4) __builtin_shufflevector ((u64x4) (v1), (u64x4) (v2), __VA_ARGS__)
188#define u64x8_shuffle2(v1, v2, ...) \
189 (u64x8) __builtin_shufflevector ((u64x8) (v1), (u64x8) (v2), __VA_ARGS__)
190
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191#define VECTOR_WORD_TYPE(t) t##x
192#define VECTOR_WORD_TYPE_LEN(t) (sizeof (VECTOR_WORD_TYPE(t)) / sizeof (t))
193
Damjan Marion927b0712018-02-20 08:33:50 +0100194#if defined (__SSE4_2__) && __GNUC__ >= 4
195#include <vppinfra/vector_sse42.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196#endif
197
Damjan Marionc5766222018-04-16 00:18:34 +0200198#if defined (__AVX2__)
199#include <vppinfra/vector_avx2.h>
200#endif
201
Damjan Marion09aeee62021-04-20 21:28:45 +0200202#if defined(__AVX512F__)
Damjan Marionc5766222018-04-16 00:18:34 +0200203#include <vppinfra/vector_avx512.h>
204#endif
205
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206#if defined (__ALTIVEC__)
207#include <vppinfra/vector_altivec.h>
208#endif
209
Christophe Fontaine33e81952016-12-19 14:41:52 +0100210#if defined (__aarch64__)
211#include <vppinfra/vector_neon.h>
212#endif
213
Damjan Mariona52e1662018-05-19 00:04:23 +0200214/* this macro generate _splat inline functions for each scalar vector type */
215#ifndef CLIB_VEC128_SPLAT_DEFINED
216#define _(t, s, c) \
217 static_always_inline t##s##x##c \
218t##s##x##c##_splat (t##s x) \
219{ \
220 t##s##x##c r; \
221 int i; \
222 \
223 for (i = 0; i < c; i++) \
224 r[i] = x; \
225 \
226 return r; \
227}
228 foreach_vec128i foreach_vec128u
229#undef _
230#endif
231
Damjan Marion6525c7f2018-02-20 12:34:40 +0100232/* *INDENT-ON* */
Dave Barachc3799992016-08-15 11:12:27 -0400233
Damjan Marion6525c7f2018-02-20 12:34:40 +0100234#endif /* included_clib_vector_h */
Dave Barachc3799992016-08-15 11:12:27 -0400235/*
236 * fd.io coding-style-patch-verification: ON
237 *
238 * Local Variables:
239 * eval: (c-set-style "gnu")
240 * End:
241 */