From f2a1dddbca2c0c6274b89dd708aefec5444c852b Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Wed, 11 May 2022 19:18:45 +0300 Subject: [PATCH] implement cu_memtest in x86-64 assembly in more efficient way that C does --- arch/x86_64.S | 29 +++++++++++++++++++++++++++++ cutil.c | 4 ++-- cutil.h | 2 +- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/arch/x86_64.S b/arch/x86_64.S index 2bc995f..207caa0 100644 --- a/arch/x86_64.S +++ b/arch/x86_64.S @@ -1,6 +1,7 @@ .globl hw_bswap16 .globl hw_bswap32 .globl hw_bswap64 +.globl cu_memtest .text @@ -19,3 +20,31 @@ hw_bswap64: bswap %rax ret +cu_memtest: + // RDI - ptr, RSI - size + xchg %rsi, %rdi + + mov %rdi, %rcx + shr $3, %rcx + je .testok +.testq: + lodsq + test %rax, %rax + jnz .testfail + loop .testq + + mov %rdi, %rcx + and $7, %rcx + je .testok +.testb: + lodsb + test %al, %al + jnz .testfail + loop .testb + +.testok: + xor %rax, %rax + ret +.testfail: + mov %rsi, %rax + ret \ No newline at end of file diff --git a/cutil.c b/cutil.c index 4c8c170..ef5ca3d 100644 --- a/cutil.c +++ b/cutil.c @@ -25,7 +25,7 @@ void cutil_exit() { } -const void* cu_memtest(const void* mem, uint size) +/*const void* cu_memtest(const void* mem, uint size) { unsigned i; for (i = 0; i < size / sizeof(uword); i++) @@ -41,4 +41,4 @@ const void* cu_memtest(const void* mem, uint size) } return NULL; -} +}*/ diff --git a/cutil.h b/cutil.h index 14de1cb..f6e2b43 100644 --- a/cutil.h +++ b/cutil.h @@ -21,6 +21,6 @@ extern void* (*cu_memmove)(void*,const void*,size_t); void cutil_init(); void cutil_exit(); -const void* cu_memtest(const void* mem, uint size); +extern const void* cu_memtest(const void* mem, uint size); #endif \ No newline at end of file