1/*	$NetBSD: bzero.S,v 1.5 2003/08/07 16:42:24 agc Exp $	*/
2
3/*
4 * Copyright (c) 1992, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * from: Header: bzero.s,v 1.1 92/06/25 12:52:46 torek Exp
36 */
37
38#include <machine/asm.h>
39#if defined(LIBC_SCCS) && !defined(lint)
40#if 0
41	.asciz "@(#)bzero.s	8.1 (Berkeley) 6/4/93"
42#else
43	RCSID("$NetBSD: bzero.S,v 1.5 2003/08/07 16:42:24 agc Exp $")
44#endif
45#endif  /* LIBC_SCCS and not lint */
46
47/*
48 * We should unroll the loop, but at the moment this would
49 * gain nothing since the `std' instructions are what limits us.
50 */
51
52#ifdef MEMSET
53/*
54 * void *
55 * memset(void *addr, int pattern, size_t len)
56 */
57ENTRY(memset)
58	! %o0 = addr, %o1 = pattern, %o2 = len
59	/*
60	 * Expand the byte pattern to fill 64 bits in an even-aligned
61	 * register pair; shuffle arguments to match those of bzero.
62	 */
63	and	%o1, 0xff, %o3
64	mov	%o2, %o1		! shuffle argument
65	sll	%o3, 8, %o2
66	or	%o2, %o3, %o2
67	mov	%o0, %g1		! save original pointer
68	sll	%o2, 16, %o3
69	or	%o2, %o3, %o2
70	! Optimize a common case: addr and len are both multiples of 8.
71	or	%o0, %o1, %o5
72	btst	7, %o5			! ((addr | len) & 7) != 0?
73	bnz	1f			! if so, cannot optimize
74	 mov	%o2, %o3		! in any case, complete pat expansion
75#else
76/*
77 * void
78 * bzero(void *addr, size_t len)
79 */
80ENTRY(bzero)
81	! %o0 = addr, %o1 = len
82
83	clr	%o2
84	! Optimize a common case: addr and len are both multiples of 8.
85	or	%o0, %o1, %o5
86	btst	7, %o5			! ((addr | len) & 7) != 0?
87	bnz	1f			! if so, cannot optimize
88	 clr	%o3			! in any case, we want o3=0
89#endif
90
91	/* `Good' operands, can just store doubles. */
920:
93	deccc	8, %o1			! while ((len -= 8) >= 0)
94	bge,a	0b
95	 std	%o2, [%o0 + %o1]	!	*(quad *)(addr + len) = 0;
96	retl
97	 nop
98
99	/*
100	 * Either the address is unaligned, or the count is not a
101	 * multiple of 8, or both.  We will have to align the address
102	 * in order to use anything `better' than stb.
103	 */
1041:
105	cmp	%o1, 15			! len >= 15?
106	bge,a	Lstd			! yes, use std
107	 btst	1, %o0			! (but first check alignment)
108
109	! not enough to bother: do byte-at-a-time loop.
1102:
111	deccc	%o1			! while (--len >= 0)
112	bge,a	2b
113	 stb	%o2, [%o0 + %o1]	!	addr[len] = 0;
114	retl
115	 nop
116
117Lstd:
118	/*
119	 * There are at least 15 bytes to zero.
120	 * We may have to zero some initial stuff to align
121	 * the address.
122	 */
123	bz,a	1f			! if (addr & 1) {
124	 btst	2, %o0
125	stb	%o2, [%o0]		!	*addr = 0;
126	inc	%o0			!	addr++;
127	dec	%o1			!	len--;
128	btst	2, %o0			! }
1291:
130	bz,a	1f			! if (addr & 2) {
131	 btst	4, %o0
132	sth	%o2, [%o0]		!	*(short *)addr = 0;
133	inc	2, %o0			!	addr += 2;
134	dec	2, %o1			!	len -= 2;
135	btst	4, %o0			! }
1361:
137	bz	1f			! if (addr & 4) {
138	 dec	8, %o1
139	st	%o2, [%o0]		!	*(int *)addr = 0;
140	inc	4, %o0			!	addr += 4;
141	dec	4, %o1			!	len -= 4;
142					! }
143	/*
144	 * Address is double word aligned; len is 8 less than
145	 * the number of bytes remaining (i.e., len is 0 if
146	 * the remaining count is 8, 1 if it is 9, etc.).
147	 */
1481:
149	std	%o2, [%o0]		! do {
1502:					!	*(quad *)addr = 0;
151	inc	8, %o0			!	addr += 8;
152	deccc	8, %o1			! } while ((len -= 8) >= 0);
153	 bge,a	2b
154	std	%o2, [%o0]
155
156	/*
157	 * Len is in [-8..-1] where -8 => done, -7 => 1 byte to zero,
158	 * -6 => two bytes, etc.  Mop up this remainder, if any.
159	 */
160	btst	4, %o1
161	bz	1f			! if (len & 4) {
162	 btst	2, %o1
163	st	%o2, [%o0]		!	*(int *)addr = 0;
164	inc	4, %o0			!	addr += 4;
1651:
166	bz	1f			! if (len & 2) {
167	 btst	1, %o1
168	sth	%o2, [%o0]		!	*(short *)addr = 0;
169	inc	2, %o0			!	addr += 2;
1701:
171	bnz,a	1f			! if (len & 1)
172	 stb	%o2, [%o0]		!	*addr = 0;
1731:
174	retl
175#ifdef MEMSET
176	 mov	%g1, %o0		! restore original pointer
177#else
178	 nop
179#endif
180