These are a few string routines (just the
mem* ones). I've tried to optimize them the best I can without having them be too big, but I'm unsure if I've done a good job.
I'd prefer size over speed unless it's just a few bytes, in which case that would be fine. I would also prefer not to sacrifice simplicity for speed.
.globl memchr memchr: mov %rdx, %rcx movzbl %sil, %eax repne scasb lea -1(%rdi), %rax test %rcx, %rcx cmove %rcx, %rax ret
.globl memcmp memcmp: mov %rdx, %rcx repe cmpsb movzbl -1(%rdi), %eax movzbl -1(%rsi), %edx sub %edx, %eax ret
.globl memcpy memcpy: mov %rdx, %rcx mov %rdi, %rax rep movsb ret
.globl memmove memmove: mov %rdx, %rcx mov %rdi, %rax cmp %rdi, %rsi jge 0f dec %rdx add %rdx, %rdi add %rdx, %rsi std 0: rep movsb cld ret
.globl memrchr memrchr: mov %rdx, %rcx add %rdx, %rdi movzbl %sil, %eax std repne scasb cld lea 1(%rdi), %rax test %rcx, %rcx cmove %rcx, %rax ret
.globl memset memset: mov %rdx, %rcx mov %rdi, %rdx movzbl %sil, %eax rep stosb mov %rdx, %rax ret
As usual for Stack Exchange sites, this code is released under CC/by-sa 3.0, but any future changes can be accessed here.