Collection of DPF-based plugins for packaging
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.2KB

  1. /// @ref gtx_bit
  2. /// @file glm/gtx/bit.inl
  3. namespace glm
  4. {
  5. ///////////////////
  6. // highestBitValue
  7. template<typename genIUType>
  8. GLM_FUNC_QUALIFIER genIUType highestBitValue(genIUType Value)
  9. {
  10. genIUType tmp = Value;
  11. genIUType result = genIUType(0);
  12. while(tmp)
  13. {
  14. result = (tmp & (~tmp + 1)); // grab lowest bit
  15. tmp &= ~result; // clear lowest bit
  16. }
  17. return result;
  18. }
  19. template<length_t L, typename T, qualifier Q>
  20. GLM_FUNC_QUALIFIER vec<L, T, Q> highestBitValue(vec<L, T, Q> const& v)
  21. {
  22. return detail::functor1<L, T, T, Q>::call(highestBitValue, v);
  23. }
  24. ///////////////////
  25. // lowestBitValue
  26. template<typename genIUType>
  27. GLM_FUNC_QUALIFIER genIUType lowestBitValue(genIUType Value)
  28. {
  29. return (Value & (~Value + 1));
  30. }
  31. template<length_t L, typename T, qualifier Q>
  32. GLM_FUNC_QUALIFIER vec<L, T, Q> lowestBitValue(vec<L, T, Q> const& v)
  33. {
  34. return detail::functor1<L, T, T, Q>::call(lowestBitValue, v);
  35. }
  36. ///////////////////
  37. // powerOfTwoAbove
  38. template<typename genType>
  39. GLM_FUNC_QUALIFIER genType powerOfTwoAbove(genType value)
  40. {
  41. return isPowerOfTwo(value) ? value : highestBitValue(value) << 1;
  42. }
  43. template<length_t L, typename T, qualifier Q>
  44. GLM_FUNC_QUALIFIER vec<L, T, Q> powerOfTwoAbove(vec<L, T, Q> const& v)
  45. {
  46. return detail::functor1<L, T, T, Q>::call(powerOfTwoAbove, v);
  47. }
  48. ///////////////////
  49. // powerOfTwoBelow
  50. template<typename genType>
  51. GLM_FUNC_QUALIFIER genType powerOfTwoBelow(genType value)
  52. {
  53. return isPowerOfTwo(value) ? value : highestBitValue(value);
  54. }
  55. template<length_t L, typename T, qualifier Q>
  56. GLM_FUNC_QUALIFIER vec<L, T, Q> powerOfTwoBelow(vec<L, T, Q> const& v)
  57. {
  58. return detail::functor1<L, T, T, Q>::call(powerOfTwoBelow, v);
  59. }
  60. /////////////////////
  61. // powerOfTwoNearest
  62. template<typename genType>
  63. GLM_FUNC_QUALIFIER genType powerOfTwoNearest(genType value)
  64. {
  65. if(isPowerOfTwo(value))
  66. return value;
  67. genType const prev = highestBitValue(value);
  68. genType const next = prev << 1;
  69. return (next - value) < (value - prev) ? next : prev;
  70. }
  71. template<length_t L, typename T, qualifier Q>
  72. GLM_FUNC_QUALIFIER vec<L, T, Q> powerOfTwoNearest(vec<L, T, Q> const& v)
  73. {
  74. return detail::functor1<L, T, T, Q>::call(powerOfTwoNearest, v);
  75. }
  76. }//namespace glm