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