implement x86-64 assembly-optimized functions _cu_memcpy, _cu_memset, cu_memzero
This commit is contained in:
parent
f2a1dddbca
commit
3229779aff
3 changed files with 57 additions and 0 deletions
|
|
@ -1,6 +1,9 @@
|
||||||
.globl hw_bswap16
|
.globl hw_bswap16
|
||||||
.globl hw_bswap32
|
.globl hw_bswap32
|
||||||
.globl hw_bswap64
|
.globl hw_bswap64
|
||||||
|
.globl _cu_memcpy
|
||||||
|
.globl _cu_memset
|
||||||
|
.globl cu_memzero
|
||||||
.globl cu_memtest
|
.globl cu_memtest
|
||||||
|
|
||||||
.text
|
.text
|
||||||
|
|
@ -20,6 +23,45 @@ hw_bswap64:
|
||||||
bswap %rax
|
bswap %rax
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
|
||||||
|
_cu_memcpy:
|
||||||
|
// RDI - dst, RSI - src, RDX - size
|
||||||
|
mov %rdx, %rcx
|
||||||
|
shr $3, %rcx
|
||||||
|
rep movsq
|
||||||
|
|
||||||
|
mov %rdx, %rcx
|
||||||
|
and $7, %rcx
|
||||||
|
rep movsb
|
||||||
|
ret
|
||||||
|
|
||||||
|
_cu_memset:
|
||||||
|
// RDI - ptr, RSI - val, RDX - size
|
||||||
|
mov %rdx, %rax
|
||||||
|
|
||||||
|
mov %rsi, %rcx
|
||||||
|
shr $3, %rcx
|
||||||
|
rep stosq
|
||||||
|
|
||||||
|
mov %rsi, %rcx
|
||||||
|
and $7, %rcx
|
||||||
|
rep stosb
|
||||||
|
|
||||||
|
ret
|
||||||
|
|
||||||
|
cu_memzero:
|
||||||
|
xor %rax, %rax
|
||||||
|
|
||||||
|
mov %rsi, %rcx
|
||||||
|
shr $3, %rcx
|
||||||
|
rep stosq
|
||||||
|
|
||||||
|
mov %rsi, %rcx
|
||||||
|
and $7, %rcx
|
||||||
|
rep stosb
|
||||||
|
|
||||||
|
ret
|
||||||
|
|
||||||
cu_memtest:
|
cu_memtest:
|
||||||
// RDI - ptr, RSI - size
|
// RDI - ptr, RSI - size
|
||||||
xchg %rsi, %rdi
|
xchg %rsi, %rdi
|
||||||
|
|
|
||||||
4
cutil.h
4
cutil.h
|
|
@ -4,6 +4,9 @@
|
||||||
#include "cutypes.h"
|
#include "cutypes.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
extern void* _cu_memset(void* dst, int val, size_t size);
|
||||||
|
extern void* _cu_memcpy(void* dst, const void* src, size_t size);
|
||||||
|
|
||||||
#define CUTIL_MALLOC malloc
|
#define CUTIL_MALLOC malloc
|
||||||
#define CUTIL_REALLOC realloc
|
#define CUTIL_REALLOC realloc
|
||||||
#define CUTIL_FREE free
|
#define CUTIL_FREE free
|
||||||
|
|
@ -22,5 +25,6 @@ void cutil_init();
|
||||||
void cutil_exit();
|
void cutil_exit();
|
||||||
|
|
||||||
extern const void* cu_memtest(const void* mem, uint size);
|
extern const void* cu_memtest(const void* mem, uint size);
|
||||||
|
extern void cu_memzero(void* dst, size_t size);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
11
test.c
11
test.c
|
|
@ -109,6 +109,17 @@ int main()
|
||||||
char val2[] = {0,0,0,1,0,0,0,0,0,0,4,0,0};
|
char val2[] = {0,0,0,1,0,0,0,0,0,0,4,0,0};
|
||||||
printf("val2\tcu_memtest\t%p\n", cu_memtest(val2, sizeof(val2)));
|
printf("val2\tcu_memtest\t%p\n", cu_memtest(val2, sizeof(val2)));
|
||||||
|
|
||||||
|
uint val3[4] = {1, 2, 3, 4};
|
||||||
|
uint val4[4];
|
||||||
|
|
||||||
|
_cu_memcpy(val4, val3, sizeof(val3));
|
||||||
|
cu_memzero(val3, sizeof(val3));
|
||||||
|
printf("cu_memzero test\t%p\n", cu_memtest(val3, sizeof(val3)));
|
||||||
|
printf("val4 values\n");
|
||||||
|
for (unsigned i = 0; i < 4; i++)
|
||||||
|
printf("\t%u\n", val4[i]);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
printf("[endian]\n");
|
printf("[endian]\n");
|
||||||
printf("cutil_endian\t%u\n", cu_endian);
|
printf("cutil_endian\t%u\n", cu_endian);
|
||||||
printf("%x\t%x\n", 0x1234, cu_bswap16(0x1234));
|
printf("%x\t%x\n", 0x1234, cu_bswap16(0x1234));
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue