strlen.S revision 1.1 1 1.1 christos /* $NetBSD: strlen.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright 2002 Wasabi Systems, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Written by Eduardo Horvath for Wasabi Systems, Inc.
8 1.1 christos *
9 1.1 christos * Redistribution and use in source and binary forms, with or without
10 1.1 christos * modification, are permitted provided that the following conditions
11 1.1 christos * are met:
12 1.1 christos * 1. Redistributions of source code must retain the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer.
14 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 christos * notice, this list of conditions and the following disclaimer in the
16 1.1 christos * documentation and/or other materials provided with the distribution.
17 1.1 christos * 3. All advertising materials mentioning features or use of this software
18 1.1 christos * must display the following acknowledgement:
19 1.1 christos * This product includes software developed for the NetBSD Project by
20 1.1 christos * Wasabi Systems, Inc.
21 1.1 christos * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 christos * or promote products derived from this software without specific prior
23 1.1 christos * written permission.
24 1.1 christos *
25 1.1 christos * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
36 1.1 christos */
37 1.1 christos
38 1.1 christos
39 1.1 christos
40 1.1 christos #include <machine/asm.h>
41 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
42 1.1 christos RCSID("$NetBSD: strlen.S,v 1.1 2005/12/20 19:28:50 christos Exp $")
43 1.1 christos #endif /* LIBC_SCCS and not lint */
44 1.1 christos
45 1.1 christos /* The algorithm here uses the following techniques:
46 1.1 christos *
47 1.1 christos * 1) Given a word 'x', we can test to see if it contains any 0 bytes
48 1.1 christos * by subtracting 0x01010101, and seeing if any of the high bits of each
49 1.1 christos * byte changed from 0 to 1. This works because the least significant
50 1.1 christos * 0 byte must have had no incoming carry (otherwise it's not the least
51 1.1 christos * significant), so it is 0x00 - 0x01 == 0xff. For all other
52 1.1 christos * byte values, either they have the high bit set initially, or when
53 1.1 christos * 1 is subtracted you get a value in the range 0x00-0x7f, none of which
54 1.1 christos * have their high bit set. The expression here is
55 1.1 christos * (x + 0xfefefeff) & ~(x | 0x7f7f7f7f), which gives 0x00000000 when
56 1.1 christos * there were no 0x00 bytes in the word.
57 1.1 christos *
58 1.1 christos * 2) Now just hunt for the first byte that's 0x00 in 'x'.
59 1.1 christos *
60 1.1 christos * This is from the book 'The PowerPC Compiler Writer's Guide',
61 1.1 christos * by Steve Hoxey, Faraydon Karim, Bill Hay and Hank Warren.
62 1.1 christos */
63 1.1 christos
64 1.1 christos ENTRY(strlen)
65 1.1 christos /*
66 1.1 christos * Calculate address for and load the first xword.
67 1.1 christos */
68 1.1 christos andn %o0, 0x7, %o1
69 1.1 christos ldx [%o1], %g1
70 1.1 christos
71 1.1 christos /*
72 1.1 christos * Now prepare some constants while the data arrives...
73 1.1 christos */
74 1.1 christos sethi %hi(0xfefefefe), %o3
75 1.1 christos sethi %hi(0x7f7f7f7f), %o2
76 1.1 christos
77 1.1 christos or %o3, %lo(0xfefefefe), %o3
78 1.1 christos or %o2, %lo(0x7f7f7f7f), %o2
79 1.1 christos
80 1.1 christos sllx %o3, 32, %o5
81 1.1 christos andcc %o0, 0x7, %g5 ! Hoisted from below to fill a slot
82 1.1 christos
83 1.1 christos sllx %o2, 32, %o4
84 1.1 christos or %o3, %o5, %o3
85 1.1 christos
86 1.1 christos sll %g5, 3, %g5 ! Convert to bytes. hoisted
87 1.1 christos or %o2, %o4, %o2
88 1.1 christos
89 1.1 christos inc %o3
90 1.1 christos neg %g5 ! hoisted
91 1.1 christos
92 1.1 christos /*
93 1.1 christos * Mask off the leading bits:
94 1.1 christos *
95 1.1 christos * if (ptr & 0x7)
96 1.1 christos * mask = -1 << (64 - ((ptr & 0x7) << 3));
97 1.1 christos */
98 1.1 christos
99 1.1 christos ! andcc %o0, 0x7, %g5 ! Hoisted above
100 1.1 christos bz,pt %icc, 0f
101 1.1 christos
102 1.1 christos
103 1.1 christos ! sll %g5, 3, %g5 ! Convert to bytes. Also hoisted
104 1.1 christos
105 1.1 christos ! neg %g5 ! Hoisted
106 1.1 christos
107 1.1 christos add %g5, 64, %g5
108 1.1 christos mov -1, %o4
109 1.1 christos
110 1.1 christos sllx %o4, %g5, %o4
111 1.1 christos
112 1.1 christos or %o4, %g1, %g1 ! Make leading bytes != 0
113 1.1 christos
114 1.1 christos 0:
115 1.1 christos or %g1, %o2, %o5 ! Do step 1 -- use or/andn instead of nor/and
116 1.1 christos add %g1, %o3, %g5
117 1.1 christos
118 1.1 christos inc 8, %o1 ! Point to next word
119 1.1 christos andncc %g5, %o5, %g0
120 1.1 christos bz,a,pt %xcc, 0b
121 1.1 christos ldx [%o1], %g1
122 1.1 christos
123 1.1 christos mov -1, %o4
124 1.1 christos dec 8, %o1
125 1.1 christos
126 1.1 christos sllx %o4, 64-8, %o5
127 1.1 christos
128 1.1 christos btst %g1, %o5 ! Check high byte
129 1.1 christos bz %xcc,0f
130 1.1 christos srlx %o5, 8, %o5
131 1.1 christos
132 1.1 christos inc %o1
133 1.1 christos btst %g1, %o5 ! Check 2nd byte
134 1.1 christos bz %xcc,0f
135 1.1 christos srlx %o5, 8, %o5
136 1.1 christos
137 1.1 christos inc %o1
138 1.1 christos btst %g1, %o5 ! Check 3rd byte
139 1.1 christos bz %xcc,0f
140 1.1 christos srlx %o5, 8, %o5
141 1.1 christos
142 1.1 christos inc %o1
143 1.1 christos btst %g1, %o5 ! Check 4th byte
144 1.1 christos bz %xcc,0f
145 1.1 christos srlx %o5, 8, %o5
146 1.1 christos
147 1.1 christos inc %o1
148 1.1 christos btst %g1, %o5 ! Check 5th byte
149 1.1 christos bz %xcc,0f
150 1.1 christos srlx %o5, 8, %o5
151 1.1 christos
152 1.1 christos inc %o1
153 1.1 christos btst %g1, %o5 ! Check 6th byte
154 1.1 christos bz %xcc,0f
155 1.1 christos srlx %o5, 8, %o5
156 1.1 christos
157 1.1 christos inc %o1
158 1.1 christos btst %g1, %o5 ! Check 7th byte
159 1.1 christos bz %xcc,0f
160 1.1 christos nop
161 1.1 christos
162 1.1 christos inc %o1
163 1.1 christos 0:
164 1.1 christos retl
165 1.1 christos sub %o1, %o0, %o0 ! return length (ptr - (origptr+1))
166