Damjan Marion | c576622 | 2018-04-16 00:18:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | #ifndef included_vector_avx2_h |
| 17 | #define included_vector_avx2_h |
| 18 | |
| 19 | #include <vppinfra/clib.h> |
| 20 | #include <x86intrin.h> |
| 21 | |
Damjan Marion | a52e166 | 2018-05-19 00:04:23 +0200 | [diff] [blame^] | 22 | /* *INDENT-OFF* */ |
Damjan Marion | c576622 | 2018-04-16 00:18:34 +0200 | [diff] [blame] | 23 | #define foreach_avx2_vec256i \ |
| 24 | _(i,8,32,epi8) _(i,16,16,epi16) _(i,32,8,epi32) _(i,64,4,epi64x) |
| 25 | #define foreach_avx2_vec256u \ |
| 26 | _(u,8,32,epi8) _(u,16,16,epi16) _(u,32,8,epi32) _(u,64,4,epi64x) |
| 27 | #define foreach_avx2_vec256f \ |
| 28 | _(f,32,8,ps) _(f,64,4,pd) |
| 29 | |
Damjan Marion | a52e166 | 2018-05-19 00:04:23 +0200 | [diff] [blame^] | 30 | /* splat, load_unaligned, store_unaligned, is_all_zero, is_equal, |
| 31 | is_all_equal */ |
Damjan Marion | c576622 | 2018-04-16 00:18:34 +0200 | [diff] [blame] | 32 | #define _(t, s, c, i) \ |
| 33 | static_always_inline t##s##x##c \ |
| 34 | t##s##x##c##_splat (t##s x) \ |
| 35 | { return (t##s##x##c) _mm256_set1_##i (x); } \ |
| 36 | \ |
| 37 | static_always_inline t##s##x##c \ |
| 38 | t##s##x##c##_load_unaligned (void *p) \ |
| 39 | { return (t##s##x##c) _mm256_loadu_si256 (p); } \ |
| 40 | \ |
| 41 | static_always_inline void \ |
| 42 | t##s##x##c##_store_unaligned (t##s##x##c v, void *p) \ |
| 43 | { _mm256_storeu_si256 ((__m256i *) p, (__m256i) v); } \ |
| 44 | \ |
| 45 | static_always_inline int \ |
| 46 | t##s##x##c##_is_all_zero (t##s##x##c x) \ |
| 47 | { return _mm256_testz_si256 ((__m256i) x, (__m256i) x); } \ |
| 48 | \ |
| 49 | static_always_inline int \ |
Damjan Marion | a52e166 | 2018-05-19 00:04:23 +0200 | [diff] [blame^] | 50 | t##s##x##c##_is_equal (t##s##x##c x, t##s##x##c y) \ |
| 51 | { return _mm256_testc_si256 ((__m256i) x, (__m256i) y); } \ |
Damjan Marion | c576622 | 2018-04-16 00:18:34 +0200 | [diff] [blame] | 52 | \ |
Damjan Marion | a52e166 | 2018-05-19 00:04:23 +0200 | [diff] [blame^] | 53 | static_always_inline int \ |
| 54 | t##s##x##c##_is_all_equal (t##s##x##c v, t##s x) \ |
| 55 | { return t##s##x##c##_is_equal (v, t##s##x##c##_splat (x)); }; \ |
Damjan Marion | c576622 | 2018-04-16 00:18:34 +0200 | [diff] [blame] | 56 | |
| 57 | foreach_avx2_vec256i foreach_avx2_vec256u |
| 58 | #undef _ |
Damjan Marion | a52e166 | 2018-05-19 00:04:23 +0200 | [diff] [blame^] | 59 | /* *INDENT-ON* */ |
| 60 | |
| 61 | always_inline u32x8 |
Damjan Marion | c576622 | 2018-04-16 00:18:34 +0200 | [diff] [blame] | 62 | u32x8_permute (u32x8 v, u32x8 idx) |
| 63 | { |
| 64 | return (u32x8) _mm256_permutevar8x32_epi32 ((__m256i) v, (__m256i) idx); |
| 65 | } |
| 66 | |
| 67 | always_inline u32x4 |
| 68 | u32x8_extract_lo (u32x8 v) |
| 69 | { |
| 70 | return (u32x4) _mm256_extracti128_si256 ((__m256i) v, 0); |
| 71 | } |
| 72 | |
| 73 | always_inline u32x4 |
| 74 | u32x8_extract_hi (u32x8 v) |
| 75 | { |
| 76 | return (u32x4) _mm256_extracti128_si256 ((__m256i) v, 1); |
| 77 | } |
| 78 | |
Damjan Marion | ee7f0bd | 2018-05-05 12:30:28 +0200 | [diff] [blame] | 79 | always_inline u32x8 |
| 80 | u32x8_insert_lo (u32x8 v1, u32x4 v2) |
| 81 | { |
| 82 | return (u32x8) _mm256_inserti128_si256 ((__m256i) v1, (__m128i) v2, 0); |
| 83 | } |
| 84 | |
| 85 | always_inline u32x8 |
| 86 | u32x8_insert_hi (u32x8 v1, u32x4 v2) |
| 87 | { |
| 88 | return (u32x8) _mm256_inserti128_si256 ((__m256i) v1, (__m128i) v2, 1); |
| 89 | } |
| 90 | |
Damjan Marion | 8c3f8a2 | 2018-05-17 21:12:13 +0200 | [diff] [blame] | 91 | static_always_inline u32 |
| 92 | u8x32_msb_mask (u8x32 v) |
| 93 | { |
| 94 | return _mm256_movemask_epi8 ((__m256i) v); |
| 95 | } |
| 96 | |
Damjan Marion | afe56de | 2018-05-17 12:44:00 +0200 | [diff] [blame] | 97 | /* _extend_to_ */ |
| 98 | /* *INDENT-OFF* */ |
| 99 | #define _(f,t,i) \ |
| 100 | static_always_inline t \ |
| 101 | f##_extend_to_##t (f x) \ |
| 102 | { return (t) _mm256_cvt##i ((__m128i) x); } |
| 103 | |
| 104 | _(u16x8, u32x8, epu16_epi32) |
| 105 | _(u16x8, u64x4, epu16_epi64) |
| 106 | _(u32x4, u64x4, epu32_epi64) |
| 107 | _(u8x16, u16x16, epu8_epi64) |
| 108 | _(u8x16, u32x8, epu8_epi32) |
| 109 | _(u8x16, u64x4, epu8_epi64) |
| 110 | _(i16x8, i32x8, epi16_epi32) |
| 111 | _(i16x8, i64x4, epi16_epi64) |
| 112 | _(i32x4, i64x4, epi32_epi64) |
| 113 | _(i8x16, i16x16, epi8_epi64) |
| 114 | _(i8x16, i32x8, epi8_epi32) |
| 115 | _(i8x16, i64x4, epi8_epi64) |
| 116 | #undef _ |
| 117 | /* *INDENT-ON* */ |
| 118 | |
Damjan Marion | c576622 | 2018-04-16 00:18:34 +0200 | [diff] [blame] | 119 | #endif /* included_vector_avx2_h */ |
| 120 | |
| 121 | /* |
| 122 | * fd.io coding-style-patch-verification: ON |
| 123 | * |
| 124 | * Local Variables: |
| 125 | * eval: (c-set-style "gnu") |
| 126 | * End: |
| 127 | */ |