xref: /openbsd/sys/lib/libkern/arch/arm/memset.S (revision 17df1aa7)
1/*	$OpenBSD: memset.S,v 1.2 2004/02/01 05:47:10 drahn Exp $	*/
2/*	$NetBSD: memset.S,v 1.1 2000/12/29 20:51:57 bjh21 Exp $	*/
3
4/*
5 * Copyright (c) 1995 Mark Brinicombe.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by Mark Brinicombe.
19 * 4. The name of the company nor the name of the author may be used to
20 *    endorse or promote products derived from this software without specific
21 *    prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * 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
36#include <machine/asm.h>
37
38/*
39 * Sets a block of memory to the specified value
40 *
41 * On entry:
42 *   r0 - dest address
43 *   r1 - byte to write
44 *   r2 - number of bytes to write
45 *
46 * On exit:
47 *   r0 - dest address
48 */
49
50ENTRY(memset)
51	stmfd	sp!, {r0}		/* Remember address for return value */
52	and	r1, r1, #0x000000ff	/* We write bytes */
53
54	cmp	r2, #0x00000004		/* Do we have less than 4 bytes */
55	blt	Lmemset_lessthanfour
56
57	/* Ok first we will word align the address */
58
59	ands	r3, r0, #0x00000003	/* Get the bottom two bits */
60	beq	Lmemset_addraligned	/* The address is word aligned */
61
62	rsb	r3, r3, #0x00000004
63	sub	r2, r2, r3
64	cmp	r3, #0x00000002
65	strb	r1, [r0], #0x0001	/* Set 1 byte */
66	strgeb	r1, [r0], #0x0001	/* Set another byte */
67	strgtb	r1, [r0], #0x0001	/* and a third */
68
69	cmp	r2, #0x00000004
70	blt	Lmemset_lessthanfour
71
72	/* Now we must be word aligned */
73
74Lmemset_addraligned:
75
76	orr	r3, r1, r1, lsl #8	/* Repeat the byte into a word */
77	orr	r3, r3, r3, lsl #16
78
79	/* We know we have at least 4 bytes ... */
80
81	cmp	r2, #0x00000020		/* If less than 32 then use words */
82	blt	Lmemset_lessthan32
83
84	/* We have at least 32 so lets use quad words */
85
86	stmfd	sp!, {r4-r6}		/* Store registers */
87	mov	r4, r3			/* Duplicate data */
88	mov	r5, r3
89	mov	r6, r3
90
91Lmemset_loop16:
92	stmia	r0!, {r3-r6}		/* Store 16 bytes */
93	sub	r2, r2, #0x00000010	/* Adjust count */
94	cmp	r2, #0x00000010		/* Still got at least 16 bytes ? */
95	bgt	Lmemset_loop16
96
97	ldmfd	sp!, {r4-r6}		/* Restore registers */
98
99	/* Do we need to set some words as well ? */
100
101	cmp	r2, #0x00000004
102	blt	Lmemset_lessthanfour
103
104	/* Have either less than 16 or less than 32 depending on route taken */
105
106Lmemset_lessthan32:
107
108	/* We have at least 4 bytes so copy as words */
109
110Lmemset_loop4:
111	str	r3, [r0], #0x0004
112	sub	r2, r2, #0x0004
113	cmp	r2, #0x00000004
114	bge	Lmemset_loop4
115
116Lmemset_lessthanfour:
117	cmp	r2, #0x00000000
118	ldmeqfd	sp!, {r0}
119#ifdef __APCS_26__
120	moveqs	pc, lr			/* Zero length so exit */
121#else
122	moveq	pc, lr			/* Zero length so exit */
123#endif
124
125	cmp	r2, #0x00000002
126	strb	r1, [r0], #0x0001	/* Set 1 byte */
127	strgeb	r1, [r0], #0x0001	/* Set another byte */
128	strgtb	r1, [r0], #0x0001	/* and a third */
129
130	ldmfd	sp!, {r0}
131#ifdef __APCS_26__
132	movs	pc, lr			/* Exit */
133#else
134	mov	pc, lr			/* Exit */
135#endif
136