From e5d49f662e9e925fa405c3cfb9ffa3e743044dd8 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 10 Apr 2020 13:10:18 +0200 Subject: common: fix mp_round_next_power_of_2() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Who write this dumb shit¹? It didn't handle 1<<31, and was unnecessarily slow. ¹ it was me --- common/common.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'common/common.c') diff --git a/common/common.c b/common/common.c index 5068995ded..0552bddda4 100644 --- a/common/common.c +++ b/common/common.c @@ -344,9 +344,10 @@ unsigned int mp_log2(uint32_t v) // 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; + if (!v) + return 1; + if (!(v & (v - 1))) + return v; + int l = mp_log2(v) + 1; + return l == 32 ? 0 : (uint32_t)1 << l; } -- cgit v1.2.3