The earlier code used a loop to determine the number of bits used and called ff_log2() on a power of two (and it would be easy to keep track of the exponent of said power-of-two); neither GCC nor Clang optimized the loop away or avoided the ff_log2(). This patch replaces the loop and the log2 with a single av_log2().
Unified · Split
| @@ -189,23 +189,10 @@ typedef struct VC2EncContext { · 188 unmodified lines | ||
| 189 | 189 | static av_always_inline void put_vc2_ue_uint(PutBitContext *pb, uint32_t val) |
| 190 | 190 | { |
| 191 | 191 | int i; |
| 192 | − int bits = 0; | |
| 193 | − unsigned topbit = 1, maxval = 1; | |
| 192 | + int bits = av_log2(++val); | |
| 193 | + unsigned topbit = 1 << bits; | |
| 194 | 194 | uint64_t pbits = 0; |
| 195 | 195 | |
| 196 | − if (!val++) { | |
| 197 | − put_bits(pb, 1, 1); | |
| 198 | − return; | |
| 199 | − } | |
| 200 | − | |
| 201 | − while (val > maxval) { | |
| 202 | − topbit <<= 1; | |
| 203 | − maxval <<= 1; | |
| 204 | − maxval |= 1; | |
| 205 | − } | |
| 206 | − | |
| 207 | − bits = ff_log2(topbit); | |
| 208 | − | |
| 209 | 196 | for (i = 0; i < bits; i++) { |
| 210 | 197 | topbit >>= 1; |
| 211 | 198 | av_assert2(pbits <= UINT64_MAX>>3); |
| @@ -219,18 +206,7 @@ static av_always_inline void put_vc2_ue_uint(PutBitContext *pb, uint32_t val) · 7 unmodified lines | ||
| 219 | 206 | |
| 220 | 207 | static av_always_inline int count_vc2_ue_uint(uint32_t val) |
| 221 | 208 | { |
| 222 | − int topbit = 1, maxval = 1; | |
| 223 | − | |
| 224 | − if (!val++) | |
| 225 | − return 1; | |
| 226 | − | |
| 227 | − while (val > maxval) { | |
| 228 | − topbit <<= 1; | |
| 229 | − maxval <<= 1; | |
| 230 | − maxval |= 1; | |
| 231 | − } | |
| 232 | − | |
| 233 | − return ff_log2(topbit)*2 + 1; | |
| 209 | + return 2 * av_log2(val + 1) + 1; | |
| 234 | 210 | } |
| 235 | 211 | |
| 236 | 212 | /* VC-2 10.4 - parse_info() */ |