aboutsummaryrefslogtreecommitdiffstats
path: root/lib/librte_eal/common/include/arch/arm/rte_memcpy_64.h
blob: beb97a71ef0cefc62892b7e0981fd3746b3c1e27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2015 Cavium, Inc
 */

#ifndef _RTE_MEMCPY_ARM64_H_
#define _RTE_MEMCPY_ARM64_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <string.h>

#include "generic/rte_memcpy.h"

#ifdef RTE_ARCH_ARM64_MEMCPY
#include <rte_common.h>
#include <rte_branch_prediction.h>

/*
 * The memory copy performance differs on different AArch64 micro-architectures.
 * And the most recent glibc (e.g. 2.23 or later) can provide a better memcpy()
 * performance compared to old glibc versions. It's always suggested to use a
 * more recent glibc if possible, from which the entire system can get benefit.
 *
 * This implementation improves memory copy on some aarch64 micro-architectures,
 * when an old glibc (e.g. 2.19, 2.17...) is being used. It is disabled by
 * default and needs "RTE_ARCH_ARM64_MEMCPY" defined to activate. It's not
 * always providing better performance than memcpy() so users need to run unit
 * test "memcpy_perf_autotest" and customize parameters in customization section
 * below for best performance.
 *
 * Compiler version will also impact the rte_memcpy() performance. It's observed
 * on some platforms and with the same code, GCC 7.2.0 compiled binaries can
 * provide better performance than GCC 4.8.5 compiled binaries.
 */

/**************************************
 * Beginning of customization section
 **************************************/
#ifndef RTE_ARM64_MEMCPY_ALIGN_MASK
#define RTE_ARM64_MEMCPY_ALIGN_MASK ((RTE_CACHE_LINE_SIZE >> 3) - 1)
#endif

#ifndef RTE_ARM64_MEMCPY_STRICT_ALIGN
/* Only src unalignment will be treated as unaligned copy */
#define RTE_ARM64_MEMCPY_IS_UNALIGNED_COPY(dst, src) \
	((uintptr_t)(src) & RTE_ARM64_MEMCPY_ALIGN_MASK)
#else
/* Both dst and src unalignment will be treated as unaligned copy */
#define RTE_ARM64_MEMCPY_IS_UNALIGNED_COPY(dst, src) \
	(((uintptr_t)(dst) | (uintptr_t)(src)) & RTE_ARM64_MEMCPY_ALIGN_MASK)
#endif


/*
 * If copy size is larger than threshold, memcpy() will be used.
 * Run "memcpy_perf_autotest" to determine the proper threshold.
 */
#ifdef RTE_ARM64_MEMCPY_ALIGNED_THRESHOLD
#define USE_ALIGNED_RTE_MEMCPY(dst, src, n) \
(!RTE_ARM64_MEMCPY_IS_UNALIGNED_COPY(dst, src) && \
n <= (size_t)RTE_ARM64_MEMCPY_ALIGNED_THRESHOLD)
#else
#define USE_ALIGNED_RTE_MEMCPY(dst, src, n) \
(!RTE_ARM64_MEMCPY_IS_UNALIGNED_COPY(dst, src))
#endif
#ifdef RTE_ARM64_MEMCPY_UNALIGNED_THRESHOLD
#define USE_UNALIGNED_RTE_MEMCPY(dst, src, n) \
(RTE_ARM64_MEMCPY_IS_UNALIGNED_COPY(dst, src) && \
n <= (size_t)RTE_ARM64_MEMCPY_UNALIGNED_THRESHOLD)
#else
#define USE_UNALIGNED_RTE_MEMCPY(dst, src, n) \
(RTE_ARM64_MEMCPY_IS_UNALIGNED_COPY(dst, src))
#endif
/*
 * The logic of USE_RTE_MEMCPY() can also be modified to best fit platform.
 */
#if defined(RTE_ARM64_MEMCPY_ALIGNED_THRESHOLD) \
|| defined(RTE_ARM64_MEMCPY_UNALIGNED_THRESHOLD)
#define USE_RTE_MEMCPY(dst, src, n) \
(USE_ALIGNED_RTE_MEMCPY(dst, src, n) || USE_UNALIGNED_RTE_MEMCPY(dst, src, n))
#else
#define USE_RTE_MEMCPY(dst, src, n) (1)
#endif
/**************************************
 * End of customization section
 **************************************/


#if defined(RTE_TOOLCHAIN_GCC) && !defined(RTE_ARM64_MEMCPY_SKIP_GCC_VER_CHECK)
#if (GCC_VERSION < 50400)
#warning "The GCC version is quite old, which may result in sub-optimal \
performance of the compiled code. It is suggested that at least GCC 5.4.0 \
be used."
#endif
#endif

