int128

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2023 License: MIT Imports: 4 Imported by: 4

README

int128

test Go Reference

The package int128 provides 128-bit integer types.

SYNOPSIS

package int128_test

import (
	"fmt"

	"github.com/shogo82148/int128"
)

func ExampleInt128_Add() {
	a := int128.Int128{0, 1} // = 1
	b := int128.Int128{0, 2} // = 2
	c := a.Add(b)
	fmt.Println(c)
	// Output: 3
}

Documentation

Overview

The package int128 provides 128-bit integer types.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Int128

type Int128 struct {
	H int64
	L uint64
}

Int128 is a signed 128-bit integer.

func Float64ToInt128

func Float64ToInt128(v float64) Int128

Float64ToUint128 returns the nearest Uint128 representation of v.

func (Int128) Add

func (a Int128) Add(b Int128) Int128

Add returns the sum a+b.

This function's execution time does not depend on the inputs.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/int128"
)

func main() {
	a := int128.Int128{0, 1} // = 1
	b := int128.Int128{0, 2} // = 2
	c := a.Add(b)
	fmt.Println(c)
}
Output:
3

func (Int128) And

func (a Int128) And(b Int128) Int128

And returns the bitwise AND a&b.

This function's execution time does not depend on the inputs.

func (Int128) AndNot

func (a Int128) AndNot(b Int128) Int128

AndNot returns the bitwise AND NOT a&^b.

This function's execution time does not depend on the inputs.

func (Int128) Append

func (a Int128) Append(dst []byte, base int) []byte

Append appends the string representation of a, as generated by a.Text(base), to buf and returns the extended buffer.

func (Int128) Cmp

func (a Int128) Cmp(b Int128) int

Cmp compares a and b and returns:

-1 if a <  b
 0 if a == b
+1 if a >  b

func (Int128) Div

func (a Int128) Div(b Int128) Int128

Div returns the quotient a/b for b != 0. If b == 0, a division-by-zero run-time panic occurs.

func (Int128) DivMod

func (a Int128) DivMod(b Int128) (Int128, Int128)

DivMod returns the quotient and remainder of a/b for b != 0. If b == 0, a division-by-zero run-time panic occurs.

DivMod implements Euclidean division and modulus (unlike Go):

q = a div b  such that
m = a - b*q  with 0 <= m < |y|

func (Int128) Format added in v0.2.0

func (a Int128) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

func (Int128) Lsh

func (a Int128) Lsh(i uint) Int128

Lsh returns the logical left shift a<<i.

This function's execution time does not depend on the inputs.

func (Int128) MarshalJSON

func (a Int128) MarshalJSON() ([]byte, error)

func (Int128) MarshalText

func (a Int128) MarshalText() ([]byte, error)

func (Int128) Mod

func (a Int128) Mod(b Int128) Int128

Mod returns the modulus x%y for y != 0. If y == 0, a division-by-zero run-time panic occurs.

func (Int128) Mul

func (a Int128) Mul(b Int128) Int128

Mul returns the product x*y.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/int128"
)

func main() {
	a := int128.Int128{0, 1} // = 1
	b := int128.Int128{0, 2} // = 2
	c := a.Mul(b)
	fmt.Println(c)
}
Output:
2

func (Int128) Neg

func (a Int128) Neg() Int128

Neg returns the negation -a.

This function's execution time does not depend on the inputs.

func (Int128) Not

func (a Int128) Not() Int128

Not returns the bitwise NOT ^a.

This function's execution time does not depend on the inputs.

func (Int128) Or

func (a Int128) Or(b Int128) Int128

Or returns the bitwise OR a|b.

This function's execution time does not depend on the inputs.

func (Int128) Quo

func (a Int128) Quo(b Int128) Int128

Quo returns the quotient a/b for b != 0. If b == 0, a division-by-zero run-time panic occurs. Quo implements truncated division (like Go); see QuoRem for more details.

func (Int128) QuoRem

func (a Int128) QuoRem(b Int128) (Int128, Int128)

