word-at-a-time.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef _ASM_WORD_AT_A_TIME_H
  2. #define _ASM_WORD_AT_A_TIME_H
  3. /*
  4. * This is largely generic for little-endian machines, but the
  5. * optimal byte mask counting is probably going to be something
  6. * that is architecture-specific. If you have a reliably fast
  7. * bit count instruction, that might be better than the multiply
  8. * and shift, for example.
  9. */
  10. #ifdef CONFIG_64BIT
  11. /*
  12. * Jan Achrenius on G+: microoptimized version of
  13. * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
  14. * that works for the bytemasks without having to
  15. * mask them first.
  16. */
  17. static inline long count_masked_bytes(unsigned long mask)
  18. {
  19. return mask*0x0001020304050608ul >> 56;
  20. }
  21. #else /* 32-bit case */
  22. /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
  23. static inline long count_masked_bytes(long mask)
  24. {
  25. /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
  26. long a = (0x0ff0001+mask) >> 23;
  27. /* Fix the 1 for 00 case */
  28. return a & mask;
  29. }
  30. #endif
  31. #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
  32. /* Return the high bit set in the first byte that is a zero */
  33. static inline unsigned long has_zero(unsigned long a)
  34. {
  35. return ((a - REPEAT_BYTE(0x01)) & ~a) & REPEAT_BYTE(0x80);
  36. }
  37. #endif /* _ASM_WORD_AT_A_TIME_H */