fix undefined shift in countLeadingZeros (#1157)
This commit is contained in:
parent
3f1141452e
commit
14cdd65dd2
11
MathExtras.h
11
MathExtras.h
|
@ -425,14 +425,17 @@ static inline int64_t SignExtend64(uint64_t X, unsigned B) {
|
||||||
/// valid arguments.
|
/// valid arguments.
|
||||||
static inline unsigned int countLeadingZeros(int x)
|
static inline unsigned int countLeadingZeros(int x)
|
||||||
{
|
{
|
||||||
unsigned count = 0;
|
|
||||||
int i;
|
int i;
|
||||||
const unsigned bits = sizeof(x) * 8;
|
const unsigned bits = sizeof(x) * 8;
|
||||||
|
unsigned count = bits;
|
||||||
|
|
||||||
|
if (x < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
for (i = bits; --i; ) {
|
for (i = bits; --i; ) {
|
||||||
if (x < 0) break;
|
if (x == 0) break;
|
||||||
count++;
|
count--;
|
||||||
x <<= 1;
|
x >>= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
|
|
Loading…
Reference in New Issue