strlen.S revision 1.1 1 /* $NetBSD: strlen.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */
2
3 /*-
4 * Copyright (C) 2001 Martin J. Laubach <mjl (at) NetBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 /*----------------------------------------------------------------------*/
30
31 #include <machine/asm.h>
32
33 /*----------------------------------------------------------------------*/
34 /* The algorithm here uses the following techniques:
35
36 1) Given a word 'x', we can test to see if it contains any 0 bytes
37 by subtracting 0x01010101, and seeing if any of the high bits of each
38 byte changed from 0 to 1. This works because the least significant
39 0 byte must have had no incoming carry (otherwise it's not the least
40 significant), so it is 0x00 - 0x01 == 0xff. For all other
41 byte values, either they have the high bit set initially, or when
42 1 is subtracted you get a value in the range 0x00-0x7f, none of which
43 have their high bit set. The expression here is
44 (x + 0xfefefeff) & ~(x | 0x7f7f7f7f), which gives 0x00000000 when
45 there were no 0x00 bytes in the word.
46
47 2) Given a word 'x', we can test to see _which_ byte was zero by
48 calculating ~(((x & 0x7f7f7f7f) + 0x7f7f7f7f) | x | 0x7f7f7f7f).
49 This produces 0x80 in each byte that was zero, and 0x00 in all
50 the other bytes. The '| 0x7f7f7f7f' clears the low 7 bits in each
51 byte, and the '| x' part ensures that bytes with the high bit set
52 produce 0x00. The addition will carry into the high bit of each byte
53 iff that byte had one of its low 7 bits set. We can then just see
54 which was the most significant bit set and divide by 8 to find how
55 many to add to the index.
56 This is from the book 'The PowerPC Compiler Writer's Guide',
57 by Steve Hoxey, Faraydon Karim, Bill Hay and Hank Warren.
58 */
59 /*----------------------------------------------------------------------*/
60
61 .text
62 .align 4
63
64 ENTRY(strlen)
65
66 /* Setup constants */
67 lis %r10, 0x7f7f
68 lis %r9, 0xfefe
69 ori %r10, %r10, 0x7f7f
70 ori %r9, %r9, 0xfeff
71
72 /* Mask out leading bytes on non aligned strings */
73 rlwinm. %r8, %r3, 3, 27, 28 /* leading bits to mask */
74 clrrwi %r5, %r3, 2 /* clear low 2 addr bits */
75 li %r0, -1
76 beq+ 3f /* skip alignment if already */
77 /* aligned */
78
79 srw %r0, %r0, %r8 /* make 0000...1111 mask */
80
81 lwz %r7, 0(%r5)
82 nor %r0, %r0, %r0 /* invert mask */
83 or %r7, %r7, %r0 /* make leading bytes != 0 */
84 b 2f
85
86 3: subi %r5, %r5, 4
87
88 1: lwzu %r7, 4(%r5) /* fetch data word */
89
90 2: nor %r0, %r7, %r10 /* do step 1 */
91 add %r6, %r7, %r9
92 and. %r0, %r0, %r6
93
94 beq+ 1b /* no NUL bytes here */
95
96 and %r8, %r7, %r10 /* ok, a NUL is somewhere */
97 or %r7, %r7, %r10 /* do step 2 to find out */
98 add %r0, %r8, %r10 /* where */
99 nor %r8, %r7, %r0
100
101 cntlzw %r0, %r8 /* offset from this word */
102 srwi %r4, %r0, 3
103
104 add %r4, %r5, %r4 /* r4 contains end pointer */
105 /* NOTE: Keep it so this function returns the end pointer
106 in r4, so we can it use from other str* calls (strcat
107 comes to mind */
108
109 subf %r3, %r3, %r4
110 blr
111
112 /*----------------------------------------------------------------------*/
113