Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 45 | #if defined (__MMX__) || defined (__IWMMXT__) || defined (__aarch64__) \ |
| 46 | || defined (__i686__) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | #define CLIB_HAVE_VEC64 |
| 48 | #endif |
| 49 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 50 | #if defined (__aarch64__) && defined(__ARM_NEON) || defined (__i686__) |
Gabriel Ganne | b81831d | 2017-12-05 17:33:37 +0100 | [diff] [blame] | 51 | #define CLIB_HAVE_VEC128 |
| 52 | #endif |
| 53 | |
Damjan Marion | 927b071 | 2018-02-20 08:33:50 +0100 | [diff] [blame] | 54 | #if defined (__SSE4_2__) && __GNUC__ >= 4 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | #define CLIB_HAVE_VEC128 |
| 56 | #endif |
| 57 | |
| 58 | #if defined (__ALTIVEC__) |
| 59 | #define CLIB_HAVE_VEC128 |
| 60 | #endif |
| 61 | |
Damjan Marion | c576622 | 2018-04-16 00:18:34 +0200 | [diff] [blame] | 62 | #if defined (__AVX2__) |
Damjan Marion | b79d886 | 2017-11-10 20:20:32 +0100 | [diff] [blame] | 63 | #define CLIB_HAVE_VEC256 |
Damjan Marion | c576622 | 2018-04-16 00:18:34 +0200 | [diff] [blame] | 64 | #if defined (__clang__) && __clang_major__ < 4 |
| 65 | #undef CLIB_HAVE_VEC256 |
| 66 | #endif |
Damjan Marion | b79d886 | 2017-11-10 20:20:32 +0100 | [diff] [blame] | 67 | #endif |
| 68 | |
Damjan Marion | 162330f | 2020-04-29 21:28:15 +0200 | [diff] [blame] | 69 | #if defined (__AVX512BITALG__) |
Damjan Marion | b79d886 | 2017-11-10 20:20:32 +0100 | [diff] [blame] | 70 | #define CLIB_HAVE_VEC512 |
| 71 | #endif |
| 72 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | /* 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))) |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 79 | #define _vector_size_unaligned(n) __attribute__ ((vector_size (n), __aligned__ (1))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | |
Damjan Marion | 6525c7f | 2018-02-20 12:34:40 +0100 | [diff] [blame] | 81 | #define foreach_vec64i _(i,8,8) _(i,16,4) _(i,32,2) |
| 82 | #define foreach_vec64u _(u,8,8) _(u,16,4) _(u,32,2) |
| 83 | #define foreach_vec64f _(f,32,2) |
| 84 | #define foreach_vec128i _(i,8,16) _(i,16,8) _(i,32,4) _(i,64,2) |
| 85 | #define foreach_vec128u _(u,8,16) _(u,16,8) _(u,32,4) _(u,64,2) |
| 86 | #define foreach_vec128f _(f,32,4) _(f,64,2) |
| 87 | #define foreach_vec256i _(i,8,32) _(i,16,16) _(i,32,8) _(i,64,4) |
| 88 | #define foreach_vec256u _(u,8,32) _(u,16,16) _(u,32,8) _(u,64,4) |
| 89 | #define foreach_vec256f _(f,32,8) _(f,64,4) |
| 90 | #define foreach_vec512i _(i,8,64) _(i,16,32) _(i,32,16) _(i,64,8) |
| 91 | #define foreach_vec512u _(u,8,64) _(u,16,32) _(u,32,16) _(u,64,8) |
| 92 | #define foreach_vec512f _(f,32,16) _(f,64,8) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | |
Damjan Marion | 6525c7f | 2018-02-20 12:34:40 +0100 | [diff] [blame] | 94 | #if defined (CLIB_HAVE_VEC512) |
| 95 | #define foreach_int_vec foreach_vec64i foreach_vec128i foreach_vec256i foreach_vec512i |
| 96 | #define foreach_uint_vec foreach_vec64u foreach_vec128u foreach_vec256u foreach_vec512u |
| 97 | #define foreach_float_vec foreach_vec64f foreach_vec128f foreach_vec256f foreach_vec512f |
| 98 | #elif defined (CLIB_HAVE_VEC256) |
| 99 | #define foreach_int_vec foreach_vec64i foreach_vec128i foreach_vec256i |
| 100 | #define foreach_uint_vec foreach_vec64u foreach_vec128u foreach_vec256u |
| 101 | #define foreach_float_vec foreach_vec64f foreach_vec128f foreach_vec256f |
| 102 | #else |
| 103 | #define foreach_int_vec foreach_vec64i foreach_vec128i |
| 104 | #define foreach_uint_vec foreach_vec64u foreach_vec128u |
| 105 | #define foreach_float_vec foreach_vec64f foreach_vec128f |
| 106 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | |
Damjan Marion | 6525c7f | 2018-02-20 12:34:40 +0100 | [diff] [blame] | 108 | #define foreach_vec foreach_int_vec foreach_uint_vec foreach_float_vec |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | |
Damjan Marion | 6525c7f | 2018-02-20 12:34:40 +0100 | [diff] [blame] | 110 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | |
Damjan Marion | 6525c7f | 2018-02-20 12:34:40 +0100 | [diff] [blame] | 112 | /* Type Definitions */ |
| 113 | #define _(t,s,c) \ |
| 114 | typedef t##s t##s##x##c _vector_size (s/8*c); \ |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 115 | typedef t##s t##s##x##c##u _vector_size_unaligned (s/8*c); \ |
Damjan Marion | 6525c7f | 2018-02-20 12:34:40 +0100 | [diff] [blame] | 116 | typedef union { \ |
| 117 | t##s##x##c as_##t##s##x##c; \ |
| 118 | t##s as_##t##s[c]; \ |
| 119 | } t##s##x##c##_union_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | |
Damjan Marion | 6525c7f | 2018-02-20 12:34:40 +0100 | [diff] [blame] | 121 | foreach_vec64i foreach_vec64u foreach_vec64f |
| 122 | foreach_vec128i foreach_vec128u foreach_vec128f |
| 123 | foreach_vec256i foreach_vec256u foreach_vec256f |
| 124 | foreach_vec512i foreach_vec512u foreach_vec512f |
| 125 | #undef _ |
Damjan Marion | b79d886 | 2017-11-10 20:20:32 +0100 | [diff] [blame] | 126 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | /* Vector word sized types. */ |
| 128 | #ifndef CLIB_VECTOR_WORD_BITS |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 129 | #ifdef CLIB_HAVE_VEC128 |
| 130 | #define CLIB_VECTOR_WORD_BITS 128 |
| 131 | #else |
| 132 | #define CLIB_VECTOR_WORD_BITS 64 |
| 133 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | #endif /* CLIB_VECTOR_WORD_BITS */ |
| 135 | |
| 136 | /* Vector word sized types. */ |
| 137 | #if CLIB_VECTOR_WORD_BITS == 128 |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 138 | typedef i8 i8x _vector_size (16); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | typedef i16 i16x _vector_size (16); |
| 140 | typedef i32 i32x _vector_size (16); |
| 141 | typedef i64 i64x _vector_size (16); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 142 | typedef u8 u8x _vector_size (16); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | typedef u16 u16x _vector_size (16); |
| 144 | typedef u32 u32x _vector_size (16); |
| 145 | typedef u64 u64x _vector_size (16); |
| 146 | #endif |
| 147 | #if CLIB_VECTOR_WORD_BITS == 64 |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 148 | typedef i8 i8x _vector_size (8); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | typedef i16 i16x _vector_size (8); |
| 150 | typedef i32 i32x _vector_size (8); |
| 151 | typedef i64 i64x _vector_size (8); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 152 | typedef u8 u8x _vector_size (8); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | typedef u16 u16x _vector_size (8); |
| 154 | typedef u32 u32x _vector_size (8); |
| 155 | typedef u64 u64x _vector_size (8); |
| 156 | #endif |
| 157 | |
Damjan Marion | 5c00ec2 | 2020-07-16 19:19:29 +0200 | [diff] [blame] | 158 | /* universal inlines */ |
| 159 | #define _(t, s, c) \ |
| 160 | static_always_inline t##s##x##c \ |
| 161 | t##s##x##c##_zero () \ |
| 162 | { return (t##s##x##c) {}; } \ |
| 163 | |
| 164 | foreach_vec |
| 165 | #undef _ |
| 166 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | #undef _vector_size |
| 168 | |
| 169 | #define VECTOR_WORD_TYPE(t) t##x |
| 170 | #define VECTOR_WORD_TYPE_LEN(t) (sizeof (VECTOR_WORD_TYPE(t)) / sizeof (t)) |
| 171 | |
Damjan Marion | 927b071 | 2018-02-20 08:33:50 +0100 | [diff] [blame] | 172 | #if defined (__SSE4_2__) && __GNUC__ >= 4 |
| 173 | #include <vppinfra/vector_sse42.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | #endif |
| 175 | |
Damjan Marion | c576622 | 2018-04-16 00:18:34 +0200 | [diff] [blame] | 176 | #if defined (__AVX2__) |
| 177 | #include <vppinfra/vector_avx2.h> |
| 178 | #endif |
| 179 | |
Damjan Marion | 162330f | 2020-04-29 21:28:15 +0200 | [diff] [blame] | 180 | #if defined (__AVX512BITALG__) |
| 181 | /* Due to power level transition issues, we don't preffer AVX-512 on |
| 182 | Skylake X and CascadeLake CPUs, AVX512BITALG is introduced on |
| 183 | icelake CPUs */ |
Damjan Marion | c576622 | 2018-04-16 00:18:34 +0200 | [diff] [blame] | 184 | #include <vppinfra/vector_avx512.h> |
| 185 | #endif |
| 186 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | #if defined (__ALTIVEC__) |
| 188 | #include <vppinfra/vector_altivec.h> |
| 189 | #endif |
| 190 | |
Christophe Fontaine | 33e8195 | 2016-12-19 14:41:52 +0100 | [diff] [blame] | 191 | #if defined (__aarch64__) |
| 192 | #include <vppinfra/vector_neon.h> |
| 193 | #endif |
| 194 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | #if (defined(CLIB_HAVE_VEC128) || defined(CLIB_HAVE_VEC64)) |
| 196 | #include <vppinfra/vector_funcs.h> |
| 197 | #endif |
| 198 | |
Damjan Marion | a52e166 | 2018-05-19 00:04:23 +0200 | [diff] [blame] | 199 | /* this macro generate _splat inline functions for each scalar vector type */ |
| 200 | #ifndef CLIB_VEC128_SPLAT_DEFINED |
| 201 | #define _(t, s, c) \ |
| 202 | static_always_inline t##s##x##c \ |
| 203 | t##s##x##c##_splat (t##s x) \ |
| 204 | { \ |
| 205 | t##s##x##c r; \ |
| 206 | int i; \ |
| 207 | \ |
| 208 | for (i = 0; i < c; i++) \ |
| 209 | r[i] = x; \ |
| 210 | \ |
| 211 | return r; \ |
| 212 | } |
| 213 | foreach_vec128i foreach_vec128u |
| 214 | #undef _ |
| 215 | #endif |
| 216 | |
Damjan Marion | 6525c7f | 2018-02-20 12:34:40 +0100 | [diff] [blame] | 217 | /* *INDENT-ON* */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 218 | |
Damjan Marion | 6525c7f | 2018-02-20 12:34:40 +0100 | [diff] [blame] | 219 | #endif /* included_clib_vector_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 220 | /* |
| 221 | * fd.io coding-style-patch-verification: ON |
| 222 | * |
| 223 | * Local Variables: |
| 224 | * eval: (c-set-style "gnu") |
| 225 | * End: |
| 226 | */ |