Optimize log2FloorNonZero with math/bits.

This commit is contained in:
Andy Balholm 2021-09-22 11:21:01 -07:00
parent ec682fbe0a
commit 1d750214c2
1 changed files with 5 additions and 11 deletions

View File

@ -1,6 +1,9 @@
package brotli
import "math"
import (
"math"
"math/bits"
)
/* Copyright 2013 Google Inc. All Rights Reserved.
@ -11,16 +14,7 @@ import "math"
/* Utilities for fast computation of logarithms. */
func log2FloorNonZero(n uint) uint32 {
/* TODO: generalize and move to platform.h */
var result uint32 = 0
for {
n >>= 1
if n == 0 {
break
}
result++
}
return result
return uint32(bits.Len(n)) - 1
}
/* A lookup table for small values of log2(int) to be used in entropy