MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / roundUpToPowerOfTwo

Function roundUpToPowerOfTwo

app/src/DSP.h:53–65  ·  view source on GitHub ↗

* @brief Rounds @a value up to the next power of two (>= 2). Used by FixedQueue * to size its backing storage so wrap-around can mask with `& (cap-1)`. */

Source from the content-addressed store, hash-verified

51 * to size its backing storage so wrap-around can mask with `& (cap-1)`.
52 */
53[[nodiscard]] inline std::size_t roundUpToPowerOfTwo(std::size_t value) noexcept
54{
55 std::size_t v = value < 2 ? 2 : value;
56 --v;
57 v |= v >> 1;
58 v |= v >> 2;
59 v |= v >> 4;
60 v |= v >> 8;
61 v |= v >> 16;
62 if constexpr (sizeof(std::size_t) > 4)
63 v |= v >> 32;
64 return v + 1;
65}
66
67/**
68 * @brief A fixed-capacity, auto-overwriting circular buffer (FIFO queue) with pow2 storage.

Callers 2

FixedQueueMethod · 0.70
resizeMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected