memcmp.S revision 1.1
11.1Schristos/*	$NetBSD: memcmp.S,v 1.1 2005/12/20 19:28:49 christos Exp $	*/
21.1Schristos
31.1Schristos/*-
41.1Schristos * Copyright (c) 1990 The Regents of the University of California.
51.1Schristos * All rights reserved.
61.1Schristos *
71.1Schristos * This code is derived from software contributed to Berkeley by
81.1Schristos * the Systems Programming Group of the University of Utah Computer
91.1Schristos * Science Department.
101.1Schristos *
111.1Schristos * Redistribution and use in source and binary forms, with or without
121.1Schristos * modification, are permitted provided that the following conditions
131.1Schristos * are met:
141.1Schristos * 1. Redistributions of source code must retain the above copyright
151.1Schristos *    notice, this list of conditions and the following disclaimer.
161.1Schristos * 2. Redistributions in binary form must reproduce the above copyright
171.1Schristos *    notice, this list of conditions and the following disclaimer in the
181.1Schristos *    documentation and/or other materials provided with the distribution.
191.1Schristos * 3. Neither the name of the University nor the names of its contributors
201.1Schristos *    may be used to endorse or promote products derived from this software
211.1Schristos *    without specific prior written permission.
221.1Schristos *
231.1Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
241.1Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
251.1Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261.1Schristos * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
271.1Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281.1Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
291.1Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301.1Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311.1Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321.1Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331.1Schristos * SUCH DAMAGE.
341.1Schristos */
351.1Schristos
361.1Schristos#include <machine/asm.h>
371.1Schristos
381.1Schristos#if defined(LIBC_SCCS) && !defined(lint)
391.1Schristos#if 0
401.1Schristos	RCSID("from: @(#)bcmp.s	5.1 (Berkeley) 5/12/90")
411.1Schristos#else
421.1Schristos	RCSID("$NetBSD: memcmp.S,v 1.1 2005/12/20 19:28:49 christos Exp $")
431.1Schristos#endif
441.1Schristos#endif /* LIBC_SCCS and not lint */
451.1Schristos
461.1Schristos/* memcmp(s1, s2, n) */
471.1Schristos
481.1Schristos/*
491.1Schristos * This is probably not the best we can do, but it is still 2-10 times
501.1Schristos * faster than the C version in the portable gen directory.
511.1Schristos *
521.1Schristos * Things that might help:
531.1Schristos *	- longword align when possible (only on the 68020)
541.1Schristos *	- use nested DBcc instructions or use one and limit size to 64K
551.1Schristos */
561.1SchristosENTRY(memcmp)
571.1Schristos	movl	%sp@(4),%a0		| string 1
581.1Schristos	movl	%sp@(8),%a1		| string 2
591.1Schristos	movl	%sp@(12),%d0		| length
601.1Schristos	jeq	bcdone			| if zero, nothing to do
611.1Schristos	movl	%a0,%d1
621.1Schristos	btst	#0,%d1			| string 1 address odd?
631.1Schristos	jeq	bceven			| no, skip alignment
641.1Schristos	cmpmb	%a0@+,%a1@+		| yes, compare a byte
651.1Schristos	jne	bcnoteq			| not equal, return non-zero
661.1Schristos	subql	#1,%d0			| adjust count
671.1Schristos	jeq	bcdone			| count 0, reutrn zero
681.1Schristosbceven:
691.1Schristos	movl	%a1,%d1
701.1Schristos	btst	#0,%d1			| string 2 address odd?
711.1Schristos	jne	bcbloop			| yes, no hope for alignment, compare bytes
721.1Schristos	movl	%d0,%d1			| no, both even
731.1Schristos	lsrl	#2,%d1			| convert count to longword count
741.1Schristos	jeq	bcbloop			| count 0, skip longword loop
751.1Schristosbclloop:
761.1Schristos	cmpml	%a0@+,%a1@+		| compare a longword
771.1Schristos	jne	bcnoteql		| not equal, return non-zero
781.1Schristos	subql	#1,%d1			| adjust count
791.1Schristos	jne	bclloop			| still more, keep comparing
801.1Schristos	andl	#3,%d0			| what remains
811.1Schristos	jeq	bcdone			| nothing, all done
821.1Schristosbcbloop:
831.1Schristos	cmpmb	%a0@+,%a1@+		| compare a byte
841.1Schristos	jne	bcnoteq			| not equal, return non-zero
851.1Schristos	subql	#1,%d0			| adjust count
861.1Schristos	jne	bcbloop			| still more, keep going
871.1Schristos	rts
881.1Schristosbcnoteql:
891.1Schristos	subql	#4,%a0
901.1Schristos	subql	#4,%a1
911.1Schristos	movl	#4,%d0
921.1Schristos	jra	bcbloop
931.1Schristosbcnoteq:
941.1Schristos	clrl	%d0
951.1Schristos	clrl	%d1
961.1Schristos	movb	%a0@-,%d0
971.1Schristos	movb	%a1@-,%d1
981.1Schristos	subl	%d1,%d0
991.1Schristosbcdone:
1001.1Schristos	rts
101