blob: 003908026c3257dabf04ec20fe2ec47a5b1df6fd [file] [log] [blame]
Damjan Marionf1213b82016-03-13 02:22:06 +01001/*
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 Marionfad3fb32017-12-14 09:30:11 +010048#ifndef included_clib_memcpy_avx512_h
49#define included_clib_memcpy_avx512_h
Damjan Marionf1213b82016-03-13 02:22:06 +010050
51#include <stdint.h>
52#include <x86intrin.h>
53
54static inline void
Dave Barachc3799992016-08-15 11:12:27 -040055clib_mov16 (u8 * dst, const u8 * src)
Damjan Marionf1213b82016-03-13 02:22:06 +010056{
Damjan Marion31e59d92017-07-05 18:15:08 +020057 __m128i xmm0;
58
59 xmm0 = _mm_loadu_si128 ((const __m128i *) src);
60 _mm_storeu_si128 ((__m128i *) dst, xmm0);
Damjan Marionf1213b82016-03-13 02:22:06 +010061}
62
63static inline void
Dave Barachc3799992016-08-15 11:12:27 -040064clib_mov32 (u8 * dst, const u8 * src)
Damjan Marionf1213b82016-03-13 02:22:06 +010065{
Damjan Marion31e59d92017-07-05 18:15:08 +020066 __m256i ymm0;
67
68 ymm0 = _mm256_loadu_si256 ((const __m256i *) src);
69 _mm256_storeu_si256 ((__m256i *) dst, ymm0);
Damjan Marionf1213b82016-03-13 02:22:06 +010070}
71
72static inline void
Dave Barachc3799992016-08-15 11:12:27 -040073clib_mov64 (u8 * dst, const u8 * src)
Damjan Marionf1213b82016-03-13 02:22:06 +010074{
Damjan Marionfad3fb32017-12-14 09:30:11 +010075 __m512i zmm0;
76
77 zmm0 = _mm512_loadu_si512 ((const void *) src);
78 _mm512_storeu_si512 ((void *) dst, zmm0);
Damjan Marionf1213b82016-03-13 02:22:06 +010079}
80
81static inline void
Dave Barachc3799992016-08-15 11:12:27 -040082clib_mov128 (u8 * dst, const u8 * src)
Damjan Marionf1213b82016-03-13 02:22:06 +010083{
Damjan Marionfad3fb32017-12-14 09:30:11 +010084 clib_mov64 (dst + 0 * 64, src + 0 * 64);
85 clib_mov64 (dst + 1 * 64, src + 1 * 64);
Damjan Marionf1213b82016-03-13 02:22:06 +010086}
87
88static inline void
Dave Barachc3799992016-08-15 11:12:27 -040089clib_mov256 (u8 * dst, const u8 * src)
Damjan Marionf1213b82016-03-13 02:22:06 +010090{
Damjan Marionfad3fb32017-12-14 09:30:11 +010091 clib_mov128 (dst + 0 * 128, src + 0 * 128);
92 clib_mov128 (dst + 1 * 128, src + 1 * 128);
Damjan Marionf1213b82016-03-13 02:22:06 +010093}
94
95static inline void
Damjan Marionfad3fb32017-12-14 09:30:11 +010096clib_mov128blocks (u8 * dst, const u8 * src, size_t n)
Damjan Marionf1213b82016-03-13 02:22:06 +010097{
Damjan Marionfad3fb32017-12-14 09:30:11 +010098 __m512i zmm0, zmm1;
Damjan Marionf1213b82016-03-13 02:22:06 +010099
Damjan Marionfad3fb32017-12-14 09:30:11 +0100100 while (n >= 128)
Dave Barachc3799992016-08-15 11:12:27 -0400101 {
Damjan Marionfad3fb32017-12-14 09:30:11 +0100102 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 Barachc3799992016-08-15 11:12:27 -0400109 }
Damjan Marionf1213b82016-03-13 02:22:06 +0100110}
111
112static inline void
Damjan Marionfad3fb32017-12-14 09:30:11 +0100113clib_mov512blocks (u8 * dst, const u8 * src, size_t n)
Damjan Marionf1213b82016-03-13 02:22:06 +0100114{
Damjan Marionfad3fb32017-12-14 09:30:11 +0100115 __m512i zmm0, zmm1, zmm2, zmm3, zmm4, zmm5, zmm6, zmm7;
Damjan Marionf1213b82016-03-13 02:22:06 +0100116
Damjan Marionfad3fb32017-12-14 09:30:11 +0100117 while (n >= 512)
Dave Barachc3799992016-08-15 11:12:27 -0400118 {
Damjan Marionfad3fb32017-12-14 09:30:11 +0100119 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 Barachc3799992016-08-15 11:12:27 -0400138 }
Damjan Marionf1213b82016-03-13 02:22:06 +0100139}
140
141static inline void *
Dave Barachc3799992016-08-15 11:12:27 -0400142clib_memcpy (void *dst, const void *src, size_t n)
Damjan Marionf1213b82016-03-13 02:22:06 +0100143{
Dave Barachc3799992016-08-15 11:12:27 -0400144 uword dstu = (uword) dst;
145 uword srcu = (uword) src;
146 void *ret = dst;
147 size_t dstofss;
148 size_t bits;
Damjan Marionf1213b82016-03-13 02:22:06 +0100149
Dave Barachc3799992016-08-15 11:12:27 -0400150 /**
Damjan Marionf1213b82016-03-13 02:22:06 +0100151 * Copy less than 16 bytes
152 */
Dave Barachc3799992016-08-15 11:12:27 -0400153 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 Marionfad3fb32017-12-14 09:30:11 +0100163 *(u16 *) dstu = *(const u16 *) srcu;
164 srcu = (uword) ((const u16 *) srcu + 1);
165 dstu = (uword) ((u16 *) dstu + 1);
Dave Barachc3799992016-08-15 11:12:27 -0400166 }
167 if (n & 0x04)
168 {
Damjan Marionfad3fb32017-12-14 09:30:11 +0100169 *(u32 *) dstu = *(const u32 *) srcu;
170 srcu = (uword) ((const u32 *) srcu + 1);
171 dstu = (uword) ((u32 *) dstu + 1);
Dave Barachc3799992016-08-15 11:12:27 -0400172 }
173 if (n & 0x08)
Damjan Marionfad3fb32017-12-14 09:30:11 +0100174 *(u64 *) dstu = *(const u64 *) srcu;
Dave Barachc3799992016-08-15 11:12:27 -0400175 return ret;
176 }
Damjan Marionf1213b82016-03-13 02:22:06 +0100177
Dave Barachc3799992016-08-15 11:12:27 -0400178 /**
Damjan Marionf1213b82016-03-13 02:22:06 +0100179 * Fast way when copy size doesn't exceed 512 bytes
180 */
Dave Barachc3799992016-08-15 11:12:27 -0400181 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 Marionfad3fb32017-12-14 09:30:11 +0100209 COPY_BLOCK_128_BACK63:
210 if (n > 64)
Dave Barachc3799992016-08-15 11:12:27 -0400211 {
Dave Barachc3799992016-08-15 11:12:27 -0400212 clib_mov64 ((u8 *) dst, (const u8 *) src);
Damjan Marionfad3fb32017-12-14 09:30:11 +0100213 clib_mov64 ((u8 *) dst - 64 + n, (const u8 *) src - 64 + n);
Dave Barachc3799992016-08-15 11:12:27 -0400214 return ret;
215 }
216 if (n > 0)
Damjan Marionfad3fb32017-12-14 09:30:11 +0100217 clib_mov64 ((u8 *) dst - 64 + n, (const u8 *) src - 64 + n);
Dave Barachc3799992016-08-15 11:12:27 -0400218 return ret;
219 }
Damjan Marionf1213b82016-03-13 02:22:06 +0100220
Dave Barachc3799992016-08-15 11:12:27 -0400221 /**
Damjan Marionf1213b82016-03-13 02:22:06 +0100222 * Make store aligned when copy size exceeds 512 bytes
223 */
Damjan Marionfad3fb32017-12-14 09:30:11 +0100224 dstofss = (uword) dst & 0x3F;
Dave Barachc3799992016-08-15 11:12:27 -0400225 if (dstofss > 0)
226 {
Damjan Marionfad3fb32017-12-14 09:30:11 +0100227 dstofss = 64 - dstofss;
Dave Barachc3799992016-08-15 11:12:27 -0400228 n -= dstofss;
Damjan Marionfad3fb32017-12-14 09:30:11 +0100229 clib_mov64 ((u8 *) dst, (const u8 *) src);
Dave Barachc3799992016-08-15 11:12:27 -0400230 src = (const u8 *) src + dstofss;
231 dst = (u8 *) dst + dstofss;
232 }
Damjan Marionf1213b82016-03-13 02:22:06 +0100233
Dave Barachc3799992016-08-15 11:12:27 -0400234 /**
Damjan Marionfad3fb32017-12-14 09:30:11 +0100235 * Copy 512-byte blocks.
Damjan Marionf1213b82016-03-13 02:22:06 +0100236 * Use copy block function for better instruction order control,
237 * which is important when load is unaligned.
238 */
Damjan Marionfad3fb32017-12-14 09:30:11 +0100239 clib_mov512blocks ((u8 *) dst, (const u8 *) src, n);
Dave Barachc3799992016-08-15 11:12:27 -0400240 bits = n;
Damjan Marionfad3fb32017-12-14 09:30:11 +0100241 n = n & 511;
Dave Barachc3799992016-08-15 11:12:27 -0400242 bits -= n;
243 src = (const u8 *) src + bits;
244 dst = (u8 *) dst + bits;
Damjan Marionf1213b82016-03-13 02:22:06 +0100245
Dave Barachc3799992016-08-15 11:12:27 -0400246 /**
Damjan Marionfad3fb32017-12-14 09:30:11 +0100247 * Copy 128-byte blocks.
Damjan Marionf1213b82016-03-13 02:22:06 +0100248 * Use copy block function for better instruction order control,
249 * which is important when load is unaligned.
250 */
Damjan Marionfad3fb32017-12-14 09:30:11 +0100251 if (n >= 128)
Dave Barachc3799992016-08-15 11:12:27 -0400252 {
Damjan Marionfad3fb32017-12-14 09:30:11 +0100253 clib_mov128blocks ((u8 *) dst, (const u8 *) src, n);
Dave Barachc3799992016-08-15 11:12:27 -0400254 bits = n;
Damjan Marionfad3fb32017-12-14 09:30:11 +0100255 n = n & 127;
Dave Barachc3799992016-08-15 11:12:27 -0400256 bits -= n;
257 src = (const u8 *) src + bits;
258 dst = (u8 *) dst + bits;
259 }
Damjan Marionf1213b82016-03-13 02:22:06 +0100260
Dave Barachc3799992016-08-15 11:12:27 -0400261 /**
Damjan Marionf1213b82016-03-13 02:22:06 +0100262 * Copy whatever left
263 */
Damjan Marionfad3fb32017-12-14 09:30:11 +0100264 goto COPY_BLOCK_128_BACK63;
Damjan Marionf1213b82016-03-13 02:22:06 +0100265}
266
267
Damjan Marionfad3fb32017-12-14 09:30:11 +0100268#endif /* included_clib_mamcpy_avx512_h */
Damjan Marionf1213b82016-03-13 02:22:06 +0100269
Dave Barachc3799992016-08-15 11:12:27 -0400270
271/*
272 * fd.io coding-style-patch-verification: ON
273 *
274 * Local Variables:
275 * eval: (c-set-style "gnu")
276 * End:
277 */