Function next_power_of_two

Synopsis

#include <src/entt/core/memory.hpp>

constexpr std::size_t next_power_of_two(const std::size_t value)

Description

Computes the smallest power of two greater than or equal to a value.

Parameters

value - The value to use.

Returns
The smallest power of two greater than or equal to the given value.

Source

Lines 84-93 in src/entt/core/memory.hpp.

[[nodiscard]] inline constexpr std::size_t next_power_of_two(const std::size_t value) ENTT_NOEXCEPT {
    ENTT_ASSERT(value < (std::size_t{1u} << (std::numeric_limits<std::size_t>::digits - 1)), "Numeric limits exceeded");
    std::size_t curr = value - (value != 0u);

    for(int next = 1; next < std::numeric_limits<std::size_t>::digits; next = next * 2) {
        curr |= curr >> next;
    }

    return ++curr;
}





Add Discussion

Log in to comment