max_pow2.c 743 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <grass/gis.h>
  2. #include <grass/gmath.h>
  3. /*!
  4. * \fn long G_math_max_pow2 (const long n)
  5. *
  6. * \brief Finds least power of 2 >= <b>n</b>
  7. *
  8. * Finds least power of 2 >= <b>n</b>.
  9. *
  10. * \param[in] n
  11. * \return long
  12. */
  13. long G_math_max_pow2(const long n)
  14. {
  15. long p2, n1;
  16. n1 = n >> 1;
  17. p2 = 1;
  18. while (n1 > 0) {
  19. n1 >>= 1;
  20. p2 <<= 1;
  21. }
  22. if (p2 < n)
  23. p2 <<= 1;
  24. return (p2);
  25. }
  26. /*!
  27. * \fn long G_math_min_pow2 (const long n)
  28. *
  29. * \brief Finds largest power of 2 <= <b>n</b>
  30. *
  31. * Finds largest power of 2 <= <b>n</b>.
  32. *
  33. * \param[in] n
  34. * \return long
  35. */
  36. long G_math_min_pow2(const long n)
  37. {
  38. long p2, n1;
  39. n1 = n >> 1;
  40. p2 = 1;
  41. while (n1 > 0) {
  42. n1 >>= 1;
  43. p2 <<= 1;
  44. }
  45. return (p2);
  46. }