Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. /// Returns the number of trailing zeros in a 32bit unsigned integer.
  2. ///
  3. /// Hacker's Delight, Reiser's algorithm.
  4. /// "Three ops including a "remainder, plus an indexed load."
  5. ///
  6. /// Works because each bit in the 32 bit integer hash uniquely to the
  7. /// prime number 37. The lowest set bit is returned via (x & -x).
  8. ntz32(int x) {
  9. assert(x < 0x100000000, "only 32bit numbers supported");
  10. return _ntzLut32[(x & -x) % 37];
  11. }
  12.  
  13. /// Returns the number of trailing zeros in a 64bit unsigned integer.
  14. ///
  15. /// Modification of ntz32 - each bit in a 64 bit integer hash to the prime
  16. /// number 131. The lowest set bit is returned via (x & -x).
  17. ntz64(int x) => _ntzLut64[(x & -x) % 131];
  18.  
  19. const _ntzLut32 = [
  20. 32, 0, 1, 26, 2, 23, 27, 0, //
  21. 3, 16, 24, 30, 28, 11, 0, 13,
  22. 4, 7, 17, 0, 25, 22, 31, 15,
  23. 29, 10, 12, 6, 0, 21, 14, 9,
  24. 5, 20, 8, 19, 18
  25. ];
  26.  
  27. const _ntzLut64 = [
  28. 64, 0, 1, -1, 2, 46, -1, -1, 3, 14, 47, 56, -1, 18, -1, //
  29. -1, 4, 43, 15, 35, 48, 38, 57, 23, -1, -1, 19, -1, -1, 51,
  30. -1, 29, 5, 63, 44, 12, 16, 41, 36, -1, 49, -1, 39, -1, 58,
  31. 60, 24, -1, -1, 62, -1, -1, 20, 26, -1, -1, -1, -1, 52, -1,
  32. -1, -1, 30, -1, 6, -1, -1, -1, 45, -1, 13, 55, 17, -1, 42,
  33. 34, 37, 22, -1, -1, 50, 28, -1, 11, 40, -1, -1, -1, 59,
  34. -1, 61, -1, 25, -1, -1, -1, -1, -1, -1, -1, -1, 54, -1,
  35. 33, 21, -1, 27, 10, -1, -1, -1, -1, -1, -1, -1, -1, 53,
  36. 32, -1, 9, -1, -1, -1, -1, 31, 8, -1, -1, 7, -1, -1,
  37. ];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement