xref: /openbmc/u-boot/arch/x86/lib/string.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2dbaef6efSGabe Black /*
3dbaef6efSGabe Black  * Copyright (C) 1991,1992,1993,1997,1998,2003, 2005 Free Software Foundation, Inc.
4dbaef6efSGabe Black  * This file is part of the GNU C Library.
5dbaef6efSGabe Black  * Copyright (c) 2011 The Chromium OS Authors.
6dbaef6efSGabe Black  */
7dbaef6efSGabe Black 
8dbaef6efSGabe Black /* From glibc-2.14, sysdeps/i386/memset.c */
9dbaef6efSGabe Black 
10dbaef6efSGabe Black #include <linux/types.h>
11afc366f0SMasahiro Yamada #include <linux/compiler.h>
12afc366f0SMasahiro Yamada #include <asm/string.h>
13dbaef6efSGabe Black 
14dbaef6efSGabe Black typedef uint32_t op_t;
15dbaef6efSGabe Black 
memset(void * dstpp,int c,size_t len)16dbaef6efSGabe Black void *memset(void *dstpp, int c, size_t len)
17dbaef6efSGabe Black {
18dbaef6efSGabe Black 	int d0;
19dbaef6efSGabe Black 	unsigned long int dstp = (unsigned long int) dstpp;
20dbaef6efSGabe Black 
21dbaef6efSGabe Black 	/* This explicit register allocation improves code very much indeed. */
22dbaef6efSGabe Black 	register op_t x asm("ax");
23dbaef6efSGabe Black 
24dbaef6efSGabe Black 	x = (unsigned char) c;
25dbaef6efSGabe Black 
26dbaef6efSGabe Black 	/* Clear the direction flag, so filling will move forward.  */
27dbaef6efSGabe Black 	asm volatile("cld");
28dbaef6efSGabe Black 
29dbaef6efSGabe Black 	/* This threshold value is optimal.  */
30dbaef6efSGabe Black 	if (len >= 12) {
31dbaef6efSGabe Black 		/* Fill X with four copies of the char we want to fill with. */
32dbaef6efSGabe Black 		x |= (x << 8);
33dbaef6efSGabe Black 		x |= (x << 16);
34dbaef6efSGabe Black 
35dbaef6efSGabe Black 		/* Adjust LEN for the bytes handled in the first loop.  */
36dbaef6efSGabe Black 		len -= (-dstp) % sizeof(op_t);
37dbaef6efSGabe Black 
38dbaef6efSGabe Black 		/*
39dbaef6efSGabe Black 		 * There are at least some bytes to set. No need to test for
40dbaef6efSGabe Black 		 * LEN == 0 in this alignment loop.
41dbaef6efSGabe Black 		 */
42dbaef6efSGabe Black 
43dbaef6efSGabe Black 		/* Fill bytes until DSTP is aligned on a longword boundary. */
44dbaef6efSGabe Black 		asm volatile(
45dbaef6efSGabe Black 			"rep\n"
46dbaef6efSGabe Black 			"stosb" /* %0, %2, %3 */ :
47dbaef6efSGabe Black 			"=D" (dstp), "=c" (d0) :
48dbaef6efSGabe Black 			"0" (dstp), "1" ((-dstp) % sizeof(op_t)), "a" (x) :
49dbaef6efSGabe Black 			"memory");
50dbaef6efSGabe Black 
51dbaef6efSGabe Black 		/* Fill longwords.  */
52dbaef6efSGabe Black 		asm volatile(
53dbaef6efSGabe Black 			"rep\n"
54dbaef6efSGabe Black 			"stosl" /* %0, %2, %3 */ :
55dbaef6efSGabe Black 			"=D" (dstp), "=c" (d0) :
56dbaef6efSGabe Black 			"0" (dstp), "1" (len / sizeof(op_t)), "a" (x) :
57dbaef6efSGabe Black 			"memory");
58dbaef6efSGabe Black 		len %= sizeof(op_t);
59dbaef6efSGabe Black 	}
60dbaef6efSGabe Black 
61dbaef6efSGabe Black 	/* Write the last few bytes. */
62dbaef6efSGabe Black 	asm volatile(
63dbaef6efSGabe Black 		"rep\n"
64dbaef6efSGabe Black 		"stosb" /* %0, %2, %3 */ :
65dbaef6efSGabe Black 		"=D" (dstp), "=c" (d0) :
66dbaef6efSGabe Black 		"0" (dstp), "1" (len), "a" (x) :
67dbaef6efSGabe Black 		"memory");
68dbaef6efSGabe Black 
69dbaef6efSGabe Black 	return dstpp;
70dbaef6efSGabe Black }
71b2c2a038SGraeme Russ 
72b2c2a038SGraeme Russ #define	OP_T_THRES	8
73b2c2a038SGraeme Russ #define OPSIZ	(sizeof(op_t))
74b2c2a038SGraeme Russ 
75b2c2a038SGraeme Russ #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes)				  \
76b2c2a038SGraeme Russ do {									  \
77b2c2a038SGraeme Russ 	int __d0;							  \
78b2c2a038SGraeme Russ 	asm volatile(							  \
79b2c2a038SGraeme Russ 		/* Clear the direction flag, so copying goes forward.  */ \
80b2c2a038SGraeme Russ 		"cld\n"							  \
81b2c2a038SGraeme Russ 		/* Copy bytes.  */					  \
82b2c2a038SGraeme Russ 		"rep\n"							  \
83b2c2a038SGraeme Russ 		"movsb" :						  \
84b2c2a038SGraeme Russ 		"=D" (dst_bp), "=S" (src_bp), "=c" (__d0) :		  \
85b2c2a038SGraeme Russ 		"0" (dst_bp), "1" (src_bp), "2" (nbytes) :		  \
86b2c2a038SGraeme Russ 		"memory");						  \
87b2c2a038SGraeme Russ } while (0)
88b2c2a038SGraeme Russ 
89b2c2a038SGraeme Russ #define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes)		  \
90b2c2a038SGraeme Russ do {									  \
91b2c2a038SGraeme Russ 	int __d0;							  \
92b2c2a038SGraeme Russ 	asm volatile(							  \
93b2c2a038SGraeme Russ 		/* Clear the direction flag, so copying goes forward.  */ \
94b2c2a038SGraeme Russ 		"cld\n"							  \
95b2c2a038SGraeme Russ 		/* Copy longwords.  */					  \
96b2c2a038SGraeme Russ 		"rep\n"							  \
97b2c2a038SGraeme Russ 		"movsl" :						  \
98b2c2a038SGraeme Russ 		"=D" (dst_bp), "=S" (src_bp), "=c" (__d0) :		  \
99b2c2a038SGraeme Russ 		"0" (dst_bp), "1" (src_bp), "2" ((nbytes) / 4) :	  \
100b2c2a038SGraeme Russ 		"memory");						  \
101b2c2a038SGraeme Russ 	(nbytes_left) = (nbytes) % 4;					  \
102b2c2a038SGraeme Russ } while (0)
103b2c2a038SGraeme Russ 
memcpy(void * dstpp,const void * srcpp,size_t len)104b2c2a038SGraeme Russ void *memcpy(void *dstpp, const void *srcpp, size_t len)
105b2c2a038SGraeme Russ {
106b2c2a038SGraeme Russ 	unsigned long int dstp = (long int)dstpp;
107b2c2a038SGraeme Russ 	unsigned long int srcp = (long int)srcpp;
108b2c2a038SGraeme Russ 
109b2c2a038SGraeme Russ 	/* Copy from the beginning to the end.  */
110b2c2a038SGraeme Russ 
111b2c2a038SGraeme Russ 	/* If there not too few bytes to copy, use word copy.  */
112b2c2a038SGraeme Russ 	if (len >= OP_T_THRES) {
113b2c2a038SGraeme Russ 		/* Copy just a few bytes to make DSTP aligned.  */
114b2c2a038SGraeme Russ 		len -= (-dstp) % OPSIZ;
115b2c2a038SGraeme Russ 		BYTE_COPY_FWD(dstp, srcp, (-dstp) % OPSIZ);
116b2c2a038SGraeme Russ 
117b2c2a038SGraeme Russ 		/* Copy from SRCP to DSTP taking advantage of the known
118b2c2a038SGraeme Russ 		 * alignment of DSTP.  Number of bytes remaining is put
119b2c2a038SGraeme Russ 		 * in the third argument, i.e. in LEN.  This number may
120b2c2a038SGraeme Russ 		 * vary from machine to machine.
121b2c2a038SGraeme Russ 		 */
122b2c2a038SGraeme Russ 		WORD_COPY_FWD(dstp, srcp, len, len);
123b2c2a038SGraeme Russ 
124b2c2a038SGraeme Russ 		/* Fall out and copy the tail.  */
125b2c2a038SGraeme Russ 	}
126b2c2a038SGraeme Russ 
127b2c2a038SGraeme Russ 	/* There are just a few bytes to copy. Use byte memory operations. */
128b2c2a038SGraeme Russ 	BYTE_COPY_FWD(dstp, srcp, len);
129b2c2a038SGraeme Russ 
130b2c2a038SGraeme Russ 	return dstpp;
131b2c2a038SGraeme Russ }
132a5b87225SSimon Glass 
memmove(void * dest,const void * src,size_t n)133a5b87225SSimon Glass void *memmove(void *dest, const void *src, size_t n)
134a5b87225SSimon Glass {
135a5b87225SSimon Glass 	int d0, d1, d2, d3, d4, d5;
136a5b87225SSimon Glass 	char *ret = dest;
137a5b87225SSimon Glass 
138a5b87225SSimon Glass 	__asm__ __volatile__(
139a5b87225SSimon Glass 		/* Handle more 16 bytes in loop */
140a5b87225SSimon Glass 		"cmp $0x10, %0\n\t"
141a5b87225SSimon Glass 		"jb	1f\n\t"
142a5b87225SSimon Glass 
143a5b87225SSimon Glass 		/* Decide forward/backward copy mode */
144a5b87225SSimon Glass 		"cmp %2, %1\n\t"
145a5b87225SSimon Glass 		"jb	2f\n\t"
146a5b87225SSimon Glass 
147a5b87225SSimon Glass 		/*
148a5b87225SSimon Glass 		 * movs instruction have many startup latency
149a5b87225SSimon Glass 		 * so we handle small size by general register.
150a5b87225SSimon Glass 		 */
151a5b87225SSimon Glass 		"cmp  $680, %0\n\t"
152a5b87225SSimon Glass 		"jb 3f\n\t"
153a5b87225SSimon Glass 		/* movs instruction is only good for aligned case */
154a5b87225SSimon Glass 		"mov %1, %3\n\t"
155a5b87225SSimon Glass 		"xor %2, %3\n\t"
156a5b87225SSimon Glass 		"and $0xff, %3\n\t"
157a5b87225SSimon Glass 		"jz 4f\n\t"
158a5b87225SSimon Glass 		"3:\n\t"
159a5b87225SSimon Glass 		"sub $0x10, %0\n\t"
160a5b87225SSimon Glass 
161a5b87225SSimon Glass 		/* We gobble 16 bytes forward in each loop */
162a5b87225SSimon Glass 		"3:\n\t"
163a5b87225SSimon Glass 		"sub $0x10, %0\n\t"
164a5b87225SSimon Glass 		"mov 0*4(%1), %3\n\t"
165a5b87225SSimon Glass 		"mov 1*4(%1), %4\n\t"
166a5b87225SSimon Glass 		"mov  %3, 0*4(%2)\n\t"
167a5b87225SSimon Glass 		"mov  %4, 1*4(%2)\n\t"
168a5b87225SSimon Glass 		"mov 2*4(%1), %3\n\t"
169a5b87225SSimon Glass 		"mov 3*4(%1), %4\n\t"
170a5b87225SSimon Glass 		"mov  %3, 2*4(%2)\n\t"
171a5b87225SSimon Glass 		"mov  %4, 3*4(%2)\n\t"
172a5b87225SSimon Glass 		"lea  0x10(%1), %1\n\t"
173a5b87225SSimon Glass 		"lea  0x10(%2), %2\n\t"
174a5b87225SSimon Glass 		"jae 3b\n\t"
175a5b87225SSimon Glass 		"add $0x10, %0\n\t"
176a5b87225SSimon Glass 		"jmp 1f\n\t"
177a5b87225SSimon Glass 
178a5b87225SSimon Glass 		/* Handle data forward by movs */
179a5b87225SSimon Glass 		".p2align 4\n\t"
180a5b87225SSimon Glass 		"4:\n\t"
181a5b87225SSimon Glass 		"mov -4(%1, %0), %3\n\t"
182a5b87225SSimon Glass 		"lea -4(%2, %0), %4\n\t"
183a5b87225SSimon Glass 		"shr $2, %0\n\t"
184a5b87225SSimon Glass 		"rep movsl\n\t"
185a5b87225SSimon Glass 		"mov %3, (%4)\n\t"
186a5b87225SSimon Glass 		"jmp 11f\n\t"
187a5b87225SSimon Glass 		/* Handle data backward by movs */
188a5b87225SSimon Glass 		".p2align 4\n\t"
189a5b87225SSimon Glass 		"6:\n\t"
190a5b87225SSimon Glass 		"mov (%1), %3\n\t"
191a5b87225SSimon Glass 		"mov %2, %4\n\t"
192a5b87225SSimon Glass 		"lea -4(%1, %0), %1\n\t"
193a5b87225SSimon Glass 		"lea -4(%2, %0), %2\n\t"
194a5b87225SSimon Glass 		"shr $2, %0\n\t"
195a5b87225SSimon Glass 		"std\n\t"
196a5b87225SSimon Glass 		"rep movsl\n\t"
197a5b87225SSimon Glass 		"mov %3,(%4)\n\t"
198a5b87225SSimon Glass 		"cld\n\t"
199a5b87225SSimon Glass 		"jmp 11f\n\t"
200a5b87225SSimon Glass 
201a5b87225SSimon Glass 		/* Start to prepare for backward copy */
202a5b87225SSimon Glass 		".p2align 4\n\t"
203a5b87225SSimon Glass 		"2:\n\t"
204a5b87225SSimon Glass 		"cmp  $680, %0\n\t"
205a5b87225SSimon Glass 		"jb 5f\n\t"
206a5b87225SSimon Glass 		"mov %1, %3\n\t"
207a5b87225SSimon Glass 		"xor %2, %3\n\t"
208a5b87225SSimon Glass 		"and $0xff, %3\n\t"
209a5b87225SSimon Glass 		"jz 6b\n\t"
210a5b87225SSimon Glass 
211a5b87225SSimon Glass 		/* Calculate copy position to tail */
212a5b87225SSimon Glass 		"5:\n\t"
213a5b87225SSimon Glass 		"add %0, %1\n\t"
214a5b87225SSimon Glass 		"add %0, %2\n\t"
215a5b87225SSimon Glass 		"sub $0x10, %0\n\t"
216a5b87225SSimon Glass 
217a5b87225SSimon Glass 		/* We gobble 16 bytes backward in each loop */
218a5b87225SSimon Glass 		"7:\n\t"
219a5b87225SSimon Glass 		"sub $0x10, %0\n\t"
220a5b87225SSimon Glass 
221a5b87225SSimon Glass 		"mov -1*4(%1), %3\n\t"
222a5b87225SSimon Glass 		"mov -2*4(%1), %4\n\t"
223a5b87225SSimon Glass 		"mov  %3, -1*4(%2)\n\t"
224a5b87225SSimon Glass 		"mov  %4, -2*4(%2)\n\t"
225a5b87225SSimon Glass 		"mov -3*4(%1), %3\n\t"
226a5b87225SSimon Glass 		"mov -4*4(%1), %4\n\t"
227a5b87225SSimon Glass 		"mov  %3, -3*4(%2)\n\t"
228a5b87225SSimon Glass 		"mov  %4, -4*4(%2)\n\t"
229a5b87225SSimon Glass 		"lea  -0x10(%1), %1\n\t"
230a5b87225SSimon Glass 		"lea  -0x10(%2), %2\n\t"
231a5b87225SSimon Glass 		"jae 7b\n\t"
232a5b87225SSimon Glass 		/* Calculate copy position to head */
233a5b87225SSimon Glass 		"add $0x10, %0\n\t"
234a5b87225SSimon Glass 		"sub %0, %1\n\t"
235a5b87225SSimon Glass 		"sub %0, %2\n\t"
236a5b87225SSimon Glass 
237a5b87225SSimon Glass 		/* Move data from 8 bytes to 15 bytes */
238a5b87225SSimon Glass 		".p2align 4\n\t"
239a5b87225SSimon Glass 		"1:\n\t"
240a5b87225SSimon Glass 		"cmp $8, %0\n\t"
241a5b87225SSimon Glass 		"jb 8f\n\t"
242a5b87225SSimon Glass 		"mov 0*4(%1), %3\n\t"
243a5b87225SSimon Glass 		"mov 1*4(%1), %4\n\t"
244a5b87225SSimon Glass 		"mov -2*4(%1, %0), %5\n\t"
245a5b87225SSimon Glass 		"mov -1*4(%1, %0), %1\n\t"
246a5b87225SSimon Glass 
247a5b87225SSimon Glass 		"mov  %3, 0*4(%2)\n\t"
248a5b87225SSimon Glass 		"mov  %4, 1*4(%2)\n\t"
249a5b87225SSimon Glass 		"mov  %5, -2*4(%2, %0)\n\t"
250a5b87225SSimon Glass 		"mov  %1, -1*4(%2, %0)\n\t"
251a5b87225SSimon Glass 		"jmp 11f\n\t"
252a5b87225SSimon Glass 
253a5b87225SSimon Glass 		/* Move data from 4 bytes to 7 bytes */
254a5b87225SSimon Glass 		".p2align 4\n\t"
255a5b87225SSimon Glass 		"8:\n\t"
256a5b87225SSimon Glass 		"cmp $4, %0\n\t"
257a5b87225SSimon Glass 		"jb 9f\n\t"
258a5b87225SSimon Glass 		"mov 0*4(%1), %3\n\t"
259a5b87225SSimon Glass 		"mov -1*4(%1, %0), %4\n\t"
260a5b87225SSimon Glass 		"mov  %3, 0*4(%2)\n\t"
261a5b87225SSimon Glass 		"mov  %4, -1*4(%2, %0)\n\t"
262a5b87225SSimon Glass 		"jmp 11f\n\t"
263a5b87225SSimon Glass 
264a5b87225SSimon Glass 		/* Move data from 2 bytes to 3 bytes */
265a5b87225SSimon Glass 		".p2align 4\n\t"
266a5b87225SSimon Glass 		"9:\n\t"
267a5b87225SSimon Glass 		"cmp $2, %0\n\t"
268a5b87225SSimon Glass 		"jb 10f\n\t"
269a5b87225SSimon Glass 		"movw 0*2(%1), %%dx\n\t"
270a5b87225SSimon Glass 		"movw -1*2(%1, %0), %%bx\n\t"
271a5b87225SSimon Glass 		"movw %%dx, 0*2(%2)\n\t"
272a5b87225SSimon Glass 		"movw %%bx, -1*2(%2, %0)\n\t"
273a5b87225SSimon Glass 		"jmp 11f\n\t"
274a5b87225SSimon Glass 
275a5b87225SSimon Glass 		/* Move data for 1 byte */
276a5b87225SSimon Glass 		".p2align 4\n\t"
277a5b87225SSimon Glass 		"10:\n\t"
278a5b87225SSimon Glass 		"cmp $1, %0\n\t"
279a5b87225SSimon Glass 		"jb 11f\n\t"
280a5b87225SSimon Glass 		"movb (%1), %%cl\n\t"
281a5b87225SSimon Glass 		"movb %%cl, (%2)\n\t"
282a5b87225SSimon Glass 		".p2align 4\n\t"
283a5b87225SSimon Glass 		"11:"
284a5b87225SSimon Glass 		: "=&c" (d0), "=&S" (d1), "=&D" (d2),
285a5b87225SSimon Glass 		  "=r" (d3), "=r" (d4), "=r"(d5)
286a5b87225SSimon Glass 		: "0" (n),
287a5b87225SSimon Glass 		 "1" (src),
288a5b87225SSimon Glass 		 "2" (dest)
289a5b87225SSimon Glass 		: "memory");
290a5b87225SSimon Glass 
291a5b87225SSimon Glass 	return ret;
292a5b87225SSimon Glass }
293