static __rte_always_inline
void rte_mov16(uint8_t *dst, const uint8_t *src)
{
	__uint128_t *dst128 = (__uint128_t *)dst;
	const __uint128_t *src128 = (const __uint128_t *)src;
	*dst128 = *src128;
}

static __rte_always_inline
void rte_mov32(uint8_t *dst, const uint8_t *src)
{
	__uint128_t *dst128 = (__uint128_t *)dst;
	const __uint128_t *src128 = (const __uint128_t *)src;
	const __uint128_t x0 = src128[0], x1 = src128[1];
	dst128[0] = x0;
	dst128[1] = x1;
}

static __rte_always_inline
void rte_mov48(uint8_t *dst, const uint8_t *src)
{
	__uint128_t *dst128 = (__uint128_t *)dst;
	const __uint128_t *src128 = (const __uint128_t *)src;
	const __uint128_t x0 = src128[0], x1 = src128[1], x2 = src128[2];
	dst128[0] = x0;
	dst128[1] = x1;
	dst128[2] = x2;
}

static __rte_always_inline
void rte_mov64(uint8_t *dst, const uint8_t *src)
{
	__uint128_t *dst128 = (__uint128_t *)dst;
	const __uint128_t *src128 = (const __uint128_t *)src;
	const __uint128_t
		x0 = src128[0], x1 = src128[1], x2 = src128[2], x3 = src128[3];
	dst128[0] = x0;
	dst128[1] = x1;
	dst128[2] = x2;
	dst128[3] = x3;
}

static __rte_always_inline
void rte_mov128(uint8_t *dst, const uint8_t *src)
{
	__uint128_t *dst128 = (__uint128_t *)dst;
	const __uint128_t *src128 = (const __uint128_t *)src;
	/* Keep below declaration & copy sequence for optimized instructions */
	const __uint128_t
		x0 = src128[0], x1 = src128[1], x2 = src128[2], x3 = src128[3];
	dst128[0] = x0;
	__uint128_t x4 = src128[4];
	dst128[1] = x1;
	__uint128_t x5 = src128[5];
	dst128[2] = x2;
	__uint128_t x6 = src128[6];
	dst128[3] = x3;
	__uint128_t x7 = src128[7];
	dst128[4] = x4;
	dst128[5] = x5;
	dst128[6] = x6;
	dst128[7] = x7;
}

static __rte_always_inline
void rte_mov256(uint8_t *dst, const uint8_t *src)
{
	rte_mov128(dst, src);
	rte_mov128(dst + 128, src + 128);
}

static __rte_always_inline void
rte_memcpy_lt16(uint8_t *dst, const uint8_t *src, size_t n)
{
	if (n & 0x08) {
		/* copy 8 ~ 15 bytes */
		*(uint64_t *)dst = *(const uint64_t *)src;
		*(uint64_t *)(dst - 8 + n) = *(const uint64_t *)(src - 8 + n);
	} else if (n & 0x04) {
		/* copy 4 ~ 7 bytes */
		*(uint32_t *)dst = *(const uint32_t *)src;
		*(uint32_t *)(dst - 4 + n) = *(const uint32_t *)(src - 4 + n);
	} else if (n & 0x02) {
		/* copy 2 ~ 3 bytes */
		*(uint16_t *)dst = *(const uint16_t *)src;
		*(uint16_t *)(dst - 2 + n) = *(const uint16_t *)(src - 2 + n);
	} else if (n & 0x01) {
		/* copy 1 byte */
		*dst = *src;
	}
}

static __rte_always_inline
void rte_memcpy_ge16_lt128(uint8_t *dst, const uint8_t *src, size_t n)
{
	if (n < 64) {
		if (n == 16) {
			rte_mov16(dst, src);
		} else if (n <= 32) {
			rte_mov16(dst, src);
			rte_mov16(dst - 16 + n, src - 16 + n);
		} else if (n <= 48) {
			rte_mov32(dst, src);
			rte_mov16(dst - 16 + n, src - 16 + n);
		} else {
			rte_mov48(dst, src);
			rte_mov16(dst - 16 + n, src - 16 + n);
		}
	} else {
		rte_mov64((uint8_t *)dst, (const uint8_t *)src);
		if (n > 48 + 64)
			rte_mov64(dst - 64 + n, src - 64 + n);
		else if (n > 32 + 64)
			rte_mov48(dst - 48 + n, src - 48 + n);
		else if (n > 16 + 64)
			rte_mov32(dst - 32 + n, src - 32 + n);
		else if (n > 64)
			rte_mov16(dst - 16 + n, src - 16 + n);
	}
}

