uint128

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2022 License: MIT Imports: 7 Imported by: 4

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func StoreBigEndian

func StoreBigEndian(b []byte, u Uint128)

StoreBigEndian stores 128-bit value in byte slice in big-endian byte order. It panics if byte slice length is less than 16.

func StoreLittleEndian

func StoreLittleEndian(b []byte, u Uint128)

StoreLittleEndian stores 128-bit value in byte slice in little-endian byte order. It panics if byte slice length is less than 16.

Types

type Uint128

type Uint128 struct {
	Lo uint64 // lower 64-bit half
	Hi uint64 // upper 64-bit half
}

Uint128 is an unsigned 128-bit number. All methods are immutable, works just like standard uint64.

Example (Json)

ExampleUint128_json is an example for JSON marshaling.

package main

import (
	"encoding/json"
	"fmt"

	"github.com/Pilatuz/bigz/uint128"
)

func main() {
	foo := map[string]interface{}{
		"bar": uint128.From64(12345),
	}

	buf, _ := json.Marshal(foo)
	fmt.Printf("%s", buf)
}
Output:

{"bar":"12345"}
Example (Load)

ExampleUint128_load is an example for LoadBigEndian and LoadLittleEndian.

package main

import (
	"fmt"
	"net"

	"github.com/Pilatuz/bigz/uint128"
)

func main() {
	ip := net.ParseIP("cafe::dead:beaf")
	fmt.Printf("%032x\n", uint128.LoadBigEndian(ip.To16()))
	fmt.Printf("%032x\n", uint128.LoadLittleEndian(ip.To16()))
}
Output:

cafe00000000000000000000deadbeaf
afbeadde00000000000000000000feca

func Add

func Add(x, y Uint128, carry uint64) (sum Uint128, carryOut uint64)

Add returns the sum with carry of x, y and carry: sum = x + y + carry. The carry input must be 0 or 1; otherwise the behavior is undefined. The carryOut output is guaranteed to be 0 or 1.

func Div

func Div(hi, lo, y Uint128) (quo, rem Uint128)

Div returns the quotient and remainder of (hi, lo) divided by y: quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper half in parameter hi and the lower half in parameter lo. Panics if y is less or equal to hi!

func From64

func From64(v uint64) Uint128

From64 converts 64-bit value v to a Uint128 value. Upper 64-bit half will be zero.

func FromBig

func FromBig(i *big.Int) Uint128

FromBig converts *big.Int to 128-bit Uint128 value ignoring overflows. If input integer is nil or negative then return Zero. If input interger overflows 128-bit then return Max.

Example

ExampleFromBig is an example for FromBig.

package main

import (
	"fmt"
	"math/big"

	"github.com/Pilatuz/bigz/uint128"
)

func main() {
	fmt.Println(uint128.FromBig(nil))
	fmt.Println(uint128.FromBig(new(big.Int).SetInt64(12345)))
}
Output:

0
12345

func FromBigEx

func FromBigEx(i *big.Int) (Uint128, bool)

FromBigEx converts *big.Int to 128-bit Uint128 value (eXtended version). Provides ok successful flag as a second return value. If input integer is negative or overflows 128-bit then ok=false. If input is nil then zero 128-bit returned.

Example

ExampleFromBigEx is an example for FromBigEx.

package main

import (
	"fmt"
	"math/big"

	"github.com/Pilatuz/bigz/uint128"
)

func main() {
	one := new(big.Int).SetInt64(1)
	fmt.Println(uint128.FromBigEx(new(big.Int).SetInt64(-1))) // => Zero()
	fmt.Println(uint128.FromBigEx(one))
	fmt.Println(uint128.FromBigEx(one.Lsh(one, 128))) // 2^128, overflows => Max()
}
Output:

0 false
1 true
340282366920938463463374607431768211455 false

func FromString added in v1.2.1

func FromString(s string) (Uint128, error)

FromString parses input string as a Uint128 value.

Example

ExampleFromString is an example for FromString.

package main

import (
	"fmt"

	"github.com/Pilatuz/bigz/uint128"
)

func main() {
	u, _ := uint128.FromString("1")
	fmt.Println(u)
	_, err := uint128.FromString("-1")
	fmt.Println(err)
}
Output:

1
out of 128-bit range

func LoadBigEndian

