uint128.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2020 The Inet.Af AUTHORS. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package netaddr
  5. import "math/bits"
  6. // uint128 represents a uint128 using two uint64s.
  7. //
  8. // When the methods below mention a bit number, bit 0 is the most
  9. // significant bit (in hi) and bit 127 is the lowest (lo&1).
  10. type uint128 struct {
  11. hi uint64
  12. lo uint64
  13. }
  14. // isZero reports whether u == 0.
  15. //
  16. // It's faster than u == (uint128{}) because the compiler (as of Go
  17. // 1.15/1.16b1) doesn't do this trick and instead inserts a branch in
  18. // its eq alg's generated code.
  19. func (u uint128) isZero() bool { return u.hi|u.lo == 0 }
  20. // and returns the bitwise AND of u and m (u&m).
  21. func (u uint128) and(m uint128) uint128 {
  22. return uint128{u.hi & m.hi, u.lo & m.lo}
  23. }
  24. // xor returns the bitwise XOR of u and m (u^m).
  25. func (u uint128) xor(m uint128) uint128 {
  26. return uint128{u.hi ^ m.hi, u.lo ^ m.lo}
  27. }
  28. // or returns the bitwise OR of u and m (u|m).
  29. func (u uint128) or(m uint128) uint128 {
  30. return uint128{u.hi | m.hi, u.lo | m.lo}
  31. }
  32. // not returns the bitwise NOT of u.
  33. func (u uint128) not() uint128 {
  34. return uint128{^u.hi, ^u.lo}
  35. }
  36. // subOne returns u - 1.
  37. func (u uint128) subOne() uint128 {
  38. lo, borrow := bits.Sub64(u.lo, 1, 0)
  39. return uint128{u.hi - borrow, lo}
  40. }
  41. // addOne returns u + 1.
  42. func (u uint128) addOne() uint128 {
  43. lo, carry := bits.Add64(u.lo, 1, 0)
  44. return uint128{u.hi + carry, lo}
  45. }
  46. func u64CommonPrefixLen(a, b uint64) uint8 {
  47. return uint8(bits.LeadingZeros64(a ^ b))
  48. }
  49. func (u uint128) commonPrefixLen(v uint128) (n uint8) {
  50. if n = u64CommonPrefixLen(u.hi, v.hi); n == 64 {
  51. n += u64CommonPrefixLen(u.lo, v.lo)
  52. }
  53. return
  54. }
  55. func (u *uint128) halves() [2]*uint64 {
  56. return [2]*uint64{&u.hi, &u.lo}
  57. }
  58. // bitsSetFrom returns a copy of u with the given bit
  59. // and all subsequent ones set.
  60. func (u uint128) bitsSetFrom(bit uint8) uint128 {
  61. return u.or(mask6[bit].not())
  62. }
  63. // bitsClearedFrom returns a copy of u with the given bit
  64. // and all subsequent ones cleared.
  65. func (u uint128) bitsClearedFrom(bit uint8) uint128 {
  66. return u.and(mask6[bit])
  67. }