static __rte_always_inline
void rte_memcpy_ge128(uint8_t *dst, const uint8_t *src, size_t n)
{
	do {
		rte_mov128(dst, src);
		src += 128;
		dst += 128;
		n -= 128;
	} while (likely(n >= 128));

	if (likely(n)) {
		if (n <= 16)
			rte_mov16(dst - 16 + n, src - 16 + n);
		else if (n <= 32)
			rte_mov32(dst - 32 + n, src - 32 + n);
		else if (n <= 48)
			rte_mov48(dst - 48 + n, src - 48 + n);
		else if (n <= 64)
			rte_mov64(dst - 64 + n, src - 64 + n);
		else
			rte_memcpy_ge16_lt128(dst, src, n);
	}
}

static __rte_always_inline
void rte_memcpy_ge16_lt64(uint8_t *dst, const uint8_t *src, size_t n)
{
	if (n == 16) {
		rte_mov16(dst, src);
	} else if (n <= 32) {
		rte_mov16(dst, src);
		rte_mov16(dst - 16 + n, src - 16 + n);
	} else if (n <= 48) {
		rte_mov32(dst, src);
		rte_mov16(dst - 16 + n, src - 16 + n);
	} else {
		rte_mov48(dst, src);
		rte_mov16(dst - 16 + n, src - 16 + n);
	}
}

static __rte_always_inline
void rte_memcpy_ge64(uint8_t *dst, const uint8_t *src, size_t n)
{
	do {
		rte_mov64(dst, src);
		src += 64;
		dst += 64;
		n -= 64;
	} while (likely(n >= 64));

	if (likely(n)) {
		if (n <= 16)
			rte_mov16(dst - 16 + n, src - 16 + n);
		else if (n <= 32)
			rte_mov32(dst - 32 + n, src - 32 + n);
		else if (n <= 48)
			rte_mov48(dst - 48 + n, src - 48 + n);
		else
			rte_mov64(dst - 64 + n, src - 64 + n);
	}
}

#if RTE_CACHE_LINE_SIZE >= 128
static __rte_always_inline
void *rte_memcpy(void *dst, const void *src, size_t n)
{
	if (n < 16) {
		rte_memcpy_lt16((uint8_t *)dst, (const uint8_t *)src, n);
		return dst;
	}
	if (n < 128) {
		rte_memcpy_ge16_lt128((uint8_t *)dst, (const uint8_t *)src, n);
		return dst;
	}
	__builtin_prefetch(src, 0, 0);
	__builtin_prefetch(dst, 1, 0);
	if (likely(USE_RTE_MEMCPY(dst, src, n))) {
		rte_memcpy_ge128((uint8_t *)dst, (const uint8_t *)src, n);
		return dst;
	} else
		return memcpy(dst, src, n);
}

#else
static __rte_always_inline
void *rte_memcpy(void *dst, const void *src, size_t n)
{
	if (n < 16) {
		rte_memcpy_lt16((uint8_t *)dst, (const uint8_t *)src, n);
		return dst;
	}
	if (n < 64) {
		rte_memcpy_ge16_lt64((uint8_t *)dst, (const uint8_t *)src, n);
		return dst;
	}
	__builtin_prefetch(src, 0, 0);
	__builtin_prefetch(dst, 1, 0);
	if (likely(USE_RTE_MEMCPY(dst, src, n))) {
		rte_memcpy_ge64((uint8_t *)dst, (const uint8_t *)src, n);
		return dst;
	} else
		return memcpy(dst, src, n);
}
#endif /* RTE_CACHE_LINE_SIZE >= 128 */

#else
static inline void
rte_mov16(uint8_t *dst, const uint8_t *src)
{
	memcpy(dst, src, 16);
}

static inline void
rte_mov32(uint8_t *dst, const uint8_t *src)
{
	memcpy(dst, src, 32);
}

static inline void
rte_mov48(uint8_t *dst, const uint8_t *src)
{
	memcpy(dst, src, 48);
}

static inline void
rte_mov64(uint8_t *dst, const uint8_t *src)
{
	memcpy(dst, src, 64);
}

static inline void
rte_mov128(uint8_t *dst, const uint8_t *src)
{
	memcpy(dst, src, 128);
}

static inline void
rte_mov256(uint8_t *dst, const uint8_t *src)
{
	memcpy(dst, src, 256);
}

#define rte_memcpy(d, s, n)	memcpy((d), (s), (n))

#endif /* RTE_ARCH_ARM64_MEMCPY */

#ifdef __cplusplus
}
#endif

#endif /* _RTE_MEMCPY_ARM_64_H_ */