func LoadBigEndian(b []byte) Uint128

LoadBigEndian loads 128-bit value from byte slice in big-endian byte order. It panics if byte slice length is less than 16.

func LoadLittleEndian

func LoadLittleEndian(b []byte) Uint128

LoadLittleEndian loads 128-bit value from byte slice in little-endian byte order. It panics if byte slice length is less than 16.

func Max

func Max() Uint128

Max is the largest possible Uint128 value.

func Mul

func Mul(x, y Uint128) (hi, lo Uint128)

Mul returns the 256-bit product of x and y: (hi, lo) = x * y with the product bits' upper half returned in hi and the lower half returned in lo.

func One

func One() Uint128

One is the lowest non-zero Uint128 value.

func Sub

func Sub(x, y Uint128, borrow uint64) (diff Uint128, borrowOut uint64)

Sub returns the difference of x, y and borrow: diff = x - y - borrow. The borrow input must be 0 or 1; otherwise the behavior is undefined. The borrowOut output is guaranteed to be 0 or 1.

func Zero

func Zero() Uint128

Zero is the lowest possible Uint128 value.

func (Uint128) Add

func (u Uint128) Add(v Uint128) Uint128

Add returns sum (u+v) of two 128-bit values. Wrap-around semantic is used here: Max().Add(From64(1)) == Zero()

func (Uint128) Add64

func (u Uint128) Add64(v uint64) Uint128

Add64 returns sum u+v of 128-bit and 64-bit values. Wrap-around semantic is used here: Max().Add64(1) == Zero()

func (Uint128) And

func (u Uint128) And(v Uint128) Uint128

And returns logical AND (u&v) of two 128-bit values.

func (Uint128) And64

func (u Uint128) And64(v uint64) Uint128

And64 returns logical AND (u&v) of 128-bit and 64-bit values.

func (Uint128) AndNot

func (u Uint128) AndNot(v Uint128) Uint128

AndNot returns logical AND NOT (u&^v) of two 128-bit values.

func (Uint128) AndNot64

func (u Uint128) AndNot64(v uint64) Uint128

AndNot64 returns logical AND NOT (u&v) of 128-bit and 64-bit values.

func (Uint128) Big

func (u Uint128) Big() *big.Int

Big returns 128-bit value as a *big.Int.

func (Uint128) BitLen

func (u Uint128) BitLen() int

BitLen returns the minimum number of bits required to represent 128-bit value. The result is 0 for u == 0.

func (Uint128) Cmp

func (u Uint128) Cmp(v Uint128) int

Cmp compares two 128-bit values and returns:

-1 if u <  v
 0 if u == v
+1 if u >  v

func (Uint128) Cmp64

func (u Uint128) Cmp64(v uint64) int

Cmp64 compares 128-bit and 64-bit values and returns:

-1 if u <  v
 0 if u == v
+1 if u >  v

func (Uint128) Div

func (u Uint128) Div(v Uint128) Uint128

Div returns division (u/v) of two 128-bit values.

func (Uint128) Div64

func (u Uint128) Div64(v uint64) Uint128

Div64 returns division (u/v) of 128-bit and 64-bit values.

func (Uint128) Equals

func (u Uint128) Equals(v Uint128) bool

Equals returns true if two 128-bit values are equal. Uint128 values can be compared directly with == operator but use of the Equals method is preferred for consistency.

func (Uint128) Equals64

func (u Uint128) Equals64(v uint64) bool

Equals64 returns true if 128-bit value equals to a 64-bit value.

func (Uint128) Format

func (u Uint128) Format(s fmt.State, ch rune)

Format does custom formatting of 128-bit value.

Example

ExampleUint128_Format is an example for Uint128.Format.

package main

import (
	"fmt"

	"github.com/Pilatuz/bigz/uint128"
)

func main() {
	fmt.Printf("%08b\n", uint128.From64(42))
	fmt.Printf("%#O\n", uint128.From64(42))
	fmt.Printf("%#x\n", uint128.Max())
}
Output:

00101010
0o52
0xffffffffffffffffffffffffffffffff

func (Uint128) IsZero

func (u Uint128) IsZero() bool

IsZero returns true if stored 128-bit value is zero.

func (Uint128) LeadingZeros

func (u Uint128) LeadingZeros() int

