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