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