maths: fix log2

This commit is contained in:
Danny Robson 2014-09-17 16:41:19 +10:00
parent c142715b66
commit b3e72051d6
2 changed files with 7 additions and 4 deletions

View File

@ -48,14 +48,14 @@ log2 (T v) {
"log2 is only implemented for integers"); "log2 is only implemented for integers");
T l = 0; T l = 0;
while (v) { while (v >>= 1)
v >>= 1; ++l;
l += 1;
}
return l; return l;
} }
template uint8_t log2 (uint8_t);
template uint16_t log2 (uint16_t);
template uint32_t log2 (uint32_t); template uint32_t log2 (uint32_t);
template uint64_t log2 (uint64_t); template uint64_t log2 (uint64_t);

View File

@ -42,5 +42,8 @@ main (int, char **) {
CHECK_EQ (to_degrees (PI), 180); CHECK_EQ (to_degrees (PI), 180);
CHECK_EQ (to_radians (180), PI); CHECK_EQ (to_radians (180), PI);
CHECK_EQ (log2 (8u), 3);
CHECK_EQ (log2 (1u), 0);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }