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