xref: /openbmc/u-boot/arch/x86/lib/string.c (revision 23ff8633)
1 /*
2  * Copyright (C) 1991,1992,1993,1997,1998,2003, 2005 Free Software Foundation, Inc.
3  * This file is part of the GNU C Library.
4  * Copyright (c) 2011 The Chromium OS Authors.
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 /* From glibc-2.14, sysdeps/i386/memset.c */
10 
11 #include <linux/types.h>
12 #include <linux/compiler.h>
13 #include <asm/string.h>
14 
15 typedef uint32_t op_t;
16 
17 void *memset(void *dstpp, int c, size_t len)
18 {
19 	int d0;
20 	unsigned long int dstp = (unsigned long int) dstpp;
21 
22 	/* This explicit register allocation improves code very much indeed. */
23 	register op_t x asm("ax");
24 
25 	x = (unsigned char) c;
26 
27 	/* Clear the direction flag, so filling will move forward.  */
28 	asm volatile("cld");
29 
30 	/* This threshold value is optimal.  */
31 	if (len >= 12) {
32 		/* Fill X with four copies of the char we want to fill with. */
33 		x |= (x << 8);
34 		x |= (x << 16);
35 
36 		/* Adjust LEN for the bytes handled in the first loop.  */
37 		len -= (-dstp) % sizeof(op_t);
38 
39 		/*
40 		 * There are at least some bytes to set. No need to test for
41 		 * LEN == 0 in this alignment loop.
42 		 */
43 
44 		/* Fill bytes until DSTP is aligned on a longword boundary. */
45 		asm volatile(
46 			"rep\n"
47 			"stosb" /* %0, %2, %3 */ :
48 			"=D" (dstp), "=c" (d0) :
49 			"0" (dstp), "1" ((-dstp) % sizeof(op_t)), "a" (x) :
50 			"memory");
51 
52 		/* Fill longwords.  */
53 		asm volatile(
54 			"rep\n"
55 			"stosl" /* %0, %2, %3 */ :
56 			"=D" (dstp), "=c" (d0) :
57 			"0" (dstp), "1" (len / sizeof(op_t)), "a" (x) :
58 			"memory");
59 		len %= sizeof(op_t);
60 	}
61 
62 	/* Write the last few bytes. */
63 	asm volatile(
64 		"rep\n"
65 		"stosb" /* %0, %2, %3 */ :
66 		"=D" (dstp), "=c" (d0) :
67 		"0" (dstp), "1" (len), "a" (x) :
68 		"memory");
69 
70 	return dstpp;
71 }
72 
73 #define	OP_T_THRES	8
74 #define OPSIZ	(sizeof(op_t))
75 
76 #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes)				  \
77 do {									  \
78 	int __d0;							  \
79 	asm volatile(							  \
80 		/* Clear the direction flag, so copying goes forward.  */ \
81 		"cld\n"							  \
82 		/* Copy bytes.  */					  \
83 		"rep\n"							  \
84 		"movsb" :						  \
85 		"=D" (dst_bp), "=S" (src_bp), "=c" (__d0) :		  \
86 		"0" (dst_bp), "1" (src_bp), "2" (nbytes) :		  \
87 		"memory");						  \
88 } while (0)
89 
90 #define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes)		  \
91 do {									  \
92 	int __d0;							  \
93 	asm volatile(							  \
94 		/* Clear the direction flag, so copying goes forward.  */ \
95 		"cld\n"							  \
96 		/* Copy longwords.  */					  \
97 		"rep\n"							  \
98 		"movsl" :						  \
99 		"=D" (dst_bp), "=S" (src_bp), "=c" (__d0) :		  \
100 		"0" (dst_bp), "1" (src_bp), "2" ((nbytes) / 4) :	  \
101 		"memory");						  \
102 	(nbytes_left) = (nbytes) % 4;					  \
103 } while (0)
104 
105 void *memcpy(void *dstpp, const void *srcpp, size_t len)
106 {
107 	unsigned long int dstp = (long int)dstpp;
108 	unsigned long int srcp = (long int)srcpp;
109 
110 	/* Copy from the beginning to the end.  */
111 
112 	/* If there not too few bytes to copy, use word copy.  */
113 	if (len >= OP_T_THRES) {
114 		/* Copy just a few bytes to make DSTP aligned.  */
115 		len -= (-dstp) % OPSIZ;
116 		BYTE_COPY_FWD(dstp, srcp, (-dstp) % OPSIZ);
117 
118 		/* Copy from SRCP to DSTP taking advantage of the known
119 		 * alignment of DSTP.  Number of bytes remaining is put
120 		 * in the third argument, i.e. in LEN.  This number may
121 		 * vary from machine to machine.
122 		 */
123 		WORD_COPY_FWD(dstp, srcp, len, len);
124 
125 		/* Fall out and copy the tail.  */
126 	}
127 
128 	/* There are just a few bytes to copy. Use byte memory operations. */
129 	BYTE_COPY_FWD(dstp, srcp, len);
130 
131 	return dstpp;
132 }
133