xref: /openbmc/u-boot/arch/x86/lib/string.c (revision 7029394d)
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  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24 
25 /* From glibc-2.14, sysdeps/i386/memset.c */
26 
27 #include <compiler.h>
28 #include <asm/string.h>
29 #include <linux/types.h>
30 
31 typedef uint32_t op_t;
32 
33 void *memset(void *dstpp, int c, size_t len)
34 {
35 	int d0;
36 	unsigned long int dstp = (unsigned long int) dstpp;
37 
38 	/* This explicit register allocation improves code very much indeed. */
39 	register op_t x asm("ax");
40 
41 	x = (unsigned char) c;
42 
43 	/* Clear the direction flag, so filling will move forward.  */
44 	asm volatile("cld");
45 
46 	/* This threshold value is optimal.  */
47 	if (len >= 12) {
48 		/* Fill X with four copies of the char we want to fill with. */
49 		x |= (x << 8);
50 		x |= (x << 16);
51 
52 		/* Adjust LEN for the bytes handled in the first loop.  */
53 		len -= (-dstp) % sizeof(op_t);
54 
55 		/*
56 		 * There are at least some bytes to set. No need to test for
57 		 * LEN == 0 in this alignment loop.
58 		 */
59 
60 		/* Fill bytes until DSTP is aligned on a longword boundary. */
61 		asm volatile(
62 			"rep\n"
63 			"stosb" /* %0, %2, %3 */ :
64 			"=D" (dstp), "=c" (d0) :
65 			"0" (dstp), "1" ((-dstp) % sizeof(op_t)), "a" (x) :
66 			"memory");
67 
68 		/* Fill longwords.  */
69 		asm volatile(
70 			"rep\n"
71 			"stosl" /* %0, %2, %3 */ :
72 			"=D" (dstp), "=c" (d0) :
73 			"0" (dstp), "1" (len / sizeof(op_t)), "a" (x) :
74 			"memory");
75 		len %= sizeof(op_t);
76 	}
77 
78 	/* Write the last few bytes. */
79 	asm volatile(
80 		"rep\n"
81 		"stosb" /* %0, %2, %3 */ :
82 		"=D" (dstp), "=c" (d0) :
83 		"0" (dstp), "1" (len), "a" (x) :
84 		"memory");
85 
86 	return dstpp;
87 }
88 
89 #define	OP_T_THRES	8
90 #define OPSIZ	(sizeof(op_t))
91 
92 #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes)				  \
93 do {									  \
94 	int __d0;							  \
95 	asm volatile(							  \
96 		/* Clear the direction flag, so copying goes forward.  */ \
97 		"cld\n"							  \
98 		/* Copy bytes.  */					  \
99 		"rep\n"							  \
100 		"movsb" :						  \
101 		"=D" (dst_bp), "=S" (src_bp), "=c" (__d0) :		  \
102 		"0" (dst_bp), "1" (src_bp), "2" (nbytes) :		  \
103 		"memory");						  \
104 } while (0)
105 
106 #define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes)		  \
107 do {									  \
108 	int __d0;							  \
109 	asm volatile(							  \
110 		/* Clear the direction flag, so copying goes forward.  */ \
111 		"cld\n"							  \
112 		/* Copy longwords.  */					  \
113 		"rep\n"							  \
114 		"movsl" :						  \
115 		"=D" (dst_bp), "=S" (src_bp), "=c" (__d0) :		  \
116 		"0" (dst_bp), "1" (src_bp), "2" ((nbytes) / 4) :	  \
117 		"memory");						  \
118 	(nbytes_left) = (nbytes) % 4;					  \
119 } while (0)
120 
121 void *memcpy(void *dstpp, const void *srcpp, size_t len)
122 {
123 	unsigned long int dstp = (long int)dstpp;
124 	unsigned long int srcp = (long int)srcpp;
125 
126 	/* Copy from the beginning to the end.  */
127 
128 	/* If there not too few bytes to copy, use word copy.  */
129 	if (len >= OP_T_THRES) {
130 		/* Copy just a few bytes to make DSTP aligned.  */
131 		len -= (-dstp) % OPSIZ;
132 		BYTE_COPY_FWD(dstp, srcp, (-dstp) % OPSIZ);
133 
134 		/* Copy from SRCP to DSTP taking advantage of the known
135 		 * alignment of DSTP.  Number of bytes remaining is put
136 		 * in the third argument, i.e. in LEN.  This number may
137 		 * vary from machine to machine.
138 		 */
139 		WORD_COPY_FWD(dstp, srcp, len, len);
140 
141 		/* Fall out and copy the tail.  */
142 	}
143 
144 	/* There are just a few bytes to copy. Use byte memory operations. */
145 	BYTE_COPY_FWD(dstp, srcp, len);
146 
147 	return dstpp;
148 }
149