wide

package module
v0.0.0-...-f80959d Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2025 License: MIT Imports: 4 Imported by: 7

README

Go Report Card

Wide

Uint128 and Int128 for Go.

Wide is free and open source software distributed under the terms of both the MIT License and the Unlicense.

Installing

go get github.com/ryanavella/wide

Usage

package main

import (
	"fmt"

	"github.com/ryanavella/wide"
)

func main() {
	a := wide.Int128FromInt64(-3)
	b := wide.Int128FromInt64(2)
	fmt.Println(a, b, a.Add(b), a.Sub(b), a.Mul(b), a.Div(b), a.Mod(b))
}

Scope

This package is intended for efficient and fast computations (i.e. for scientific and mathematical applications). There are no plans to support applications which require constant-time cryptographic security.

Contributions

See contributor guidelines.

Documentation

Overview

Package wide provides implementations of Int128 and Uint128 for Go. See README.md for more information.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Int128

type Int128 struct {
	Hi int64
	Lo uint64
}

Int128 is a representation of a signed 128-bit integer

func Int128FromBigInt

func Int128FromBigInt(a *big.Int) (z Int128)

Int128FromBigInt returns an Int128 from a big.Int

func Int128FromInt64

func Int128FromInt64(x int64) Int128

Int128FromInt64 returns an Int128 from an int64

func NewInt128

func NewInt128(hi int64, lo uint64) Int128

NewInt128 returns an Int128 from the high and low 64 bits

func (Int128) Abs

func (x Int128) Abs() Int128

Abs returns the absolute value of an Int128's

func (Int128) Add

func (x Int128) Add(y Int128) (z Int128)

Add returns the sum of two Int128's

func (Int128) And

func (x Int128) And(y Int128) (z Int128)

And returns the bitwise AND of two Int128's

func (Int128) AndNot

func (x Int128) AndNot(y Int128) (z Int128)

AndNot returns the bitwise AndNot of two Int128's

func (Int128) Cmp

func (x Int128) Cmp(y Int128) int

Cmp compares x and y and returns:

-1 if x <  y
 0 if x == y
+1 if x >  y

func (Int128) CmpAbs

func (x Int128) CmpAbs(y Int128) int

CmpAbs compares |x| and |y| and returns:

-1 if |x| <  |y|
 0 if |x| == |y|
+1 if |x| >  |y|

func (Int128) Dec

func (x Int128) Dec() (z Int128)

Dec returns the predecessor of an Int128

func (Int128) Div

func (x Int128) Div(d Int128) (q Int128)

Div returns the quotient corresponding to the provided dividend and divisor

Div panics on division by 0. It checks some common/faster cases before fully committing to long division. This can probably be further optimized by implementing a successive approximation algorithm, with an initial seed value determined by a 64-bit division of the most significant bits.

func (Int128) DivMod

func (x Int128) DivMod(d Int128) (q, r Int128)

DivMod returns the quotient and remainder corresponding to the provided dividend and divisor

DivMod panics on division by 0. It checks some common/faster cases before fully committing to long division. This can probably be further optimized by implementing a successive approximation algorithm, with an initial seed value determined by a 64-bit division of the most significant bits.

func (Int128) Eq

func (x Int128) Eq(y Int128) bool

Eq returns whether x is equal to y

func (Int128) Gt

func (x Int128) Gt(y Int128) bool

Gt returns whether x is greater than y

func (Int128) Gte

func (x Int128) Gte(y Int128) bool

Gte returns whether x is greater than or equal to y

func (Int128) Inc

func (x Int128) Inc() (z Int128)

Inc returns the successor of an Int128

func (Int128) Int64

func (x Int128) Int64() int64

Int64 returns a representation of the Int128 as the builtin int64

This function overflows silently

func (Int128) IsInt64

func (x Int128) IsInt64() bool

IsInt64 checks if the Int128 can be represented as an int64

func (Int128) IsNeg

func (x Int128) IsNeg() bool

IsNeg returns whether or not the Int128 is negative

func (Int128) IsPos

func (x Int128) IsPos() bool

IsPos returns whether or not the Int128 is positive

func (Int128) IsUint64

func (x Int128) IsUint64() bool

IsUint64 checks if the Int128 can be represented as a uint64 without wrapping

func (Int128) LShift

func (x Int128) LShift() (z Int128)

LShift returns an Int128 left-shifted by 1

func (Int128) LShiftN

func (x Int128) LShiftN(n uint) (z Int128)

LShiftN returns an Int128 left-shifted by a uint (i.e. x << n)

func (Int128) Lt

