xref: /netbsd-src/common/lib/libc/arch/m68k/string/memcmp.S (revision 18ec38ea7c8331c5bc28d877de4a5b1391957c39)
1*18ec38eaSchs/*	$NetBSD: memcmp.S,v 1.5 2013/09/07 19:06:29 chs Exp $	*/
237c9f0a6Schristos
337c9f0a6Schristos/*-
437c9f0a6Schristos * Copyright (c) 1990 The Regents of the University of California.
537c9f0a6Schristos * All rights reserved.
637c9f0a6Schristos *
737c9f0a6Schristos * This code is derived from software contributed to Berkeley by
837c9f0a6Schristos * the Systems Programming Group of the University of Utah Computer
937c9f0a6Schristos * Science Department.
1037c9f0a6Schristos *
1137c9f0a6Schristos * Redistribution and use in source and binary forms, with or without
1237c9f0a6Schristos * modification, are permitted provided that the following conditions
1337c9f0a6Schristos * are met:
1437c9f0a6Schristos * 1. Redistributions of source code must retain the above copyright
1537c9f0a6Schristos *    notice, this list of conditions and the following disclaimer.
1637c9f0a6Schristos * 2. Redistributions in binary form must reproduce the above copyright
1737c9f0a6Schristos *    notice, this list of conditions and the following disclaimer in the
1837c9f0a6Schristos *    documentation and/or other materials provided with the distribution.
1937c9f0a6Schristos * 3. Neither the name of the University nor the names of its contributors
2037c9f0a6Schristos *    may be used to endorse or promote products derived from this software
2137c9f0a6Schristos *    without specific prior written permission.
2237c9f0a6Schristos *
2337c9f0a6Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2437c9f0a6Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2537c9f0a6Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2637c9f0a6Schristos * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2737c9f0a6Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2837c9f0a6Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2937c9f0a6Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3037c9f0a6Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3137c9f0a6Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3237c9f0a6Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3337c9f0a6Schristos * SUCH DAMAGE.
3437c9f0a6Schristos */
3537c9f0a6Schristos
3637c9f0a6Schristos#include <machine/asm.h>
3737c9f0a6Schristos
3837c9f0a6Schristos#if defined(LIBC_SCCS) && !defined(lint)
3937c9f0a6Schristos#if 0
4037c9f0a6Schristos	RCSID("from: @(#)bcmp.s	5.1 (Berkeley) 5/12/90")
4137c9f0a6Schristos#else
42*18ec38eaSchs	RCSID("$NetBSD: memcmp.S,v 1.5 2013/09/07 19:06:29 chs Exp $")
4337c9f0a6Schristos#endif
4437c9f0a6Schristos#endif /* LIBC_SCCS and not lint */
4537c9f0a6Schristos
4637c9f0a6Schristos/* memcmp(s1, s2, n) */
4737c9f0a6Schristos
481d398a17Smatt#ifdef __mcoldfire__
491d398a17Smatt#define	CMPMB(a,b)	movb b,%d2; cmpb a,%d2
501d398a17Smatt#define	CMPMW(a,b)	movw b,%d2; cmpw a,%d2
511d398a17Smatt#define	CMPML(a,b)	movl b,%d2; cmpl a,%d2
521d398a17Smatt#else
531d398a17Smatt#define	CMPMB(a,b)	cmpmb a,b
541d398a17Smatt#define	CMPMW(a,b)	cmpmw a,b
551d398a17Smatt#define	CMPML(a,b)	cmpml a,b
561d398a17Smatt#endif
571d398a17Smatt
5837c9f0a6Schristos/*
5937c9f0a6Schristos * This is probably not the best we can do, but it is still 2-10 times
6037c9f0a6Schristos * faster than the C version in the portable gen directory.
6137c9f0a6Schristos *
6237c9f0a6Schristos * Things that might help:
6337c9f0a6Schristos *	- longword align when possible (only on the 68020)
6437c9f0a6Schristos *	- use nested DBcc instructions or use one and limit size to 64K
6537c9f0a6Schristos */
6637c9f0a6SchristosENTRY(memcmp)
6707a0a325Smatt	movl	4(%sp),%a0		| string 1
6807a0a325Smatt	movl	8(%sp),%a1		| string 2
6907a0a325Smatt	movl	12(%sp),%d0		| length
701d398a17Smatt#ifdef __mcoldfire__
711d398a17Smatt	movl	%d2,-(%sp)		| save temp
721d398a17Smatt#endif
73*18ec38eaSchs	jeq	.Lbcdone		| if zero, nothing to do
7437c9f0a6Schristos	movl	%a0,%d1
7537c9f0a6Schristos	btst	#0,%d1			| string 1 address odd?
76*18ec38eaSchs	jeq	.Lbceven		| no, skip alignment
771d398a17Smatt	CMPMB((%a0)+,(%a1)+)		| yes, compare a byte
78*18ec38eaSchs	jne	.Lbcnoteq		| not equal, return non-zero
7937c9f0a6Schristos	subql	#1,%d0			| adjust count
80*18ec38eaSchs	jeq	.Lbcdone		| count 0, reutrn zero
81*18ec38eaSchs.Lbceven:
8237c9f0a6Schristos	movl	%a1,%d1
8337c9f0a6Schristos	btst	#0,%d1			| string 2 address odd?
84*18ec38eaSchs	jne	.Lbcbloop		| yes, no hope for alignment, compare bytes
8537c9f0a6Schristos	movl	%d0,%d1			| no, both even
8637c9f0a6Schristos	lsrl	#2,%d1			| convert count to longword count
87*18ec38eaSchs	jeq	.Lbcbloop		| count 0, skip longword loop
88*18ec38eaSchs.Lbclloop:
891d398a17Smatt	CMPML((%a0)+,(%a1)+)		| compare a longword
90*18ec38eaSchs	jne	.Lbcnoteql		| not equal, return non-zero
9137c9f0a6Schristos	subql	#1,%d1			| adjust count
92*18ec38eaSchs	jne	.Lbclloop		| still more, keep comparing
9337c9f0a6Schristos	andl	#3,%d0			| what remains
94*18ec38eaSchs	jeq	.Lbcdone		| nothing, all done
95*18ec38eaSchs.Lbcbloop:
961d398a17Smatt	CMPMB((%a0)+,(%a1)+)		| compare a byte
97*18ec38eaSchs	jne	.Lbcnoteq		| not equal, return non-zero
9837c9f0a6Schristos	subql	#1,%d0			| adjust count
99*18ec38eaSchs	jne	.Lbcbloop		| still more, keep going
10037c9f0a6Schristos	rts
101*18ec38eaSchs.Lbcnoteql:
10237c9f0a6Schristos	subql	#4,%a0
10337c9f0a6Schristos	subql	#4,%a1
10437c9f0a6Schristos	movl	#4,%d0
105*18ec38eaSchs	jra	.Lbcbloop
106*18ec38eaSchs.Lbcnoteq:
10737c9f0a6Schristos	clrl	%d0
10837c9f0a6Schristos	clrl	%d1
10907a0a325Smatt	movb	-(%a0),%d0
11007a0a325Smatt	movb	-(%a1),%d1
11137c9f0a6Schristos	subl	%d1,%d0
112*18ec38eaSchs.Lbcdone:
1131d398a17Smatt#ifdef __mcoldfire__
1141d398a17Smatt	movl	(%sp)+,%sp		| restore temp
1151d398a17Smatt#endif
11637c9f0a6Schristos	rts
11765726debSmattEND(memcmp)
118