Damjan Marion | c576622 | 2018-04-16 00:18:34 +0200 | [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 | #ifndef included_vector_avx512_h |
| 17 | #define included_vector_avx512_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_avx512_vec512i \ |
| 24 | _(i,8,64,epi8) _(i,16,32,epi16) _(i,32,16,epi32) _(i,64,8,epi64) |
| 25 | #define foreach_avx512_vec512u \ |
| 26 | _(u,8,64,epi8) _(u,16,32,epi16) _(u,32,16,epi32) _(u,64,8,epi64) |
| 27 | #define foreach_avx512_vec512f \ |
| 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, |
Damjan Marion | 1cf9a16 | 2018-05-23 20:21:51 +0200 | [diff] [blame] | 31 | is_all_equal, is_zero_mask */ |
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) _mm512_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) _mm512_loadu_si512 (p); } \ |
| 40 | \ |
| 41 | static_always_inline void \ |
| 42 | t##s##x##c##_store_unaligned (t##s##x##c v, void *p) \ |
| 43 | { _mm512_storeu_si512 ((__m512i *) p, (__m512i) v); } \ |
| 44 | \ |
Damjan Marion | a52e166 | 2018-05-19 00:04:23 +0200 | [diff] [blame] | 45 | static_always_inline int \ |
| 46 | t##s##x##c##_is_all_zero (t##s##x##c v) \ |
| 47 | { return (_mm512_test_epi64_mask ((__m512i) v, (__m512i) v) == 0); } \ |
| 48 | \ |
| 49 | static_always_inline int \ |
| 50 | t##s##x##c##_is_equal (t##s##x##c a, t##s##x##c b) \ |
Damjan Marion | 1486477 | 2018-05-22 14:07:47 +0200 | [diff] [blame] | 51 | { return t##s##x##c##_is_all_zero (a ^ b); } \ |
Damjan Marion | a52e166 | 2018-05-19 00:04:23 +0200 | [diff] [blame] | 52 | \ |
| 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 | 1cf9a16 | 2018-05-23 20:21:51 +0200 | [diff] [blame] | 56 | \ |
| 57 | static_always_inline u##c \ |
| 58 | t##s##x##c##_is_zero_mask (t##s##x##c v) \ |
| 59 | { return _mm512_test_##i##_mask ((__m512i) v, (__m512i) v); } \ |
| 60 | |
Damjan Marion | c576622 | 2018-04-16 00:18:34 +0200 | [diff] [blame] | 61 | |
| 62 | foreach_avx512_vec512i foreach_avx512_vec512u |
| 63 | #undef _ |
Damjan Marion | 8c3f8a2 | 2018-05-17 21:12:13 +0200 | [diff] [blame] | 64 | /* *INDENT-ON* */ |
| 65 | |
| 66 | static_always_inline u32 |
| 67 | u16x32_msb_mask (u16x32 v) |
| 68 | { |
| 69 | return (u32) _mm512_movepi16_mask ((__m512i) v); |
| 70 | } |
| 71 | |
Damjan Marion | 4e08316 | 2019-04-12 17:44:35 +0200 | [diff] [blame^] | 72 | |
| 73 | static_always_inline void |
| 74 | u32x16_transpose (u32x16 m[16]) |
| 75 | { |
| 76 | __m512i r[16], a, b, c, d, x, y; |
| 77 | |
| 78 | /* *INDENT-OFF* */ |
| 79 | __m512i pm1 = (__m512i) (u64x8) { 0, 1, 8, 9, 4, 5, 12, 13}; |
| 80 | __m512i pm2 = (__m512i) (u64x8) { 2, 3, 10, 11, 6, 7, 14, 15}; |
| 81 | __m512i pm3 = (__m512i) (u64x8) { 0, 1, 2, 3, 8, 9, 10, 11}; |
| 82 | __m512i pm4 = (__m512i) (u64x8) { 4, 5, 6, 7, 12, 13, 14, 15}; |
| 83 | /* *INDENT-ON* */ |
| 84 | |
| 85 | r[0] = _mm512_unpacklo_epi32 ((__m512i) m[0], (__m512i) m[1]); |
| 86 | r[1] = _mm512_unpacklo_epi32 ((__m512i) m[2], (__m512i) m[3]); |
| 87 | r[2] = _mm512_unpacklo_epi32 ((__m512i) m[4], (__m512i) m[5]); |
| 88 | r[3] = _mm512_unpacklo_epi32 ((__m512i) m[6], (__m512i) m[7]); |
| 89 | r[4] = _mm512_unpacklo_epi32 ((__m512i) m[8], (__m512i) m[9]); |
| 90 | r[5] = _mm512_unpacklo_epi32 ((__m512i) m[10], (__m512i) m[11]); |
| 91 | r[6] = _mm512_unpacklo_epi32 ((__m512i) m[12], (__m512i) m[13]); |
| 92 | r[7] = _mm512_unpacklo_epi32 ((__m512i) m[14], (__m512i) m[15]); |
| 93 | |
| 94 | r[8] = _mm512_unpackhi_epi32 ((__m512i) m[0], (__m512i) m[1]); |
| 95 | r[9] = _mm512_unpackhi_epi32 ((__m512i) m[2], (__m512i) m[3]); |
| 96 | r[10] = _mm512_unpackhi_epi32 ((__m512i) m[4], (__m512i) m[5]); |
| 97 | r[11] = _mm512_unpackhi_epi32 ((__m512i) m[6], (__m512i) m[7]); |
| 98 | r[12] = _mm512_unpackhi_epi32 ((__m512i) m[8], (__m512i) m[9]); |
| 99 | r[13] = _mm512_unpackhi_epi32 ((__m512i) m[10], (__m512i) m[11]); |
| 100 | r[14] = _mm512_unpackhi_epi32 ((__m512i) m[12], (__m512i) m[13]); |
| 101 | r[15] = _mm512_unpackhi_epi32 ((__m512i) m[14], (__m512i) m[15]); |
| 102 | |
| 103 | a = _mm512_unpacklo_epi64 (r[0], r[1]); |
| 104 | b = _mm512_unpacklo_epi64 (r[2], r[3]); |
| 105 | c = _mm512_unpacklo_epi64 (r[4], r[5]); |
| 106 | d = _mm512_unpacklo_epi64 (r[6], r[7]); |
| 107 | x = _mm512_permutex2var_epi64 (a, pm1, b); |
| 108 | y = _mm512_permutex2var_epi64 (c, pm1, d); |
| 109 | m[0] = (u32x16) _mm512_permutex2var_epi64 (x, pm3, y); |
| 110 | m[8] = (u32x16) _mm512_permutex2var_epi64 (x, pm4, y); |
| 111 | x = _mm512_permutex2var_epi64 (a, pm2, b); |
| 112 | y = _mm512_permutex2var_epi64 (c, pm2, d); |
| 113 | m[4] = (u32x16) _mm512_permutex2var_epi64 (x, pm3, y); |
| 114 | m[12] = (u32x16) _mm512_permutex2var_epi64 (x, pm4, y); |
| 115 | |
| 116 | a = _mm512_unpacklo_epi64 (r[8], r[9]); |
| 117 | b = _mm512_unpacklo_epi64 (r[10], r[11]); |
| 118 | c = _mm512_unpacklo_epi64 (r[12], r[13]); |
| 119 | d = _mm512_unpacklo_epi64 (r[14], r[15]); |
| 120 | x = _mm512_permutex2var_epi64 (a, pm1, b); |
| 121 | y = _mm512_permutex2var_epi64 (c, pm1, d); |
| 122 | m[2] = (u32x16) _mm512_permutex2var_epi64 (x, pm3, y); |
| 123 | m[10] = (u32x16) _mm512_permutex2var_epi64 (x, pm4, y); |
| 124 | x = _mm512_permutex2var_epi64 (a, pm2, b); |
| 125 | y = _mm512_permutex2var_epi64 (c, pm2, d); |
| 126 | m[6] = (u32x16) _mm512_permutex2var_epi64 (x, pm3, y); |
| 127 | m[14] = (u32x16) _mm512_permutex2var_epi64 (x, pm4, y); |
| 128 | |
| 129 | a = _mm512_unpackhi_epi64 (r[0], r[1]); |
| 130 | b = _mm512_unpackhi_epi64 (r[2], r[3]); |
| 131 | c = _mm512_unpackhi_epi64 (r[4], r[5]); |
| 132 | d = _mm512_unpackhi_epi64 (r[6], r[7]); |
| 133 | x = _mm512_permutex2var_epi64 (a, pm1, b); |
| 134 | y = _mm512_permutex2var_epi64 (c, pm1, d); |
| 135 | m[1] = (u32x16) _mm512_permutex2var_epi64 (x, pm3, y); |
| 136 | m[9] = (u32x16) _mm512_permutex2var_epi64 (x, pm4, y); |
| 137 | x = _mm512_permutex2var_epi64 (a, pm2, b); |
| 138 | y = _mm512_permutex2var_epi64 (c, pm2, d); |
| 139 | m[5] = (u32x16) _mm512_permutex2var_epi64 (x, pm3, y); |
| 140 | m[13] = (u32x16) _mm512_permutex2var_epi64 (x, pm4, y); |
| 141 | |
| 142 | a = _mm512_unpackhi_epi64 (r[8], r[9]); |
| 143 | b = _mm512_unpackhi_epi64 (r[10], r[11]); |
| 144 | c = _mm512_unpackhi_epi64 (r[12], r[13]); |
| 145 | d = _mm512_unpackhi_epi64 (r[14], r[15]); |
| 146 | x = _mm512_permutex2var_epi64 (a, pm1, b); |
| 147 | y = _mm512_permutex2var_epi64 (c, pm1, d); |
| 148 | m[3] = (u32x16) _mm512_permutex2var_epi64 (x, pm3, y); |
| 149 | m[11] = (u32x16) _mm512_permutex2var_epi64 (x, pm4, y); |
| 150 | x = _mm512_permutex2var_epi64 (a, pm2, b); |
| 151 | y = _mm512_permutex2var_epi64 (c, pm2, d); |
| 152 | m[7] = (u32x16) _mm512_permutex2var_epi64 (x, pm3, y); |
| 153 | m[15] = (u32x16) _mm512_permutex2var_epi64 (x, pm4, y); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | |
| 158 | static_always_inline void |
| 159 | u64x8_transpose (u64x8 m[8]) |
| 160 | { |
| 161 | __m512i r[8], x, y; |
| 162 | |
| 163 | /* *INDENT-OFF* */ |
| 164 | __m512i pm1 = (__m512i) (u64x8) { 0, 1, 8, 9, 4, 5, 12, 13}; |
| 165 | __m512i pm2 = (__m512i) (u64x8) { 2, 3, 10, 11, 6, 7, 14, 15}; |
| 166 | __m512i pm3 = (__m512i) (u64x8) { 0, 1, 2, 3, 8, 9, 10, 11}; |
| 167 | __m512i pm4 = (__m512i) (u64x8) { 4, 5, 6, 7, 12, 13, 14, 15}; |
| 168 | /* *INDENT-ON* */ |
| 169 | |
| 170 | r[0] = _mm512_unpacklo_epi64 ((__m512i) m[0], (__m512i) m[1]); |
| 171 | r[1] = _mm512_unpacklo_epi64 ((__m512i) m[2], (__m512i) m[3]); |
| 172 | r[2] = _mm512_unpacklo_epi64 ((__m512i) m[4], (__m512i) m[5]); |
| 173 | r[3] = _mm512_unpacklo_epi64 ((__m512i) m[6], (__m512i) m[7]); |
| 174 | r[4] = _mm512_unpackhi_epi64 ((__m512i) m[0], (__m512i) m[1]); |
| 175 | r[5] = _mm512_unpackhi_epi64 ((__m512i) m[2], (__m512i) m[3]); |
| 176 | r[6] = _mm512_unpackhi_epi64 ((__m512i) m[4], (__m512i) m[5]); |
| 177 | r[7] = _mm512_unpackhi_epi64 ((__m512i) m[6], (__m512i) m[7]); |
| 178 | |
| 179 | x = _mm512_permutex2var_epi64 (r[0], pm1, r[1]); |
| 180 | y = _mm512_permutex2var_epi64 (r[2], pm1, r[3]); |
| 181 | m[0] = (u64x8) _mm512_permutex2var_epi64 (x, pm3, y); |
| 182 | m[4] = (u64x8) _mm512_permutex2var_epi64 (x, pm4, y); |
| 183 | x = _mm512_permutex2var_epi64 (r[0], pm2, r[1]); |
| 184 | y = _mm512_permutex2var_epi64 (r[2], pm2, r[3]); |
| 185 | m[2] = (u64x8) _mm512_permutex2var_epi64 (x, pm3, y); |
| 186 | m[6] = (u64x8) _mm512_permutex2var_epi64 (x, pm4, y); |
| 187 | |
| 188 | x = _mm512_permutex2var_epi64 (r[4], pm1, r[5]); |
| 189 | y = _mm512_permutex2var_epi64 (r[6], pm1, r[7]); |
| 190 | m[1] = (u64x8) _mm512_permutex2var_epi64 (x, pm3, y); |
| 191 | m[5] = (u64x8) _mm512_permutex2var_epi64 (x, pm4, y); |
| 192 | x = _mm512_permutex2var_epi64 (r[4], pm2, r[5]); |
| 193 | y = _mm512_permutex2var_epi64 (r[6], pm2, r[7]); |
| 194 | m[3] = (u64x8) _mm512_permutex2var_epi64 (x, pm3, y); |
| 195 | m[7] = (u64x8) _mm512_permutex2var_epi64 (x, pm4, y); |
| 196 | } |
| 197 | |
Damjan Marion | 8c3f8a2 | 2018-05-17 21:12:13 +0200 | [diff] [blame] | 198 | #endif /* included_vector_avx512_h */ |
Damjan Marion | c576622 | 2018-04-16 00:18:34 +0200 | [diff] [blame] | 199 | /* |
| 200 | * fd.io coding-style-patch-verification: ON |
| 201 | * |
| 202 | * Local Variables: |
| 203 | * eval: (c-set-style "gnu") |
| 204 | * End: |
| 205 | */ |