0

The problem I have right now, is figuring out which intrinsic is best suited for the job.

I am attempting to parse a header as fast as possible. I check the first 16 bytes of the header with a few masks to get the METHOD first. The one I was recommended to use is _mm_cmpeq_epi8 and then load the mask with _mm_movemask_epi8.

Now I need to check the index of space characters to get the route. For example,

GET /this/is/a/route?id=777 HTTP/1.1\r\n

index 3, and 27.

Which intrinsic would be best suited for the job?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 2
    bit-scan one the movemask results, with an intrinsic for `tzcnt` or for the old `bsf`. – Peter Cordes Aug 28 '21 at 18:47
  • @PeterCordes Are you saying to continue using cmpeq_epi8 with a mask of all spaces, and then use bsf or tzcnt to somehow extract the index? – Christopher Clark Aug 28 '21 at 19:38
  • yes. `bsf` gives you the position of the lowest `1` bit. So it's not "somehow", it just does it. (Although to find every space, you'd have to loop through each vector with the clear-lowest-set-bit bithack, `mask &= mask-1` (which can compile to [blsr](https://www.felixcloutier.com/x86/blsr) if you have BMI1) – Peter Cordes Aug 28 '21 at 23:47
  • I'll play with it a bit. I'm not really seeing how to do that. @PeterCordes – Christopher Clark Aug 29 '21 at 03:12
  • I can't seem to import bsf. Am I missing something. `#include #include ` and none of the bsf's here are found. https://software.intel.com/sites/landingpage/IntrinsicsGuide/#!=undefined&text=bsf&expand=464 @PeterCordes – Christopher Clark Sep 04 '21 at 21:34
  • See [Find position of first (lowest) set bit in 32-bit number](https://stackoverflow.com/q/58439198). `__builtin_ctz` should work, or if you're only using it on non-zero inputs then `_mm_tzcnt_32` works, too, even on CPUs with BMI1, if you're using ICC or MSVC. – Peter Cordes Sep 04 '21 at 22:10
  • Or if you're using MSVC, [\_BitScanForward \_BitScanForward64 missing (VS2017) Snappy](https://stackoverflow.com/q/48998618) - MSVC defines `_BitScanForward` in `` – Peter Cordes Sep 04 '21 at 22:11
  • With C++20, `std::countr_zero`. – Peter Cordes Oct 22 '22 at 20:39

0 Answers0