4.4BSD snapshot (revision 8.1)
[unix-history] / usr / src / lib / libc / mips / string / bcopy.s
CommitLineData
db679d76 1/*-
1ddae302
KB
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
db679d76
KB
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Ralph Campbell.
7 *
8 * %sccs.include.redist.c%
9 */
10
ad4c803f 11#include <machine/machAsmDefs.h>
3e6938a3 12
db679d76 13#if defined(LIBC_SCCS) && !defined(lint)
1ddae302 14 ASMSTR("@(#)bcopy.s 8.1 (Berkeley) %G%")
db679d76
KB
15#endif /* LIBC_SCCS and not lint */
16
db679d76
KB
17/* bcopy(s1, s2, n) */
18
7e44e9c6 19#ifdef MIPSEL
3efc1bf9
RC
20# define LWHI lwr
21# define LWLO lwl
22# define SWHI swr
23# define SWLO swl
24#endif
7e44e9c6 25#ifdef MIPSEB
3efc1bf9
RC
26# define LWHI lwl
27# define LWLO lwr
28# define SWHI swl
29# define SWLO swr
30#endif
31
db679d76
KB
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,
d3f8a1f6 45 subu t0, t0, 1 # doesnt happen often so do slow way
db679d76
KB
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
3efc1bf9
RC
62 LWHI v0, 0(a0) # get next 4 bytes (unaligned)
63 LWLO v0, 3(a0)
db679d76 64 addu a0, a0, a3
3efc1bf9 65 SWHI v0, 0(a1) # store 1, 2, or 3 bytes to align a1
db679d76
KB
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:
3efc1bf9
RC
73 LWHI v0, 0(a0) # copy words a0 unaligned, a1 aligned
74 LWLO v0, 3(a0)
db679d76
KB
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
3efc1bf9 84 LWHI v0, 0(a0) # copy 1, 2, or 3 bytes to align
db679d76 85 addu a0, a0, a3
3efc1bf9 86 SWHI v0, 0(a1)
db679d76
KB
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)