word-at-a-time.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef _ASM_WORD_AT_A_TIME_H
  2. #define _ASM_WORD_AT_A_TIME_H
  3. #include <linux/kernel.h>
  4. /*
  5. * This is largely generic for little-endian machines, but the
  6. * optimal byte mask counting is probably going to be something
  7. * that is architecture-specific. If you have a reliably fast
  8. * bit count instruction, that might be better than the multiply
  9. * and shift, for example.
  10. */
  11. #ifdef CONFIG_64BIT
  12. /*
  13. * Jan Achrenius on G+: microoptimized version of
  14. * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
  15. * that works for the bytemasks without having to
  16. * mask them first.
  17. */
  18. static inline long count_masked_bytes(unsigned long mask)
  19. {
  20. return mask*0x0001020304050608ul >> 56;
  21. }
  22. #else /* 32-bit case */
  23. /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
  24. static inline long count_masked_bytes(long mask)
  25. {
  26. /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
  27. long a = (0x0ff0001+mask) >> 23;
  28. /* Fix the 1 for 00 case */
  29. return a & mask;
  30. }
  31. #endif
  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. /*
  38. * Load an unaligned word from kernel space.
  39. *
  40. * In the (very unlikely) case of the word being a page-crosser
  41. * and the next page not being mapped, take the exception and
  42. * return zeroes in the non-existing part.
  43. */
  44. static inline unsigned long load_unaligned_zeropad(const void *addr)
  45. {
  46. unsigned long ret, dummy;
  47. asm(
  48. "1:\tmov %2,%0\n"
  49. "2:\n"
  50. ".section .fixup,\"ax\"\n"
  51. "3:\t"
  52. "lea %2,%1\n\t"
  53. "and %3,%1\n\t"
  54. "mov (%1),%0\n\t"
  55. "leal %2,%%ecx\n\t"
  56. "andl %4,%%ecx\n\t"
  57. "shll $3,%%ecx\n\t"
  58. "shr %%cl,%0\n\t"
  59. "jmp 2b\n"
  60. ".previous\n"
  61. _ASM_EXTABLE(1b, 3b)
  62. :"=&r" (ret),"=&c" (dummy)
  63. :"m" (*(unsigned long *)addr),
  64. "i" (-sizeof(unsigned long)),
  65. "i" (sizeof(unsigned long)-1));
  66. return ret;
  67. }
  68. #endif /* _ASM_WORD_AT_A_TIME_H */