Skip to content

Commit

Permalink
correct utils::nextHighestPowerOf2
Browse files Browse the repository at this point in the history
The bit shift was done as int and not as current type (size_t for example), thus the computed next highest power of 2 was wrong on large numbers.
  • Loading branch information
BorisMansencal committed May 6, 2017
1 parent 80314d9 commit 5b92eeb
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion gpu/utils/StaticUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static_assert(!isPowerOf2(3333), "isPowerOf2");

template <typename T>
constexpr __host__ __device__ T nextHighestPowerOf2(T v) {
return (isPowerOf2(v) ? (T) 2 * v : (1 << (log2(v) + 1)));
return (isPowerOf2(v) ? (T) 2 * v : ((T)1 << (log2(v) + 1)));
}

static_assert(nextHighestPowerOf2(1) == 2, "nextHighestPowerOf2");
Expand All @@ -73,4 +73,7 @@ static_assert(nextHighestPowerOf2(15) == 16, "nextHighestPowerOf2");
static_assert(nextHighestPowerOf2(16) == 32, "nextHighestPowerOf2");
static_assert(nextHighestPowerOf2(17) == 32, "nextHighestPowerOf2");

static_assert(nextHighestPowerOf2(1536000000u) == 2147483648u, "nextHighestPowerOf2");
static_assert(nextHighestPowerOf2<size_t>(2147483648) == (size_t)4294967296, "nextHighestPowerOf2");

} } } // namespace

0 comments on commit 5b92eeb

Please sign in to comment.