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