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