]> git.saurik.com Git - apple/xnu.git/blob - libkern/ppc/memcmp.s
xnu-517.tar.gz
[apple/xnu.git] / libkern / ppc / memcmp.s
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 ;
26 #include <ppc/asm.h>
27 #include <ppc/proc_reg.h>
28 ;
29 ; int memcmp(const void *LHS, const void *RHS, size_t len);
30 ;
31 ; Memcmp returns the difference between the first two different bytes,
32 ; or 0 if the two strings are equal. Because we compare a word at a
33 ; time, this requires a little additional processing once we find a
34 ; difference.
35 ; r3 - LHS
36 ; r4 - RHS
37 ; r5 - len
38
39 .align 5
40 .globl EXT(memcmp)
41 LEXT(memcmp)
42
43 cmpwi cr1,r5,6 ; six is the most common length
44 mr r6,r3 ; we want to use r3 for compare result
45 mr. r3,r5 ; test length for 0
46 bgt cr1,Llong ; handle long strings
47 blt cr1,Lshort ; and short strings
48
49 ; six char strings are special cased because they are the most common
50 Lsix:
51 lwz r8,0(r6) ; first 4 bytes of LHS
52 lwz r7,0(r4) ; and RHS
53 xor. r3,r8,r7 ; compare first 4
54 bne Ldifferent ; first 4 differed
55 lhz r8,4(r6) ; last 2 of LHS
56 lhz r7,4(r4) ; last 2 of RHS
57 xor. r3,r8,r7 ; compare last 2
58 beqlr ; done if equal
59
60 ; strings differ, so we must compute difference between first two
61 ; differing bytes.
62 ; r8 = LHS bytes
63 ; r7 = RHS bytes
64 ; r3 = r8 xor r7 (r3!=0)
65 Ldifferent:
66 cntlzw r9,r3 ; count leading 0s in xor
67 rlwinm r10,r9,0,0,28 ; mask off low 3 bits, so r10 = 0, 8, 16, or 24
68 subfic r6,r10,24 ; r6 := (24 - r10)
69 srw r4,r8,r6 ; r4 = LHS differing byte
70 srw r5,r7,r6 ; r5 = RHS differing byte
71 sub r3,r4,r5 ; r3 = difference
72 blr
73
74 ; handle long strings
75 Llong:
76 srwi r0,r5,2 ; r0 = word length
77 mtctr r0 ; set up for loop
78 Llongloop:
79 lwz r8,0(r6) ; next 4 bytes from LHS
80 addi r6,r6,4
81 lwz r7,0(r4) ; next 4 from RHS
82 addi r4,r4,4
83 xor. r3,r8,r7 ; compare next 4 bytes
84 bdnzt+ eq,Llongloop ; loop if ctr!=0 and cr0_eq
85 bne Ldifferent ; these 4 bytes not equal
86
87 andi. r5,r5,3 ; more to go?
88
89 ; compare short strings (0-5 bytes long)
90 ; r5 = length (0-5)
91 ; cr0= set on length
92 ; r3 = if r5=0, then r3=0
93 Lshort:
94 beqlr ; 0-length strings are defined to be equal (r3=0)
95 mtctr r5
96 Lshortloop:
97 lbz r8,0(r6) ; get next byte from LHS
98 addi r6,r6,1
99 lbz r7,0(r4) ; and next byte from RHS
100 addi r4,r4,1
101 sub. r3,r8,r7 ; compare
102 bdnzt+ eq,Lshortloop ; lloop if ctr!=0 and cr0_eq
103 blr ; done, r3 set correctly by the subtract