Skip to content

Commit

Permalink
Common: Fix incorrect condition in PreviousPow2/NextPow2
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Nov 21, 2024
1 parent 378fd80 commit e6892e0
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/common/align.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,17 @@ constexpr T PreviousPow2(T value)
value |= (value >> 1);
value |= (value >> 2);
value |= (value >> 4);
if constexpr (sizeof(T) >= 16)
if constexpr (sizeof(T) >= 2)
value |= (value >> 8);
if constexpr (sizeof(T) >= 32)
if constexpr (sizeof(T) >= 4)
value |= (value >> 16);
if constexpr (sizeof(T) >= 64)
if constexpr (sizeof(T) >= 8)
value |= (value >> 32);
return value - (value >> 1);

return (value >> 1) + 1;
}

/// NOTE: Undefined for values greater than (1 << BITS-1), i.e. 0x80000000 for 32-bit.
template<typename T>
constexpr T NextPow2(T value)
{
Expand All @@ -77,11 +80,11 @@ constexpr T NextPow2(T value)
value |= (value >> 1);
value |= (value >> 2);
value |= (value >> 4);
if constexpr (sizeof(T) >= 16)
if constexpr (sizeof(T) >= 2)
value |= (value >> 8);
if constexpr (sizeof(T) >= 32)
if constexpr (sizeof(T) >= 4)
value |= (value >> 16);
if constexpr (sizeof(T) >= 64)
if constexpr (sizeof(T) >= 8)
value |= (value >> 32);
value++;
return value;
Expand Down

0 comments on commit e6892e0

Please sign in to comment.