xref: /freebsd/lib/libc/arm/string/memset.S (revision 6c5bc49c5a803101b52861068594eacf6775e08c)
12357939bSOlivier Houchard/*	$NetBSD: memset.S,v 1.4 2003/10/14 07:51:45 scw Exp $	*/
22357939bSOlivier Houchard
32357939bSOlivier Houchard/*
42357939bSOlivier Houchard * Copyright 2003 Wasabi Systems, Inc.
52357939bSOlivier Houchard * All rights reserved.
62357939bSOlivier Houchard *
72357939bSOlivier Houchard * Written by Steve C. Woodford for Wasabi Systems, Inc.
82357939bSOlivier Houchard *
92357939bSOlivier Houchard * Redistribution and use in source and binary forms, with or without
102357939bSOlivier Houchard * modification, are permitted provided that the following conditions
112357939bSOlivier Houchard * are met:
122357939bSOlivier Houchard * 1. Redistributions of source code must retain the above copyright
132357939bSOlivier Houchard *    notice, this list of conditions and the following disclaimer.
142357939bSOlivier Houchard * 2. Redistributions in binary form must reproduce the above copyright
152357939bSOlivier Houchard *    notice, this list of conditions and the following disclaimer in the
162357939bSOlivier Houchard *    documentation and/or other materials provided with the distribution.
172357939bSOlivier Houchard * 3. All advertising materials mentioning features or use of this software
182357939bSOlivier Houchard *    must display the following acknowledgement:
192357939bSOlivier Houchard *      This product includes software developed for the NetBSD Project by
202357939bSOlivier Houchard *      Wasabi Systems, Inc.
212357939bSOlivier Houchard * 4. The name of Wasabi Systems, Inc. may not be used to endorse
222357939bSOlivier Houchard *    or promote products derived from this software without specific prior
232357939bSOlivier Houchard *    written permission.
242357939bSOlivier Houchard *
252357939bSOlivier Houchard * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
262357939bSOlivier Houchard * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
272357939bSOlivier Houchard * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
282357939bSOlivier Houchard * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
292357939bSOlivier Houchard * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
302357939bSOlivier Houchard * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
312357939bSOlivier Houchard * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
322357939bSOlivier Houchard * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
332357939bSOlivier Houchard * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
342357939bSOlivier Houchard * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
352357939bSOlivier Houchard * POSSIBILITY OF SUCH DAMAGE.
362357939bSOlivier Houchard */
372357939bSOlivier Houchard/*
382357939bSOlivier Houchard * Copyright (c) 1995 Mark Brinicombe.
392357939bSOlivier Houchard * All rights reserved.
402357939bSOlivier Houchard *
412357939bSOlivier Houchard * Redistribution and use in source and binary forms, with or without
422357939bSOlivier Houchard * modification, are permitted provided that the following conditions
432357939bSOlivier Houchard * are met:
442357939bSOlivier Houchard * 1. Redistributions of source code must retain the above copyright
452357939bSOlivier Houchard *    notice, this list of conditions and the following disclaimer.
462357939bSOlivier Houchard * 2. Redistributions in binary form must reproduce the above copyright
472357939bSOlivier Houchard *    notice, this list of conditions and the following disclaimer in the
482357939bSOlivier Houchard *    documentation and/or other materials provided with the distribution.
492357939bSOlivier Houchard * 3. All advertising materials mentioning features or use of this software
502357939bSOlivier Houchard *    must display the following acknowledgement:
512357939bSOlivier Houchard *	This product includes software developed by Mark Brinicombe.
522357939bSOlivier Houchard * 4. The name of the company nor the name of the author may be used to
532357939bSOlivier Houchard *    endorse or promote products derived from this software without specific
542357939bSOlivier Houchard *    prior written permission.
552357939bSOlivier Houchard *
562357939bSOlivier Houchard * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
572357939bSOlivier Houchard * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
582357939bSOlivier Houchard * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
592357939bSOlivier Houchard * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
602357939bSOlivier Houchard * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
612357939bSOlivier Houchard * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
622357939bSOlivier Houchard * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
632357939bSOlivier Houchard * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
642357939bSOlivier Houchard * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
652357939bSOlivier Houchard * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
662357939bSOlivier Houchard * SUCH DAMAGE.
672357939bSOlivier Houchard */
682357939bSOlivier Houchard
692357939bSOlivier Houchard#include <machine/asm.h>
702357939bSOlivier Houchard__FBSDID("$FreeBSD$");
712357939bSOlivier Houchard
72a215cdfdSAndrew Turner.syntax	unified
73a215cdfdSAndrew Turner
742357939bSOlivier Houchard/*
752357939bSOlivier Houchard * memset: Sets a block of memory to the specified value
762357939bSOlivier Houchard *
772357939bSOlivier Houchard * On entry:
782357939bSOlivier Houchard *   r0 - dest address
792357939bSOlivier Houchard *   r1 - byte to write
802357939bSOlivier Houchard *   r2 - number of bytes to write
812357939bSOlivier Houchard *
822357939bSOlivier Houchard * On exit:
832357939bSOlivier Houchard *   r0 - dest address
842357939bSOlivier Houchard */
852357939bSOlivier Houchard#ifdef _BZERO
862357939bSOlivier Houchard/* LINTSTUB: Func: void bzero(void *, size_t) */
872357939bSOlivier HouchardENTRY(bzero)
882357939bSOlivier Houchard	mov	r3, #0x00
892357939bSOlivier Houchard#else
902357939bSOlivier Houchard/* LINTSTUB: Func: void *memset(void *, int, size_t) */
912357939bSOlivier HouchardENTRY(memset)
922357939bSOlivier Houchard	and	r3, r1, #0xff		/* We deal with bytes */
932357939bSOlivier Houchard	mov	r1, r2
942357939bSOlivier Houchard#endif
952357939bSOlivier Houchard	cmp	r1, #0x04		/* Do we have less than 4 bytes */
962357939bSOlivier Houchard	mov	ip, r0
972357939bSOlivier Houchard	blt	.Lmemset_lessthanfour
982357939bSOlivier Houchard
992357939bSOlivier Houchard	/* Ok first we will word align the address */
1002357939bSOlivier Houchard	ands	r2, ip, #0x03		/* Get the bottom two bits */
1012357939bSOlivier Houchard	bne	.Lmemset_wordunaligned	/* The address is not word aligned */
1022357939bSOlivier Houchard
1032357939bSOlivier Houchard	/* We are now word aligned */
1042357939bSOlivier Houchard.Lmemset_wordaligned:
1052357939bSOlivier Houchard#ifndef _BZERO
1062357939bSOlivier Houchard	orr	r3, r3, r3, lsl #8	/* Extend value to 16-bits */
1072357939bSOlivier Houchard#endif
108fa030de0SOlivier Houchard#ifdef _ARM_ARCH_5E
109fa030de0SOlivier Houchard	tst	ip, #0x04		/* Quad-align for armv5e */
1102357939bSOlivier Houchard#else
1112357939bSOlivier Houchard	cmp	r1, #0x10
1122357939bSOlivier Houchard#endif
1132357939bSOlivier Houchard#ifndef _BZERO
1142357939bSOlivier Houchard	orr	r3, r3, r3, lsl #16	/* Extend value to 32-bits */
1152357939bSOlivier Houchard#endif
116fa030de0SOlivier Houchard#ifdef _ARM_ARCH_5E
117*6c5bc49cSAndrew Turner	itt	ne
1182357939bSOlivier Houchard	subne	r1, r1, #0x04		/* Quad-align if necessary */
1192357939bSOlivier Houchard	strne	r3, [ip], #0x04
1202357939bSOlivier Houchard	cmp	r1, #0x10
1212357939bSOlivier Houchard#endif
1222357939bSOlivier Houchard	blt	.Lmemset_loop4		/* If less than 16 then use words */
1232357939bSOlivier Houchard	mov	r2, r3			/* Duplicate data */
1242357939bSOlivier Houchard	cmp	r1, #0x80		/* If < 128 then skip the big loop */
1252357939bSOlivier Houchard	blt	.Lmemset_loop32
1262357939bSOlivier Houchard
1272357939bSOlivier Houchard	/* Do 128 bytes at a time */
1282357939bSOlivier Houchard.Lmemset_loop128:
1292357939bSOlivier Houchard	subs	r1, r1, #0x80
130fa030de0SOlivier Houchard#ifdef _ARM_ARCH_5E
131*6c5bc49cSAndrew Turner	itttt	ge
132a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
133a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
134a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
135a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
136*6c5bc49cSAndrew Turner	itttt	ge
137a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
138a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
139a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
140a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
141*6c5bc49cSAndrew Turner	itttt	ge
142a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
143a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
144a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
145a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
146*6c5bc49cSAndrew Turner	itttt	ge
147a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
148a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
149a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
150a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
1512357939bSOlivier Houchard#else
152a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
153a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
154a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
155a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
156a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
157a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
158a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
159a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
160a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
161a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
162a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
163a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
164a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
165a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
166a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
167a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
1682357939bSOlivier Houchard#endif
1692357939bSOlivier Houchard	bgt	.Lmemset_loop128
170*6c5bc49cSAndrew Turner	it	eq
17131489a9aSOlivier Houchard	RETeq			/* Zero length so just exit */
1722357939bSOlivier Houchard
1732357939bSOlivier Houchard	add	r1, r1, #0x80		/* Adjust for extra sub */
1742357939bSOlivier Houchard
1752357939bSOlivier Houchard	/* Do 32 bytes at a time */
1762357939bSOlivier Houchard.Lmemset_loop32:
1772357939bSOlivier Houchard	subs	r1, r1, #0x20
178*6c5bc49cSAndrew Turner	itttt	ge
179fa030de0SOlivier Houchard#ifdef _ARM_ARCH_5E
180a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
181a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
182a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
183a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
1842357939bSOlivier Houchard#else
185a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
186a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
187a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
188a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
1892357939bSOlivier Houchard#endif
1902357939bSOlivier Houchard	bgt	.Lmemset_loop32
191*6c5bc49cSAndrew Turner	it	eq
19231489a9aSOlivier Houchard	RETeq			/* Zero length so just exit */
1932357939bSOlivier Houchard
1942357939bSOlivier Houchard	adds	r1, r1, #0x10		/* Partially adjust for extra sub */
1952357939bSOlivier Houchard
1962357939bSOlivier Houchard	/* Deal with 16 bytes or more */
197*6c5bc49cSAndrew Turner	itt	ge
198fa030de0SOlivier Houchard#ifdef _ARM_ARCH_5E
199a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
200a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
2012357939bSOlivier Houchard#else
202a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
203a215cdfdSAndrew Turner	stmiage	ip!, {r2-r3}
2042357939bSOlivier Houchard#endif
205*6c5bc49cSAndrew Turner	it	eq
20631489a9aSOlivier Houchard	RETeq			/* Zero length so just exit */
2072357939bSOlivier Houchard
208*6c5bc49cSAndrew Turner	it	lt
2092357939bSOlivier Houchard	addlt	r1, r1, #0x10		/* Possibly adjust for extra sub */
2102357939bSOlivier Houchard
2112357939bSOlivier Houchard	/* We have at least 4 bytes so copy as words */
2122357939bSOlivier Houchard.Lmemset_loop4:
2132357939bSOlivier Houchard	subs	r1, r1, #0x04
214*6c5bc49cSAndrew Turner	it	ge
2152357939bSOlivier Houchard	strge	r3, [ip], #0x04
2162357939bSOlivier Houchard	bgt	.Lmemset_loop4
217*6c5bc49cSAndrew Turner	it	eq
21831489a9aSOlivier Houchard	RETeq			/* Zero length so just exit */
2192357939bSOlivier Houchard
220fa030de0SOlivier Houchard#ifdef _ARM_ARCH_5E
2212357939bSOlivier Houchard	/* Compensate for 64-bit alignment check */
2222357939bSOlivier Houchard	adds	r1, r1, #0x04
223*6c5bc49cSAndrew Turner	it	eq
22431489a9aSOlivier Houchard	RETeq
2252357939bSOlivier Houchard	cmp	r1, #2
2262357939bSOlivier Houchard#else
2272357939bSOlivier Houchard	cmp	r1, #-2
2282357939bSOlivier Houchard#endif
2292357939bSOlivier Houchard
2302357939bSOlivier Houchard	strb	r3, [ip], #0x01		/* Set 1 byte */
231*6c5bc49cSAndrew Turner	it	ge
232a215cdfdSAndrew Turner	strbge	r3, [ip], #0x01		/* Set another byte */
233*6c5bc49cSAndrew Turner	it	gt
234a215cdfdSAndrew Turner	strbgt	r3, [ip]		/* and a third */
23531489a9aSOlivier Houchard	RET			/* Exit */
2362357939bSOlivier Houchard
2372357939bSOlivier Houchard.Lmemset_wordunaligned:
2382357939bSOlivier Houchard	rsb	r2, r2, #0x004
2392357939bSOlivier Houchard	strb	r3, [ip], #0x01		/* Set 1 byte */
2402357939bSOlivier Houchard	cmp	r2, #0x02
241*6c5bc49cSAndrew Turner	it	ge
242a215cdfdSAndrew Turner	strbge	r3, [ip], #0x01		/* Set another byte */
2432357939bSOlivier Houchard	sub	r1, r1, r2
244*6c5bc49cSAndrew Turner	it	gt
245a215cdfdSAndrew Turner	strbgt	r3, [ip], #0x01		/* and a third */
2462357939bSOlivier Houchard	cmp	r1, #0x04		/* More than 4 bytes left? */
247*6c5bc49cSAndrew Turner	it	ge
2482357939bSOlivier Houchard	bge	.Lmemset_wordaligned	/* Yup */
2492357939bSOlivier Houchard
2502357939bSOlivier Houchard.Lmemset_lessthanfour:
2512357939bSOlivier Houchard	cmp	r1, #0x00
252*6c5bc49cSAndrew Turner	it	eq
25331489a9aSOlivier Houchard	RETeq			/* Zero length so exit */
2542357939bSOlivier Houchard	strb	r3, [ip], #0x01		/* Set 1 byte */
2552357939bSOlivier Houchard	cmp	r1, #0x02
256*6c5bc49cSAndrew Turner	it	ge
257a215cdfdSAndrew Turner	strbge	r3, [ip], #0x01		/* Set another byte */
258*6c5bc49cSAndrew Turner	it	gt
259a215cdfdSAndrew Turner	strbgt	r3, [ip]		/* and a third */
26031489a9aSOlivier Houchard	RET			/* Exit */
261f2e71517SIan Lepore#ifdef _BZERO
262f2e71517SIan LeporeEND(bzero)
263f2e71517SIan Lepore#else
264f2e71517SIan LeporeEND(memset)
265f2e71517SIan Lepore#endif
266