-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemmove.c
99 lines (75 loc) · 1.53 KB
/
memmove.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void* my_memmove(void* _src, void* _dest, size_t amount)
{
unsigned char * src = _src;
unsigned char * dest = _dest;
/* few cases, the man page for memmove says that 'as if a
temporrary buffer' were used
*/
if(src == dest)
return dest;
/* copy backwards */
if(dest > src) {
src += (amount-1);
dst += (amount-1);
while(amount--){
*dest = *src;
dest--;
src--;
}
}
else{
while(amount--){
*dest = *src;
src++;
dest++;
}
}
return _dest;
}
/* attempt at an optimized memcpy, possibly force using xmm regs */
static void* my_memmove_fast(void* _src, void* _dest, size_t amount)
{
unsigned long long *src = _src;
unsigned long long *dest = _dest;
/* standard says (I think) guaranteed to be AT LEAST 64 bit */
size_t longsize = sizeof(unsigned long long);
if(src == dest)
return dest;
if( dest > src) {
while(amount >= longsize) {
*dest = *src;
dest--;
src--;
amount -= longsize;
}
}
else{
while(amount >= longsize) {
*dest = *src;
dest++;
src++;
amount -= longsize;
}
}
/* clean up if cpy amount was not % 64 bit */
/* inline me! */
if(amount > 0){
my_memmove(src,dest,amount);
}
return _dest;
}
int main(int argc, char **argv)
{
size_t strlength = strlen(argv[1]);
char * temp = malloc(strlength+1);
if(!temp) {
exit(EXIT_FAILURE);
}
temp[strlength] = '\0';
temp = my_memmove_fast(argv[1],temp,strlength);
printf("%s %s\n", argv[1],temp);
return EXIT_SUCCESS;
}