github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/libnetwork/ipbits/uint128.go (about)

     1  package ipbits
     2  
     3  import (
     4  	"encoding/binary"
     5  	"math/bits"
     6  )
     7  
     8  type uint128 struct{ hi, lo uint64 }
     9  
    10  func uint128From16(b [16]byte) uint128 {
    11  	return uint128{
    12  		hi: binary.BigEndian.Uint64(b[:8]),
    13  		lo: binary.BigEndian.Uint64(b[8:]),
    14  	}
    15  }
    16  
    17  func uint128From(x uint64) uint128 {
    18  	return uint128{lo: x}
    19  }
    20  
    21  func (x uint128) add(y uint128) uint128 {
    22  	lo, carry := bits.Add64(x.lo, y.lo, 0)
    23  	hi, _ := bits.Add64(x.hi, y.hi, carry)
    24  	return uint128{hi: hi, lo: lo}
    25  }
    26  
    27  func (x uint128) lsh(n uint) uint128 {
    28  	if n > 64 {
    29  		return uint128{hi: x.lo << (n - 64)}
    30  	}
    31  	return uint128{
    32  		hi: x.hi<<n | x.lo>>(64-n),
    33  		lo: x.lo << n,
    34  	}
    35  }
    36  
    37  func (x uint128) rsh(n uint) uint128 {
    38  	if n > 64 {
    39  		return uint128{lo: x.hi >> (n - 64)}
    40  	}
    41  	return uint128{
    42  		hi: x.hi >> n,
    43  		lo: x.lo>>n | x.hi<<(64-n),
    44  	}
    45  }
    46  
    47  func (x uint128) and(y uint128) uint128 {
    48  	return uint128{hi: x.hi & y.hi, lo: x.lo & y.lo}
    49  }
    50  
    51  func (x uint128) not() uint128 {
    52  	return uint128{hi: ^x.hi, lo: ^x.lo}
    53  }
    54  
    55  func (x uint128) fill16(a *[16]byte) {
    56  	binary.BigEndian.PutUint64(a[:8], x.hi)
    57  	binary.BigEndian.PutUint64(a[8:], x.lo)
    58  }
    59  
    60  func (x uint128) uint64() uint64 {
    61  	return x.lo
    62  }