/*	$NetBSD: bcopy.S,v 1.2.40.1 2009/08/16 03:02:47 matt Exp $	*/

/*
 * Mach Operating System
 * Copyright (c) 1993 Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */

/*
 *	File:	mips_bcopy.s
 *	Author:	Chris Maeda
 *	Date:	June 1993
 *
 *	Fast copy routine.  Derived from aligned_block_copy.
 */


#include <mips/asm.h>
#ifndef _LOCORE
#define _LOCORE		/* XXX not really, just assembly-code source */
#endif
#include <machine/endian.h>


#if defined(LIBC_SCCS) && !defined(lint)
	/* RCSID("from: @(#)mips_bcopy.s	2.2 CMU 18/06/93") */
	RCSID("$NetBSD: bcopy.S,v 1.2.40.1 2009/08/16 03:02:47 matt Exp $")
#endif /* LIBC_SCCS and not lint */

/*
 *	bcopy(caddr_t src, caddr_t dst, unsigned int len)
 *
 *	a0 	src address
 *	a1	dst address
 *	a2	length
 */

#if defined(MEMCOPY) || defined(MEMMOVE)
#ifdef MEMCOPY
#define	FUNCTION	memcpy
#else
#define FUNCTION	memmove
#endif
#define	SRCREG		a1
#define	DSTREG		a0
#else
#define	FUNCTION	bcopy
#define	SRCREG		a0
#define	DSTREG		a1
#endif

#define	SIZEREG		a2

LEAF(FUNCTION)
	.set	noat
	.set	noreorder

#if defined(MEMCOPY) || defined(MEMMOVE)
	/* set up return value, while we still can */
	move	v0,DSTREG
#endif
	/*
	 *	Make sure we can copy forwards.
	 */
	sltu	t0,SRCREG,DSTREG	# t0 == SRCREG < DSTREG
	bne	t0,zero,6f		# copy backwards

	/*
	 * 	There are four alignment cases (with frequency)
	 *	(Based on measurements taken with a DECstation 5000/200
	 *	 inside a Mach kernel.)
	 *
	 * 	aligned   -> aligned		(mostly)
	 * 	unaligned -> aligned		(sometimes)
	 * 	aligned,unaligned -> unaligned	(almost never)
	 *
	 *	Note that we could add another case that checks if
	 *	the destination and source are unaligned but the
	 *	copy is alignable.  eg if src and dest are both
	 *	on a halfword boundary.
	 */
#if 1
	andi		t1,DSTREG,(SZREG-1)	# get last bits of dest
	bne		t1,zero,3f		# dest unaligned
	andi		t0,SRCREG,(SZREG-1)	# get last bits of src
	bne		t0,zero,5f
#else
	andi		t1,DSTREG,(SZREG-1)	# get last bits of dest
	andi		t0,SRCREG,(SZREG-1)	# get last bits of src
	beq		t1,t0,97f		# aligned on non-word
	nop;
	bne		t1,zero,3f		# dest unaligned
	nop
	b		5f			# source unaligned
	nop

97:
	sltiu		t1,SIZEREG,SZREG
	bne		t1,zero,3f
	nop
	subu		t2,zero,t2		# t2 = -t0
	andi		t2,t2,(SZREG-1)		# t2 &= (SZREG-1)
						# t0 + t2 == SZREG
	PTR_SUBU	SIZEREG,SIZEREG,t2	# retreat to word boundary
	PTR_ADDU	DSTREG,DSTREG,t2	# advance to word boundary
	PTR_ADDU	SRCREG,SRCREG,t2	# advance to word boundary
	REG_L		a3,-SZREG(DSTREG)
	REG_L		v1,-SZREG(SRCREG)
	sll		t0,t0,3			# bits to clear in dest
	sll		t1,t1,3			# bits to clear in source
/*
 * DST = 01 23 45 67 BE: 01234567 LE: 67452301
 *			 01000000     00000001
 * SRC = 89 ab cd ef BE: 89abcdef LE: efcdab89
 *			 00abcdef     efcdab00
 *			 01abcdef
 */
