bitsperlong.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_GENERIC_BITS_PER_LONG
  3. #define __ASM_GENERIC_BITS_PER_LONG
  4. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  5. #ifndef _UAPI__ASM_GENERIC_BITS_PER_LONG
  6. #define _UAPI__ASM_GENERIC_BITS_PER_LONG
  7. #ifndef __BITS_PER_LONG
  8. /*
  9. * In order to keep safe and avoid regression, only unify uapi
  10. * bitsperlong.h for some archs which are using newer toolchains
  11. * that have the definitions of __CHAR_BIT__ and __SIZEOF_LONG__.
  12. * See the following link for more info:
  13. * https://lore.kernel.org/linux-arch/[email protected]/
  14. */
  15. #if defined(__CHAR_BIT__) && defined(__SIZEOF_LONG__)
  16. #define __BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__)
  17. #else
  18. /*
  19. * There seems to be no way of detecting this automatically from user
  20. * space, so 64 bit architectures should override this in their
  21. * bitsperlong.h. In particular, an architecture that supports
  22. * both 32 and 64 bit user space must not rely on CONFIG_64BIT
  23. * to decide it, but rather check a compiler provided macro.
  24. */
  25. #define __BITS_PER_LONG 32
  26. #endif
  27. #endif
  28. #endif /* _UAPI__ASM_GENERIC_BITS_PER_LONG */
  29. #ifdef CONFIG_64BIT
  30. #ifndef BITS_PER_LONG
  31. #define BITS_PER_LONG 64
  32. #endif
  33. #else
  34. #define BITS_PER_LONG 32
  35. #endif /* CONFIG_64BIT */
  36. /*
  37. * FIXME: The check currently breaks x86-64 build, so it's
  38. * temporarily disabled. Please fix x86-64 and reenable
  39. */
  40. #if 0 && BITS_PER_LONG != __BITS_PER_LONG
  41. #error Inconsistent word size. Check asm/bitsperlong.h
  42. #endif
  43. #ifndef BITS_PER_LONG_LONG
  44. #define BITS_PER_LONG_LONG 64
  45. #endif
  46. /*
  47. * small_const_nbits(n) is true precisely when it is known at compile-time
  48. * that BITMAP_SIZE(n) is 1, i.e. 1 <= n <= BITS_PER_LONG. This allows
  49. * various bit/bitmap APIs to provide a fast inline implementation. Bitmaps
  50. * of size 0 are very rare, and a compile-time-known-size 0 is most likely
  51. * a sign of error. They will be handled correctly by the bit/bitmap APIs,
  52. * but using the out-of-line functions, so that the inline implementations
  53. * can unconditionally dereference the pointer(s).
  54. */
  55. #define small_const_nbits(nbits) \
  56. (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG && (nbits) > 0)
  57. #endif /* __ASM_GENERIC_BITS_PER_LONG */