Merge pull request #2522 from dneto0/intlog2

Add IntLog2 and use it
This commit is contained in:
Greg Fischer
2021-04-14 15:16:04 -06:00
committed by GitHub
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_

View File

@@ -5602,14 +5602,7 @@ void TParseContext::setLayoutQualifier(const TSourceLoc& loc, TPublicType& publi
if (! IsPow2(value))
error(loc, "must be a power of 2", "buffer_reference_align", "");
else
#ifdef __ANDROID__
// Android NDK r15c tageting ABI 15 doesn't have full support for C++11
// (no std::exp2/log2). ::exp2 is available from C99 but ::log2 isn't
// available up until ABI 18 so we use the mathematical equivalent form
publicType.qualifier.layoutBufferReferenceAlign = (unsigned int)(std::log(value) / std::log(2.0));
#else
publicType.qualifier.layoutBufferReferenceAlign = (unsigned int)std::log2(value);
#endif
publicType.qualifier.layoutBufferReferenceAlign = IntLog2(value);
if (nonLiteral)
error(loc, "needs a literal integer", "buffer_reference_align", "");
return;