1*249415e0Skettenis /* $OpenBSD: memset.c,v 1.8 2017/01/24 08:09:05 kettenis Exp $ */
2bb80aa89Sderaadt /* $NetBSD: memset.c,v 1.6 1998/03/27 05:35:47 cgd Exp $ */
3bb80aa89Sderaadt
4bb80aa89Sderaadt /*-
5bb80aa89Sderaadt * Copyright (c) 1990, 1993
6bb80aa89Sderaadt * The Regents of the University of California. All rights reserved.
7bb80aa89Sderaadt *
8bb80aa89Sderaadt * This code is derived from software contributed to Berkeley by
9bb80aa89Sderaadt * Mike Hibler and Chris Torek.
10bb80aa89Sderaadt *
11bb80aa89Sderaadt * Redistribution and use in source and binary forms, with or without
12bb80aa89Sderaadt * modification, are permitted provided that the following conditions
13bb80aa89Sderaadt * are met:
14bb80aa89Sderaadt * 1. Redistributions of source code must retain the above copyright
15bb80aa89Sderaadt * notice, this list of conditions and the following disclaimer.
16bb80aa89Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
17bb80aa89Sderaadt * notice, this list of conditions and the following disclaimer in the
18bb80aa89Sderaadt * documentation and/or other materials provided with the distribution.
1929295d1cSmillert * 3. Neither the name of the University nor the names of its contributors
20bb80aa89Sderaadt * may be used to endorse or promote products derived from this software
21bb80aa89Sderaadt * without specific prior written permission.
22bb80aa89Sderaadt *
23bb80aa89Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24bb80aa89Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25bb80aa89Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26bb80aa89Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27bb80aa89Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28bb80aa89Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29bb80aa89Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30bb80aa89Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31bb80aa89Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32bb80aa89Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33bb80aa89Sderaadt * SUCH DAMAGE.
34bb80aa89Sderaadt */
35bb80aa89Sderaadt
36bb80aa89Sderaadt #include <sys/types.h>
37bb80aa89Sderaadt
385733d2deSespie #include <sys/limits.h>
39c83ce2fbSmickey #include <sys/systm.h>
40bb80aa89Sderaadt #include <lib/libkern/libkern.h>
41bb80aa89Sderaadt
42*249415e0Skettenis #undef bzero
43*249415e0Skettenis #undef memset
44*249415e0Skettenis
45bb80aa89Sderaadt #define wsize sizeof(u_int)
46bb80aa89Sderaadt #define wmask (wsize - 1)
47bb80aa89Sderaadt
48bb80aa89Sderaadt #ifdef BZERO
49bb80aa89Sderaadt #define RETURN return
50bb80aa89Sderaadt #define VAL 0
51bb80aa89Sderaadt #define WIDEVAL 0
52bb80aa89Sderaadt
53bb80aa89Sderaadt void
bzero(void * dst0,size_t length)54a26aa419Sderaadt bzero(void *dst0, size_t length)
55bb80aa89Sderaadt #else
56bb80aa89Sderaadt #define RETURN return (dst0)
57bb80aa89Sderaadt #define VAL c0
58bb80aa89Sderaadt #define WIDEVAL c
59bb80aa89Sderaadt
60bb80aa89Sderaadt void *
61a26aa419Sderaadt memset(void *dst0, int c0, size_t length)
62bb80aa89Sderaadt #endif
63bb80aa89Sderaadt {
64bb80aa89Sderaadt size_t t;
65c83ce2fbSmickey #ifndef BZERO
66bb80aa89Sderaadt u_int c;
67c83ce2fbSmickey #endif
68bb80aa89Sderaadt u_char *dst;
69bb80aa89Sderaadt
70bb80aa89Sderaadt dst = dst0;
71bb80aa89Sderaadt /*
72bb80aa89Sderaadt * If not enough words, just fill bytes. A length >= 2 words
73bb80aa89Sderaadt * guarantees that at least one of them is `complete' after
74bb80aa89Sderaadt * any necessary alignment. For instance:
75bb80aa89Sderaadt *
76bb80aa89Sderaadt * |-----------|-----------|-----------|
77bb80aa89Sderaadt * |00|01|02|03|04|05|06|07|08|09|0A|00|
78bb80aa89Sderaadt * ^---------------------^
79bb80aa89Sderaadt * dst dst+length-1
80bb80aa89Sderaadt *
81bb80aa89Sderaadt * but we use a minimum of 3 here since the overhead of the code
82bb80aa89Sderaadt * to do word writes is substantial.
83bb80aa89Sderaadt */
84bb80aa89Sderaadt if (length < 3 * wsize) {
85bb80aa89Sderaadt while (length != 0) {
86bb80aa89Sderaadt *dst++ = VAL;
87bb80aa89Sderaadt --length;
88bb80aa89Sderaadt }
89bb80aa89Sderaadt RETURN;
90bb80aa89Sderaadt }
91bb80aa89Sderaadt
92bb80aa89Sderaadt #ifndef BZERO
93bb80aa89Sderaadt if ((c = (u_char)c0) != 0) { /* Fill the word. */
94bb80aa89Sderaadt c = (c << 8) | c; /* u_int is 16 bits. */
95bb80aa89Sderaadt #if UINT_MAX > 0xffff
96bb80aa89Sderaadt c = (c << 16) | c; /* u_int is 32 bits. */
97bb80aa89Sderaadt #endif
98bb80aa89Sderaadt #if UINT_MAX > 0xffffffff
99bb80aa89Sderaadt c = (c << 32) | c; /* u_int is 64 bits. */
100bb80aa89Sderaadt #endif
101bb80aa89Sderaadt }
102bb80aa89Sderaadt #endif
103bb80aa89Sderaadt /* Align destination by filling in bytes. */
104bb80aa89Sderaadt if ((t = (u_long)dst & wmask) != 0) {
105bb80aa89Sderaadt t = wsize - t;
106bb80aa89Sderaadt length -= t;
107bb80aa89Sderaadt do {
108bb80aa89Sderaadt *dst++ = VAL;
109bb80aa89Sderaadt } while (--t != 0);
110bb80aa89Sderaadt }
111bb80aa89Sderaadt
112bb80aa89Sderaadt /* Fill words. Length was >= 2*words so we know t >= 1 here. */
113bb80aa89Sderaadt t = length / wsize;
114bb80aa89Sderaadt do {
115bb80aa89Sderaadt *(u_int *)dst = WIDEVAL;
116bb80aa89Sderaadt dst += wsize;
117bb80aa89Sderaadt } while (--t != 0);
118bb80aa89Sderaadt
119bb80aa89Sderaadt /* Mop up trailing bytes, if any. */
120bb80aa89Sderaadt t = length & wmask;
121bb80aa89Sderaadt if (t != 0)
122bb80aa89Sderaadt do {
123bb80aa89Sderaadt *dst++ = VAL;
124bb80aa89Sderaadt } while (--t != 0);
125bb80aa89Sderaadt RETURN;
126bb80aa89Sderaadt }
127