LeadingZeros returns the number of leading zero bits. The result is 128 for u == 0.

func (Uint128) Lsh

func (u Uint128) Lsh(n uint) Uint128

Lsh returns left shift (u<<n).

func (Uint128) MarshalText

func (u Uint128) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface.

func (Uint128) Mod

func (u Uint128) Mod(v Uint128) Uint128

Mod returns modulo (u%v) of two 128-bit values.

func (Uint128) Mod64

func (u Uint128) Mod64(v uint64) uint64

Mod64 returns modulo (u%v) of 128-bit and 64-bit values.

func (Uint128) Mul

func (u Uint128) Mul(v Uint128) Uint128

Mul returns multiplication (u*v) of two 128-bit values. Wrap-around semantic is used here: Max().Mul(Max()) == From64(1).

func (Uint128) Mul64

func (u Uint128) Mul64(v uint64) Uint128

Mul64 returns multiplication (u*v) of 128-bit and 64-bit values. Wrap-around semantic is used here: Max().Mul64(2) == Max().Sub64(1).

func (Uint128) Not

func (u Uint128) Not() Uint128

Not returns logical NOT (^u) of 128-bit value.

func (Uint128) OnesCount

func (u Uint128) OnesCount() int

OnesCount returns the number of one bits ("population count").

func (Uint128) Or

func (u Uint128) Or(v Uint128) Uint128

Or returns logical OR (u|v) of two 128-bit values.

func (Uint128) Or64

func (u Uint128) Or64(v uint64) Uint128

Or64 returns logical OR (u|v) of 128-bit and 64-bit values.

func (Uint128) QuoRem

func (u Uint128) QuoRem(v Uint128) (Uint128, Uint128)

QuoRem returns quotient (u/v) and remainder (u%v) of two 128-bit values.

func (Uint128) QuoRem64

func (u Uint128) QuoRem64(v uint64) (Uint128, uint64)

QuoRem64 returns quotient (u/v) and remainder (u%v) of 128-bit and 64-bit values.

func (Uint128) Reverse

func (u Uint128) Reverse() Uint128

Reverse returns the value with bits in reversed order.

func (Uint128) ReverseBytes

func (u Uint128) ReverseBytes() Uint128

ReverseBytes returns the value with bytes in reversed order.

func (Uint128) RotateLeft

func (u Uint128) RotateLeft(k int) Uint128

RotateLeft returns the value of u rotated left by (k mod 128) bits.

func (Uint128) RotateRight

func (u Uint128) RotateRight(k int) Uint128

RotateRight returns the value of u rotated left by (k mod 128) bits.

func (Uint128) Rsh

func (u Uint128) Rsh(n uint) Uint128

Rsh returns right shift (u>>n).

func (*Uint128) Scan added in v1.2.1

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

Scan implements fmt.Scanner.

func (Uint128) String

func (u Uint128) String() string

String returns the base-10 representation of 128-bit value.

Example

ExampleUint128_String is an example for Uint128.String.

package main

import (
	"fmt"

	"github.com/Pilatuz/bigz/uint128"
)

func main() {
	fmt.Println(uint128.Zero())
	fmt.Println(uint128.One())
	fmt.Println(uint128.Max())
}
Output:

0
1
340282366920938463463374607431768211455

func (Uint128) Sub

func (u Uint128) Sub(v Uint128) Uint128

Sub returns difference (u-v) of two 128-bit values. Wrap-around semantic is used here: Zero().Sub(From64(1)) == Max().

func (Uint128) Sub64

func (u Uint128) Sub64(v uint64) Uint128

Sub64 returns difference (u-v) of 128-bit and 64-bit values. Wrap-around semantic is used here: Zero().Sub64(1) == Max().

func (Uint128) TrailingZeros

func (u Uint128) TrailingZeros() int

TrailingZeros returns the number of trailing zero bits. The result is 128 for u == 0.

func (*Uint128) UnmarshalText

func (u *Uint128) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Uint128) Xor

func (u Uint128) Xor(v Uint128) Uint128

Xor returns logical XOR (u^v) of two 128-bit values.

func (Uint128) Xor64

func (u Uint128) Xor64(v uint64) Uint128

Xor64 returns logical XOR (u^v) of 128-bit and 64-bit values.

Jump to

Keyboard shortcuts

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