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.
6 * SPDX-License-Identifier: GPL-2.0+
9 /* From glibc-2.14, sysdeps/i386/memset.c */
11 #include <linux/types.h>
12 #include <linux/compiler.h>
13 #include <asm/string.h>
15 typedef uint32_t op_t;
17 void *memset(void *dstpp, int c, size_t len)
20 unsigned long int dstp = (unsigned long int) dstpp;
22 /* This explicit register allocation improves code very much indeed. */
23 register op_t x asm("ax");
25 x = (unsigned char) c;
27 /* Clear the direction flag, so filling will move forward. */
30 /* This threshold value is optimal. */
32 /* Fill X with four copies of the char we want to fill with. */
36 /* Adjust LEN for the bytes handled in the first loop. */
37 len -= (-dstp) % sizeof(op_t);
40 * There are at least some bytes to set. No need to test for
41 * LEN == 0 in this alignment loop.
44 /* Fill bytes until DSTP is aligned on a longword boundary. */
47 "stosb" /* %0, %2, %3 */ :
48 "=D" (dstp), "=c" (d0) :
49 "0" (dstp), "1" ((-dstp) % sizeof(op_t)), "a" (x) :
55 "stosl" /* %0, %2, %3 */ :
56 "=D" (dstp), "=c" (d0) :
57 "0" (dstp), "1" (len / sizeof(op_t)), "a" (x) :
62 /* Write the last few bytes. */
65 "stosb" /* %0, %2, %3 */ :
66 "=D" (dstp), "=c" (d0) :
67 "0" (dstp), "1" (len), "a" (x) :
74 #define OPSIZ (sizeof(op_t))
76 #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes) \
80 /* Clear the direction flag, so copying goes forward. */ \
85 "=D" (dst_bp), "=S" (src_bp), "=c" (__d0) : \
86 "0" (dst_bp), "1" (src_bp), "2" (nbytes) : \
90 #define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes) \
94 /* Clear the direction flag, so copying goes forward. */ \
96 /* Copy longwords. */ \
99 "=D" (dst_bp), "=S" (src_bp), "=c" (__d0) : \
100 "0" (dst_bp), "1" (src_bp), "2" ((nbytes) / 4) : \
102 (nbytes_left) = (nbytes) % 4; \
105 void *memcpy(void *dstpp, const void *srcpp, size_t len)
107 unsigned long int dstp = (long int)dstpp;
108 unsigned long int srcp = (long int)srcpp;
110 /* Copy from the beginning to the end. */
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);
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.
123 WORD_COPY_FWD(dstp, srcp, len, len);
125 /* Fall out and copy the tail. */
128 /* There are just a few bytes to copy. Use byte memory operations. */
129 BYTE_COPY_FWD(dstp, srcp, len);