2 * Copyright (c) 2012 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 * This file implements the following function for the arm64 architecture:
30 * int strncmp(const char *s1, const char *s2, size_t n);
32 * Returns 0 if the two strings are equal up to the first n bytes or to the
33 * end of the string, whichever comes first. Otherwise, returns the difference
34 * of the first mismatched characters interpreted as uint8_t.
39 /*****************************************************************************
41 *****************************************************************************/
44 stp fp, lr, [sp, #-16]!
48 .macro ClearFrameAndReturn
53 #include "../mach/arm/vm_param.h"
54 #define kVectorSize 16
56 /*****************************************************************************
58 *****************************************************************************/
63 .quad 0x0706050403020100, 0x0f0e0d0c0b0a0908
65 /*****************************************************************************
67 *****************************************************************************/
73 // Compare one byte at a time until s1 has vector alignment.
74 0: tst x0, #(kVectorSize-1)
76 ldrb w4, [x0],#1 // load byte from src1
77 ldrb w5, [x1],#1 // load byte from src2
78 subs x3, x4, x5 // if the are not equal
79 ccmp w4, #0, #4, eq // or we find an EOS
80 b.eq L_scalarDone // return the difference
81 subs x2, x2, #1 // decrement length
82 b.ne 0b // continue loop if non-zero
84 // We found a mismatch or EOS before s1 became aligned. Simply return the
85 // difference between the last bytes that we loaded.
91 // If s2 is similarly aligned to s1, then we can use a naive vector comparison
92 // from this point on without worrying about spurious page faults; none of our
93 // loads will ever cross a page boundary, because they are all aligned.
94 tst x1, #(kVectorSize-1)
97 /*****************************************************************************
98 * Careful chunk comparison *
99 *****************************************************************************/
101 // Otherwise, we need to be careful; although vector loads from s1 cannot
102 // cross a page boundary because they are aligned, s2 is not aligned. We
103 // compute the multiple of vector size that we can safely load before reaching
104 // a page boundary, and compare only that far before switching over to scalar
105 // comparisons to step across the page boundary. If this number happens to
106 // be zero, we jump directly to the scalar comparison.
108 ands x7, x7, #(PAGE_MIN_SIZE-kVectorSize)
112 // If n is less than the number of bytes before a page-crossing load, jump
113 // into the naive vector path instead, since we will not even reach a page
114 // crossing. Otherwise, decrement n by that number before we monkey with it,
115 // and set the decremented value aside.
119 // Use vector comparisons until a mismatch or EOS is encountered, or the next
120 // vector load from s2 would be page-crossing.
121 1: ldr q0, [x0],#(kVectorSize)
122 ldr q1, [x1],#(kVectorSize)
124 and.16b v0, v0, v1 // contains zero byte iff mismatch or EOS
126 fmov w3, s1 // zero only iff comparison is finished
128 subs x7, x7, #(kVectorSize)
130 // Restore the updated n to x2
132 // The next vector load will cross a page boundary. Instead, compare one byte
133 // at a time until s1 again has vector alignment, at which point we will have
134 // compared exactly 16 bytes.
135 2: ldrb w4, [x0],#1 // load byte from src1
136 ldrb w5, [x1],#1 // load byte from src2
137 subs x3, x4, x5 // if the are not equal
138 ccmp w4, #0, #4, eq // or we find an EOS
139 b.eq L_scalarDone // return the difference
140 subs x2, x2, #1 // decrement length
141 b.eq L_scalarDone // exit loop if zero.
142 tst x0, #(kVectorSize-1)
144 // Having compared one vector's worth of bytes using a scalar comparison, we
145 // know that we are safely across the page boundary. Initialize x7 and jump
146 // back into the vector comparison part of the loop.
147 mov x7, #(PAGE_MIN_SIZE-kVectorSize)
150 /*****************************************************************************
151 * Naive vector comparison *
152 *****************************************************************************/
156 ldr q0, [x0],#(kVectorSize)
157 ldr q1, [x1],#(kVectorSize)
159 and.16b v0, v0, v1 // contains zero byte iff mismatch or EOS
161 fmov w3, s1 // zero only iff comparison is finished
171 // Load the bytes corresponding to the first mismatch or EOS and return
174 cmhi.16b v0, v0, v1 // force non-zero lanes to 0xff
176 orr.16b v0, v0, v1 // lane index in lanes containing mismatch or EOS
179 // If the index of the mismatch or EOS is greater than or equal to n, it
180 // occurs after the first n bytes of the string, and doesn't count.
183 sub x3, x3, #(kVectorSize)