add more library routines
[rpi-open-firmware.git] / arm_chainloader / lib / arm_memcmp.s
1 /*
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 // ARM Assembly implementation of memcmp( ) from <string.h>
25 // Uses Thumb2 if it is available, otherwise generates ARM code.
26 //
27 // -- Stephen Canon, August 2009
28 //
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.
33 //
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.
36 //
37 // int bcmp(void *src1, void *src2, size_t length);
38 // int memcmp(void *src1, void *src2, size_t length);
39
40 .text
41 .syntax unified
42 .code 32
43 .globl bcmp
44 .globl memcmp
45 .align 3
46 bcmp:
47 memcmp:
48
49 #define _ARM_ARCH_6
50
51 #ifdef _ARM_ARCH_6
52 subs ip, r2, #4 // if length < 4
53 bmi L_useByteCompares // jump to the byte comparison loop
54
55 orr r3, r0, r1 // if the buffers are
56 tst r3, #3 // not word aligned
57 bne L_useByteCompares // jump to the byte comparison loop
58
59 .align 3
60 L_wordCompare: // Here we know that both buffers are word
61 ldr r2, [r0], #4 // aligned, and (length - 4) > 0, so at least
62 ldr r3, [r1], #4 // four bytes remain to be compared. We load
63 subs ip, #4 // a word from each buffer, and byte reverse
64 bmi L_lastWord // the loaded words. We also decrement the
65 rev r2, r2 // length by four and jump out of this loop if
66 rev r3, r3 // the result is negative. Then we compare the
67 cmp r2, r3 // reversed words, and continue the loop only
68 beq L_wordCompare // if they are equal.
69 L_wordsUnequal:
70 ite hi // If the words compared unequal, return +/- 1
71 movhi r0, #1 // according to the result of the comparison.
72 movls r0, #-1 //
73 bx lr //
74 L_lastWord:
75 rev r2, r2 // If we just loaded the last complete words
76 rev r3, r3 // from the buffers, byte-reverse them and
77 cmp r2, r3 // compare. If they are unequal, jump to the
78 bne L_wordsUnequal // return path.
79 add r2, ip, #4 // Otherwise, fall into the cleanup code.
80 #endif // _ARM_ARCH_6
81
82 L_useByteCompares:
83 tst r2, r2 // If the length is exactly zero
84 beq L_returnZero // avoid doing any loads and return zero.
85 mov r3, r0
86 .align 3
87 L_byteCompareLoop:
88 ldrb r0, [r3], #1 // Load a byte from each buffer, and decrement
89 ldrb ip, [r1], #1 // the length by one. If the decremented
90 subs r2, #1 // length is zero, exit the loop. Otherwise
91 beq L_lastByte // subtract the loaded bytes; if their
92 subs r0, ip // difference is zero, continue the comparison
93 beq L_byteCompareLoop // loop. Otherwise, return their difference.
94 bx lr
95 L_returnZero:
96 mov r0, ip
97 L_lastByte:
98 sub r0, ip // Return the difference of the final bytes
99 bx lr
This page took 0.068177 seconds and 4 git commands to generate.