Back to index

glibc  2.9
memset.c
Go to the documentation of this file.
00001 /* Set a block of memory to some byte value.
00002    For Intel 80x86, x>=3.
00003    Copyright (C) 1991,1992,1993,1997,1998,2003, 2005 Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005    Contributed by Torbjorn Granlund (tege@sics.se).
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 #include <string.h>
00023 #include <memcopy.h>
00024 
00025 #ifdef __GNUC__
00026 
00027 #undef memset
00028 
00029 void *
00030 memset (void *dstpp, int c, size_t len)
00031 {
00032   int d0;
00033   unsigned long int dstp = (unsigned long int) dstpp;
00034 
00035   /* This explicit register allocation
00036      improves code very much indeed.  */
00037   register op_t x asm("ax");
00038 
00039   x = (unsigned char) c;
00040 
00041   /* Clear the direction flag, so filling will move forward.  */
00042   asm volatile("cld");
00043 
00044   /* This threshold value is optimal.  */
00045   if (len >= 12)
00046     {
00047       /* Fill X with four copies of the char we want to fill with.  */
00048       x |= (x << 8);
00049       x |= (x << 16);
00050 
00051       /* Adjust LEN for the bytes handled in the first loop.  */
00052       len -= (-dstp) % OPSIZ;
00053 
00054       /* There are at least some bytes to set.
00055         No need to test for LEN == 0 in this alignment loop.  */
00056 
00057       /* Fill bytes until DSTP is aligned on a longword boundary.  */
00058       asm volatile("rep\n"
00059                  "stosb" /* %0, %2, %3 */ :
00060                  "=D" (dstp), "=c" (d0) :
00061                  "0" (dstp), "1" ((-dstp) % OPSIZ), "a" (x) :
00062                  "memory");
00063 
00064       /* Fill longwords.  */
00065       asm volatile("rep\n"
00066                  "stosl" /* %0, %2, %3 */ :
00067                  "=D" (dstp), "=c" (d0) :
00068                  "0" (dstp), "1" (len / OPSIZ), "a" (x) :
00069                  "memory");
00070       len %= OPSIZ;
00071     }
00072 
00073   /* Write the last few bytes.  */
00074   asm volatile("rep\n"
00075               "stosb" /* %0, %2, %3 */ :
00076               "=D" (dstp), "=c" (d0) :
00077               "0" (dstp), "1" (len), "a" (x) :
00078               "memory");
00079 
00080   return dstpp;
00081 }
00082 libc_hidden_builtin_def (memset)
00083 
00084 #else
00085 #include <string/memset.c>
00086 #endif