uint128

package
v1.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2025 License: BSD-3-Clause, MIT Imports: 6 Imported by: 0

Documentation

Overview

Package uint128 provides 128-bit unsigned integer type and basic operations.

Index

Constants

View Source
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

View Source
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

func QuoRem256By128

func QuoRem256By128(u Uint128, carry Uint128, v Uint128) (Uint128, Uint128, errors.Error)

QuoRem256By128 returns quotient, remainder and error.

Types

type Uint128

type Uint128 struct {
	Lo uint64
	Hi uint64
}

Uint128 is a 128-bit unsigned integer type.

func FromBigInt

func FromBigInt(i *big.Int) (Uint128, errors.Error)

FromBigInt creates a new Uint128 from a *big.Int

func FromBytes

func FromBytes(bs [16]byte) Uint128

FromBytes creates a new Uint128 from a [16]byte

func FromBytesBigEndian

func FromBytesBigEndian(b [16]byte) Uint128

FromBytesBigEndian creates a new Uint128 from a [16]byte in big endian

func FromString

func FromString[S string | []byte](s S) (Uint128, errors.Error)

FromString creates a new Uint128 from a string

func FromUint64

func FromUint64(u uint64) Uint128

FromUint64 creates a new Uint128 from a uint64

func QuoRem192By64

func QuoRem192By64(u Uint128, carry uint64, v uint64) (Uint128, uint64, errors.Error)

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

func SubUnsafe(u Uint128, v Uint128) Uint128

SubUnsafe returns u - v with u >= v must be called only when u >= v or the result will be incorrect

func (Uint128) Add

func (self Uint128) Add(other Uint128) (Uint128, errors.Error)

Add returns self + other and an error if the result overflows.

func (Uint128) Add64

func (self Uint128) Add64(other uint64) (Uint128, errors.Error)

Add64 returns self + other and an error if the result overflows.

func (Uint128) And

func (self Uint128) And(other Uint128) Uint128

And returns the bitwise AND of two 128-bit unsigned integers.

func (Uint128) And64

func (self Uint128) And64(other uint64) Uint128

And64 returns the bitwise AND of a 128-bit unsigned integer and a 64-bit unsigned integer.

func (Uint128) AppendBytes

func (self Uint128) AppendBytes(bs []byte) []byte

AppendBytes appends the Uint128 to the byte slice bs in little-endian order.

func (Uint128) AppendBytesBigEndian

func (self Uint128) AppendBytesBigEndian(bs []byte) []byte

AppendBytesBigEndian appends the Uint128 to the byte slice bs in big-endian order.

func (Uint128) BigInt

func (self Uint128) BigInt() *big.Int

BigInt returns the value as a big.Int.

func (Uint128) BitLen

func (self Uint128) BitLen() int

BitLen returns the number of bits required to represent the value.

func (Uint128) Bytes

func (self Uint128) Bytes() [16]byte

Bytes returns the value as a [16]byte array.

func (Uint128) BytesBigEndian

func (self Uint128) BytesBigEndian() [16]byte

BytesBigEndian returns the value as a [16]byte array in big-endian order.

func (Uint128) Compare

func (self Uint128) Compare(other Uint128) int

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) Div

func (self Uint128) Div(other Uint128) (Uint128, errors.Error)

Div returns self / other and an error if the divisor is zero.

func (Uint128) Div64

func (self Uint128) Div64(other uint64) (Uint128, errors.Error)

Div64 returns self / other and an error if the divisor is zero.

func (Uint128) Equal

func (self Uint128) Equal(other Uint128) bool

Equal returns true if the value is equal to the other value.

func (Uint128) IsZero

func (self Uint128) IsZero() bool

IsZero returns true if the value is zero.

func (Uint128) LeadingZeroBitsCount

func (self Uint128) LeadingZeroBitsCount() int

LeadingZeroBitsCount returns the number of leading zero bits in a 128-bit unsigned integer.

func (Uint128) Lsh

func (self Uint128) Lsh(n uint) Uint128

Lsh returns the result of shifting a 128-bit unsigned integer to the left by n bits.

func (Uint128) MarshalText

func (self Uint128) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Uint128) Mod

func (self Uint128) Mod(other Uint128) (Uint128, errors.Error)

Mod returns self % other and an error if the divisor is zero.

func (Uint128) Mod64

