max_pow2.c 869 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. {
  20. n1 >>= 1;
  21. p2 <<= 1;
  22. }
  23. if (p2 < n) 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. {
  43. n1 >>= 1;
  44. p2 <<= 1;
  45. }
  46. return(p2);
  47. }