Add IntLog2 and use it

Replace uses of floating point log2 when we want an integer result from
an integer operand.  This avoids concerns about accuracy of floating
point library functions.
This commit is contained in:
David Neto 2021-02-03 13:56:06 -05:00 committed by Jeremy Hayes
parent 5c4f421121
commit fa6e3c2737
4 changed files with 179 additions and 8 deletions

View file

@ -286,6 +286,18 @@ template <class T> bool IsMultipleOfPow2(T number, int powerOf2)
return ! (number & (powerOf2 - 1));
}
// Returns log2 of an integer power of 2.
// T should be integral.
template <class T> int IntLog2(T n)
{
assert(IsPow2(n));
int result = 0;
while ((T(1) << result) != n) {
result++;
}
return result;
}
} // end namespace glslang
#endif // _COMMON_INCLUDED_