Skip to content

Instantly share code, notes, and snippets.

@nandor
Created January 23, 2014 17:34
Show Gist options
  • Save nandor/8583077 to your computer and use it in GitHub Desktop.
Save nandor/8583077 to your computer and use it in GitHub Desktop.
Implementation of strcmp with pcmpistri
.text
.globl fstrcmp
.type fstrcmp, @function
fstrcmp:
# If the address is already aligned, skip
testb $15, %dil
jz 1f
# If the first string is empty, quit
movsbl (%rdi), %edx
testb %dl, %dl
jnz 2f
movsbl (%rsi), %eax
negl %eax
ret
2:
movsbl (%rsi), %ecx
3:
# Compare bytes until we get to an aligned address
# If the difference is found, the function returns
movl %edx, %eax
subl %ecx, %eax
jne 3f
test $15, %dil
jz 1f
test %dl, %dl
jnz 4f
ret
4:
# Fetch next chars
addq $1, %rsi
addq $1, %rdi
movsbl (%rdi), %edx
movsbl (%rsi), %ecx
jmp 3b
3:
# Unaligned end
test $15, %dil
jz 1f
ret
1:
xorl %eax, %eax
xorq %rcx, %rcx
movaps (%rdi), %xmm0
pcmpistri $0x12, (%rsi), %xmm0
ja 5f
jc 7f
5:
addq $16, %rsi
addq $16, %rdi
movaps (%rdi), %xmm0
pcmpistri $0x12, (%rsi), %xmm0
ja 5b
jc 7f
6:
xorl %eax, %eax
ret
7:
movsbl (%rdi,%rcx,1), %eax
movsbl (%rsi,%rcx,1), %ecx
subl %ecx, %eax
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment