strlen_arm.S revision 1.4
1/*-
2 * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Matt Thomas of 3am Software Foundry.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <machine/asm.h>
31
32RCSID("$NetBSD: strlen_arm.S,v 1.4 2013/08/19 01:17:32 matt Exp $")
33
34#ifdef __ARMEL__
35#define	BYTE0	0x000000ff
36#define	BYTE1	0x0000ff00
37#define	BYTE2	0x00ff0000
38#define	BYTE3	0xff000000
39#else
40#define	BYTE0	0xff000000
41#define	BYTE1	0x00ff0000
42#define	BYTE2	0x0000ff00
43#define	BYTE3	0x000000ff
44#endif
45
46#ifdef STRNLEN
47#define	FUNCNAME	strnlen
48#else
49#define	FUNCNAME	strlen
50#endif
51
52	.text
53ENTRY(FUNCNAME)
54#ifdef __ARM_EABI__
55	.fnstart
56	.cfi_startproc
57#endif
58#ifdef STRNLEN
59	push	{r4,r5}			/* save some registers */
60#ifdef __ARM_EABI__
61	.save	{r4,r5}
62	.cfi_def_cfa_offset 8
63	.cfi_offset 5, -4
64	.cfi_offset 4, -8
65#endif
66	add	r5, r0, r1		/* get ptr to end of string */
67	mov	r4, r1			/* save maxlen */
68#endif
69	add	ip, r0, #4		/* for the final post-inc */
701:	tst	r0, #3			/* test for word alignment */
71	beq	.Lpre_main_loop		/*   finally word aligned */
72#ifdef STRNLEN
73	cmp	r0, r5			/* have we gone too far? */
74	beq	.Lmaxed_out		/*   yes, return maxlen */
75#endif
76	ldrb	r3, [r0], #1		/* load a byte */
77	teq	r3, #0			/* is it 0? */
78	bne	1b			/*   no, try next byte */
79	sub	ip, ip, #3		/* subtract (4 - the NUL) */
80	sub	r0, r0, ip		/* subtract start */
81#ifdef STRNLEN
82	pop	{r4, r5}		/* restore registers */
83#endif
84	RET				/* return */
85.Lpre_main_loop:
86#if defined(_ARM_ARCH_7)
87	movw	r1, #0xfefe		/* magic constant; 254 in each byte */
88	movt	r1, #0xfefe		/* magic constant; 254 in each byte */
89#elif defined(_ARM_ARCH_6)
90	mov	r1, #0xfe		/* put 254 in low byte */
91	orr	r1, r1, r1, lsl #8	/* move to next byte */
92	orr	r1, r1, r1, lsl #16	/* move to next halfword */
93#endif /* _ARM_ARCH_6 */
94.Lmain_loop:
95#ifdef STRNLEN
96	cmp	r0, r5			/* gone too far? */
97	bge	.Lmaxed_out		/*   yes, return maxlen */
98#endif
99	ldr	r3, [r0], #4		/* load next word */
100#if defined(_ARM_ARCH_6)
101	/*
102	 * Add 254 to each byte using the UQADD8 (unsigned saturating add 8)
103	 * instruction.  For every non-NUL byte, the result for that byte will
104	 * become 255.  For NUL, it will be 254.  When we complement the
105	 * result, if the result is non-0 then we must have encountered a NUL.
106	 */
107	uqadd8	r3, r3, r1		/* magic happens here */
108	mvns	r3, r3			/* is the complemented result non-0? */
109	beq	.Lmain_loop		/*    no, then we encountered no NULs */
110#else
111	/*
112	 * No fancy shortcuts so just test each byte lane for a NUL.
113	 * (other tests for NULs in a word take more instructions/cycles).
114	 */
115	tst	r3, #BYTE0		/* is this byte 0? */
116	tstne	r3, #BYTE1		/*   no, is this byte 0? */
117	tstne	r3, #BYTE2		/*   no, is this byte 0? */
118	tstne	r3, #BYTE3		/*   no, is this byte 0? */
119	bne	.Lmain_loop		/*   no, then get next word */
120#endif
121#if defined(_ARM_ARCH_6)
122	/*
123	 * We encountered a NUL.  Find out where by doing a CLZ and then
124	 * shifting right by 3.  That will be the number of non-NUL bytes.
125	 */
126#ifdef __ARMEL__
127	rev	r3, r3			/* we want this in BE for the CLZ */
128#endif
129	clz	r3, r3			/* count how many leading zeros */
130	add	r0, r0, r3, lsr #3	/* divide that by 8 and add to count */
131#else
132	/*
133	 * We encountered a NUL.
134	 */
135	tst	r3, #BYTE0		/* 1st byte was NUL? */
136	beq	1f			/*   yes, done adding */
137	add	r0, r0, #1		/* we have one more non-NUL byte */
138	tst	r3, #BYTE1		/* 2nd byte was NUL? */
139	beq	1f			/*   yes, done adding */
140	add	r0, r0, #1		/* we have one more non-NUL byte */
141	tst	r3, #BYTE2		/* 3rd byte was NUL? */
142	addne	r0, r0, #1		/* no, we have one more non-NUL byte */
1431:
144#endif /* _ARM_ARCH_6 */
145	/*
146	 * r0 now points to 4 past the NUL due to the post-inc.  Subtract the
147	 * start of the string (which also has 4 added to it to compensate for
148	 * the post-inc.
149	 */
150	sub	r0, r0, ip		/* subtract start to get length */
151#ifdef STRNLEN
152	cmp	r0, r4			/* is it larger than maxlen? */
153	movgt	r0, r4			/*   yes, return maxlen */
154	pop	{r4, r5}		/* restore registers */
155#endif
156	RET				/* return */
157
158#ifdef STRNLEN
159.Lmaxed_out:
160	mov	r0, r4			/* return maxlen */
161	pop	{r4, r5}		/* restore registers */
162	RET				/* return */
163#endif
164#ifdef __ARM_EABI__
165	.cfi_endproc
166	.fnend
167#endif
168END(FUNCNAME)
169