Back to index

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