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