Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #ifndef _RTE_MEMCPY_X86_64_H_
6 : : #define _RTE_MEMCPY_X86_64_H_
7 : :
8 : : /**
9 : : * @file
10 : : *
11 : : * Functions for SSE/AVX/AVX2/AVX512 implementation of memcpy().
12 : : */
13 : :
14 : : #include <stdio.h>
15 : : #include <stdint.h>
16 : : #include <string.h>
17 : : #include <rte_vect.h>
18 : : #include <rte_common.h>
19 : : #include <rte_config.h>
20 : :
21 : : #ifdef __cplusplus
22 : : extern "C" {
23 : : #endif
24 : :
25 : : /*
26 : : * GCC older than version 11 doesn't compile AVX properly, so use SSE instead.
27 : : * There are no problems with AVX2.
28 : : */
29 : : #if defined __AVX2__
30 : : #define RTE_MEMCPY_AVX
31 : : #elif defined __AVX__ && !(defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 110000))
32 : : #define RTE_MEMCPY_AVX
33 : : #endif
34 : :
35 : : /**
36 : : * Copy bytes from one location to another,
37 : : * locations must not overlap.
38 : : * Use with n <= 15.
39 : : */
40 : : static __rte_always_inline void *
41 : 0 : rte_mov15_or_less(void *__rte_restrict dst, const void *__rte_restrict src, size_t n)
42 : : {
43 : : /**
44 : : * Use the following structs to avoid violating C standard
45 : : * alignment requirements and to avoid strict aliasing bugs
46 : : */
47 : 0 : struct __rte_packed_begin rte_uint64_alias {
48 : : uint64_t val;
49 : : } __rte_packed_end __rte_may_alias;
50 : 0 : struct __rte_packed_begin rte_uint32_alias {
51 : : uint32_t val;
52 : : } __rte_packed_end __rte_may_alias;
53 : 0 : struct __rte_packed_begin rte_uint16_alias {
54 : : uint16_t val;
55 : : } __rte_packed_end __rte_may_alias;
56 : :
57 : 0 : void *ret = dst;
58 [ + + + + : 46335 : if (n & 8) {
+ + + + +
+ + - - -
- + - - +
- - + - -
- - + + +
- + + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- + + - -
- - - - -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
59 [ # # # # ]: 2152589 : ((struct rte_uint64_alias *)dst)->val =
60 [ # # ]: 2150904 : ((const struct rte_uint64_alias *)src)->val;
61 : 1599 : src = (const uint64_t *)src + 1;
62 : 1652 : dst = (uint64_t *)dst + 1;
63 : : }
64 [ + + + + : 2282 : if (n & 4) {
+ + + + -
+ + - - -
+ - - - +
- - + - -
- - + - +
- + + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
+ - - + #
# # # # #
# # # # ]
65 [ # # ]: 2000967 : ((struct rte_uint32_alias *)dst)->val =
66 [ # # ]: 770 : ((const struct rte_uint32_alias *)src)->val;
67 : 770 : src = (const uint32_t *)src + 1;
68 : 770 : dst = (uint32_t *)dst + 1;
69 : : }
70 [ + + + + : 2282 : if (n & 2) {
+ + + + -
+ - + - -
+ - - - -
+ + - - -
- - + + -
+ - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- + - + #
# # # # #
# # # # ]
71 [ # # # # : 2000470 : ((struct rte_uint16_alias *)dst)->val =
# # # # #
# ]
72 [ # # # # : 457 : ((const struct rte_uint16_alias *)src)->val;
# # ]
73 : 457 : src = (const uint16_t *)src + 1;
74 : 457 : dst = (uint16_t *)dst + 1;
75 : : }
76 [ + + + + : 2282 : if (n & 1)
+ + - + -
+ - + - -
+ - - - -
+ - + - -
- - + - -
+ - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- + - + #
# # # # #
# # # # ]
77 [ # # # # ]: 419 : *(uint8_t *)dst = *(const uint8_t *)src;
78 : 0 : return ret;
79 : : }
80 : :
81 : : /**
82 : : * Copy 16 bytes from one location to another,
83 : : * locations must not overlap.
84 : : */
85 : : static __rte_always_inline void
86 : 0 : rte_mov16(uint8_t *__rte_restrict dst, const uint8_t *__rte_restrict src)
87 : : {
88 : 0 : __m128i xmm0;
89 : :
90 : 0 : xmm0 = _mm_loadu_si128((const __m128i *)(const void *)src);
91 [ # # # # : 0 : _mm_storeu_si128((__m128i *)(void *)dst, xmm0);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
92 : 0 : }
93 : :
94 : : /**
95 : : * Copy 32 bytes from one location to another,
96 : : * locations must not overlap.
97 : : */
98 : : static __rte_always_inline void
99 : 0 : rte_mov32(uint8_t *__rte_restrict dst, const uint8_t *__rte_restrict src)
100 : : {
101 : : #if defined RTE_MEMCPY_AVX
102 : 0 : __m256i ymm0;
103 : :
104 : 0 : ymm0 = _mm256_loadu_si256((const __m256i *)(const void *)src);
105 : 0 : _mm256_storeu_si256((__m256i *)(void *)dst, ymm0);
106 : : #else /* SSE implementation */
107 : : rte_mov16((uint8_t *)dst + 0 * 16, (const uint8_t *)src + 0 * 16);
108 : : rte_mov16((uint8_t *)dst + 1 * 16, (const uint8_t *)src + 1 * 16);
109 : : #endif
110 : 3194585 : }
111 : :
112 : : /**
113 : : * Copy 48 bytes from one location to another,
114 : : * locations must not overlap.
115 : : */
116 : : static __rte_always_inline void
117 : : rte_mov48(uint8_t *__rte_restrict dst, const uint8_t *__rte_restrict src)
118 : : {
119 : : #if defined RTE_MEMCPY_AVX
120 : : rte_mov32((uint8_t *)dst, (const uint8_t *)src);
121 : : rte_mov16((uint8_t *)dst + 32, (const uint8_t *)src + 32);
122 : : #else /* SSE implementation */
123 : : rte_mov16((uint8_t *)dst + 0 * 16, (const uint8_t *)src + 0 * 16);
124 : : rte_mov16((uint8_t *)dst + 1 * 16, (const uint8_t *)src + 1 * 16);
125 : : rte_mov16((uint8_t *)dst + 2 * 16, (const uint8_t *)src + 2 * 16);
126 : : #endif
127 : : }
128 : :
129 : : /**
130 : : * Copy 64 bytes from one location to another,
131 : : * locations must not overlap.
132 : : */
133 : : static __rte_always_inline void
134 : 0 : rte_mov64(uint8_t *__rte_restrict dst, const uint8_t *__rte_restrict src)
135 : : {
136 : : #if defined __AVX512F__ && defined RTE_MEMCPY_AVX512
137 : : __m512i zmm0;
138 : :
139 : : zmm0 = _mm512_loadu_si512((const void *)src);
140 : : _mm512_storeu_si512((void *)dst, zmm0);
141 : : #else /* AVX2, AVX & SSE implementation */
142 : 0 : rte_mov32((uint8_t *)dst + 0 * 32, (const uint8_t *)src + 0 * 32);
143 : 0 : rte_mov32((uint8_t *)dst + 1 * 32, (const uint8_t *)src + 1 * 32);
144 : : #endif
145 : : }
146 : :
147 : : /**
148 : : * Copy 128 bytes from one location to another,
149 : : * locations must not overlap.
150 : : */
151 : : static __rte_always_inline void
152 : 0 : rte_mov128(uint8_t *__rte_restrict dst, const uint8_t *__rte_restrict src)
153 : : {
154 : 0 : rte_mov64(dst + 0 * 64, src + 0 * 64);
155 : 0 : rte_mov64(dst + 1 * 64, src + 1 * 64);
156 : : }
157 : :
158 : : /**
159 : : * Copy 256 bytes from one location to another,
160 : : * locations must not overlap.
161 : : */
162 : : static __rte_always_inline void
163 : : rte_mov256(uint8_t *__rte_restrict dst, const uint8_t *__rte_restrict src)
164 : : {
165 : : rte_mov128(dst + 0 * 128, src + 0 * 128);
166 : : rte_mov128(dst + 1 * 128, src + 1 * 128);
167 : : }
168 : :
169 : : #if defined __AVX512F__ && defined RTE_MEMCPY_AVX512
170 : :
171 : : /**
172 : : * AVX512 implementation below
173 : : */
174 : :
175 : : #define RTE_MEMCPY_ALIGNMENT_MASK 0x3F
176 : :
177 : : /**
178 : : * Copy 128-byte blocks from one location to another,
179 : : * locations must not overlap.
180 : : */
181 : : static __rte_always_inline void
182 : : rte_mov128blocks(uint8_t *__rte_restrict dst, const uint8_t *__rte_restrict src, size_t n)
183 : : {
184 : : __m512i zmm0, zmm1;
185 : :
186 : : while (n >= 128) {
187 : : zmm0 = _mm512_loadu_si512((const void *)(src + 0 * 64));
188 : : n -= 128;
189 : : zmm1 = _mm512_loadu_si512((const void *)(src + 1 * 64));
190 : : src = src + 128;
191 : : _mm512_storeu_si512((void *)(dst + 0 * 64), zmm0);
192 : : _mm512_storeu_si512((void *)(dst + 1 * 64), zmm1);
193 : : dst = dst + 128;
194 : : }
195 : : }
196 : :
197 : : /**
198 : : * Copy 512-byte blocks from one location to another,
199 : : * locations must not overlap.
200 : : */
201 : : static inline void
202 : : rte_mov512blocks(uint8_t *__rte_restrict dst, const uint8_t *__rte_restrict src, size_t n)
203 : : {
204 : : __m512i zmm0, zmm1, zmm2, zmm3, zmm4, zmm5, zmm6, zmm7;
205 : :
206 : : while (n >= 512) {
207 : : zmm0 = _mm512_loadu_si512((const void *)(src + 0 * 64));
208 : : n -= 512;
209 : : zmm1 = _mm512_loadu_si512((const void *)(src + 1 * 64));
210 : : zmm2 = _mm512_loadu_si512((const void *)(src + 2 * 64));
211 : : zmm3 = _mm512_loadu_si512((const void *)(src + 3 * 64));
212 : : zmm4 = _mm512_loadu_si512((const void *)(src + 4 * 64));
213 : : zmm5 = _mm512_loadu_si512((const void *)(src + 5 * 64));
214 : : zmm6 = _mm512_loadu_si512((const void *)(src + 6 * 64));
215 : : zmm7 = _mm512_loadu_si512((const void *)(src + 7 * 64));
216 : : src = src + 512;
217 : : _mm512_storeu_si512((void *)(dst + 0 * 64), zmm0);
218 : : _mm512_storeu_si512((void *)(dst + 1 * 64), zmm1);
219 : : _mm512_storeu_si512((void *)(dst + 2 * 64), zmm2);
220 : : _mm512_storeu_si512((void *)(dst + 3 * 64), zmm3);
221 : : _mm512_storeu_si512((void *)(dst + 4 * 64), zmm4);
222 : : _mm512_storeu_si512((void *)(dst + 5 * 64), zmm5);
223 : : _mm512_storeu_si512((void *)(dst + 6 * 64), zmm6);
224 : : _mm512_storeu_si512((void *)(dst + 7 * 64), zmm7);
225 : : dst = dst + 512;
226 : : }
227 : : }
228 : :
229 : : /**
230 : : * Copy bytes from one location to another,
231 : : * locations must not overlap.
232 : : * Use with n > 64.
233 : : */
234 : : static __rte_always_inline void *
235 : : rte_memcpy_generic_more_than_64(void *__rte_restrict dst, const void *__rte_restrict src,
236 : : size_t n)
237 : : {
238 : : void *ret = dst;
239 : : size_t dstofss;
240 : : size_t bits;
241 : :
242 : : /**
243 : : * Fast way when copy size doesn't exceed 512 bytes
244 : : */
245 : : if (n <= 512) {
246 : : if (n >= 256) {
247 : : n -= 256;
248 : : rte_mov256((uint8_t *)dst, (const uint8_t *)src);
249 : : src = (const uint8_t *)src + 256;
250 : : dst = (uint8_t *)dst + 256;
251 : : }
252 : : if (n >= 128) {
253 : : n -= 128;
254 : : rte_mov128((uint8_t *)dst, (const uint8_t *)src);
255 : : src = (const uint8_t *)src + 128;
256 : : dst = (uint8_t *)dst + 128;
257 : : }
258 : : COPY_BLOCK_128_BACK63:
259 : : if (n > 64) {
260 : : rte_mov64((uint8_t *)dst, (const uint8_t *)src);
261 : : rte_mov64((uint8_t *)dst - 64 + n,
262 : : (const uint8_t *)src - 64 + n);
263 : : return ret;
264 : : }
265 : : if (n > 0)
266 : : rte_mov64((uint8_t *)dst - 64 + n,
267 : : (const uint8_t *)src - 64 + n);
268 : : return ret;
269 : : }
270 : :
271 : : /**
272 : : * Make store aligned when copy size exceeds 512 bytes
273 : : */
274 : : dstofss = ((uintptr_t)dst & 0x3F);
275 : : if (dstofss > 0) {
276 : : dstofss = 64 - dstofss;
277 : : n -= dstofss;
278 : : rte_mov64((uint8_t *)dst, (const uint8_t *)src);
279 : : src = (const uint8_t *)src + dstofss;
280 : : dst = (uint8_t *)dst + dstofss;
281 : : }
282 : :
283 : : /**
284 : : * Copy 512-byte blocks.
285 : : * Use copy block function for better instruction order control,
286 : : * which is important when load is unaligned.
287 : : */
288 : : rte_mov512blocks((uint8_t *)dst, (const uint8_t *)src, n);
289 : : bits = n;
290 : : n = n & 511;
291 : : bits -= n;
292 : : src = (const uint8_t *)src + bits;
293 : : dst = (uint8_t *)dst + bits;
294 : :
295 : : /**
296 : : * Copy 128-byte blocks.
297 : : * Use copy block function for better instruction order control,
298 : : * which is important when load is unaligned.
299 : : */
300 : : if (n >= 128) {
301 : : rte_mov128blocks((uint8_t *)dst, (const uint8_t *)src, n);
302 : : bits = n;
303 : : n = n & 127;
304 : : bits -= n;
305 : : src = (const uint8_t *)src + bits;
306 : : dst = (uint8_t *)dst + bits;
307 : : }
308 : :
309 : : /**
310 : : * Copy whatever left
311 : : */
312 : : goto COPY_BLOCK_128_BACK63;
313 : : }
314 : :
315 : : #elif defined RTE_MEMCPY_AVX
316 : :
317 : : /**
318 : : * AVX implementation below
319 : : */
320 : :
321 : : #define RTE_MEMCPY_ALIGNMENT_MASK 0x1F
322 : :
323 : : /**
324 : : * Copy 128-byte blocks from one location to another,
325 : : * locations must not overlap.
326 : : */
327 : : static __rte_always_inline void
328 : 0 : rte_mov128blocks(uint8_t *__rte_restrict dst, const uint8_t *__rte_restrict src, size_t n)
329 : : {
330 : 0 : __m256i ymm0, ymm1, ymm2, ymm3;
331 : :
332 [ + + + + : 49741 : while (n >= 128) {
+ + + + -
- + + + +
- - - - -
- - - - -
- - - - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
333 : 0 : ymm0 = _mm256_loadu_si256((const __m256i *)(const void *)
334 : : ((const uint8_t *)src + 0 * 32));
335 : 46050 : n -= 128;
336 : 0 : ymm1 = _mm256_loadu_si256((const __m256i *)(const void *)
337 : : ((const uint8_t *)src + 1 * 32));
338 : 0 : ymm2 = _mm256_loadu_si256((const __m256i *)(const void *)
339 : : ((const uint8_t *)src + 2 * 32));
340 : 0 : ymm3 = _mm256_loadu_si256((const __m256i *)(const void *)
341 : : ((const uint8_t *)src + 3 * 32));
342 : 46050 : src = (const uint8_t *)src + 128;
343 : 0 : _mm256_storeu_si256((__m256i *)(void *)
344 : : ((uint8_t *)dst + 0 * 32), ymm0);
345 : 0 : _mm256_storeu_si256((__m256i *)(void *)
346 : : ((uint8_t *)dst + 1 * 32), ymm1);
347 : 0 : _mm256_storeu_si256((__m256i *)(void *)
348 : : ((uint8_t *)dst + 2 * 32), ymm2);
349 : 0 : _mm256_storeu_si256((__m256i *)(void *)
350 : : ((uint8_t *)dst + 3 * 32), ymm3);
351 : 46050 : dst = (uint8_t *)dst + 128;
352 : : }
353 : : }
354 : :
355 : : /**
356 : : * Copy bytes from one location to another,
357 : : * locations must not overlap.
358 : : * Use with n > 64.
359 : : */
360 : : static __rte_always_inline void *
361 : 0 : rte_memcpy_generic_more_than_64(void *__rte_restrict dst, const void *__rte_restrict src,
362 : : size_t n)
363 : : {
364 : 0 : void *ret = dst;
365 : 0 : size_t dstofss;
366 : 0 : size_t bits;
367 : :
368 : : /**
369 : : * Fast way when copy size doesn't exceed 256 bytes
370 : : */
371 [ + + + + : 7316593 : if (n <= 256) {
- + - + -
- + + - +
- - - - -
- + - - -
- - - - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
372 [ + + + + : 7312902 : if (n >= 128) {
- - - - -
- - + - -
- - - - -
- - + - -
- - - - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
373 [ # # ]: 759388 : n -= 128;
374 : 0 : rte_mov128((uint8_t *)dst, (const uint8_t *)src);
375 : 759330 : src = (const uint8_t *)src + 128;
376 : 759388 : dst = (uint8_t *)dst + 128;
377 : : }
378 : 6553514 : COPY_BLOCK_128_BACK31:
379 [ + + + + : 7316593 : if (n >= 64) {
- + - + -
- + + - +
- - - - -
- - + - -
- - - - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
380 : 6556136 : n -= 64;
381 : 0 : rte_mov64((uint8_t *)dst, (const uint8_t *)src);
382 : 6556136 : src = (const uint8_t *)src + 64;
383 : 6556136 : dst = (uint8_t *)dst + 64;
384 : : }
385 [ + + + + : 7316593 : if (n > 32) {
- + + + -
- + + + -
- - - - -
- + - - -
- - - - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
386 : 0 : rte_mov32((uint8_t *)dst, (const uint8_t *)src);
387 : 3363250 : rte_mov32((uint8_t *)dst - 32 + n,
388 : 3363250 : (const uint8_t *)src - 32 + n);
389 : 3363250 : return ret;
390 : : }
391 [ + + + + : 3953343 : if (n > 0) {
- + - + -
- - + - -
- - - - -
- - - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
392 : 3194585 : rte_mov32((uint8_t *)dst - 32 + n,
393 : 3194585 : (const uint8_t *)src - 32 + n);
394 : : }
395 : : return ret;
396 : : }
397 : :
398 : : /**
399 : : * Make store aligned when copy size exceeds 256 bytes
400 : : */
401 : 3691 : dstofss = (uintptr_t)dst & 0x1F;
402 [ + + + - : 3691 : if (dstofss > 0) {
- + - + -
- + - + -
- - - - -
- - - - -
- - - - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
403 : 3458 : dstofss = 32 - dstofss;
404 : 3458 : n -= dstofss;
405 : 0 : rte_mov32((uint8_t *)dst, (const uint8_t *)src);
406 : 3458 : src = (const uint8_t *)src + dstofss;
407 : 3458 : dst = (uint8_t *)dst + dstofss;
408 : : }
409 : :
410 : : /**
411 : : * Copy 128-byte blocks
412 : : */
413 : 0 : rte_mov128blocks((uint8_t *)dst, (const uint8_t *)src, n);
414 : 0 : bits = n;
415 : 3691 : n = n & 127;
416 : 3691 : bits -= n;
417 : 3691 : src = (const uint8_t *)src + bits;
418 : 3691 : dst = (uint8_t *)dst + bits;
419 : :
420 : : /**
421 : : * Copy whatever left
422 : : */
423 [ # # # # : 3691 : goto COPY_BLOCK_128_BACK31;
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
424 : : }
425 : :
426 : : #else /* __AVX512F__ */
427 : :
428 : : /**
429 : : * SSE implementation below
430 : : */
431 : :
432 : : #define RTE_MEMCPY_ALIGNMENT_MASK 0x0F
433 : :
434 : : /**
435 : : * Macro for copying unaligned block from one location to another with constant load offset,
436 : : * 47 bytes leftover maximum,
437 : : * locations must not overlap.
438 : : * Requirements:
439 : : * - Store is aligned
440 : : * - Load offset is <offset>, which must be immediate value within [1, 15]
441 : : * - For <src>, make sure <offset> bit backwards & <16 - offset> bit forwards are available for loading
442 : : * - <dst>, <src>, <len> must be variables
443 : : * - __m128i <xmm0> ~ <xmm8> must be pre-defined
444 : : */
445 : : #define MOVEUNALIGNED_LEFT47_IMM(dst, src, len, offset) \
446 : : { \
447 : : size_t tmp; \
448 : : while (len >= 128 + 16 - offset) { \
449 : : xmm0 = _mm_loadu_si128((const __m128i *)(const void *)((const uint8_t *)src - offset + 0 * 16)); \
450 : : len -= 128; \
451 : : xmm1 = _mm_loadu_si128((const __m128i *)(const void *)((const uint8_t *)src - offset + 1 * 16)); \
452 : : xmm2 = _mm_loadu_si128((const __m128i *)(const void *)((const uint8_t *)src - offset + 2 * 16)); \
453 : : xmm3 = _mm_loadu_si128((const __m128i *)(const void *)((const uint8_t *)src - offset + 3 * 16)); \
454 : : xmm4 = _mm_loadu_si128((const __m128i *)(const void *)((const uint8_t *)src - offset + 4 * 16)); \
455 : : xmm5 = _mm_loadu_si128((const __m128i *)(const void *)((const uint8_t *)src - offset + 5 * 16)); \
456 : : xmm6 = _mm_loadu_si128((const __m128i *)(const void *)((const uint8_t *)src - offset + 6 * 16)); \
457 : : xmm7 = _mm_loadu_si128((const __m128i *)(const void *)((const uint8_t *)src - offset + 7 * 16)); \
458 : : xmm8 = _mm_loadu_si128((const __m128i *)(const void *)((const uint8_t *)src - offset + 8 * 16)); \
459 : : src = (const uint8_t *)src + 128; \
460 : : _mm_storeu_si128((__m128i *)(void *)((uint8_t *)dst + 0 * 16), _mm_alignr_epi8(xmm1, xmm0, offset)); \
461 : : _mm_storeu_si128((__m128i *)(void *)((uint8_t *)dst + 1 * 16), _mm_alignr_epi8(xmm2, xmm1, offset)); \
462 : : _mm_storeu_si128((__m128i *)(void *)((uint8_t *)dst + 2 * 16), _mm_alignr_epi8(xmm3, xmm2, offset)); \
463 : : _mm_storeu_si128((__m128i *)(void *)((uint8_t *)dst + 3 * 16), _mm_alignr_epi8(xmm4, xmm3, offset)); \
464 : : _mm_storeu_si128((__m128i *)(void *)((uint8_t *)dst + 4 * 16), _mm_alignr_epi8(xmm5, xmm4, offset)); \
465 : : _mm_storeu_si128((__m128i *)(void *)((uint8_t *)dst + 5 * 16), _mm_alignr_epi8(xmm6, xmm5, offset)); \
466 : : _mm_storeu_si128((__m128i *)(void *)((uint8_t *)dst + 6 * 16), _mm_alignr_epi8(xmm7, xmm6, offset)); \
467 : : _mm_storeu_si128((__m128i *)(void *)((uint8_t *)dst + 7 * 16), _mm_alignr_epi8(xmm8, xmm7, offset)); \
468 : : dst = (uint8_t *)dst + 128; \
469 : : } \
470 : : tmp = len; \
471 : : len = ((len - 16 + offset) & 127) + 16 - offset; \
472 : : tmp -= len; \
473 : : src = (const uint8_t *)src + tmp; \
474 : : dst = (uint8_t *)dst + tmp; \
475 : : if (len >= 32 + 16 - offset) { \
476 : : while (len >= 32 + 16 - offset) { \
477 : : xmm0 = _mm_loadu_si128((const __m128i *)(const void *)((const uint8_t *)src - offset + 0 * 16)); \
478 : : len -= 32; \
479 : : xmm1 = _mm_loadu_si128((const __m128i *)(const void *)((const uint8_t *)src - offset + 1 * 16)); \
480 : : xmm2 = _mm_loadu_si128((const __m128i *)(const void *)((const uint8_t *)src - offset + 2 * 16)); \
481 : : src = (const uint8_t *)src + 32; \
482 : : _mm_storeu_si128((__m128i *)(void *)((uint8_t *)dst + 0 * 16), _mm_alignr_epi8(xmm1, xmm0, offset)); \
483 : : _mm_storeu_si128((__m128i *)(void *)((uint8_t *)dst + 1 * 16), _mm_alignr_epi8(xmm2, xmm1, offset)); \
484 : : dst = (uint8_t *)dst + 32; \
485 : : } \
486 : : tmp = len; \
487 : : len = ((len - 16 + offset) & 31) + 16 - offset; \
488 : : tmp -= len; \
489 : : src = (const uint8_t *)src + tmp; \
490 : : dst = (uint8_t *)dst + tmp; \
491 : : } \
492 : : }
493 : :
494 : : /**
495 : : * Macro for copying unaligned block from one location to another,
496 : : * 47 bytes leftover maximum,
497 : : * locations must not overlap.
498 : : * Use switch here because the aligning instruction requires immediate value for shift count.
499 : : * Requirements:
500 : : * - Store is aligned
501 : : * - Load offset is <offset>, which must be within [1, 15]
502 : : * - For <src>, make sure <offset> bit backwards & <16 - offset> bit forwards are available for loading
503 : : * - <dst>, <src>, <len> must be variables
504 : : * - __m128i <xmm0> ~ <xmm8> used in MOVEUNALIGNED_LEFT47_IMM must be pre-defined
505 : : */
506 : : #define MOVEUNALIGNED_LEFT47(dst, src, len, offset) \
507 : : { \
508 : : switch (offset) { \
509 : : case 0x01: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x01); break; \
510 : : case 0x02: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x02); break; \
511 : : case 0x03: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x03); break; \
512 : : case 0x04: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x04); break; \
513 : : case 0x05: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x05); break; \
514 : : case 0x06: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x06); break; \
515 : : case 0x07: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x07); break; \
516 : : case 0x08: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x08); break; \
517 : : case 0x09: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x09); break; \
518 : : case 0x0A: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x0A); break; \
519 : : case 0x0B: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x0B); break; \
520 : : case 0x0C: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x0C); break; \
521 : : case 0x0D: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x0D); break; \
522 : : case 0x0E: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x0E); break; \
523 : : case 0x0F: MOVEUNALIGNED_LEFT47_IMM(dst, src, n, 0x0F); break; \
524 : : default:; \
525 : : } \
526 : : }
527 : :
528 : : /**
529 : : * Copy bytes from one location to another,
530 : : * locations must not overlap.
531 : : * Use with n > 64.
532 : : */
533 : : static __rte_always_inline void *
534 : : rte_memcpy_generic_more_than_64(void *__rte_restrict dst, const void *__rte_restrict src,
535 : : size_t n)
536 : : {
537 : : __m128i xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8;
538 : : void *ret = dst;
539 : : size_t dstofss;
540 : : size_t srcofs;
541 : :
542 : : /**
543 : : * Fast way when copy size doesn't exceed 512 bytes
544 : : */
545 : : if (n <= 128) {
546 : : goto COPY_BLOCK_128_BACK15;
547 : : }
548 : : if (n <= 512) {
549 : : if (n >= 256) {
550 : : n -= 256;
551 : : rte_mov128((uint8_t *)dst, (const uint8_t *)src);
552 : : rte_mov128((uint8_t *)dst + 128, (const uint8_t *)src + 128);
553 : : src = (const uint8_t *)src + 256;
554 : : dst = (uint8_t *)dst + 256;
555 : : }
556 : : COPY_BLOCK_255_BACK15:
557 : : if (n >= 128) {
558 : : n -= 128;
559 : : rte_mov128((uint8_t *)dst, (const uint8_t *)src);
560 : : src = (const uint8_t *)src + 128;
561 : : dst = (uint8_t *)dst + 128;
562 : : }
563 : : COPY_BLOCK_128_BACK15:
564 : : if (n >= 64) {
565 : : n -= 64;
566 : : rte_mov64((uint8_t *)dst, (const uint8_t *)src);
567 : : src = (const uint8_t *)src + 64;
568 : : dst = (uint8_t *)dst + 64;
569 : : }
570 : : COPY_BLOCK_64_BACK15:
571 : : if (n >= 32) {
572 : : n -= 32;
573 : : rte_mov32((uint8_t *)dst, (const uint8_t *)src);
574 : : src = (const uint8_t *)src + 32;
575 : : dst = (uint8_t *)dst + 32;
576 : : }
577 : : if (n > 16) {
578 : : rte_mov16((uint8_t *)dst, (const uint8_t *)src);
579 : : rte_mov16((uint8_t *)dst - 16 + n, (const uint8_t *)src - 16 + n);
580 : : return ret;
581 : : }
582 : : if (n > 0) {
583 : : rte_mov16((uint8_t *)dst - 16 + n, (const uint8_t *)src - 16 + n);
584 : : }
585 : : return ret;
586 : : }
587 : :
588 : : /**
589 : : * Make store aligned when copy size exceeds 512 bytes,
590 : : * and make sure the first 15 bytes are copied, because
591 : : * unaligned copy functions require up to 15 bytes
592 : : * backwards access.
593 : : */
594 : : dstofss = (uintptr_t)dst & 0x0F;
595 : : if (dstofss > 0) {
596 : : dstofss = 16 - dstofss + 16;
597 : : n -= dstofss;
598 : : rte_mov32((uint8_t *)dst, (const uint8_t *)src);
599 : : src = (const uint8_t *)src + dstofss;
600 : : dst = (uint8_t *)dst + dstofss;
601 : : }
602 : : srcofs = ((uintptr_t)src & 0x0F);
603 : :
604 : : /**
605 : : * For aligned copy
606 : : */
607 : : if (srcofs == 0) {
608 : : /**
609 : : * Copy 256-byte blocks
610 : : */
611 : : for (; n >= 256; n -= 256) {
612 : : rte_mov256((uint8_t *)dst, (const uint8_t *)src);
613 : : dst = (uint8_t *)dst + 256;
614 : : src = (const uint8_t *)src + 256;
615 : : }
616 : :
617 : : /**
618 : : * Copy whatever left
619 : : */
620 : : goto COPY_BLOCK_255_BACK15;
621 : : }
622 : :
623 : : /**
624 : : * For copy with unaligned load
625 : : */
626 : : MOVEUNALIGNED_LEFT47(dst, src, n, srcofs);
627 : :
628 : : /**
629 : : * Copy whatever left
630 : : */
631 : : goto COPY_BLOCK_64_BACK15;
632 : : }
633 : :
634 : : #endif /* __AVX512F__ */
635 : :
636 : : /**
637 : : * Copy bytes from one vector register size aligned location to another,
638 : : * locations must not overlap.
639 : : * Use with n > 64.
640 : : */
641 : : static __rte_always_inline void *
642 : : rte_memcpy_aligned_more_than_64(void *__rte_restrict dst, const void *__rte_restrict src,
643 : : size_t n)
644 : : {
645 : : void *ret = dst;
646 : :
647 : : /* Copy 64 bytes blocks */
648 [ + + + + : 268628 : for (; n > 64; n -= 64) {
+ + + + +
+ - - + +
+ + + + -
- + + - -
- - - - -
- - - - -
- - - - +
+ + + - -
- - - - -
- - - - -
- - - - -
- + + - -
- - - - -
- - - - -
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
649 : 0 : rte_mov64((uint8_t *)dst, (const uint8_t *)src);
650 : 137057 : dst = (uint8_t *)dst + 64;
651 : 137057 : src = (const uint8_t *)src + 64;
652 : : }
653 : :
654 : : /* Copy whatever left */
655 : 131571 : rte_mov64((uint8_t *)dst - 64 + n,
656 : 131571 : (const uint8_t *)src - 64 + n);
657 : :
658 : 0 : return ret;
659 : : }
660 : :
661 : : /**
662 : : * Copy bytes from one location to another. The locations must not overlap.
663 : : *
664 : : * @param dst
665 : : * Pointer to the destination of the data.
666 : : * @param src
667 : : * Pointer to the source data.
668 : : * @param n
669 : : * Number of bytes to copy.
670 : : * @return
671 : : * Pointer to the destination data.
672 : : */
673 : : static __rte_always_inline void *
674 : 0 : rte_memcpy(void *__rte_restrict dst, const void *__rte_restrict src, size_t n)
675 : : {
676 : : /* Fast way when copy size doesn't exceed 64 bytes. */
677 [ + + + + : 10099491 : if (n < 16)
+ + + + +
+ + + + +
+ + - + -
+ + + - +
- - - - +
- + - + +
- + - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - -
+ - + - +
- - - - -
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
678 [ # # # # : 0 : return rte_mov15_or_less(dst, src, n);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
679 [ + + + + : 10053124 : if (n <= 32) {
+ + + + +
+ + + - +
+ + - + +
- - + + +
- - - - -
- - - + -
+ - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - -
- - - - -
- - - - -
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
680 [ + + - + : 626248 : if (__rte_constant(n) && n == 32) {
- + - - -
+ - - - +
- - - + -
- - + - -
- - - - -
+ - - - -
- - - + -
- - - - -
- + - - -
- - - - -
- - - - -
- - - - -
- + - - -
+ - - - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
681 : 0 : rte_mov32((uint8_t *)dst, (const uint8_t *)src);
682 : 0 : return dst;
683 : : }
684 [ # # # # : 0 : rte_mov16((uint8_t *)dst, (const uint8_t *)src);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
685 [ + + + - : 626248 : if (__rte_constant(n) && n == 16)
- + - - -
+ - - - +
- - - + -
- - + - -
- - - - -
+ - - - -
- - - + -
- - - - -
- + - - -
- - - - -
- - - - -
- - - - -
- + - - -
+ - - - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
686 : : return dst; /* avoid (harmless) duplicate copy */
687 : 626248 : rte_mov16((uint8_t *)dst - 16 + n, (const uint8_t *)src - 16 + n);
688 : 626248 : return dst;
689 : : }
690 [ + + + + : 9426876 : if (n <= 64) {
+ + + + -
+ - + - +
+ + - + -
- - + - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - -
- - - - -
- - - - -
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
691 [ - + - - : 1978714 : if (__rte_constant(n) && n == 64) {
- + - - -
+ - - - +
- - - - -
- - - - -
- - - - -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
692 : 0 : rte_mov64((uint8_t *)dst, (const uint8_t *)src);
693 : 0 : return dst;
694 : : }
695 : : #if defined RTE_MEMCPY_AVX
696 : 0 : rte_mov32((uint8_t *)dst, (const uint8_t *)src);
697 [ # # # # ]: 1978714 : rte_mov32((uint8_t *)dst - 32 + n, (const uint8_t *)src - 32 + n);
698 : : #else /* SSE implementation */
699 : : rte_mov16((uint8_t *)dst + 0 * 16, (const uint8_t *)src + 0 * 16);
700 : : rte_mov16((uint8_t *)dst + 1 * 16, (const uint8_t *)src + 1 * 16);
701 : : if (n > 48)
702 : : rte_mov16((uint8_t *)dst + 2 * 16, (const uint8_t *)src + 2 * 16);
703 : : rte_mov16((uint8_t *)dst - 16 + n, (const uint8_t *)src - 16 + n);
704 : : #endif
705 : 1978715 : return dst;
706 : : }
707 : :
708 : : /* Implementation for size > 64 bytes depends on alignment with vector register size. */
709 [ + + + + : 7448169 : if ((((uintptr_t)dst | (uintptr_t)src) & RTE_MEMCPY_ALIGNMENT_MASK) == 0)
+ + + + +
- - + + +
+ - + - -
- + + - -
- - - - -
- - - - -
- - - - +
- + - - -
- - - - -
- - - - -
- - - - -
- + - - -
- - - - -
- - - - -
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
710 : 131571 : return rte_memcpy_aligned_more_than_64(dst, src, n);
711 : : else
712 [ # # # # : 5 : return rte_memcpy_generic_more_than_64(dst, src, n);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
713 : : }
714 : :
715 : : #undef RTE_MEMCPY_ALIGNMENT_MASK
716 : :
717 : : #ifdef __cplusplus
718 : : }
719 : : #endif
720 : :
721 : : #endif /* _RTE_MEMCPY_X86_64_H_ */
|