xref: /freebsd/lib/libc/arm/string/memset.S (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
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>
70a215cdfdSAndrew Turner.syntax	unified
71a215cdfdSAndrew Turner
722357939bSOlivier Houchard/*
732357939bSOlivier Houchard * memset: Sets a block of memory to the specified value
742357939bSOlivier Houchard *
752357939bSOlivier Houchard * On entry:
762357939bSOlivier Houchard *   r0 - dest address
772357939bSOlivier Houchard *   r1 - byte to write
782357939bSOlivier Houchard *   r2 - number of bytes to write
792357939bSOlivier Houchard *
802357939bSOlivier Houchard * On exit:
812357939bSOlivier Houchard *   r0 - dest address
822357939bSOlivier Houchard */
832357939bSOlivier Houchard#ifdef _BZERO
842357939bSOlivier Houchard/* LINTSTUB: Func: void bzero(void *, size_t) */
852357939bSOlivier HouchardENTRY(bzero)
862357939bSOlivier Houchard	mov	r3, #0x00
872357939bSOlivier Houchard#else
882357939bSOlivier Houchard/* LINTSTUB: Func: void *memset(void *, int, size_t) */
892357939bSOlivier HouchardENTRY(memset)
902357939bSOlivier Houchard	and	r3, r1, #0xff		/* We deal with bytes */
912357939bSOlivier Houchard	mov	r1, r2
922357939bSOlivier Houchard#endif
932357939bSOlivier Houchard	cmp	r1, #0x04		/* Do we have less than 4 bytes */
942357939bSOlivier Houchard	mov	ip, r0
952357939bSOlivier Houchard	blt	.Lmemset_lessthanfour
962357939bSOlivier Houchard
972357939bSOlivier Houchard	/* Ok first we will word align the address */
982357939bSOlivier Houchard	ands	r2, ip, #0x03		/* Get the bottom two bits */
992357939bSOlivier Houchard	bne	.Lmemset_wordunaligned	/* The address is not word aligned */
1002357939bSOlivier Houchard
1012357939bSOlivier Houchard	/* We are now word aligned */
1022357939bSOlivier Houchard.Lmemset_wordaligned:
1032357939bSOlivier Houchard#ifndef _BZERO
1042357939bSOlivier Houchard	orr	r3, r3, r3, lsl #8	/* Extend value to 16-bits */
1052357939bSOlivier Houchard#endif
106fa030de0SOlivier Houchard	tst	ip, #0x04		/* Quad-align for armv5e */
1072357939bSOlivier Houchard#ifndef _BZERO
1082357939bSOlivier Houchard	orr	r3, r3, r3, lsl #16	/* Extend value to 32-bits */
1092357939bSOlivier Houchard#endif
1106c5bc49cSAndrew Turner	itt	ne
1112357939bSOlivier Houchard	subne	r1, r1, #0x04		/* Quad-align if necessary */
1122357939bSOlivier Houchard	strne	r3, [ip], #0x04
1132357939bSOlivier Houchard	cmp	r1, #0x10
1142357939bSOlivier Houchard	blt	.Lmemset_loop4		/* If less than 16 then use words */
1152357939bSOlivier Houchard	mov	r2, r3			/* Duplicate data */
1162357939bSOlivier Houchard	cmp	r1, #0x80		/* If < 128 then skip the big loop */
1172357939bSOlivier Houchard	blt	.Lmemset_loop32
1182357939bSOlivier Houchard
1192357939bSOlivier Houchard	/* Do 128 bytes at a time */
1202357939bSOlivier Houchard.Lmemset_loop128:
1212357939bSOlivier Houchard	subs	r1, r1, #0x80
1226c5bc49cSAndrew Turner	itttt	ge
123a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
124a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
125a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
126a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
1276c5bc49cSAndrew Turner	itttt	ge
128a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
129a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
130a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
131a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
1326c5bc49cSAndrew Turner	itttt	ge
133a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
134a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
135a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
136a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
1376c5bc49cSAndrew Turner	itttt	ge
138a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
139a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
140a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
141a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
1422357939bSOlivier Houchard	bgt	.Lmemset_loop128
1436c5bc49cSAndrew Turner	it	eq
14431489a9aSOlivier Houchard	RETeq			/* Zero length so just exit */
1452357939bSOlivier Houchard
1462357939bSOlivier Houchard	add	r1, r1, #0x80		/* Adjust for extra sub */
1472357939bSOlivier Houchard
1482357939bSOlivier Houchard	/* Do 32 bytes at a time */
1492357939bSOlivier Houchard.Lmemset_loop32:
1502357939bSOlivier Houchard	subs	r1, r1, #0x20
1516c5bc49cSAndrew Turner	itttt	ge
152a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
153a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
154a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
155a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
1562357939bSOlivier Houchard	bgt	.Lmemset_loop32
1576c5bc49cSAndrew Turner	it	eq
15831489a9aSOlivier Houchard	RETeq			/* Zero length so just exit */
1592357939bSOlivier Houchard
1602357939bSOlivier Houchard	adds	r1, r1, #0x10		/* Partially adjust for extra sub */
1612357939bSOlivier Houchard
1622357939bSOlivier Houchard	/* Deal with 16 bytes or more */
1636c5bc49cSAndrew Turner	itt	ge
164a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
165a215cdfdSAndrew Turner	strdge	r2, [ip], #0x08
1666c5bc49cSAndrew Turner	it	eq
16731489a9aSOlivier Houchard	RETeq			/* Zero length so just exit */
1682357939bSOlivier Houchard
1696c5bc49cSAndrew Turner	it	lt
1702357939bSOlivier Houchard	addlt	r1, r1, #0x10		/* Possibly adjust for extra sub */
1712357939bSOlivier Houchard
1722357939bSOlivier Houchard	/* We have at least 4 bytes so copy as words */
1732357939bSOlivier Houchard.Lmemset_loop4:
1742357939bSOlivier Houchard	subs	r1, r1, #0x04
1756c5bc49cSAndrew Turner	it	ge
1762357939bSOlivier Houchard	strge	r3, [ip], #0x04
1772357939bSOlivier Houchard	bgt	.Lmemset_loop4
1786c5bc49cSAndrew Turner	it	eq
17931489a9aSOlivier Houchard	RETeq			/* Zero length so just exit */
1802357939bSOlivier Houchard
1812357939bSOlivier Houchard	/* Compensate for 64-bit alignment check */
1822357939bSOlivier Houchard	adds	r1, r1, #0x04
1836c5bc49cSAndrew Turner	it	eq
18431489a9aSOlivier Houchard	RETeq
1852357939bSOlivier Houchard	cmp	r1, #2
1862357939bSOlivier Houchard
1872357939bSOlivier Houchard	strb	r3, [ip], #0x01		/* Set 1 byte */
1886c5bc49cSAndrew Turner	it	ge
189a215cdfdSAndrew Turner	strbge	r3, [ip], #0x01		/* Set another byte */
1906c5bc49cSAndrew Turner	it	gt
191a215cdfdSAndrew Turner	strbgt	r3, [ip]		/* and a third */
19231489a9aSOlivier Houchard	RET			/* Exit */
1932357939bSOlivier Houchard
1942357939bSOlivier Houchard.Lmemset_wordunaligned:
1952357939bSOlivier Houchard	rsb	r2, r2, #0x004
1962357939bSOlivier Houchard	strb	r3, [ip], #0x01		/* Set 1 byte */
1972357939bSOlivier Houchard	cmp	r2, #0x02
1986c5bc49cSAndrew Turner	it	ge
199a215cdfdSAndrew Turner	strbge	r3, [ip], #0x01		/* Set another byte */
2002357939bSOlivier Houchard	sub	r1, r1, r2
2016c5bc49cSAndrew Turner	it	gt
202a215cdfdSAndrew Turner	strbgt	r3, [ip], #0x01		/* and a third */
2032357939bSOlivier Houchard	cmp	r1, #0x04		/* More than 4 bytes left? */
2046c5bc49cSAndrew Turner	it	ge
2052357939bSOlivier Houchard	bge	.Lmemset_wordaligned	/* Yup */
2062357939bSOlivier Houchard
2072357939bSOlivier Houchard.Lmemset_lessthanfour:
2082357939bSOlivier Houchard	cmp	r1, #0x00
2096c5bc49cSAndrew Turner	it	eq
21031489a9aSOlivier Houchard	RETeq			/* Zero length so exit */
2112357939bSOlivier Houchard	strb	r3, [ip], #0x01		/* Set 1 byte */
2122357939bSOlivier Houchard	cmp	r1, #0x02
2136c5bc49cSAndrew Turner	it	ge
214a215cdfdSAndrew Turner	strbge	r3, [ip], #0x01		/* Set another byte */
2156c5bc49cSAndrew Turner	it	gt
216a215cdfdSAndrew Turner	strbgt	r3, [ip]		/* and a third */
21731489a9aSOlivier Houchard	RET			/* Exit */
218f2e71517SIan Lepore#ifdef _BZERO
219f2e71517SIan LeporeEND(bzero)
220f2e71517SIan Lepore#else
221f2e71517SIan LeporeEND(memset)
222f2e71517SIan Lepore#endif
223*96cdb0abSKonstantin Belousov
224*96cdb0abSKonstantin Belousov	.section .note.GNU-stack,"",%progbits
225