xref: /original-bsd/lib/libc/mips/string/bcmp.s (revision 860e07fc)
1/*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Ralph Campbell.
7 *
8 * %sccs.include.redist.c%
9 */
10
11#include <machine/machAsmDefs.h>
12
13#if defined(LIBC_SCCS) && !defined(lint)
14	ASMSTR("@(#)bcmp.s	5.4 (Berkeley) 07/26/92")
15#endif /* LIBC_SCCS and not lint */
16
17/* bcmp(s1, s2, n) */
18
19#include <machine/endian.h>
20
21#if BYTE_ORDER == LITTLE_ENDIAN
22#	define	LWHI	lwr
23#	define	LWLO	lwl
24#	define	SWHI	swr
25#	define	SWLO	swl
26#endif
27#if BYTE_ORDER == BIG_ENDIAN
28#	define	LWHI	lwl
29#	define	LWLO	lwr
30#	define	SWHI	swl
31#	define	SWLO	swr
32#endif
33
34LEAF(bcmp)
35	.set	noreorder
36	blt	a2, 16, small		# is it worth any trouble?
37	xor	v0, a0, a1		# compare low two bits of addresses
38	and	v0, v0, 3
39	subu	a3, zero, a1		# compute # bytes to word align address
40	bne	v0, zero, unaligned	# not possible to align addresses
41	and	a3, a3, 3
42
43	beq	a3, zero, 1f
44	subu	a2, a2, a3		# subtract from remaining count
45	move	v0, v1			# init v0,v1 so unmodified bytes match
46	LWHI	v0, 0(a0)		# read 1, 2, or 3 bytes
47	LWHI	v1, 0(a1)
48	addu	a1, a1, a3
49	bne	v0, v1, nomatch
50	addu	a0, a0, a3
511:
52	and	a3, a2, ~3		# compute number of whole words left
53	subu	a2, a2, a3		#   which has to be >= (16-3) & ~3
54	addu	a3, a3, a0		# compute ending address
552:
56	lw	v0, 0(a0)		# compare words
57	lw	v1, 0(a1)
58	addu	a0, a0, 4
59	bne	v0, v1, nomatch
60	addu	a1, a1, 4
61	bne	a0, a3, 2b
62	nop
63	b	small			# finish remainder
64	nop
65unaligned:
66	beq	a3, zero, 2f
67	subu	a2, a2, a3		# subtract from remaining count
68	addu	a3, a3, a0		# compute ending address
691:
70	lbu	v0, 0(a0)		# compare bytes until a1 word aligned
71	lbu	v1, 0(a1)
72	addu	a0, a0, 1
73	bne	v0, v1, nomatch
74	addu	a1, a1, 1
75	bne	a0, a3, 1b
76	nop
772:
78	and	a3, a2, ~3		# compute number of whole words left
79	subu	a2, a2, a3		#   which has to be >= (16-3) & ~3
80	addu	a3, a3, a0		# compute ending address
813:
82	LWHI	v0, 0(a0)		# compare words a0 unaligned, a1 aligned
83	LWLO	v0, 3(a0)
84	lw	v1, 0(a1)
85	addu	a0, a0, 4
86	bne	v0, v1, nomatch
87	addu	a1, a1, 4
88	bne	a0, a3, 3b
89	nop
90small:
91	ble	a2, zero, match
92	addu	a3, a2, a0		# compute ending address
931:
94	lbu	v0, 0(a0)
95	lbu	v1, 0(a1)
96	addu	a0, a0, 1
97	bne	v0, v1, nomatch
98	addu	a1, a1, 1
99	bne	a0, a3, 1b
100	nop
101match:
102	j	ra
103	move	v0, zero
104nomatch:
105	j	ra
106	li	v0, 1
107	.set	reorder
108END(bcmp)
109