Code:
int asm_memcmp_block (const void *s1, const void *s2, int len)
{
register int retval;
asm volatile (
"xorq %%rbx, %%rbx\n"
"cmp_8:"
"cmpq $8, %%rax\n"
"jl cmp_4\n"
"movq (%%rsi, %%rbx, 0x8), %%rcx\n"
"movq (%%rdi, %%rbx, 0x8), %%rdx\n"
"cmpq %%rcx, %%rdx\n"
"jne _diff\n"
"addq $8, %%rbx\n"
"subq $8, %%rax\n"
"jmp cmp_8\n"
"cmp_4:"
"cmpq $4, %%rax\n"
"jl cmp_2\n"
"movl (%%rsi, %%rbx, 0x4), %%ecx\n"
"movl (%%rdi, %%rbx, 0x4), %%edx\n"
"cmpl %%ecx, %%edx\n"
"jne _diff\n"
"addq $4, %%rbx\n"
"subq $4, %%rax\n"
"jmp cmp_4\n"
"cmp_2:"
"cmpq $2, %%rax\n"
"jl cmp_1\n"
"movw (%%rsi, %%rbx, 0x2), %%cx\n"
"movw (%%rdi, %%rbx, 0x2), %%dx\n"
"cmpw %%cx, %%dx\n"
"jne _diff\n"
"addq $2, %%rbx\n"
"subq $2, %%rax\n"
"jmp cmp_2\n"
"cmp_1:"
"cmpq $1, %%rax\n"
"jl _same\n"
"movb (%%rsi, %%rbx, 0x1), %%cl\n"
"movb (%%rdi, %%rbx, 0x1), %%dl\n"
"cmp %%cl, %%dl\n"
"jne _diff\n"
"incq %%rbx\n"
"decq %%rax\n"
"jmp cmp_1\n"
"_same:"
"xorq %%rcx, %%rcx\n"
"jmp _out\n"
"_diff:"
"movq $1, %%rcx\n"
"jmp _out\n"
"_out:"
: "=c" (retval)
: "S" (s1), "D" (s2), "a" (len)
: "rbx", "rcx", "rdx"
);
return retval;
}
Does anyone have any suggestions as to shorten and/or improve the code? Perhaps using a macro for each value?