xref: /original-bsd/lib/libc/mips/string/bcopy.s (revision c3e32dec)
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 * %sccs.include.redist.c%
9 */
10
11#include <machine/machAsmDefs.h>
12
13#if defined(LIBC_SCCS) && !defined(lint)
14	ASMSTR("@(#)bcopy.s	8.1 (Berkeley) 06/04/93")
15#endif /* LIBC_SCCS and not lint */
16
17/* bcopy(s1, s2, n) */
18
19#ifdef MIPSEL
20#	define	LWHI	lwr
21#	define	LWLO	lwl
22#	define	SWHI	swr
23#	define	SWLO	swl
24#endif
25#ifdef MIPSEB
26#	define	LWHI	lwl
27#	define	LWLO	lwr
28#	define	SWHI	swl
29#	define	SWLO	swr
30#endif
31
32LEAF(bcopy)
33	.set	noreorder
34	addu	t0, a0, a2		# t0 = end of s1 region
35	sltu	t1, a1, t0
36	sltu	t2, a0, a1
37	and	t1, t1, t2		# t1 = true if from < to < (from+len)
38	beq	t1, zero, forward	# non overlapping, do forward copy
39	slt	t2, a2, 12		# check for small copy
40
41	ble	a2, zero, 2f
42	addu	t1, a1, a2		# t1 = end of to region
431:
44	lb	v0, -1(t0)		# copy bytes backwards,
45	subu	t0, t0, 1		#   doesnt happen often so do slow way
46	subu	t1, t1, 1
47	bne	t0, a0, 1b
48	sb	v0, 0(t1)
492:
50	j	ra
51	nop
52forward:
53	bne	t2, zero, smallcpy	# do a small bcopy
54	xor	v0, a0, a1		# compare low two bits of addresses
55	and	v0, v0, 3
56	subu	a3, zero, a1		# compute # bytes to word align address
57	beq	v0, zero, aligned	# addresses can be word aligned
58	and	a3, a3, 3
59
60	beq	a3, zero, 1f
61	subu	a2, a2, a3		# subtract from remaining count
62	LWHI	v0, 0(a0)		# get next 4 bytes (unaligned)
63	LWLO	v0, 3(a0)
64	addu	a0, a0, a3
65	SWHI	v0, 0(a1)		# store 1, 2, or 3 bytes to align a1
66	addu	a1, a1, a3
671:
68	and	v0, a2, 3		# compute number of words left
69	subu	a3, a2, v0
70	move	a2, v0
71	addu	a3, a3, a0		# compute ending address
722:
73	LWHI	v0, 0(a0)		# copy words a0 unaligned, a1 aligned
74	LWLO	v0, 3(a0)
75	addu	a0, a0, 4
76	addu	a1, a1, 4
77	bne	a0, a3, 2b
78	sw	v0, -4(a1)
79	b	smallcpy
80	nop
81aligned:
82	beq	a3, zero, 1f
83	subu	a2, a2, a3		# subtract from remaining count
84	LWHI	v0, 0(a0)		# copy 1, 2, or 3 bytes to align
85	addu	a0, a0, a3
86	SWHI	v0, 0(a1)
87	addu	a1, a1, a3
881:
89	and	v0, a2, 3		# compute number of whole words left
90	subu	a3, a2, v0
91	move	a2, v0
92	addu	a3, a3, a0		# compute ending address
932:
94	lw	v0, 0(a0)		# copy words
95	addu	a0, a0, 4
96	addu	a1, a1, 4
97	bne	a0, a3, 2b
98	sw	v0, -4(a1)
99smallcpy:
100	ble	a2, zero, 2f
101	addu	a3, a2, a0		# compute ending address
1021:
103	lbu	v0, 0(a0)		# copy bytes
104	addu	a0, a0, 1
105	addu	a1, a1, 1
106	bne	a0, a3, 1b
107	sb	v0, -1(a1)
1082:
109	j	ra
110	nop
111	.set	reorder
112END(bcopy)
113