xref: /original-bsd/lib/libc/mips/string/bcopy.s (revision b193be73)
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 "DEFS.h"
12
13#if defined(LIBC_SCCS) && !defined(lint)
14	ASMSTR("@(#)bcopy.s	5.2 (Berkeley) 02/29/92")
15#endif /* LIBC_SCCS and not lint */
16
17/* bcopy(s1, s2, n) */
18
19LEAF(bcopy)
20	.set	noreorder
21	addu	t0, a0, a2		# t0 = end of s1 region
22	sltu	t1, a1, t0
23	sltu	t2, a0, a1
24	and	t1, t1, t2		# t1 = true if from < to < (from+len)
25	beq	t1, zero, forward	# non overlapping, do forward copy
26	slt	t2, a2, 12		# check for small copy
27
28	ble	a2, zero, 2f
29	addu	t1, a1, a2		# t1 = end of to region
301:
31	lb	v0, -1(t0)		# copy bytes backwards,
32	subu	t0, t0, 1		#   doesn't happen often so do slow way
33	subu	t1, t1, 1
34	bne	t0, a0, 1b
35	sb	v0, 0(t1)
362:
37	j	ra
38	nop
39forward:
40	bne	t2, zero, smallcpy	# do a small bcopy
41	xor	v0, a0, a1		# compare low two bits of addresses
42	and	v0, v0, 3
43	subu	a3, zero, a1		# compute # bytes to word align address
44	beq	v0, zero, aligned	# addresses can be word aligned
45	and	a3, a3, 3
46
47	beq	a3, zero, 1f
48	subu	a2, a2, a3		# subtract from remaining count
49	lwr	v0, 0(a0)		# get next 4 bytes (unaligned)
50	lwl	v0, 3(a0)
51	addu	a0, a0, a3
52	swr	v0, 0(a1)		# store 1, 2, or 3 bytes to align a1
53	addu	a1, a1, a3
541:
55	and	v0, a2, 3		# compute number of words left
56	subu	a3, a2, v0
57	move	a2, v0
58	addu	a3, a3, a0		# compute ending address
592:
60	lwr	v0, 0(a0)		# copy words a0 unaligned, a1 aligned
61	lwl	v0, 3(a0)
62	addu	a0, a0, 4
63	addu	a1, a1, 4
64	bne	a0, a3, 2b
65	sw	v0, -4(a1)
66	b	smallcpy
67	nop
68aligned:
69	beq	a3, zero, 1f
70	subu	a2, a2, a3		# subtract from remaining count
71	lwr	v0, 0(a0)		# copy 1, 2, or 3 bytes to align
72	addu	a0, a0, a3
73	swr	v0, 0(a1)
74	addu	a1, a1, a3
751:
76	and	v0, a2, 3		# compute number of whole words left
77	subu	a3, a2, v0
78	move	a2, v0
79	addu	a3, a3, a0		# compute ending address
802:
81	lw	v0, 0(a0)		# copy words
82	addu	a0, a0, 4
83	addu	a1, a1, 4
84	bne	a0, a3, 2b
85	sw	v0, -4(a1)
86smallcpy:
87	ble	a2, zero, 2f
88	addu	a3, a2, a0		# compute ending address
891:
90	lbu	v0, 0(a0)		# copy bytes
91	addu	a0, a0, 1
92	addu	a1, a1, 1
93	bne	a0, a3, 1b
94	sb	v0, -1(a1)
952:
96	j	ra
97	nop
98	.set	reorder
99END(bcopy)
100