memchr.S 482 B

123456789101112131415161718192021
  1. /*
  2. * Written by J.T. Conklin <jtc@netbsd.org>.
  3. * Public domain.
  4. * Adapted for NetBSD/x86_64 by Frank van der Linden <fvdl@wasabisystems.com>
  5. */
  6. #include <machine/asm.h>
  7. ENTRY(memchr)
  8. movb %sil,%al /* set character to search for */
  9. movq %rdx,%rcx /* set length of search */
  10. testq %rcx,%rcx /* test for len == 0 */
  11. jz L1
  12. repne /* search! */
  13. scasb
  14. jne L1 /* scan failed, return null */
  15. leaq -1(%rdi),%rax /* adjust result of scan */
  16. ret
  17. L1: xorq %rax,%rax
  18. ret