func (x Int128) Lt(y Int128) bool

Lt returns whether x is less than y

func (Int128) Lte

func (x Int128) Lte(y Int128) bool

Lte returns whether x is less than or equal to y

func (Int128) Mod

func (x Int128) Mod(d Int128) (r Int128)

Mod returns the remainder corresponding to the provided dividend and divisor

Mod panics on division by 0. It checks some common/faster cases before fully committing to long division. This can probably be further optimized by implementing a successive approximation algorithm, with an initial seed value determined by a 64-bit division of the most significant bits.

func (Int128) Mul

func (x Int128) Mul(y Int128) (z Int128)

Mul returns the product of two Int128's

func (Int128) Nand

func (x Int128) Nand(y Int128) (z Int128)

Nand returns the bitwise NAND of two Int128's

func (Int128) Neg

func (x Int128) Neg() (z Int128)

Neg returns the additive inverse of an Int128

func (Int128) Nor

func (x Int128) Nor(y Int128) (z Int128)

Nor returns the bitwise NOR of two Int128's

func (Int128) Not

func (x Int128) Not() (z Int128)

Not returns the bitwise Not of an Int128

func (Int128) Or

func (x Int128) Or(y Int128) (z Int128)

Or returns the bitwise OR of two Int128's

func (Int128) RShift

func (x Int128) RShift() (z Int128)

RShift returns an Int128 right-shifted by 1

func (Int128) RShift128

func (x Int128) RShift128(y Uint128) (z Int128)

RShift128 returns an Int128 right-shifted by a Uint128 (i.e. x >> y)

func (Int128) RShiftN

func (x Int128) RShiftN(n uint) (z Int128)

RShiftN returns an Int128 right-shifted by a uint (i.e. x >> n)

Could probably be made faster with sign extension

func (Int128) Sign

func (x Int128) Sign() int

Sign returns the sign of an Int128

func (Int128) String

func (x Int128) String() string

String returns a hexadecimal (string) representation of an Int128

func (Int128) Sub

func (x Int128) Sub(y Int128) (z Int128)

Sub returns the difference of two Int128's

func (Int128) Uint128

func (x Int128) Uint128() (z Uint128)

Uint128 returns a Uint128 representation of an Int128

This function overflows silently

func (Int128) Uint64

func (x Int128) Uint64() uint64

Uint64 returns a representation of the Int128 as the builtin uint64

This function overflows silently

func (Int128) Xnor

func (x Int128) Xnor(y Int128) (z Int128)

Xnor returns the bitwise XNOR of two Int128's

func (Int128) Xor

func (x Int128) Xor(y Int128) (z Int128)

Xor returns the bitwise XOR of two Int128's

type Uint128

type Uint128 struct {
	Hi, Lo uint64
}

Uint128 is a representation of an unsigned 128-bit integer

func NewUint128

func NewUint128(hi, lo uint64) Uint128

NewUint128 returns a Uint128 from the high and low 64 bits

func RandUint128

func RandUint128() (z Uint128)

RandUint128 returns a pseudo-random Uint128

func Uint128FromBigInt

func Uint128FromBigInt(a *big.Int) (z Uint128)

Uint128FromBigInt returns a Uint128 from a big.Int

func Uint128FromUint64

func Uint128FromUint64(x uint64) Uint128

Uint128FromUint64 returns a Uint128 from a uint64

func (Uint128) Add

func (x Uint128) Add(y Uint128) (z Uint128)

Add returns the sum of two Uint128's

func (Uint128) And

func (x Uint128) And(y Uint128) (z Uint128)

And returns the bitwise AND of two Uint128's

func (Uint128) AndNot

func (x Uint128) AndNot(y Uint128) (z Uint128)

AndNot returns the bitwise AndNot of two Uint128's

func (Uint128) Cmp

func (x Uint128) Cmp(y Uint128) int

Cmp compares x and y and returns:

-1 if x <  y
 0 if x == y
+1 if x >  y

func (Uint128) Dec

func (x Uint128) Dec() (z Uint128)

Dec returns the predecessor of a Uint128

func (Uint128) Div

func (x Uint128) Div(d Uint128) (q Uint128)

Div returns the quotient corresponding to the provided dividend and divisor

Div panics on division by 0. It checks some common/faster cases before fully committing to long division. This can probably be further optimized by implementing a successive approximation algorithm, with an initial seed value determined by a 64-bit division of the most significant bits.

func (Uint128) DivMod

func (x Uint128) DivMod(d Uint128) (q, r Uint128)

DivMod returns the quotient and remainder corresponding to the provided dividend and divisor

