clz_compat.go 479 B

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