pt_log2.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES
  4. *
  5. * Helper macros for working with log2 values
  6. *
  7. */
  8. #ifndef __GENERIC_PT_LOG2_H
  9. #define __GENERIC_PT_LOG2_H
  10. #include <linux/bitops.h>
  11. #include <linux/limits.h>
  12. /* Compute a */
  13. #define log2_to_int_t(type, a_lg2) ((type)(((type)1) << (a_lg2)))
  14. static_assert(log2_to_int_t(unsigned int, 0) == 1);
  15. /* Compute a - 1 (aka all low bits set) */
  16. #define log2_to_max_int_t(type, a_lg2) ((type)(log2_to_int_t(type, a_lg2) - 1))
  17. /* Compute a / b */
  18. #define log2_div_t(type, a, b_lg2) ((type)(((type)a) >> (b_lg2)))
  19. static_assert(log2_div_t(unsigned int, 4, 2) == 1);
  20. /*
  21. * Compute:
  22. * a / c == b / c
  23. * aka the high bits are equal
  24. */
  25. #define log2_div_eq_t(type, a, b, c_lg2) \
  26. (log2_div_t(type, (a) ^ (b), c_lg2) == 0)
  27. static_assert(log2_div_eq_t(unsigned int, 1, 1, 2));
  28. /* Compute a % b */
  29. #define log2_mod_t(type, a, b_lg2) \
  30. ((type)(((type)a) & log2_to_max_int_t(type, b_lg2)))
  31. static_assert(log2_mod_t(unsigned int, 1, 2) == 1);
  32. /*
  33. * Compute:
  34. * a % b == b - 1
  35. * aka the low bits are all 1s
  36. */
  37. #define log2_mod_eq_max_t(type, a, b_lg2) \
  38. (log2_mod_t(type, a, b_lg2) == log2_to_max_int_t(type, b_lg2))
  39. static_assert(log2_mod_eq_max_t(unsigned int, 3, 2));
  40. /*
  41. * Return a value such that:
  42. * a / b == ret / b
  43. * ret % b == val
  44. * aka set the low bits to val. val must be < b
  45. */
  46. #define log2_set_mod_t(type, a, val, b_lg2) \
  47. ((((type)(a)) & (~log2_to_max_int_t(type, b_lg2))) | ((type)(val)))
  48. static_assert(log2_set_mod_t(unsigned int, 3, 1, 2) == 1);
  49. /* Return a value such that:
  50. * a / b == ret / b
  51. * ret % b == b - 1
  52. * aka set the low bits to all 1s
  53. */
  54. #define log2_set_mod_max_t(type, a, b_lg2) \
  55. (((type)(a)) | log2_to_max_int_t(type, b_lg2))
  56. static_assert(log2_set_mod_max_t(unsigned int, 2, 2) == 3);
  57. /* Compute a * b */
  58. #define log2_mul_t(type, a, b_lg2) ((type)(((type)a) << (b_lg2)))
  59. static_assert(log2_mul_t(unsigned int, 2, 2) == 8);
  60. #define _dispatch_sz(type, fn, a) \
  61. (sizeof(type) == 4 ? fn##32((u32)a) : fn##64(a))
  62. /*
  63. * Return the highest value such that:
  64. * fls_t(u32, 0) == 0
  65. * fls_t(u3, 1) == 1
  66. * a >= log2_to_int(ret - 1)
  67. * aka find last set bit
  68. */
  69. static inline unsigned int fls32(u32 a)
  70. {
  71. return fls(a);
  72. }
  73. #define fls_t(type, a) _dispatch_sz(type, fls, a)
  74. /*
  75. * Return the highest value such that:
  76. * ffs_t(u32, 0) == UNDEFINED
  77. * ffs_t(u32, 1) == 0
  78. * log_mod(a, ret) == 0
  79. * aka find first set bit
  80. */
  81. static inline unsigned int __ffs32(u32 a)
  82. {
  83. return __ffs(a);
  84. }
  85. #define ffs_t(type, a) _dispatch_sz(type, __ffs, a)
  86. /*
  87. * Return the highest value such that:
  88. * ffz_t(u32, U32_MAX) == UNDEFINED
  89. * ffz_t(u32, 0) == 0
  90. * ffz_t(u32, 1) == 1
  91. * log_mod(a, ret) == log_to_max_int(ret)
  92. * aka find first zero bit
  93. */
  94. static inline unsigned int ffz32(u32 a)
  95. {
  96. return ffz(a);
  97. }
  98. static inline unsigned int ffz64(u64 a)
  99. {
  100. if (sizeof(u64) == sizeof(unsigned long))
  101. return ffz(a);
  102. if ((u32)a == U32_MAX)
  103. return ffz32(a >> 32) + 32;
  104. return ffz32(a);
  105. }
  106. #define ffz_t(type, a) _dispatch_sz(type, ffz, a)
  107. #endif