#if _BYTE_ORDER == _BIG_ENDIAN
	REG_SRLV	a3,a3,t1		# clear lsb dest bits
	REG_SLLV	a3,a3,t1
	REG_SLLV	v1,v1,t0		# clear msb source bits
	REG_SRLV	v1,v1,t0
#endif
#if _BYTE_ORDER == _LITTLE_ENDIAN
	REG_SLLV	a3,a3,t1		# clear msb dest bits
	REG_SRLV	a3,a3,t1
	REG_SRLV	v1,v1,t0		# clear lsb source bits
	REG_SLLV	v1,v1,t0
#endif
	or		a3,a3,v1		# merge
	REG_S		a3,-SZREG(DSTREG)	# and save
99:
#endif

	/*
	 *	Forward aligned->aligned copy, 8*4 bytes at a time.
	 */
98:
	li		AT,-(SZREG*8)
	and		t0,SIZEREG,AT		# count truncated to multiples
	PTR_ADDU	a3,SRCREG,t0		# run fast loop up to this addr
	sltu		AT,SRCREG,a3		# any work to do?
	beq		AT,zero,2f
	PTR_SUBU	SIZEREG,t0

	/*
	 *	loop body
	 */
1:	# cp
	REG_L		t3,(0*SZREG)(SRCREG)
	REG_L		v1,(1*SZREG)(SRCREG)
	REG_L		t0,(2*SZREG)(SRCREG)
	REG_L		t1,(3*SZREG)(SRCREG)
	PTR_ADDU	SRCREG,SZREG*8
	REG_S		t3,(0*SZREG)(DSTREG)
	REG_S		v1,(1*SZREG)(DSTREG)
	REG_S		t0,(2*SZREG)(DSTREG)
	REG_S		t1,(3*SZREG)(DSTREG)
	REG_L		t3,(-4*SZREG)(SRCREG)
	REG_L		v1,(-3*SZREG)(SRCREG)
	REG_L		t0,(-2*SZREG)(SRCREG)
	REG_L		t1,(-1*SZREG)(SRCREG)
	PTR_ADDU	DSTREG,SZREG*8
	REG_S		t1,(-1*SZREG)(DSTREG)
	REG_S		t0,(-2*SZREG)(DSTREG)
	REG_S		v1,(-3*SZREG)(DSTREG)
	bne		SRCREG,a3,1b
	REG_S		t3,(-4*SZREG)(DSTREG)

	/*
	 *	Copy a word at a time, no loop unrolling.
	 */
2:	# wordcopy
	andi		t2,SIZEREG,(SZREG-1)	# get byte count / SZREG
	xor		t2,SIZEREG,t2		# t2 = words to copy * SZREG
	beq		t2,zero,3f
	PTR_ADDU	t0,SRCREG,t2		# stop at t0
	PTR_SUBU	SIZEREG,SIZEREG,t2
1:
	REG_L		t3,0(SRCREG)
	PTR_ADDU	SRCREG,SZREG
	REG_S		t3,0(DSTREG)
	bne		SRCREG,t0,1b
	PTR_ADDU	DSTREG,SZREG

3:	# bytecopy
	beq		SIZEREG,zero,4f		# nothing left to do?
	nop
1:
	lb		t3,0(SRCREG)
	PTR_ADDU	SRCREG,1
	sb		t3,0(DSTREG)
	PTR_SUBU	SIZEREG,1
	bgtz		SIZEREG,1b
	PTR_ADDU	DSTREG,1

4:	# copydone
	j	ra
	nop

	/*
	 *	Copy from unaligned source to aligned dest.
	 */
5:	# destaligned
	andi		t0,SIZEREG,(SZREG-1)	# t0 = bytecount mod SZREG
	PTR_SUBU	a3,SIZEREG,t0		# number of words to transfer
	beq		a3,zero,3b
	nop
	move		SIZEREG,t0		# this many to do after we are done
	PTR_ADDU	a3,SRCREG,a3		# stop point