func (self Uint128) Mod64(other uint64) (uint64, errors.Error)

Mod64 returns self % other and an error if the divisor is zero.

func (Uint128) Mul

func (self Uint128) Mul(other Uint128) (Uint128, errors.Error)

Mul returns self * other and an error if the result overflows.

func (Uint128) Mul64

func (self Uint128) Mul64(other uint64) (Uint128, errors.Error)

Mul64 returns self * other and an error if the result overflows.

func (Uint128) MulCarry

func (self Uint128) MulCarry(other Uint128) (Uint128, Uint128)

MulCarry returns self * other and carry.

func (Uint128) NonZeroBitsCount

func (self Uint128) NonZeroBitsCount() int

NonZeroBitsCount returns the number of non-zero bits in a 128-bit unsigned integer.

func (Uint128) Or

func (self Uint128) Or(other Uint128) Uint128

Or returns the bitwise OR of two 128-bit unsigned integers.

func (Uint128) Or64

func (self Uint128) Or64(other uint64) Uint128

Or64 returns the bitwise OR of a 128-bit unsigned integer and a 64-bit unsigned integer.

func (Uint128) PutBytes

func (self Uint128) PutBytes(bs []byte) errors.Error

PutBytes writes the Uint128 to the byte slice bs in little-endian order.

func (Uint128) PutBytesBigEndian

func (self Uint128) PutBytesBigEndian(bs []byte) errors.Error

PutBytesBigEndian writes the Uint128 to the byte slice bs in big-endian order.

func (Uint128) QuoRem

func (self Uint128) QuoRem(other Uint128) (Uint128, Uint128, errors.Error)

QuoRem returns self / other and self % other and an error if the divisor is zero.

func (Uint128) QuoRem64

func (self Uint128) QuoRem64(other uint64) (Uint128, uint64, errors.Error)

QuoRem64 returns self / other and self % other and an error if the divisor is zero.

func (Uint128) ReverseBits

func (self Uint128) ReverseBits() Uint128

ReverseBits returns the result of reversing the bits of a 128-bit unsigned integer.

func (Uint128) ReverseBytes

func (self Uint128) ReverseBytes() Uint128

ReverseBytes returns the Uint128 with the byte order reversed.

func (Uint128) RotateBitsLeft

func (self Uint128) RotateBitsLeft(k int) Uint128

RotateBitsLeft returns the result of rotating a 128-bit unsigned integer to the left by k bits.

func (Uint128) RotateBitsRight

func (self Uint128) RotateBitsRight(k int) Uint128

RotateBitsRight returns the result of rotating a 128-bit unsigned integer to the right by k bits.

func (Uint128) Rsh

func (self Uint128) Rsh(n uint) Uint128

Rsh returns the result of shifting a 128-bit unsigned integer to the right by n bits.

func (*Uint128) Scan

func (self *Uint128) Scan(s fmt.ScanState, ch rune) error

Scan scans the value.

func (Uint128) String

func (self Uint128) String() string

String returns the value as a string.

func (Uint128) StringToBuf

func (self Uint128) StringToBuf(buf []byte) []byte

StringToBuf writes the value as a string to the given buffer (from end to start) and returns a slice containing the string.

func (Uint128) Sub

func (self Uint128) Sub(other Uint128) (Uint128, errors.Error)

Sub returns self - other and an error if the result underflows.

func (Uint128) Sub64

func (self Uint128) Sub64(other uint64) (Uint128, errors.Error)

Sub64 returns self - other and an error if the result underflows.

func (Uint128) TrailingZeroBitsCount

func (self Uint128) TrailingZeroBitsCount() int

TrailingZeroBitsCount returns the number of trailing zero bits in a 128-bit unsigned integer.

func (Uint128) Uint64

func (self Uint128) Uint64() (uint64, errors.Error)

Uint64 returns the value as uint64 if it fits, otherwise it returns an error.

func (*Uint128) UnmarshalText

func (self *Uint128) UnmarshalText(b []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Uint128) Xor

func (self Uint128) Xor(other Uint128) Uint128

Xor returns the bitwise XOR of two 128-bit unsigned integers.

func (Uint128) Xor64

func (self Uint128) Xor64(other uint64) Uint128

Xor64 returns the bitwise XOR of a 128-bit unsigned integer and a 64-bit unsigned integer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL