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