1 /* $NetBSD: memcmp.S,v 1.1 2014/09/03 19:34:25 matt Exp $ */
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
35 * int memcmp(const char *s1, const char *s2, size_t n);
37 * for (; n-- != 0; s1++, s2++) {
45 * Return: ((s1 > s2) ? 1 : (s1 < s2) ? -1 : 0)
47 * ==========================================================================
50 #include <machine/asm.h>
54 /* LINTSTUB: Func: void *memcmp(const void *, const void *, size_t) */
58 * Check count passed in R5. If zero, return 0; otherwise continue.
60 l.sfeqi r5, 0 /* nothing to compare? */
61 l.bf .Lret_0 /* yes, return equality */
65 l.sfeqi r5, 6 /* less than two words? */
66 l.bnf .Lsixbyte_compare /* yes, just compare by bytes */
70 l.sfgesi r5, 7 /* less than two words? */
71 l.bnf .Lbyte_compare /* yes, just compare by bytes */
74 l.xor r6, r3, r4 /* check alignment compatibility */
75 l.andi r6, r6, 3 /* only care about the two bits */
76 l.sfeqi r6, 0 /* same alignment? */
77 l.bnf .Lmisaligned /* no, avoid alignment errors */
81 * At this point, we know we read the data via word accesses.
84 l.andi r7, r3, 3 /* check alignment */
85 l.sfeqi r7, 0 /* word aligned? */
86 l.bf .Lword_compare /* yes, it is. */
88 l.sub r3, r3, r7 /* align string 1 */
89 l.sub r4, r4, r7 /* align string 2 */
90 l.add r5, r5, r7 /* pad length */
92 l.lwz r15, 0(r3) /* load word from s1 */
93 l.lwz r17, 0(r4) /* load word from s2 */
95 l.slli r7, r7, 3 /* bytes to bits */
96 l.sll r15, r15, r7 /* shift away leading bytes */
97 l.sll r17, r17, r7 /* shift away leading bytes */
98 l.j .Lword_compare /* now we can compare them */
102 l.lwz r15, 0(r3) /* load s1 word */
103 l.lwz r17, 0(r4) /* load s2 word */
105 l.sfeq r15, r17 /* compare s1 and s2 words */
106 l.bnf .Lall_done /* different? we're done */
108 l.addi r3, r3, 4 /* advance s1 one word */
109 l.addi r4, r4, 4 /* advance s2 one word */
110 l.addi r5, r5, -4 /* decrement one word */
111 l.sfgtsi r5, 4 /* at least more than a word? */
112 l.bf .Lword_loop /* yes, loop around */
114 l.sfeqi r5, 0 /* nothing left? */
115 l.bf .Lret_0 /* yes, return equality */
119 * Fall through to handle the last word
122 l.sub r3, r0, r5 /* If count <= 4, handle */
123 l.andi r3, r3, 3 /* mask off low 2 bits */
124 l.slli r3, r3, 3 /* count *= 8 */
125 l.srl r15, r15, r3 /* discard extra s1 bytes */
126 l.srl r17, r17, r3 /* discard extra s2 bytes */
128 l.sfeq r17, r15 /* compare result */
136 * The two string don't have the same word alignment.
139 l.sfeqi r6, 2 /* check for halfword alignment */
151 l.j .Lhalfword_compare
181 l.sub r11, r15, r17 /* subtract s2 from s1 */
182 l.srai r11, r11, 30 /* replicate sign bit thru bit 1 */
183 l.ori r11, r11, 1 /* make sure bit 0 is set */