1:
	LWHI		t3,0(SRCREG)
	LWLO		t3,3(SRCREG)
	PTR_ADDI	SRCREG,SZREG
	sw		t3,0(DSTREG)
	bne		SRCREG,a3,1b
	PTR_ADDI	DSTREG,SZREG

	j		3b
	nop

6:	# backcopy -- based on above
	PTR_ADDU	SRCREG,SIZEREG
	PTR_ADDU	DSTREG,SIZEREG
	andi		t1,DSTREG,3		# get last 3 bits of dest
	bne		t1,zero,3f
	andi		t0,SRCREG,3		# get last 3 bits of src
	bne		t0,zero,5f

	/*
	 *	Forward aligned->aligned copy, 8*4 bytes at a time.
	 */
	li		AT,(-8*SZREG)
	and		t0,SIZEREG,AT		# count truncated to multiple of 32
	beq		t0,zero,2f		# any work to do?
	PTR_SUBU	SIZEREG,t0
	PTR_SUBU	a3,SRCREG,t0

	/*
	 *	loop body
	 */
1:	# cp
	REG_L		t3,(-4*SZREG)(SRCREG)
	REG_L		v1,(-3*SZREG)(SRCREG)
	REG_L		t0,(-2*SZREG)(SRCREG)
	REG_L		t1,(-1*SZREG)(SRCREG)
	PTR_SUBU	SRCREG,8*SZREG
	REG_S		t3,(-4*SZREG)(DSTREG)
	REG_S		v1,(-3*SZREG)(DSTREG)
	REG_S		t0,(-2*SZREG)(DSTREG)
	REG_S		t1,(-1*SZREG)(DSTREG)
	REG_L		t3,(0*SZREG)(SRCREG)
	REG_L		v1,(1*SZREG)(SRCREG)
	REG_L		t0,(2*SZREG)(SRCREG)
	REG_L		t1,(3*SZREG)(SRCREG)
	PTR_SUBU	DSTREG,8*SZREG
	REG_S		t1,(3*SZREG)(DSTREG)
	REG_S		t0,(2*SZREG)(DSTREG)
	REG_S		v1,(1*SZREG)(DSTREG)
	bne		SRCREG,a3,1b
	REG_S		t3,(0*SZREG)(DSTREG)

	/*
	 *	Copy a word at a time, no loop unrolling.
	 */
2:	# wordcopy
	andi		t2,SIZEREG,SZREG-1	# get byte count / 4
	xor		t2,SIZEREG,t2		# t2 = number of words to copy
	beq		t2,zero,3f
	PTR_SUBU	t0,SRCREG,t2		# stop at t0
	PTR_SUBU	SIZEREG,SIZEREG,t2
1:
	REG_L		t3,-SZREG(SRCREG)
	PTR_SUBU	SRCREG,SZREG
	REG_S		t3,-SZREG(DSTREG)
	bne		SRCREG,t0,1b
	PTR_SUBU	DSTREG,SZREG

3:	# bytecopy
	beq		SIZEREG,zero,4f		# nothing left to do?
	nop
1:
	lb		t3,-1(SRCREG)
	PTR_SUBU	SRCREG,1
	sb		t3,-1(DSTREG)
	PTR_SUBU	SIZEREG,1
	bgtz		SIZEREG,1b
	PTR_SUBU	DSTREG,1

4:	# copydone
	j	ra
	nop

	/*
	 *	Copy from unaligned source to aligned dest.
	 */
5:	# destaligned
	andi		t0,SIZEREG,3		# t0 = bytecount mod 4
	PTR_SUBU	a3,SIZEREG,t0		# number of words to transfer
	beq		a3,zero,3b
	nop
	move		SIZEREG,t0		# this many to do after we are done
	PTR_SUBU	a3,SRCREG,a3		# stop point

1:
	LWHI		t3,-4(SRCREG)
	LWLO		t3,-1(SRCREG)
	PTR_SUBU	SRCREG,4
	sw		t3,-4(DSTREG)
	bne		SRCREG,a3,1b
	PTR_SUBU	DSTREG,4

	j		3b
	nop

	.set	reorder
	.set	at
	END(FUNCTION)
