"image/draw"
"internal/byteorder"
"io"
+ "math/bits"
)
// Graphic control extension fields.
gcBlockSize = 0x04
)
-var log2Lookup = [8]int{2, 4, 8, 16, 32, 64, 128, 256}
-
func log2(x int) int {
- for i, v := range log2Lookup {
- if x <= v {
- return i
- }
+ if x < 2 {
+ return 0
}
- return -1
+ return bits.Len(uint(x-1)) - 1
}
// writer is a buffered writer.
}
func encodeColorTable(dst []byte, p color.Palette, size int) (int, error) {
- if uint(size) >= uint(len(log2Lookup)) {
+ if uint(size) >= 8 {
return 0, errors.New("gif: cannot encode color table with more than 256 entries")
}
for i, c := range p {
dst[3*i+1] = g
dst[3*i+2] = b
}
- n := log2Lookup[size]
+ n := 1 << (size + 1)
if n > len(p) {
// Pad with black.
clear(dst[3*len(p) : 3*n])
"internal/runtime/atomic"
"internal/runtime/gc"
"internal/runtime/sys"
+ "math/bits"
"unsafe"
)
// stacklog2 returns ⌊log_2(n)⌋.
func stacklog2(n uintptr) int {
- log2 := 0
- for n > 1 {
- n >>= 1
- log2++
+ if n == 0 {
+ return 0
}
- return log2
+ return bits.Len64(uint64(n))
}
// Allocates a stack from the free pool. Must be called with
package sort_test
import (
+ "math/bits"
"runtime"
. "sort"
stringspkg "strings"
// log2 computes the binary logarithm of x, rounded up to the next integer.
// (log2(0) == 0, log2(1) == 0, log2(2) == 1, log2(3) == 2, etc.)
func log2(x int) int {
- n := 0
- for p := 1; p < x; p += p {
- // p == 2**n
- n++
+ if x < 1 {
+ return 0
}
- // p/2 < x <= p == 2**n
- return n
+ return bits.Len(uint(x - 1))
}
func TestSearchEfficiency(t *testing.T) {