1 /* Copy memory to memory until the specified number of bytes
2 has been copied. Overlap is handled correctly.
4 Copyright (C) 2017-2024 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <https://www.gnu.org/licenses/>. */
25 .register %g2, #scratch
29 mov %o0, %g2 /* Save pointer to destination */
30 cmp %o1, %o0 /* if from address is >= to use forward copy */
31 bgeu,a %XCC, 2f /* else use backward if ... */
32 cmp %o2, 17 /* delay slot, for small counts copy bytes */
34 sub %o0, %o1, %o4 /* get difference of two addresses */
35 cmp %o2, %o4 /* compare size and difference of addresses */
36 bgu %XCC, .Lovbc /* if size is bigger, have to do overlapped copy */
37 cmp %o2, 17 /* delay slot, for small counts copy bytes */
39 * normal, copy forwards
41 2: bleu %XCC, .Ldbytecp
42 andcc %o1, 3, %o5 /* is src word aligned */
44 cmp %o5, 2 /* is src half-word aligned */
46 cmp %o5, 3 /* src is byte aligned */
47 ldub [%o1], %o3 /* move 1 or 3 bytes to align it */
49 stb %o3, [%o0] /* move a byte to align src */
53 b .Lald /* now go align dest */
57 lduh [%o1], %o3 /* know src is 2 byte aligned */
60 stb %o4, [%o0] /* have to do bytes, */
61 stb %o3, [%o0 + 1] /* don't know dst alignment */
66 andcc %o0, 3, %o5 /* align the destination address */
67 .Lald: bz,pn %icc, .Lw4cp
79 andn %o2, 3, %o3 /* i3 is aligned word count */
80 dec 4, %o3 /* avoid reading beyond tail of src */
81 sub %o1, %o0, %o1 /* i1 gets the difference */
83 1: sll %o4, 8, %g1 /* save residual bytes */
86 srl %o4, 24, %o5 /* merge with residual */
91 sub %o1, 3, %o1 /* used one byte of last word read */
102 dec 4, %o3 /* avoid reading beyond tail of src */
103 sub %o1, %o0, %o1 /* i1 gets the difference */
105 2: sll %o4, 24, %g1 /* save residual bytes */
108 srl %o4, 8, %o5 /* merge with residual */
113 sub %o1, 1, %o1 /* used three bytes of last word read */
125 andn %o2, 3, %o3 /* i3 is aligned word count */
126 dec 4, %o3 /* avoid reading beyond tail of src */
127 sub %o1, %o0, %o1 /* i1 gets the difference */
129 3: sll %o4, 16, %g1 /* save residual bytes */
132 srl %o4, 16, %o5 /* merge with residual */
137 sub %o1, 2, %o1 /* used two bytes of last word read */
143 andn %o2, 3, %o3 /* i3 is aligned word count */
144 sub %o1, %o0, %o1 /* i1 gets the difference */
146 1: lduw [%o1+%o0], %o4 /* read from address */
147 deccc 4, %o3 /* decrement count */
148 st %o4, [%o0] /* write at destination address */
150 inc 4, %o0 /* increment to address */
152 and %o2, 3, %o2 /* number of leftover bytes, if any */
155 * differenced byte copy, works with any alignment
159 sub %o1, %o0, %o1 /* i1 gets the difference */
161 4: stb %o4, [%o0] /* write to address */
162 inc %o0 /* inc to address */
163 7: deccc %o2 /* decrement count */
164 bge,a %XCC, 4b /* loop till done */
165 ldub [%o1+%o0], %o4 /* read from address */
167 mov %g2, %o0 /* return pointer to destination */
170 * an overlapped copy that must be done "backwards"
173 add %o1, %o2, %o1 /* get to end of source space */
174 add %o0, %o2, %o0 /* get to end of destination space */
175 sub %o1, %o0, %o1 /* i1 gets the difference */
177 5: dec %o0 /* decrement to address */
178 ldub [%o1+%o0], %o3 /* read a byte */
179 deccc %o2 /* decrement count */
180 bg,pt %XCC, 5b /* loop until done */
181 stb %o3, [%o0] /* write byte */
183 mov %g2, %o0 /* return pointer to destination */
186 libc_hidden_builtin_def (memmove)