XU XUv0.16.0

#math/bits

import: #math/bits

counts: 34 functions, 0 methods, 1 types, 0 constants, 0 method sets

Full-width unsigned arithmetic

mul64 and div64 interpret int inputs as unsigned 64-bit bit patterns, including negative signed representations. Multiplication returns the low lane in Carry.lo and the high lane in Carry.carry. Division consumes the full 128-bit pair (hi, lo), returning quotient and remainder in those fields. Zero divisors and quotients that do not fit 64 bits are errors. These software operations do not provide a constant-time guarantee.

len

XuAPI
pub fn len(x int) int

Purpose

len: minimum number of bits needed to represent x (0 for x == 0). Equal to 64 - leading_zeros(x), matching bits.Len64.

Parameters

x int
value (int)

Returns

int — a value of this type

leading_zeros

XuAPI
pub fn leading_zeros(x int) int

Purpose

leading_zeros: number of zero bits before the highest set bit. Binary search: each (v >> n) == 0 test is sign-agnostic (arithmetic shift yields 0 iff the top n bits are all zero, since the sign bit is in that window), then the low bits are shifted up.

Parameters

x int
value (int)

Returns

int — a value of this type

trailing_zeros

XuAPI
pub fn trailing_zeros(x int) int

Purpose

trailing_zeros: number of zero bits after the lowest set bit. Binary search on the low bits; the arithmetic right shifts are safe because only the low (64-n) bits of the result are ever inspected, and those are identical for arithmetic and logical shifts.

Parameters

x int
value (int)

Returns

int — a value of this type

ones_count

XuAPI
pub fn ones_count(x int) int

Purpose

ones_count: population count via SWAR parallel summing of adjacent bits ( bits.OnesCount64, from "Hacker's Delight" ch. 5). After masking, each byte holds at most eight set bits. Multiplication sums the byte counts into the high byte without carries between lanes.

Parameters

x int
value (int)

Returns

int — a value of this type

rotate_left

XuAPI
pub fn rotate_left(x int, k int) int

Purpose

rotate_left: k is reduced mod 64 ( bits.RotateLeft64). The low part is a logical right shift by (64-s) masked to s bits; s == 0 is handled separately so no shift-by-64 is ever emitted.

Parameters

x int
value (int)
k int
key or index (int)

Returns

int — a value of this type

rotate_right

XuAPI
pub fn rotate_right(x int, k int) int

Purpose

rotate_right: bits.RotateRight64 (k mod 64). The low part is a logical right shift by s masked to (64-s) bits; the high part moves the low s bits to the top.

Parameters

x int
value (int)
k int
key or index (int)

Returns

int — a value of this type

reverse

XuAPI
pub fn reverse(x int) int

Purpose

reverse: full bit reversal ( bits.Reverse64). 5-stage mask swap over the bit-swap masks m0/m1/m2 then the byte-swap masks m3/m4/m5 each (v >> n) & mask zeroes the sign extension.

Parameters

x int
value (int)

Returns

int — a value of this type

reverse_bytes

XuAPI
pub fn reverse_bytes(x int) int

Purpose

reverse_bytes: byte-order reversal ( bits.ReverseBytes64).

Parameters

x int
value (int)

Returns

int — a value of this type

struct Carry

XuAPI
pub struct Carry {
    pub lo int
    pub carry int
}

Purpose

Structure encapsulating lo, carry.

Public fields

lo int
field (int)
carry int
field (int)

add64

XuAPI
pub fn add64(x int, y int, carry_in int) Carry

Purpose

Execute add64.

Parameters

x int
value (int)
y int
value (int)
carry_in int
carry_in (int)

Returns

Carry — a value of this type

sub64

XuAPI
pub fn sub64(x int, y int, borrow_in int) Carry

Purpose

Execute sub64.

Parameters

x int
value (int)
y int
value (int)
borrow_in int
borrow_in (int)

Returns

Carry — a value of this type

mul64

XuAPI
pub fn mul64(x int, y int) Carry

Purpose

Execute mul64.

Parameters

x int
value (int)
y int
value (int)

Returns

Carry — a value of this type

div64

XuAPI
pub fn div64(hi int, lo int, y int) Carry

Purpose

