0

I have a general-purpose (e.g. rax) register that represents a sequence of ascii chars (each of them taking up one byte) and I want to determine if there is a zero byte somewhere. There must be some logical operations to do this (...more efficiently than shifting and checking each byte individually).

Example:

0x7172706b -> no zero byte

0x7172006b -> zero byte is present

  • 1
    On x86-64, SSE2 is always available, so zero XMM0, then `movq xmm1, rax` / `pcmpeqb xmm1, xmm0` / `pmovmskb ecx, xmm0` / `test cl,cl` sets ZF if there were no zero-bytes in RAX. Unless you're in kernel code and don't want to touch the vector registers, then it's worth considering a zero-in-word bithack (https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord / [Why does glibc's strlen need to be so complicated to run quickly?](https://stackoverflow.com/q/57650895) - glibc uses that as the portable fallback when hand-written for platforms without a hand-written asm version.) – Peter Cordes Jun 08 '22 at 21:04
  • If you're writing a strlen, of course it's better to load 16 bytes of string data into XMM register in the first place, not bounce through integer registers. Or just 8 if that's all you have. – Peter Cordes Jun 08 '22 at 21:36

0 Answers0