1 /* $NetBSD: bzero.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
35 * from: Header: bzero.s,v 1.1 92/06/25 12:52:46 torek Exp
38 #include <machine/asm.h>
39 #if defined(LIBC_SCCS) && !defined(lint)
41 .asciz "@(#)bzero.s 8.1 (Berkeley) 6/4/93"
43 RCSID("$NetBSD: bzero.S,v 1.1 2005/12/20 19:28:50 christos Exp $")
45 #endif /* LIBC_SCCS and not lint */
48 * We should unroll the loop, but at the moment this would
49 * gain nothing since the `std' instructions are what limits us.
55 * memset(void *addr, int pattern, size_t len)
58 ! %o0 = addr, %o1 = pattern, %o2 = len
60 * Expand the byte pattern to fill 64 bits in an even-aligned
61 * register pair; shuffle arguments to match those of bzero.
64 mov %o2, %o1 ! shuffle argument
67 mov %o0, %g1 ! save original pointer
70 ! Optimize a common case: addr and len are both multiples of 8.
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
78 * bzero(void *addr, size_t len)
81 ! %o0 = addr, %o1 = len
84 ! Optimize a common case: addr and len are both multiples of 8.
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
91 /* `Good' operands, can just store doubles. */
93 deccc 8, %o1 ! while ((len -= 8) >= 0)
95 std %o2, [%o0 + %o1] ! *(quad *)(addr + len) = 0;
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.
105 cmp %o1, 15 ! len >= 15?
106 bge,a Lstd ! yes, use std
107 btst 1, %o0 ! (but first check alignment)
109 ! not enough to bother: do byte-at-a-time loop.
111 deccc %o1 ! while (--len >= 0)
113 stb %o2, [%o0 + %o1] ! addr[len] = 0;
119 * There are at least 15 bytes to zero.
120 * We may have to zero some initial stuff to align
123 bz,a 1f ! if (addr & 1) {
125 stb %o2, [%o0] ! *addr = 0;
130 bz,a 1f ! if (addr & 2) {
132 sth %o2, [%o0] ! *(short *)addr = 0;
133 inc 2, %o0 ! addr += 2;
134 dec 2, %o1 ! len -= 2;
137 bz 1f ! if (addr & 4) {
139 st %o2, [%o0] ! *(int *)addr = 0;
140 inc 4, %o0 ! addr += 4;
141 dec 4, %o1 ! len -= 4;
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.).
149 std %o2, [%o0] ! do {
150 2: ! *(quad *)addr = 0;
151 inc 8, %o0 ! addr += 8;
152 deccc 8, %o1 ! } while ((len -= 8) >= 0);
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.
161 bz 1f ! if (len & 4) {
163 st %o2, [%o0] ! *(int *)addr = 0;
164 inc 4, %o0 ! addr += 4;
166 bz 1f ! if (len & 2) {
168 sth %o2, [%o0] ! *(short *)addr = 0;
169 inc 2, %o0 ! addr += 2;
171 bnz,a 1f ! if (len & 1)
172 stb %o2, [%o0] ! *addr = 0;
176 mov %g1, %o0 ! restore original pointer