const_hweight.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #ifndef _ASM_GENERIC_BITOPS_CONST_HWEIGHT_H_
  2. #define _ASM_GENERIC_BITOPS_CONST_HWEIGHT_H_
  3. /*
  4. * Compile time versions of __arch_hweightN()
  5. */
  6. #define __const_hweight8(w) \
  7. ((unsigned int) \
  8. ((!!((w) & (1ULL << 0))) + \
  9. (!!((w) & (1ULL << 1))) + \
  10. (!!((w) & (1ULL << 2))) + \
  11. (!!((w) & (1ULL << 3))) + \
  12. (!!((w) & (1ULL << 4))) + \
  13. (!!((w) & (1ULL << 5))) + \
  14. (!!((w) & (1ULL << 6))) + \
  15. (!!((w) & (1ULL << 7)))))
  16. #define __const_hweight16(w) (__const_hweight8(w) + __const_hweight8((w) >> 8 ))
  17. #define __const_hweight32(w) (__const_hweight16(w) + __const_hweight16((w) >> 16))
  18. #define __const_hweight64(w) (__const_hweight32(w) + __const_hweight32((w) >> 32))
  19. /*
  20. * Generic interface.
  21. */
  22. #define hweight8(w) (__builtin_constant_p(w) ? __const_hweight8(w) : __arch_hweight8(w))
  23. #define hweight16(w) (__builtin_constant_p(w) ? __const_hweight16(w) : __arch_hweight16(w))
  24. #define hweight32(w) (__builtin_constant_p(w) ? __const_hweight32(w) : __arch_hweight32(w))
  25. #define hweight64(w) (__builtin_constant_p(w) ? __const_hweight64(w) : __arch_hweight64(w))
  26. /*
  27. * Interface for known constant arguments
  28. */
  29. #define HWEIGHT8(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight8(w))
  30. #define HWEIGHT16(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight16(w))
  31. #define HWEIGHT32(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight32(w))
  32. #define HWEIGHT64(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight64(w))
  33. /*
  34. * Type invariant interface to the compile time constant hweight functions.
  35. */
  36. #define HWEIGHT(w) HWEIGHT64((u64)w)
  37. #endif /* _ASM_GENERIC_BITOPS_CONST_HWEIGHT_H_ */