bitrev.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef _LINUX_BITREV_H
  2. #define _LINUX_BITREV_H
  3. #include <linux/types.h>
  4. #ifdef CONFIG_HAVE_ARCH_BITREVERSE
  5. #include <asm/bitrev.h>
  6. #define __bitrev32 __arch_bitrev32
  7. #define __bitrev16 __arch_bitrev16
  8. #define __bitrev8 __arch_bitrev8
  9. #else
  10. extern u8 const byte_rev_table[256];
  11. static inline u8 __bitrev8(u8 byte)
  12. {
  13. return byte_rev_table[byte];
  14. }
  15. static inline u16 __bitrev16(u16 x)
  16. {
  17. return (__bitrev8(x & 0xff) << 8) | __bitrev8(x >> 8);
  18. }
  19. static inline u32 __bitrev32(u32 x)
  20. {
  21. return (__bitrev16(x & 0xffff) << 16) | __bitrev16(x >> 16);
  22. }
  23. #endif /* CONFIG_HAVE_ARCH_BITREVERSE */
  24. #define __constant_bitrev32(x) \
  25. ({ \
  26. u32 ___x = x; \
  27. ___x = (___x >> 16) | (___x << 16); \
  28. ___x = ((___x & (u32)0xFF00FF00UL) >> 8) | ((___x & (u32)0x00FF00FFUL) << 8); \
  29. ___x = ((___x & (u32)0xF0F0F0F0UL) >> 4) | ((___x & (u32)0x0F0F0F0FUL) << 4); \
  30. ___x = ((___x & (u32)0xCCCCCCCCUL) >> 2) | ((___x & (u32)0x33333333UL) << 2); \
  31. ___x = ((___x & (u32)0xAAAAAAAAUL) >> 1) | ((___x & (u32)0x55555555UL) << 1); \
  32. ___x; \
  33. })
  34. #define __constant_bitrev16(x) \
  35. ({ \
  36. u16 ___x = x; \
  37. ___x = (___x >> 8) | (___x << 8); \
  38. ___x = ((___x & (u16)0xF0F0U) >> 4) | ((___x & (u16)0x0F0FU) << 4); \
  39. ___x = ((___x & (u16)0xCCCCU) >> 2) | ((___x & (u16)0x3333U) << 2); \
  40. ___x = ((___x & (u16)0xAAAAU) >> 1) | ((___x & (u16)0x5555U) << 1); \
  41. ___x; \
  42. })
  43. #define __constant_bitrev8(x) \
  44. ({ \
  45. u8 ___x = x; \
  46. ___x = (___x >> 4) | (___x << 4); \
  47. ___x = ((___x & (u8)0xCCU) >> 2) | ((___x & (u8)0x33U) << 2); \
  48. ___x = ((___x & (u8)0xAAU) >> 1) | ((___x & (u8)0x55U) << 1); \
  49. ___x; \
  50. })
  51. #define bitrev32(x) \
  52. ({ \
  53. u32 __x = x; \
  54. __builtin_constant_p(__x) ? \
  55. __constant_bitrev32(__x) : \
  56. __bitrev32(__x); \
  57. })
  58. #define bitrev16(x) \
  59. ({ \
  60. u16 __x = x; \
  61. __builtin_constant_p(__x) ? \
  62. __constant_bitrev16(__x) : \
  63. __bitrev16(__x); \
  64. })
  65. #define bitrev8(x) \
  66. ({ \
  67. u8 __x = x; \
  68. __builtin_constant_p(__x) ? \
  69. __constant_bitrev8(__x) : \
  70. __bitrev8(__x) ; \
  71. })
  72. #endif /* _LINUX_BITREV_H */