QuoRem returns the quotient a/b and the remainder a%b for b != 0. a division-by-zero run-time panic occurs.

QuoRem implements T-division and modulus (like Go):

q = a/b      with the result truncated to zero
r = a - b*q

func (Int128) Rem

func (a Int128) Rem(b Int128) Int128

Rem returns he remainder a%b for b != 0. If b == 0, a division-by-zero run-time panic occurs. Rem implements truncated modulus (like Go); see QuoRem for more details.

func (Int128) Rsh

func (a Int128) Rsh(i uint) Int128

Rsh returns the logical right shift a>>i.

This function's execution time does not depend on the inputs.

func (Int128) String

func (a Int128) String() string

String returns the decimal representation of a as generated by a.Text(10).

func (Int128) Sub

func (a Int128) Sub(b Int128) Int128

Sub returns the difference x-y.

This function's execution time does not depend on the inputs.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/int128"
)

func main() {
	a := int128.Int128{0, 1} // = 1
	b := int128.Int128{0, 2} // = 2
	c := a.Sub(b)
	fmt.Println(c)
}
Output:
-1

func (Int128) Text

func (a Int128) Text(base int) string

Text returns the string representation of a in the given base. Base must be between 2 and 62, inclusive. The result uses the lower-case letters 'a' to 'z' for digit values 10 to 35, and the upper-case letters 'A' to 'Z' for digit values 36 to 61. No prefix (such as "0x") is added to the string.

func (Int128) Uint128

func (a Int128) Uint128() Uint128

Uint128 returns a as a unsigned 128-bit integer.

func (Int128) Xor

func (a Int128) Xor(b Int128) Int128

Xor returns the bitwise XOR a^b.

This function's execution time does not depend on the inputs.

type Uint128

type Uint128 struct {
	H uint64
	L uint64
}

Uint128 is a 128-bit unsigned integer.

func Float64ToUint128

func Float64ToUint128(v float64) Uint128

Float64ToUint128 returns the nearest Uint128 representation of v.

func (Uint128) Add

func (a Uint128) Add(b Uint128) Uint128

Add returns the sum a+b.

This function's execution time does not depend on the inputs.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/int128"
)

func main() {
	a := int128.Uint128{0, 1} // = 1
	b := int128.Uint128{0, 2} // = 2
	c := a.Add(b)
	fmt.Println(c)
}
Output:
3

func (Uint128) And

func (a Uint128) And(b Uint128) Uint128

And returns the bitwise AND a&b.

This function's execution time does not depend on the inputs.

func (Uint128) AndNot

func (a Uint128) AndNot(b Uint128) Uint128

AndNot returns the bitwise AND NOT a&^b.

This function's execution time does not depend on the inputs.

func (Uint128) Append

func (a Uint128) Append(dst []byte, base int) []byte

Append appends the string representation of a, as generated by a.Text(base), to buf and returns the extended buffer.

func (Uint128) Cmp

func (a Uint128) Cmp(b Uint128) int

Cmp compares a and b and returns:

-1 if a <  b
 0 if a == b
+1 if a >  b

func (Uint128) Div

func (a Uint128) Div(b Uint128) Uint128

Div returns the quotient a/b for b != 0. If b == 0, a division-by-zero run-time panic occurs.

func (Uint128) DivMod

func (a Uint128) DivMod(b Uint128) (Uint128, Uint128)

DivMod returns the quotient and remainder of a/b for b != 0. If b == 0, a division-by-zero run-time panic occurs.

q = a div b  such that
m = a - b*q  with 0 <= m < |y|

func (Uint128) Format added in v0.2.0

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

Format implements fmt.Formatter.

func (Uint128) Int128

func (a Uint128) Int128() Int128

Int128 returns a as a signed 128-bit integer.

func (Uint128) LeadingZeros

func (a Uint128) LeadingZeros() int

LeadingZeros returns the number of leading zero bits in a; the result is 128 for a == 0.

func (Uint128) Len

