diff options
author | wm4 <wm4@nowhere> | 2019-11-06 21:35:49 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2019-11-06 21:35:49 +0100 |
commit | abb089431d0467c3609207d0b27359ef08a6c16d (patch) | |
tree | 95aa48afe3560ca1abbf6858b35b4c7826b8e949 /common/common.c | |
parent | 48fc642e0c98025e12eafd388655da37848f94ef (diff) | |
download | mpv-abb089431d0467c3609207d0b27359ef08a6c16d.tar.bz2 mpv-abb089431d0467c3609207d0b27359ef08a6c16d.tar.xz |
common: add a helper to round up to next power of 2
This is something relatively frequently needed, and there must be half a
dozen ad-hoc implementations in mpv. The next commit uses this, the
suspected duplicate implementations are hiding.
Diffstat (limited to 'common/common.c')
-rw-r--r-- | common/common.c | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/common/common.c b/common/common.c index 967ab961c0..5068995ded 100644 --- a/common/common.c +++ b/common/common.c @@ -336,3 +336,17 @@ unsigned int mp_log2(uint32_t v) return 0; #endif } + +// If a power of 2, return it, otherwise return the next highest one, or 0. +// mp_round_next_power_of_2(65) == 128 +// mp_round_next_power_of_2(64) == 64 +// mp_round_next_power_of_2(0) == 1 +// mp_round_next_power_of_2(UINT32_MAX) == 0 +uint32_t mp_round_next_power_of_2(uint32_t v) +{ + for (int n = 0; n < 30; n++) { + if ((1 << n) >= v) + return 1 << n; + } + return 0; +} |