summaryrefslogtreecommitdiff
path: root/arch/openrisc/lib/memcpy.c
blob: e2af9b5108043388d362275aa8486640e42f58a1 (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
// SPDX-License-Identifier: GPL-2.0
/*
 * arch/openrisc/lib/memcpy.c
 *
 * Optimized memory copy routines for openrisc.  These are mostly copied
 * from ohter sources but slightly entended based on ideas discuassed in
 * #openrisc.
 *
 * The word unroll implementation is an extension to the arm byte
 * unrolled implementation, but using word copies (if things are
 * properly aligned)
 *
 * The great arm loop unroll algorithm can be found at:
 *  arch/arm/boot/compressed/string.c
 */

#include <linux/export.h>

#include <linux/string.h>

#ifdef CONFIG_OR1K_1200
/*
 * Do memcpy with word copies and loop unrolling. This gives the
 * best performance on the OR1200 and MOR1KX archirectures
 */
void *memcpy(void *dest, __const void *src, __kernel_size_t n)
{
	int i = 0;
	unsigned char *d, *s;
	uint32_t *dest_w = (uint32_t *)dest, *src_w = (uint32_t *)src;

	/* If both source and dest are word aligned copy words */
	if (!((unsigned int)dest_w & 3) && !((unsigned int)src_w & 3)) {
		/* Copy 32 bytes per loop */
		for (i = n >> 5; i > 0; i--) {
			*dest_w++ = *src_w++;
			*dest_w++ = *src_w++;
			*dest_w++ = *src_w++;
			*dest_w++ = *src_w++;
			*dest_w++ = *src_w++;
			*dest_w++ = *src_w++;
			*dest_w++ = *src_w++;
			*dest_w++ = *src_w++;
		}

		if (n & 1 << 4) {
			*dest_w++ = *src_w++;
			*dest_w++ = *src_w++;
			*dest_w++ = *src_w++;
			*dest_w++ = *src_w++;
		}

		if (n & 1 << 3) {
			*dest_w++ = *src_w++;
			*dest_w++ = *src_w++;
		}

		if (n & 1 << 2)
			*dest_w++ = *src_w++;

		d = (unsigned char *)dest_w;
		s = (unsigned char *)src_w;

	} else {
		d = (unsigned char *)dest_w;
		s = (unsigned char *)src_w;

		for (i = n >> 3; i > 0; i--) {
			*d++ = *s++;
			*d++ = *s++;
			*d++ = *s++;
			*d++ = *s++;
			*d++ = *s++;
			*d++ = *s++;
			*d++ = *s++;
			*d++ = *s++;
		}

		if (n & 1 << 2) {
			*d++ = *s++;
			*d++ = *s++;
			*d++ = *s++;
			*d++ = *s++;
		}
	}

	if (n & 1 << 1) {
		*d++ = *s++;
		*d++ = *s++;
	}

	if (n & 1)
		*d++ = *s++;

	return dest;
}
#else
/*
 * Use word copies but no loop unrolling as we cannot assume there
 * will be benefits on the archirecture
 */
void *memcpy(void *dest, __const void *src, __kernel_size_t n)
{
	unsigned char *d, *s;
	uint32_t *dest_w = (uint32_t *)dest, *src_w = (uint32_t *)src;

	/* If both source and dest are word aligned copy words */
	if (!((unsigned int)dest_w & 3) && !((unsigned int)src_w & 3)) {
		for (; n >= 4; n -= 4)
			*dest_w++ = *src_w++;
	}

	d = (unsigned char *)dest_w;
	s = (unsigned char *)src_w;

	/* For remaining or if not aligned, copy bytes */
	for (; n >= 1; n -= 1)
		*d++ = *s++;

	return dest;

}
#endif

EXPORT_SYMBOL(memcpy);