DivMod panics on division by 0. It checks some common/faster cases before fully committing to long division. This can probably be further optimized by implementing a successive approximation algorithm, with an initial seed value determined by a 64-bit division of the most significant bits.

func (Uint128) Eq

func (x Uint128) Eq(y Uint128) bool

Eq returns whether x is equal to y

func (Uint128) Gt

func (x Uint128) Gt(y Uint128) bool

Gt returns whether x is greater than y

func (Uint128) Gte

func (x Uint128) Gte(y Uint128) bool

Gte returns whether x is greater than or equal to y

func (Uint128) Inc

func (x Uint128) Inc() (z Uint128)

Inc returns the successor of a Uint128

func (Uint128) Int128

func (x Uint128) Int128() (z Int128)

Int128 returns a Int128 representation of a Uint128

This function overflows silently

func (Uint128) Int64

func (x Uint128) Int64() int64

Int64 returns a representation of the Uint128 as the builtin int64

This function overflows silently

func (Uint128) IsInt64

func (x Uint128) IsInt64() bool

IsInt64 checks if the Uint128 can be represented as an int64 without overflowing

func (Uint128) IsUint64

func (x Uint128) IsUint64() bool

IsUint64 checks if the Uint128 can be represented as a uint64 without overflowing

func (Uint128) LShift

func (x Uint128) LShift() (z Uint128)

LShift returns a Uint128 left-shifted by 1

func (Uint128) LShiftN

func (x Uint128) LShiftN(n uint) (z Uint128)

LShiftN returns a Uint128 left-shifted by a uint (i.e. x << n)

func (Uint128) Len

func (x Uint128) Len() uint

Len returns the minimum number of bits required to represent x

Edge cases:

Uint128{0, 0}.Len() -> 0

func (Uint128) Lt

func (x Uint128) Lt(y Uint128) bool

Lt returns whether x is less than y

func (Uint128) Lte

func (x Uint128) Lte(y Uint128) bool

Lte returns whether x is less than or equal to y

func (Uint128) Mod

func (x Uint128) Mod(d Uint128) (r Uint128)

Mod returns the remainder corresponding to the provided dividend and divisor

Mod panics on division by 0. It checks some common/faster cases before fully committing to long division. This can probably be further optimized by implementing a successive approximation algorithm, with an initial seed value determined by a 64-bit division of the most significant bits.

func (Uint128) Mul

func (x Uint128) Mul(y Uint128) (z Uint128)

Mul returns the product of two Uint128's

func (Uint128) Nand

func (x Uint128) Nand(y Uint128) (z Uint128)

Nand returns the bitwise NAND of two Uint128's

func (Uint128) Neg

func (x Uint128) Neg() (z Uint128)

Neg returns the additive inverse of a Uint128

func (Uint128) Nor

func (x Uint128) Nor(y Uint128) (z Uint128)

Nor returns the bitwise NOR of two Uint128's

func (Uint128) Not

func (x Uint128) Not() (z Uint128)

Not returns the bitwise Not of a Uint128

func (Uint128) Or

func (x Uint128) Or(y Uint128) (z Uint128)

Or returns the bitwise OR of two Uint128's

func (Uint128) RShift

func (x Uint128) RShift() (z Uint128)

RShift returns a Uint128 right-shifted by 1

func (Uint128) RShift128

func (x Uint128) RShift128(y Uint128) (z Uint128)

RShift128 returns a Uint128 right-shifted by a Uint128 (i.e. x >> y)

func (Uint128) RShiftN

func (x Uint128) RShiftN(n uint) (z Uint128)

RShiftN returns a Uint128 right-shifted by a uint (i.e. x >> n)

func (Uint128) String

func (x Uint128) String() string

String returns a hexadecimal representation of a Uint128

func (Uint128) Sub

func (x Uint128) Sub(y Uint128) (z Uint128)

Sub returns the difference of two Uint128's

func (Uint128) Uint64

func (x Uint128) Uint64() uint64

Uint64 returns a representation of the Uint128 as the builtin uint64

This function overflows silently

func (Uint128) Xnor

func (x Uint128) Xnor(y Uint128) (z Uint128)

Xnor returns the bitwise XNOR of two Uint128's

func (Uint128) Xor

func (x Uint128) Xor(y Uint128) (z Uint128)

Xor returns the bitwise XOR of two Uint128's

Directories

Path Synopsis
internal
bits
Package bits provides an alternative implementation of bits.Len64 from math/bits.
Package bits provides an alternative implementation of bits.Len64 from math/bits.

Jump to

Keyboard shortcuts

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