Add native memset() function on x86
[coreboot.git] / src / arch / x86 / lib / memset.c
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  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 /* From glibc-2.14, sysdeps/i386/memset.c */
25
26 #include <string.h>
27 #include <stdint.h>
28
29 typedef uint32_t op_t;
30
31 void *memset(void *dstpp, int c, size_t len)
32 {
33         int d0;
34         unsigned long int dstp = (unsigned long int) dstpp;
35
36         /* This explicit register allocation improves code very much indeed. */
37         register op_t x asm("ax");
38
39         x = (unsigned char) c;
40
41         /* Clear the direction flag, so filling will move forward.  */
42         asm volatile("cld");
43
44         /* This threshold value is optimal.  */
45         if (len >= 12) {
46                 /* Fill X with four copies of the char we want to fill with. */
47                 x |= (x << 8);
48                 x |= (x << 16);
49
50                 /* Adjust LEN for the bytes handled in the first loop.  */
51                 len -= (-dstp) % sizeof(op_t);
52
53                 /*
54                  * There are at least some bytes to set. No need to test for
55                  * LEN == 0 in this alignment loop.
56                  */
57
58                 /* Fill bytes until DSTP is aligned on a longword boundary. */
59                 asm volatile(
60                         "rep\n"
61                         "stosb" /* %0, %2, %3 */ :
62                         "=D" (dstp), "=c" (d0) :
63                         "0" (dstp), "1" ((-dstp) % sizeof(op_t)), "a" (x) :
64                         "memory");
65
66                 /* Fill longwords.  */
67                 asm volatile(
68                         "rep\n"
69                         "stosl" /* %0, %2, %3 */ :
70                         "=D" (dstp), "=c" (d0) :
71                         "0" (dstp), "1" (len / sizeof(op_t)), "a" (x) :
72                         "memory");
73                 len %= sizeof(op_t);
74         }
75
76         /* Write the last few bytes. */
77         asm volatile(
78                 "rep\n"
79                 "stosb" /* %0, %2, %3 */ :
80                 "=D" (dstp), "=c" (d0) :
81                 "0" (dstp), "1" (len), "a" (x) :
82                 "memory");
83
84         return dstpp;
85 }