Back to index

glibc  2.9
memset.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1997, 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <string.h>
00020 #include <memcopy.h>
00021 
00022 #undef memset
00023 
00024 void *
00025 memset (dstpp, c, len)
00026      void *dstpp;
00027      int c;
00028      size_t len;
00029 {
00030   long int dstp = (long int) dstpp;
00031 
00032   if (len >= 8)
00033     {
00034       size_t xlen;
00035       op_t cccc;
00036 
00037       cccc = (unsigned char) c;
00038       cccc |= cccc << 8;
00039       cccc |= cccc << 16;
00040       if (OPSIZ > 4)
00041        /* Do the shift in two steps to avoid warning if long has 32 bits.  */
00042        cccc |= (cccc << 16) << 16;
00043 
00044       /* There are at least some bytes to set.
00045         No need to test for LEN == 0 in this alignment loop.  */
00046       while (dstp % OPSIZ != 0)
00047        {
00048          ((byte *) dstp)[0] = c;
00049          dstp += 1;
00050          len -= 1;
00051        }
00052 
00053       /* Write 8 `op_t' per iteration until less than 8 `op_t' remain.  */
00054       xlen = len / (OPSIZ * 8);
00055       while (xlen > 0)
00056        {
00057          ((op_t *) dstp)[0] = cccc;
00058          ((op_t *) dstp)[1] = cccc;
00059          ((op_t *) dstp)[2] = cccc;
00060          ((op_t *) dstp)[3] = cccc;
00061          ((op_t *) dstp)[4] = cccc;
00062          ((op_t *) dstp)[5] = cccc;
00063          ((op_t *) dstp)[6] = cccc;
00064          ((op_t *) dstp)[7] = cccc;
00065          dstp += 8 * OPSIZ;
00066          xlen -= 1;
00067        }
00068       len %= OPSIZ * 8;
00069 
00070       /* Write 1 `op_t' per iteration until less than OPSIZ bytes remain.  */
00071       xlen = len / OPSIZ;
00072       while (xlen > 0)
00073        {
00074          ((op_t *) dstp)[0] = cccc;
00075          dstp += OPSIZ;
00076          xlen -= 1;
00077        }
00078       len %= OPSIZ;
00079     }
00080 
00081   /* Write the last few bytes.  */
00082   while (len > 0)
00083     {
00084       ((byte *) dstp)[0] = c;
00085       dstp += 1;
00086       len -= 1;
00087     }
00088 
00089   return dstpp;
00090 }
00091 libc_hidden_builtin_def (memset)