Documentation
¶
Overview ¶
Package uint128 provides 128-bit unsigned integer type and basic operations.
Index ¶
- Constants
- Variables
- func QuoRem256By128(u Uint128, carry Uint128, v Uint128) (Uint128, Uint128, errors.Error)
- type Uint128
- func FromBigInt(i *big.Int) (Uint128, errors.Error)
- func FromBytes(bs [16]byte) Uint128
- func FromBytesBigEndian(b [16]byte) Uint128
- func FromString[S string | []byte](s S) (Uint128, errors.Error)
- func FromUint64(u uint64) Uint128
- func QuoRem192By64(u Uint128, carry uint64, v uint64) (Uint128, uint64, errors.Error)
- func SubUnsafe(u Uint128, v Uint128) Uint128
- func (self Uint128) Add(other Uint128) (Uint128, errors.Error)
- func (self Uint128) Add64(other uint64) (Uint128, errors.Error)
- func (self Uint128) And(other Uint128) Uint128
- func (self Uint128) And64(other uint64) Uint128
- func (self Uint128) AppendBytes(bs []byte) []byte
- func (self Uint128) AppendBytesBigEndian(bs []byte) []byte
- func (self Uint128) BigInt() *big.Int
- func (self Uint128) BitLen() int
- func (self Uint128) Bytes() [16]byte
- func (self Uint128) BytesBigEndian() [16]byte
- func (self Uint128) Compare(other Uint128) int
- func (self Uint128) Div(other Uint128) (Uint128, errors.Error)
- func (self Uint128) Div64(other uint64) (Uint128, errors.Error)
- func (self Uint128) Equal(other Uint128) bool
- func (self Uint128) IsZero() bool
- func (self Uint128) LeadingZeroBitsCount() int
- func (self Uint128) Lsh(n uint) Uint128
- func (self Uint128) MarshalText() ([]byte, error)
- func (self Uint128) Mod(other Uint128) (Uint128, errors.Error)
- func (self Uint128) Mod64(other uint64) (uint64, errors.Error)
- func (self Uint128) Mul(other Uint128) (Uint128, errors.Error)
- func (self Uint128) Mul64(other uint64) (Uint128, errors.Error)
- func (self Uint128) MulCarry(other Uint128) (Uint128, Uint128)
- func (self Uint128) NonZeroBitsCount() int
- func (self Uint128) Or(other Uint128) Uint128
- func (self Uint128) Or64(other uint64) Uint128
- func (self Uint128) PutBytes(bs []byte) errors.Error
- func (self Uint128) PutBytesBigEndian(bs []byte) errors.Error
- func (self Uint128) QuoRem(other Uint128) (Uint128, Uint128, errors.Error)
- func (self Uint128) QuoRem64(other uint64) (Uint128, uint64, errors.Error)
- func (self Uint128) ReverseBits() Uint128
- func (self Uint128) ReverseBytes() Uint128
- func (self Uint128) RotateBitsLeft(k int) Uint128
- func (self Uint128) RotateBitsRight(k int) Uint128
- func (self Uint128) Rsh(n uint) Uint128
- func (self *Uint128) Scan(s fmt.ScanState, ch rune) error
- func (self Uint128) String() string
- func (self Uint128) StringToBuf(buf []byte) []byte
- func (self Uint128) Sub(other Uint128) (Uint128, errors.Error)
- func (self Uint128) Sub64(other uint64) (Uint128, errors.Error)
- func (self Uint128) TrailingZeroBitsCount() int
- func (self Uint128) Uint64() (uint64, errors.Error)
- func (self *Uint128) UnmarshalText(b []byte) error
- func (self Uint128) Xor(other Uint128) Uint128
- func (self Uint128) Xor64(other uint64) Uint128
Constants ¶
const ( // MaxUint64Str is the string representation of the maximum uint64 value. MaxUint64Str = "18446744073709551615" // MaxUint128Str is the string representation of the maximum uint128 value. MaxUint128Str = "340282366920938463463374607431768211455" // MaxStrLen is the maximum number of digits in a 128-bit unsigned integer. MaxStrLen = 39 // MaxStrLen64 is the maximum number of digits in a 64-bit unsigned integer. MaxStrLen64 = 20 // MaxSafeStrLen64 is the maximum number of digits that can be safely parsed as a 64-bit unsigned integer. MaxSafeStrLen64 = 19 // ZeroStr is the string representation of the zero value. ZeroStr = "0" )
Variables ¶
var ( // Zero is the zero Uint128 value. Zero = Uint128{} // One is the Uint128 value of 1. One = Uint128{Lo: 1} // Max is the maximum Uint128 value. Max = Uint128{math.MaxUint64, math.MaxUint64} // Max64 is the maximum Uint128 value that fits in a 64-bit unsigned integer. Max64 = Uint128{math.MaxUint64, 0} // Pow10Uint64 is an array of precalculated powers of 10 for uint64 values (from 10^0 to 10^19). Pow10Uint64 = [...]uint64{ 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000, 10000000000000000000, } // Pow10Uint128 is an array of precalculated powers of 10 for Uint128 values (from 10^0 to 10^38). Pow10Uint128 = [...]Uint128{ {Lo: 1}, {Lo: 10}, {Lo: 100}, {Lo: 1000}, {Lo: 10000}, {Lo: 100000}, {Lo: 1000000}, {Lo: 10000000}, {Lo: 100000000}, {Lo: 1000000000}, {Lo: 10000000000}, {Lo: 100000000000}, {Lo: 1000000000000}, {Lo: 10000000000000}, {Lo: 100000000000000}, {Lo: 1000000000000000}, {Lo: 10000000000000000}, {Lo: 100000000000000000}, {Lo: 1000000000000000000}, {Lo: 10000000000000000000}, {Lo: 7766279631452241920, Hi: 5}, {Lo: 3875820019684212736, Hi: 54}, {Lo: 1864712049423024128, Hi: 542}, {Lo: 200376420520689664, Hi: 5421}, {Lo: 2003764205206896640, Hi: 54210}, {Lo: 1590897978359414784, Hi: 542101}, {Lo: 15908979783594147840, Hi: 5421010}, {Lo: 11515845246265065472, Hi: 54210108}, {Lo: 4477988020393345024, Hi: 542101086}, {Lo: 7886392056514347008, Hi: 5421010862}, {Lo: 5076944270305263616, Hi: 54210108624}, {Lo: 13875954555633532928, Hi: 542101086242}, {Lo: 9632337040368467968, Hi: 5421010862427}, {Lo: 4089650035136921600, Hi: 54210108624275}, {Lo: 4003012203950112768, Hi: 542101086242752}, {Lo: 3136633892082024448, Hi: 5421010862427522}, {Lo: 12919594847110692864, Hi: 54210108624275221}, {Lo: 68739955140067328, Hi: 542101086242752217}, {Lo: 687399551400673280, Hi: 5421010862427522170}, } )
Functions ¶
Types ¶
type Uint128 ¶
Uint128 is a 128-bit unsigned integer type.
func FromBigInt ¶
FromBigInt creates a new Uint128 from a *big.Int
func FromBytesBigEndian ¶
FromBytesBigEndian creates a new Uint128 from a [16]byte in big endian
func FromString ¶
FromString creates a new Uint128 from a string
func QuoRem192By64 ¶
QuoRem192By64 return q, r which: q must be a u128 u = q*v + r Returns error if u.carry >= v, because the result can't fit into u128
func SubUnsafe ¶
SubUnsafe returns u - v with u >= v must be called only when u >= v or the result will be incorrect
func (Uint128) And64 ¶
And64 returns the bitwise AND of a 128-bit unsigned integer and a 64-bit unsigned integer.
func (Uint128) AppendBytes ¶
AppendBytes appends the Uint128 to the byte slice bs in little-endian order.
func (Uint128) AppendBytesBigEndian ¶
AppendBytesBigEndian appends the Uint128 to the byte slice bs in big-endian order.
func (Uint128) BytesBigEndian ¶
BytesBigEndian returns the value as a [16]byte array in big-endian order.
func (Uint128) Compare ¶
Compare returns -1 if the value is less than the other value, 0 if the value is equal to the other value, and 1 if the value is greater than the other value.
func (Uint128) LeadingZeroBitsCount ¶
LeadingZeroBitsCount returns the number of leading zero bits in a 128-bit unsigned integer.
func (Uint128) Lsh ¶
Lsh returns the result of shifting a 128-bit unsigned integer to the left by n bits.
func (Uint128) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (Uint128) NonZeroBitsCount ¶
NonZeroBitsCount returns the number of non-zero bits in a 128-bit unsigned integer.
func (Uint128) Or64 ¶
Or64 returns the bitwise OR of a 128-bit unsigned integer and a 64-bit unsigned integer.
func (Uint128) PutBytesBigEndian ¶
PutBytesBigEndian writes the Uint128 to the byte slice bs in big-endian order.
func (Uint128) QuoRem ¶
QuoRem returns self / other and self % other and an error if the divisor is zero.
func (Uint128) QuoRem64 ¶
QuoRem64 returns self / other and self % other and an error if the divisor is zero.
func (Uint128) ReverseBits ¶
ReverseBits returns the result of reversing the bits of a 128-bit unsigned integer.
func (Uint128) ReverseBytes ¶
ReverseBytes returns the Uint128 with the byte order reversed.
func (Uint128) RotateBitsLeft ¶
RotateBitsLeft returns the result of rotating a 128-bit unsigned integer to the left by k bits.
func (Uint128) RotateBitsRight ¶
RotateBitsRight returns the result of rotating a 128-bit unsigned integer to the right by k bits.
func (Uint128) Rsh ¶
Rsh returns the result of shifting a 128-bit unsigned integer to the right by n bits.
func (Uint128) StringToBuf ¶
StringToBuf writes the value as a string to the given buffer (from end to start) and returns a slice containing the string.
func (Uint128) TrailingZeroBitsCount ¶
TrailingZeroBitsCount returns the number of trailing zero bits in a 128-bit unsigned integer.
func (Uint128) Uint64 ¶
Uint64 returns the value as uint64 if it fits, otherwise it returns an error.
func (*Uint128) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.