utils.h 552 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2010
  4. * Texas Instruments, <www.ti.com>
  5. * Aneesh V <aneesh@ti.com>
  6. */
  7. #ifndef _UTILS_H_
  8. #define _UTILS_H_
  9. static inline s32 log_2_n_round_up(u32 n)
  10. {
  11. s32 log2n = -1;
  12. u32 temp = n;
  13. while (temp) {
  14. log2n++;
  15. temp >>= 1;
  16. }
  17. if (n & (n - 1))
  18. return log2n + 1; /* not power of 2 - round up */
  19. else
  20. return log2n; /* power of 2 */
  21. }
  22. static inline s32 log_2_n_round_down(u32 n)
  23. {
  24. s32 log2n = -1;
  25. u32 temp = n;
  26. while (temp) {
  27. log2n++;
  28. temp >>= 1;
  29. }
  30. return log2n;
  31. }
  32. #endif