Your first function just do not work and is wrong.
You do not the correct parameter types. Do not use register
as it has no effect at all. Register is ignored at any level of optimization. Compilers are better than me or you in micro optimizations – so help the compiler optimizer with the const
and restrict
when applicable.
The second one can be simplified.
int stringsequal1(const char *restrict s1, const char *restrict s2)
{
do
{
if(*s1 != *s2) return 0;
}
while(*s1++ + *s2++);
return 1;
}
and thew result code for both is:
int stringsequal1(const char *s1, const char *s2)
{
do
{
if(*s1 != *s2) return 0;
}
while(*s1++ + *s2++);
return 1;
}
stringsequal1:
xor edx, edx
jmp .L11
.L15:
add rdx, 1
add eax, eax
je .L14
.L11:
movsx eax, BYTE PTR [rdi+rdx]
cmp al, BYTE PTR [rsi+rdx]
je .L15
mov eax, 1
ret
.L14:
ret
int stringsequal2(register char *s1, register char *s2)
{
while(1)
{
if(!((*s1 + *s2) ^ 0)) return 1;
if(*(s1 ++) ^ *(s2 ++)) break;
}
return 0;
}
stringsequal2:
xor eax, eax
jmp .L18
.L22:
add rax, 1
cmp cl, r9b
jne .L21
.L18:
movsx r8d, BYTE PTR [rdi+rax]
movsx r9d, BYTE PTR [rsi+rax]
mov ecx, r8d
add r8d, r9d
jne .L22
mov eax, 1
ret
.L21:
xor eax, eax
ret
0
solved Is there any way I can make my function for testing the equality of strings even faster?