func (a Uint128) Len() int

Len returns the minimum number of bits required to represent a; the result is 0 for a == 0.

func (Uint128) Lsh

func (a Uint128) Lsh(i uint) Uint128

Lsh returns the logical left shift a<<i.

This function's execution time does not depend on the inputs.

func (Uint128) MarshalJSON

func (a Uint128) MarshalJSON() ([]byte, error)

func (Uint128) MarshalText

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

func (Uint128) Mod

func (a Uint128) Mod(b Uint128) Uint128

Mod returns the modulus x%y for y != 0. If y == 0, a division-by-zero run-time panic occurs.

func (Uint128) Mul

func (a Uint128) Mul(b Uint128) Uint128

Mul returns the product x*y.

This function's execution time does not depend on the inputs.

func (Uint128) Neg

func (a Uint128) Neg() Uint128

Neg returns the negation -a.

This function's execution time does not depend on the inputs.

func (Uint128) Not

func (a Uint128) Not() Uint128

Not returns the bitwise NOT ^a.

This function's execution time does not depend on the inputs.

func (Uint128) OnesCount

func (a Uint128) OnesCount() int

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

func (Uint128) Or

func (a Uint128) Or(b Uint128) Uint128

Or returns the bitwise OR a|b.

This function's execution time does not depend on the inputs.

func (Uint128) Quo

func (a Uint128) Quo(b Uint128) Uint128

Quo returns the quotient a/b for b != 0. If b == 0, a division-by-zero run-time panic occurs. Quo is the same as Div in Uint128.

func (Uint128) QuoRem

func (a Uint128) QuoRem(b Uint128) (Uint128, Uint128)

QuoRem returns the quotient a/b and the remainder a%b for b != 0. a division-by-zero run-time panic occurs.

q = a/b      with the result truncated to zero
r = a - b*q

QuoRem is the same as DivMod in Uint128.

func (Uint128) Rem

func (a Uint128) Rem(b Uint128) Uint128

Rem returns he remainder a%b for b != 0. If b == 0, a division-by-zero run-time panic occurs. Rem is the same as Mod in Uint128.

func (Uint128) Reverse

func (a Uint128) Reverse() Uint128

Reverse returns the value of a with its bits in reversed order.

func (Uint128) ReverseBytes

func (a Uint128) ReverseBytes() Uint128

ReverseBytes returns the value of a with its bytes in reversed order.

This function's execution time does not depend on the inputs.

func (Uint128) RotateLeft

func (a Uint128) RotateLeft(k int) Uint128

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

This function's execution time does not depend on the inputs.

func (Uint128) Rsh

func (a Uint128) Rsh(i uint) Uint128

Rsh returns the logical right shift a>>i.

This function's execution time does not depend on the inputs.

func (Uint128) String

func (a Uint128) String() string

String returns the decimal representation of a as generated by a.Text(10).

func (Uint128) Sub

func (a Uint128) Sub(b Uint128) Uint128

Sub returns the difference x-y.

This function's execution time does not depend on the inputs.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/int128"
)

func main() {
	a := int128.Uint128{0, 3} // = 3
	b := int128.Uint128{0, 2} // = 2
	c := a.Sub(b)
	fmt.Println(c)
}
Output:
1

func (Uint128) Text

func (a Uint128) Text(base int) string

Text returns the string representation of a in the given base. Base must be between 2 and 62, inclusive. The result uses the lower-case letters 'a' to 'z' for digit values 10 to 35, and the upper-case letters 'A' to 'Z' for digit values 36 to 61. No prefix (such as "0x") is added to the string.

func (Uint128) TrailingZeros

func (a Uint128) TrailingZeros() int

TrailingZeros returns the number of trailing zero bits in a; the result is 128 for a == 0.

func (Uint128) Xor

func (a Uint128) Xor(b Uint128) Uint128

Xor returns the bitwise XOR a^b.

This function's execution time does not depend on the inputs.

Jump to

Keyboard shortcuts

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