div64 divides the unsigned 128-bit (hi, lo) by the unsigned y. Carry.lo is the quotient and Carry.carry is the remainder. All lanes use int bit patterns; neither comparison nor division is signed. Throws on failure; handle the error or propagate it to the caller.

Parameters

hi int
hi (int)
lo int
lo (int)
y int
value (int)

Returns

Carry — a value of this type

len32

XuAPI
pub fn len32(x int) int

Purpose

Execute len32.

Parameters

x int
value (int)

Returns

int — a value of this type

leading_zeros32

XuAPI
pub fn leading_zeros32(x int) int

Purpose

Leading zeros32.

Parameters

x int
value (int)

Returns

int — a value of this type

trailing_zeros32

XuAPI
pub fn trailing_zeros32(x int) int

Purpose

Trailing zeros32.

Parameters

x int
value (int)

Returns

int — a value of this type

ones_count32

XuAPI
pub fn ones_count32(x int) int

Purpose

Ones count32.

Parameters

x int
value (int)

Returns

int — a value of this type

ones_count16

XuAPI
pub fn ones_count16(x int) int

Purpose

Ones count16.

Parameters

x int
value (int)

Returns

int — a value of this type

ones_count8

XuAPI
pub fn ones_count8(x int) int

Purpose

Ones count8.

Parameters

x int
value (int)

Returns

int — a value of this type

trailing_zeros16

XuAPI
pub fn trailing_zeros16(x int) int

Purpose

Trailing zeros16.

Parameters

x int
value (int)

Returns

int — a value of this type

trailing_zeros8

XuAPI
pub fn trailing_zeros8(x int) int

Purpose

Trailing zeros8.

Parameters

x int
value (int)

Returns

int — a value of this type

len16

XuAPI
pub fn len16(x int) int

Purpose

Execute len16.

Parameters

x int
value (int)

Returns

int — a value of this type

len8

XuAPI
pub fn len8(x int) int

Purpose

Execute len8.

Parameters

x int
value (int)

Returns

int — a value of this type

leading_zeros16

XuAPI
pub fn leading_zeros16(x int) int

Purpose

Leading zeros16.

Parameters

x int
value (int)

Returns

int — a value of this type

leading_zeros8

XuAPI
pub fn leading_zeros8(x int) int

Purpose

Leading zeros8.

Parameters

x int
value (int)

Returns

int — a value of this type

reverse16

XuAPI
pub fn reverse16(x int) int

Purpose

Execute reverse16.

Parameters

x int
value (int)

Returns

int — a value of this type

reverse8

XuAPI
pub fn reverse8(x int) int

Purpose

Execute reverse8.

Parameters

x int
value (int)

Returns

int — a value of this type

reverse_bytes32

XuAPI
pub fn reverse_bytes32(x int) int

Purpose

Reverse bytes32.

Parameters

x int
value (int)

Returns

int — a value of this type

reverse_bytes16

XuAPI
pub fn reverse_bytes16(x int) int

Purpose

Reverse bytes16.

Parameters

x int
value (int)

Returns

int — a value of this type

reverse_bytes8

XuAPI
pub fn reverse_bytes8(x int) int

Purpose

Reverse bytes8.

Parameters

x int
value (int)

Returns

int — a value of this type

bit_extract

XuAPI
pub fn bit_extract(x int, pos int, width int) int

Purpose

Extract width unsigned bits starting at bit pos (0 = LSB, 0 < width <= 64).

Parameters

x int
value (int)
pos int
pos (int)
width int
width (int)

Returns

int — a value of this type

rotate_left16

XuAPI
pub fn rotate_left16(x int, k int) int

Purpose

Rotate left16.

Parameters

x int
value (int)
k int
key or index (int)

Returns

int — a value of this type

rotate_left8

XuAPI
pub fn rotate_left8(x int, k int) int

Purpose

Rotate left8.

Parameters

x int
value (int)
k int
key or index (int)

Returns

int — a value of this type

rotate_right16

XuAPI
pub fn rotate_right16(x int, k int) int

Purpose

Rotate right16.

Parameters

x int
value (int)
k int
key or index (int)

Returns

int — a value of this type

rotate_right8

XuAPI
pub fn rotate_right8(x int, k int) int

Purpose

Rotate right8.

Parameters

x int
value (int)
k int
key or index (int)

Returns

int — a value of this type