diff options
author | wm4 <wm4@nowhere> | 2020-04-10 13:10:18 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2020-04-10 13:10:18 +0200 |
commit | e5d49f662e9e925fa405c3cfb9ffa3e743044dd8 (patch) | |
tree | d328ea7a8891050432afd3ed7f5b36eb32430995 | |
parent | 40c8df8a542f62126c61a8fa5bf6518275ea9e05 (diff) | |
download | mpv-e5d49f662e9e925fa405c3cfb9ffa3e743044dd8.tar.bz2 mpv-e5d49f662e9e925fa405c3cfb9ffa3e743044dd8.tar.xz |
common: fix mp_round_next_power_of_2()
Who write this dumb shit¹? It didn't handle 1<<31, and was unnecessarily
slow.
¹ it was me
-rw-r--r-- | common/common.c | 11 |
1 files changed, 6 insertions, 5 deletions
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; } |