word-at-a-time.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /*
  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 */