1 |
93 |
Agner |
/********************************* memcpy.as ********************************
|
2 |
|
|
* Author: Agner Fog
|
3 |
|
|
* date created: 2018-03-25
|
4 |
|
|
* Last modified: 2021-04-25
|
5 |
|
|
* Version: 1.11
|
6 |
|
|
* Project: ForwardCom library libc.li
|
7 |
|
|
* Description: memcpy and memmove functions. Copy memory block
|
8 |
|
|
* C declaration: void *memcpy (void *dest, const void *src, uint64_t n)
|
9 |
|
|
* C declaration: void *memmove(void *dest, const void *src, uint64_t n)
|
10 |
|
|
*
|
11 |
|
|
* Copyright 2018-2021 GNU General Public License http://www.gnu.org/licenses
|
12 |
|
|
*****************************************************************************/
|
13 |
|
|
|
14 |
|
|
// ! To do: Make vector read and write aligned for better performance
|
15 |
|
|
|
16 |
|
|
public _memcpy: function, reguse = 0x1F, 1
|
17 |
|
|
public _memmove: function, reguse = 0x1F, 1
|
18 |
|
|
|
19 |
|
|
code section execute align = 4
|
20 |
|
|
|
21 |
|
|
// r0 = destination
|
22 |
|
|
// r1 = source
|
23 |
|
|
// r2 = n
|
24 |
|
|
_memcpy function
|
25 |
|
|
_memmove:
|
26 |
|
|
int64 r3 = r0 - r1
|
27 |
|
|
if (uint64 r3 >= r2) {
|
28 |
|
|
// destination will not overwrite source. copy forwards
|
29 |
|
|
int64 r3 = r0 + r2 // end of destination
|
30 |
|
|
int64 r4 = r1 + r2 // end of source
|
31 |
|
|
// vector loop. count down r2
|
32 |
|
|
for (int8 v0 in [r3-r2]) {
|
33 |
|
|
int8 v0 = [r4-r2, length = r2] // read from source
|
34 |
|
|
int8 [r3-r2, length = r2] = v0 // write to destination
|
35 |
|
|
}
|
36 |
|
|
}
|
37 |
|
|
else {
|
38 |
|
|
// destination overlaps source. copy backwards
|
39 |
|
|
push (r0) // save destination
|
40 |
|
|
int8 v0 = set_len(v0, r2) // length = min(n,maxlen)
|
41 |
|
|
int64 r3 = get_len(v0) // this will be the block size
|
42 |
|
|
int64 r0 = r0 + r2 - r3 // last block of destination
|
43 |
|
|
int64 r1 = r1 + r2 - r3 // last block of source
|
44 |
|
|
int64 r4 = r3 // current block size
|
45 |
|
|
while (uint64 r2 > 0) { // loop counting down remaining bytes
|
46 |
|
|
int8 v0 = [r1, length = r4] // read from source
|
47 |
|
|
int8 [r0, length = r4] = v0 // write to destination
|
48 |
|
|
int64 r2 -= r4 // subtract block size from remaining size
|
49 |
|
|
uint64 r4 = min_u(r2, r3) // block size = minimum of remaining bytes and maxlen
|
50 |
|
|
int64 r0 -= r4 // subtract next block size from destination
|
51 |
|
|
int64 r1 -= r4 // subtract next block size from source
|
52 |
|
|
}
|
53 |
|
|
pop (r0) // restore destination
|
54 |
|
|
}
|
55 |
|
|
return // return dest in r0 unchanged
|
56 |
|
|
_memcpy end
|
57 |
|
|
|
58 |
|
|
code end
|