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