uint128

package module
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2023 License: BSD-2-Clause Imports: 5 Imported by: 1

README

github.com/weborama/uint128

Go Report Card

This package provides an implementation of a 128 bit unsigned integer with some implementations of:

  • arithmetic operators (Add, Sub, Incr, Decr)
  • binary operations (AND, OR, XOR, NOT, AND NOT, shifts)
  • math/bits operations (LeadingZeros, Len, OnesCount, Reverse, ReverseBytes, TrailingZeros)

Missing operators (Mult, Div, Mod, RotateLeft etc.) to be added later.

Uses a modified copy (changes to the generated package declaration) of make_tables.go from math/bits as well as code sourced from github.com/mengzhuo/uint128 and github.com/davidminor/uint128.

Original use case for this package is implementing IPv6 calculations (see github.com/weborama/cidr).

Documentation

Overview

Copyright 2018 Weborama. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Copyright 2018 Weborama. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cmp

func Cmp(x, y Uint128) int

Cmp compares two Uint128 and returns one of the following values:

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

nolint: varnamelen

func IsZero

func IsZero(x Uint128) bool

IsZero returns true if x is zero.

func LeadingZeros

func LeadingZeros(x Uint128) int

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

func Len

func Len(x Uint128) int

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

func OnesCount

func OnesCount(x Uint128) int

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

func TrailingZeros

func TrailingZeros(x Uint128) int

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

Types

type Uint128

type Uint128 struct {
	H, L uint64
}

Uint128 defines an unsigned integer of 128 bits.

func Add

func Add(x, y Uint128) Uint128

Add adds x and y.

func Add128 added in v0.7.1

func Add128(x, y, carry Uint128) (sum, carryOut Uint128)

Add128 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 And

func And(x, y Uint128) Uint128

And returns the logical AND of x and y.

func AndNot

func AndNot(x, y Uint128) Uint128

AndNot returns the logical AND NOT of x and y.

func Decr

func Decr(x Uint128) Uint128

Decr decrements x by one. nolint: ifshort, varnamelen

func Incr

func Incr(x Uint128) Uint128

Incr increments x by one.

func MaxUint128

func MaxUint128() Uint128

MaxUint128 returns the maximum value of an Uint128.

func NewFromString

func NewFromString(str string) (x Uint128, err error)

NewFromString creates a new Uint128 from its hexadecimal string representation. XXX: Do a proper job of it.

func Not

func Not(x Uint128) Uint128

Not returns the logical NOT of x and o.

func Or

func Or(x, y Uint128) Uint128

Or returns the logical OR of x and y.

func Reverse

func Reverse(x Uint128) (y Uint128)

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

func ReverseBytes

func ReverseBytes(x Uint128) (y Uint128)

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

func ShiftLeft

func ShiftLeft(x Uint128, bits uint) Uint128

ShiftLeft shifts x to the left by the provided number of bits. nolint: varnamelen

func ShiftRight

func ShiftRight(x Uint128, bits uint) Uint128

ShiftRight shifts x to the right by the provided number of bits. nolint: varnamelen

func Sub

func Sub(x, y Uint128) Uint128

Sub subtracts x and y. nolint: ifshort, varnamelen

func Xor

func Xor(x, y Uint128) Uint128

Xor returns the logical XOR of x and y.

func Zero

func Zero() Uint128

Zero is a 0 valued Uint128.

func (Uint128) Add

func (x Uint128) Add(y Uint128) Uint128

Add adds x and y.

func (Uint128) Add128 added in v0.7.1

func (x Uint128) Add128(y, carry Uint128) (sum, carryOut Uint128)

Add128 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 (Uint128) And

func (x Uint128) And(y Uint128) Uint128

And returns the logical AND of x and y.

func (Uint128) AndNot

func (x Uint128) AndNot(y Uint128) Uint128

AndNot returns the logical AND NOT of x and y.

func (Uint128) Cmp

func (x Uint128) Cmp(y Uint128) int

Cmp compares two Uint128 and returns one of the following values:

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

func (Uint128) Decr

func (x Uint128) Decr() Uint128

Decr decrements x by one.

func (Uint128) Format

func (x Uint128) Format(fmtState fmt.State, c rune)

Format is a custom formatter for Uint128 XXX: Do a proper job of it.

func (Uint128) HexString

func (x Uint128) HexString() string

HexString returns a Hexadecimal string representation of an Uint128. XXX: Do a proper job of it.

func (Uint128) Incr

func (x Uint128) Incr() Uint128

Incr increments x by one.

func (Uint128) IsZero

func (x Uint128) IsZero() bool

IsZero returns true if x is zero.

func (Uint128) Not

func (x Uint128) Not() Uint128

Not returns the logical AND of x and y.

func (Uint128) Or

func (x Uint128) Or(y Uint128) Uint128

Or returns the logical OR of x and y.

func (Uint128) ShiftLeft

func (x Uint128) ShiftLeft(bits uint) Uint128

ShiftLeft shifts x to the left by the provided number of bits.

func (Uint128) ShiftRight

func (x Uint128) ShiftRight(bits uint) Uint128

ShiftRight shifts x to the right by the provided number of bits.

func (Uint128) String

func (x Uint128) String() string

func (Uint128) Sub

func (x Uint128) Sub(y Uint128) Uint128

Sub subtracts x and y.

func (Uint128) Xor

func (x Uint128) Xor(y Uint128) Uint128

Xor returns the logical XOR of x and y.

Jump to

Keyboard shortcuts

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