summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/common.c14
-rw-r--r--common/common.h1
2 files changed, 15 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;
+}
diff --git a/common/common.h b/common/common.h
index 5e6950c931..8dbb304625 100644
--- a/common/common.h
+++ b/common/common.h
@@ -89,6 +89,7 @@ bool mp_rect_contains(struct mp_rect *rc, int x, int y);
bool mp_rect_equals(struct mp_rect *rc1, struct mp_rect *rc2);
unsigned int mp_log2(uint32_t v);
+uint32_t mp_round_next_power_of_2(uint32_t v);
int mp_snprintf_cat(char *str, size_t size, const char *format, ...)
PRINTF_ATTRIBUTE(3, 4);