Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | * BSD LICENSE |
| 17 | * |
| 18 | * Copyright(c) 2010-2014 Intel Corporation. All rights reserved. |
| 19 | * All rights reserved. |
| 20 | * |
| 21 | * Redistribution and use in source and binary forms, with or without |
| 22 | * modification, are permitted provided that the following conditions |
| 23 | * are met: |
| 24 | * |
| 25 | * * Redistributions of source code must retain the above copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * * Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in |
| 29 | * the documentation and/or other materials provided with the |
| 30 | * distribution. |
| 31 | * * Neither the name of Intel Corporation nor the names of its |
| 32 | * contributors may be used to endorse or promote products derived |
| 33 | * from this software without specific prior written permission. |
| 34 | * |
| 35 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 46 | */ |
| 47 | |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 48 | #ifndef included_clib_memcpy_avx512_h |
| 49 | #define included_clib_memcpy_avx512_h |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 50 | |
| 51 | #include <stdint.h> |
| 52 | #include <x86intrin.h> |
| 53 | |
| 54 | static inline void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 55 | clib_mov16 (u8 * dst, const u8 * src) |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 56 | { |
Damjan Marion | 31e59d9 | 2017-07-05 18:15:08 +0200 | [diff] [blame] | 57 | __m128i xmm0; |
| 58 | |
| 59 | xmm0 = _mm_loadu_si128 ((const __m128i *) src); |
| 60 | _mm_storeu_si128 ((__m128i *) dst, xmm0); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static inline void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 64 | clib_mov32 (u8 * dst, const u8 * src) |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 65 | { |
Damjan Marion | 31e59d9 | 2017-07-05 18:15:08 +0200 | [diff] [blame] | 66 | __m256i ymm0; |
| 67 | |
| 68 | ymm0 = _mm256_loadu_si256 ((const __m256i *) src); |
| 69 | _mm256_storeu_si256 ((__m256i *) dst, ymm0); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static inline void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 73 | clib_mov64 (u8 * dst, const u8 * src) |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 74 | { |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 75 | __m512i zmm0; |
| 76 | |
| 77 | zmm0 = _mm512_loadu_si512 ((const void *) src); |
| 78 | _mm512_storeu_si512 ((void *) dst, zmm0); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static inline void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 82 | clib_mov128 (u8 * dst, const u8 * src) |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 83 | { |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 84 | clib_mov64 (dst + 0 * 64, src + 0 * 64); |
| 85 | clib_mov64 (dst + 1 * 64, src + 1 * 64); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static inline void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 89 | clib_mov256 (u8 * dst, const u8 * src) |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 90 | { |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 91 | clib_mov128 (dst + 0 * 128, src + 0 * 128); |
| 92 | clib_mov128 (dst + 1 * 128, src + 1 * 128); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static inline void |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 96 | clib_mov128blocks (u8 * dst, const u8 * src, size_t n) |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 97 | { |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 98 | __m512i zmm0, zmm1; |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 99 | |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 100 | while (n >= 128) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 101 | { |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 102 | zmm0 = _mm512_loadu_si512 ((const void *) (src + 0 * 64)); |
| 103 | n -= 128; |
| 104 | zmm1 = _mm512_loadu_si512 ((const void *) (src + 1 * 64)); |
| 105 | src = src + 128; |
| 106 | _mm512_storeu_si512 ((void *) (dst + 0 * 64), zmm0); |
| 107 | _mm512_storeu_si512 ((void *) (dst + 1 * 64), zmm1); |
| 108 | dst = dst + 128; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 109 | } |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static inline void |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 113 | clib_mov512blocks (u8 * dst, const u8 * src, size_t n) |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 114 | { |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 115 | __m512i zmm0, zmm1, zmm2, zmm3, zmm4, zmm5, zmm6, zmm7; |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 116 | |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 117 | while (n >= 512) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 118 | { |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 119 | zmm0 = _mm512_loadu_si512 ((const void *) (src + 0 * 64)); |
| 120 | n -= 512; |
| 121 | zmm1 = _mm512_loadu_si512 ((const void *) (src + 1 * 64)); |
| 122 | zmm2 = _mm512_loadu_si512 ((const void *) (src + 2 * 64)); |
| 123 | zmm3 = _mm512_loadu_si512 ((const void *) (src + 3 * 64)); |
| 124 | zmm4 = _mm512_loadu_si512 ((const void *) (src + 4 * 64)); |
| 125 | zmm5 = _mm512_loadu_si512 ((const void *) (src + 5 * 64)); |
| 126 | zmm6 = _mm512_loadu_si512 ((const void *) (src + 6 * 64)); |
| 127 | zmm7 = _mm512_loadu_si512 ((const void *) (src + 7 * 64)); |
| 128 | src = src + 512; |
| 129 | _mm512_storeu_si512 ((void *) (dst + 0 * 64), zmm0); |
| 130 | _mm512_storeu_si512 ((void *) (dst + 1 * 64), zmm1); |
| 131 | _mm512_storeu_si512 ((void *) (dst + 2 * 64), zmm2); |
| 132 | _mm512_storeu_si512 ((void *) (dst + 3 * 64), zmm3); |
| 133 | _mm512_storeu_si512 ((void *) (dst + 4 * 64), zmm4); |
| 134 | _mm512_storeu_si512 ((void *) (dst + 5 * 64), zmm5); |
| 135 | _mm512_storeu_si512 ((void *) (dst + 6 * 64), zmm6); |
| 136 | _mm512_storeu_si512 ((void *) (dst + 7 * 64), zmm7); |
| 137 | dst = dst + 512; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 138 | } |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static inline void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 142 | clib_memcpy (void *dst, const void *src, size_t n) |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 143 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 144 | uword dstu = (uword) dst; |
| 145 | uword srcu = (uword) src; |
| 146 | void *ret = dst; |
| 147 | size_t dstofss; |
| 148 | size_t bits; |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 149 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 150 | /** |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 151 | * Copy less than 16 bytes |
| 152 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 153 | if (n < 16) |
| 154 | { |
| 155 | if (n & 0x01) |
| 156 | { |
| 157 | *(u8 *) dstu = *(const u8 *) srcu; |
| 158 | srcu = (uword) ((const u8 *) srcu + 1); |
| 159 | dstu = (uword) ((u8 *) dstu + 1); |
| 160 | } |
| 161 | if (n & 0x02) |
| 162 | { |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 163 | *(u16 *) dstu = *(const u16 *) srcu; |
| 164 | srcu = (uword) ((const u16 *) srcu + 1); |
| 165 | dstu = (uword) ((u16 *) dstu + 1); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 166 | } |
| 167 | if (n & 0x04) |
| 168 | { |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 169 | *(u32 *) dstu = *(const u32 *) srcu; |
| 170 | srcu = (uword) ((const u32 *) srcu + 1); |
| 171 | dstu = (uword) ((u32 *) dstu + 1); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 172 | } |
| 173 | if (n & 0x08) |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 174 | *(u64 *) dstu = *(const u64 *) srcu; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 175 | return ret; |
| 176 | } |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 177 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 178 | /** |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 179 | * Fast way when copy size doesn't exceed 512 bytes |
| 180 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 181 | if (n <= 32) |
| 182 | { |
| 183 | clib_mov16 ((u8 *) dst, (const u8 *) src); |
| 184 | clib_mov16 ((u8 *) dst - 16 + n, (const u8 *) src - 16 + n); |
| 185 | return ret; |
| 186 | } |
| 187 | if (n <= 64) |
| 188 | { |
| 189 | clib_mov32 ((u8 *) dst, (const u8 *) src); |
| 190 | clib_mov32 ((u8 *) dst - 32 + n, (const u8 *) src - 32 + n); |
| 191 | return ret; |
| 192 | } |
| 193 | if (n <= 512) |
| 194 | { |
| 195 | if (n >= 256) |
| 196 | { |
| 197 | n -= 256; |
| 198 | clib_mov256 ((u8 *) dst, (const u8 *) src); |
| 199 | src = (const u8 *) src + 256; |
| 200 | dst = (u8 *) dst + 256; |
| 201 | } |
| 202 | if (n >= 128) |
| 203 | { |
| 204 | n -= 128; |
| 205 | clib_mov128 ((u8 *) dst, (const u8 *) src); |
| 206 | src = (const u8 *) src + 128; |
| 207 | dst = (u8 *) dst + 128; |
| 208 | } |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 209 | COPY_BLOCK_128_BACK63: |
| 210 | if (n > 64) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 211 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 212 | clib_mov64 ((u8 *) dst, (const u8 *) src); |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 213 | clib_mov64 ((u8 *) dst - 64 + n, (const u8 *) src - 64 + n); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 214 | return ret; |
| 215 | } |
| 216 | if (n > 0) |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 217 | clib_mov64 ((u8 *) dst - 64 + n, (const u8 *) src - 64 + n); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 218 | return ret; |
| 219 | } |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 220 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 221 | /** |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 222 | * Make store aligned when copy size exceeds 512 bytes |
| 223 | */ |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 224 | dstofss = (uword) dst & 0x3F; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 225 | if (dstofss > 0) |
| 226 | { |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 227 | dstofss = 64 - dstofss; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 228 | n -= dstofss; |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 229 | clib_mov64 ((u8 *) dst, (const u8 *) src); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 230 | src = (const u8 *) src + dstofss; |
| 231 | dst = (u8 *) dst + dstofss; |
| 232 | } |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 233 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 234 | /** |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 235 | * Copy 512-byte blocks. |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 236 | * Use copy block function for better instruction order control, |
| 237 | * which is important when load is unaligned. |
| 238 | */ |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 239 | clib_mov512blocks ((u8 *) dst, (const u8 *) src, n); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 240 | bits = n; |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 241 | n = n & 511; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 242 | bits -= n; |
| 243 | src = (const u8 *) src + bits; |
| 244 | dst = (u8 *) dst + bits; |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 245 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 246 | /** |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 247 | * Copy 128-byte blocks. |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 248 | * Use copy block function for better instruction order control, |
| 249 | * which is important when load is unaligned. |
| 250 | */ |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 251 | if (n >= 128) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 252 | { |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 253 | clib_mov128blocks ((u8 *) dst, (const u8 *) src, n); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 254 | bits = n; |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 255 | n = n & 127; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 256 | bits -= n; |
| 257 | src = (const u8 *) src + bits; |
| 258 | dst = (u8 *) dst + bits; |
| 259 | } |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 260 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 261 | /** |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 262 | * Copy whatever left |
| 263 | */ |
Damjan Marion | fad3fb3 | 2017-12-14 09:30:11 +0100 | [diff] [blame] | 264 | goto COPY_BLOCK_128_BACK63; |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
Paul Vinciguerra | ec11b13 | 2018-09-24 05:25:00 -0700 | [diff] [blame] | 268 | #endif /* included_clib_memcpy_avx512_h */ |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 269 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 270 | |
| 271 | /* |
| 272 | * fd.io coding-style-patch-verification: ON |
| 273 | * |
| 274 | * Local Variables: |
| 275 | * eval: (c-set-style "gnu") |
| 276 | * End: |
| 277 | */ |