2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 ; Strlen, optimized for PPC. The routine we use is 2-3x faster
26 ; then the simple loop which checks each byte for zero.
27 ; For 0- and 1-byte strings, the simple routine is faster, but
28 ; only by a few cycles. The algorithm used was adapted from the
29 ; Mac OS 9 stdCLib strcopy routine, which was originally
30 ; written by Gary Davidian. It relies on the following rather
31 ; inobvious but very efficient test:
33 ; y = dataWord + 0xFEFEFEFF
34 ; z = ~dataWord & 0x80808080
35 ; if ( y & z ) = 0 then all bytes in dataWord are non-zero
37 ; The test maps any non-zero byte to zeros and any zero byte to 0x80,
38 ; with one exception: 0x01 bytes preceeding the first zero are also
42 #include <ppc/proc_reg.h>
52 andi. r4,r3,0x03 ; test alignment first
53 mr r9,r3 ; store the original address for later use....
54 bne LalignSource ; align the source addr if not already aligned
56 lis r5,hi16(0xFEFEFEFF)
57 lis r6,hi16(0x80808080)
58 subi r3,r3,0x04 ; pre-decrement r3 for the lwzu
59 ori r5,r5,lo16(0xFEFEFEFF) ; r5=0xFEFEFEFF
60 ori r6,r6,lo16(0x80808080) ; r6=0x80808080
63 lwzu r8,4(r3) ; get the first 4 bytes and increment address
64 add r4,r5,r8 ; r4= data + 0xFEFEFEFF
65 andc r7,r6,r8 ; r7= ~data & 0x80808080
66 and. r4,r4,r7 ; r4= r4 & r7
67 beq LLoop ; if r4 is zero, then all bytes are non-zero
69 ; Now we know one of the bytes in r8 is zero,
70 ; we just have to figure out which one.
71 ; We have mapped 0 bytes to 0x80, and nonzero bytes to 0x00,
73 ; 0x01 bytes preceeding the first zero are also mapped to 0x80.
74 ; So we have to mask out the 0x80s caused by 0x01s before
75 ; counting leading zeroes to get the bytes in last word.
77 rlwinm r5,r8,7,0,31 ; move 0x01 bits to 0x80 position
78 subf r3,r9,r3 ; start to compute string length
79 andc r4,r4,r5 ; turn off false hits from 0x0100 worst case
80 cntlzw r7,r4 ; now we can count leading 0s
81 srwi r7,r7,3 ; convert 0,8,16,24 to 0,1,2,3
82 add r3,r3,r7 ; add in nonzero bytes in last word
85 ; We must align the source address for two reasons: to avoid spurious page
86 ; faults, and for speed.
87 ; r4 = low 2 bits of address (1,2, or 3)
89 ; r9 = original address (still same as r3)
92 lbz r5,0(r3) ; get the first byte...
93 subic. r4,r4,2 ; test for 1, 2 or 3 bytes
94 addi r3,r3,1 ; increment address
95 addi r6,r9,1 ; now r6==r3
96 cmpwi cr1,r5,0 ; zero?
97 beq cr1,Lreturn ; if its zero return zero
98 bgt Llentry ; address is aligned now if low bits were 3
100 lbz r5,0(r3) ; get the next byte...
101 addi r3,r3,1 ; increment address
102 cmpwi cr1,r5,0 ; zero?
103 beq cr1,Lreturn ; if its zero return one
104 beq Llentry ; addr is aligned now if low bits were 2
106 lbz r5,0(r3) ; get the next byte...
107 addi r3,r3,1 ; increment address
108 cmpwi cr1,r5,0 ; zero?
109 bne cr1,Llentry ; not zero, continue check (now aligned)
111 sub r3,r3,r6 ; get string length (0, 1, or 2)