github.com/coyove/sdss@v0.0.0-20231129015646-c2ec58cca6a2/contrib/roaring/clz_compat.go (about)

     1  //go:build !go1.9
     2  // +build !go1.9
     3  
     4  package roaring
     5  
     6  // LeadingZeroBits returns the number of consecutive most significant zero
     7  // bits of x.
     8  func countLeadingZeros(i uint64) int {
     9  	if i == 0 {
    10  		return 64
    11  	}
    12  	n := 1
    13  	x := uint32(i >> 32)
    14  	if x == 0 {
    15  		n += 32
    16  		x = uint32(i)
    17  	}
    18  	if (x >> 16) == 0 {
    19  		n += 16
    20  		x <<= 16
    21  	}
    22  	if (x >> 24) == 0 {
    23  		n += 8
    24  		x <<= 8
    25  	}
    26  	if x>>28 == 0 {
    27  		n += 4
    28  		x <<= 4
    29  	}
    30  	if x>>30 == 0 {
    31  		n += 2
    32  		x <<= 2
    33  
    34  	}
    35  	n -= int(x >> 31)
    36  	return n
    37  }