XU XUv0.8.8
中文

API

#math/bits

import: #math/bits

counts: 32 fns, 0 methods, 1 types, 0 consts, 0 ifaces

Bit length, leading zeros, rotate, add/sub/mul/div with carry.

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). The three mask constants m0/m1/m2 zero the sign-extension bits after the arithmetic shifts, and after the `& m2` step the value is non-negative so the trailing `+ (v >> n)` folds are plain logical shifts.

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

Public struct `Carry`.

Parameters

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

add64

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

Purpose

add64; returns Carry.

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

sub64; returns Carry.

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

mul64; returns Carry.

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; returns Carry. Throws on failure.

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

len32; returns int.

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

len16; returns int.

Parameters

x int
value (int)

Returns

int — a value of this type

len8

XuAPI
pub fn len8(x int) int

Purpose

len8; returns int.

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

reverse16; returns int.

Parameters

x int
value (int)

Returns

int — a value of this type

reverse8

XuAPI
pub fn reverse8(x int) int

Purpose

reverse8; returns int.

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

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