Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libcr / mips / string / bcmp.S
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Ralph Campbell.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #include <machine/asm.h>
38
39 #if defined(LIBC_SCCS)
40         .text
41         .asciz "$OpenBSD$"
42 #endif /* LIBC_SCCS */
43
44 /* bcmp(s1, s2, n) */
45
46 LEAF(bcmp)
47         .set    noreorder
48         blt     a2, 16, small           # is it worth any trouble?
49         xor     v0, a0, a1              # compare low two bits of addresses
50         and     v0, v0, 3
51         subu    a3, zero, a1            # compute # bytes to word align address
52         bne     v0, zero, unaligned     # not possible to align addresses
53         and     a3, a3, 3
54
55         beq     a3, zero, 1f
56         subu    a2, a2, a3              # subtract from remaining count
57         move    v0, v1                  # init v0,v1 so unmodified bytes match
58         LWHI    v0, 0(a0)               # read 1, 2, or 3 bytes
59         LWHI    v1, 0(a1)
60         addu    a1, a1, a3
61         bne     v0, v1, nomatch
62         addu    a0, a0, a3
63 1:
64         and     a3, a2, ~3              # compute number of whole words left
65         subu    a2, a2, a3              #   which has to be >= (16-3) & ~3
66         addu    a3, a3, a0              # compute ending address
67 2:
68         lw      v0, 0(a0)               # compare words
69         lw      v1, 0(a1)
70         addu    a0, a0, 4
71         bne     v0, v1, nomatch
72         addu    a1, a1, 4
73         bne     a0, a3, 2b
74         nop
75         b       small                   # finish remainder
76         nop
77 unaligned:
78         beq     a3, zero, 2f
79         subu    a2, a2, a3              # subtract from remaining count
80         addu    a3, a3, a0              # compute ending address
81 1:
82         lbu     v0, 0(a0)               # compare bytes until a1 word aligned
83         lbu     v1, 0(a1)
84         addu    a0, a0, 1
85         bne     v0, v1, nomatch
86         addu    a1, a1, 1
87         bne     a0, a3, 1b
88         nop
89 2:
90         and     a3, a2, ~3              # compute number of whole words left
91         subu    a2, a2, a3              #   which has to be >= (16-3) & ~3
92         addu    a3, a3, a0              # compute ending address
93 3:
94         LWHI    v0, 0(a0)               # compare words a0 unaligned, a1 aligned
95         LWLO    v0, 3(a0)
96         lw      v1, 0(a1)
97         addu    a0, a0, 4
98         bne     v0, v1, nomatch
99         addu    a1, a1, 4
100         bne     a0, a3, 3b
101         nop
102 small:
103         ble     a2, zero, match
104         addu    a3, a2, a0              # compute ending address
105 1:
106         lbu     v0, 0(a0)
107         lbu     v1, 0(a1)
108         addu    a0, a0, 1
109         bne     v0, v1, nomatch
110         addu    a1, a1, 1
111         bne     a0, a3, 1b
112         nop
113 match:
114         j       ra
115         move    v0, zero
116 nomatch:
117         j       ra
118         li      v0, 1
119         .set    reorder
120 END(bcmp)