2 * Copyright (c) 2009 Apple Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 // ARM Assembly implementation of memcmp( ) from <string.h>
25 // Uses Thumb2 if it is available, otherwise generates ARM code.
27 // -- Stephen Canon, August 2009
29 // The basic idea is to use word compares instead of byte compares as long as
30 // at least four bytes remain to be compared. However, because memcmp( )
31 // compares the buffers as though they were big-endian unsigned integers, we
32 // need to byte-reverse each word before comparing them.
34 // If the buffers are not word aligned, or they are shorter than four bytes,
35 // we just use a simple byte comparison loop instead.
37 // int bcmp(void *src1, void *src2, size_t length);
38 // int memcmp(void *src1, void *src2, size_t length);
42 #if defined __thumb2__
50 #define ESTABLISH_FRAME \
53 #define CLEAR_FRAME_AND_RETURN \
58 #if defined _ARM_ARCH_6
59 #define BYTE_REVERSE(reg,tmp) \
61 #else // defined _ARM_ARCH_6
62 // Prior to ARMv6, the REV instruction is not available. We use a very cute
63 // software workaround instead, which needs only a single scratch register.
64 #define BYTE_REVERSE(reg,tmp) \
65 eor tmp, reg, reg, ror #16;\
66 bic tmp, tmp, #0xff0000 ;\
67 mov tmp, tmp, lsr #8 ;\
68 eor reg, tmp, reg, ror #8
69 #endif // defined _ARM_ARCH_6
76 // If both buffers are not word aligned, jump to a byte-comparison loop.
80 bne L_useByteComparisons
82 // As long as at least four bytes of length remain, load one word from each
83 // buffer and check if they are equal.
85 blo L_lessThanFourBytesRemain
91 // If words from the two buffers compared unequal, we end up here. We need
92 // to byte-swap both words, then subtract to determine the result (+/-1).
99 CLEAR_FRAME_AND_RETURN
101 L_lessThanFourBytesRemain:
103 L_useByteComparisons:
105 // If no bytes remain to compare, the buffers are equal and we return zero.
106 // Otherwise, load one byte from each buffer and check if they are equal.
108 blo L_buffersAreEqual
113 CLEAR_FRAME_AND_RETURN
117 CLEAR_FRAME_AND_RETURN