xref: /csrg-svn/lib/libc/string/memset.c (revision 59960)
141967Sbostic /*-
241967Sbostic  * Copyright (c) 1990 The Regents of the University of California.
334479Sbostic  * All rights reserved.
434479Sbostic  *
541967Sbostic  * This code is derived from software contributed to Berkeley by
659959Sbostic  * Mike Hibler and Chris Torek.
741967Sbostic  *
841967Sbostic  * %sccs.include.redist.c%
924197Skre  */
1024197Skre 
1126529Sdonn #if defined(LIBC_SCCS) && !defined(lint)
12*59960Sbostic static char sccsid[] = "@(#)memset.c	5.8 (Berkeley) 05/12/93";
1334479Sbostic #endif /* LIBC_SCCS and not lint */
1424197Skre 
1559959Sbostic #include <sys/types.h>
1659959Sbostic 
1759959Sbostic #include <limits.h>
1841967Sbostic #include <string.h>
1941967Sbostic 
2059959Sbostic #define	wsize	sizeof(u_int)
2159959Sbostic #define	wmask	(wsize - 1)
2259959Sbostic 
23*59960Sbostic #ifdef BZERO
24*59960Sbostic void
25*59960Sbostic bzero(dst0, length)
26*59960Sbostic 	void *dst0;
27*59960Sbostic 	register size_t length;
28*59960Sbostic #else
2941967Sbostic void *
3059959Sbostic memset(dst0, c0, length)
3159959Sbostic 	void *dst0;
3259959Sbostic 	register int c0;
3359959Sbostic 	register size_t length;
34*59960Sbostic #endif
3524197Skre {
3659959Sbostic 	register size_t t;
3759959Sbostic 	register u_int c;
38*59960Sbostic 	register u_char *dst;
3924197Skre 
40*59960Sbostic 	dst = dst0;
4159959Sbostic 	/*
4259959Sbostic 	 * If not enough words, just fill bytes.  A length >= 2 words
4359959Sbostic 	 * guarantees that at least one of them is `complete' after
4459959Sbostic 	 * any necessary alignment.  For instance:
4559959Sbostic 	 *
4659959Sbostic 	 *	|-----------|-----------|-----------|
4759959Sbostic 	 *	|00|01|02|03|04|05|06|07|08|09|0A|00|
4859959Sbostic 	 *	          ^---------------------^
4959959Sbostic 	 *		 dst		 dst+length-1
5059959Sbostic 	 *
5159959Sbostic 	 * but we use a minimum of 3 here since the overhead of the code
5259959Sbostic 	 * to do word writes is substantial.
5359959Sbostic 	 */
5459959Sbostic 	if (length < 3 * wsize) {
5559959Sbostic 		while (length != 0) {
56*59960Sbostic #ifdef BZERO
57*59960Sbostic 			*dst++ = 0;
58*59960Sbostic #else
5959959Sbostic 			*dst++ = c0;
60*59960Sbostic #endif
6159959Sbostic 			--length;
6259959Sbostic 		}
63*59960Sbostic #ifdef BZERO
64*59960Sbostic 		return;
65*59960Sbostic #else
6659959Sbostic 		return (dst0);
67*59960Sbostic #endif
6859959Sbostic 	}
6924197Skre 
70*59960Sbostic #ifndef BZERO
71*59960Sbostic 	if ((c = (u_char)c0) != 0) {	/* Fill the word. */
7259959Sbostic 		c = (c << 8) | c;	/* u_int is 16 bits. */
73*59960Sbostic #if UINT_MAX > 0xffff
7459959Sbostic 		c = (c << 16) | c;	/* u_int is 32 bits. */
7559959Sbostic #endif
76*59960Sbostic #if UINT_MAX > 0xffffffff
7759959Sbostic 		c = (c << 32) | c;	/* u_int is 64 bits. */
7859959Sbostic #endif
7941967Sbostic 	}
80*59960Sbostic #endif
8159959Sbostic 	/* Align destination by filling in bytes. */
8259959Sbostic 	if ((t = (int)dst & wmask) != 0) {
8359959Sbostic 		t = wsize - t;
8459959Sbostic 		length -= t;
8559959Sbostic 		do {
86*59960Sbostic #ifdef BZERO
87*59960Sbostic 			*dst++ = 0;
88*59960Sbostic #else
8959959Sbostic 			*dst++ = c0;
90*59960Sbostic #endif
9159959Sbostic 		} while (--t != 0);
9259959Sbostic 	}
9359959Sbostic 
9459959Sbostic 	/* Fill words.  Length was >= 2*words so we know t >= 1 here. */
9559959Sbostic 	t = length / wsize;
9659959Sbostic 	do {
97*59960Sbostic #ifdef	BZERO
98*59960Sbostic 		*(u_int *)dst = 0;
99*59960Sbostic #else
10059959Sbostic 		*(u_int *)dst = c;
101*59960Sbostic #endif
10259959Sbostic 		dst += wsize;
10359959Sbostic 	} while (--t != 0);
10459959Sbostic 
10559959Sbostic 	/* Mop up trailing bytes, if any. */
10659959Sbostic 	t = length & wmask;
10759959Sbostic 	if (t != 0)
10859959Sbostic 		do {
109*59960Sbostic #ifdef BZERO
110*59960Sbostic 			*dst++ = 0;
111*59960Sbostic #else
11259959Sbostic 			*dst++ = c0;
113*59960Sbostic #endif
11459959Sbostic 		} while (--t != 0);
115*59960Sbostic #ifdef BZERO
116*59960Sbostic 	return;
117*59960Sbostic #else
11859959Sbostic 	return (dst0);
119*59960Sbostic #endif
12024197Skre }
121