Axle v0.14.1
Package

std/core/bits

std/bits — integer bit-twiddling primitives.

Population count + leading/trailing-zero count + (when applicable)
byte swap route through the matching cttz_* / ctlz_* /
popcnt_* compiler builtin. Each lowers to one LLVM intrinsic
(@llvm.cttz / @llvm.ctlz / @llvm.ctpop), which the backend
further compiles to a single native instruction (tzcnt /
lzcnt / popcnt on x86 BMI1+POPCNT). Routing through the builtin
reclaims the intended one-instruction lowering instead of a long
testb-chain expansion in every probe.

All functions take signed i32 / i64 but treat the operand as a raw bit
pattern. leadingZerosN(0) = N and trailingZerosN(0) = N by convention,
which matches LLVM's is_zero_poison = false.

Free functions

TypeMethod and description
i32
popcount32(x : i32) : i32 Count of set bits in the 32-bit operand.
i32
popcount64(x : i64) : i32 Count of set bits in the 64-bit operand.
i32
leadingZeros32(x : i32) : i32 Number of leading zero bits in the 32-bit operand.
i32
leadingZeros64(x : i64) : i32 Number of leading zero bits in the 64-bit operand.
i32
trailingZeros32(x : i32) : i32 Number of trailing zero bits in the 32-bit operand.
i32
trailingZeros64(x : i64) : i32 Number of trailing zero bits in the 64-bit operand.
i32
byteSwap32(x : i32) : i32 Reverse the byte order of a 32-bit value.
i64
byteSwap64(x : i64) : i64 Reverse the byte order of a 64-bit value.
i32
rotateLeft32(x : i32, n : i32) : i32 Rotate the 32-bit value left by n bits.
i64
rotateLeft64(x : i64, n : i32) : i64 Rotate the 64-bit value left by n bits.
i32
rotateRight32(x : i32, n : i32) : i32 Rotate the 32-bit value right by n bits.
i64
rotateRight64(x : i64, n : i32) : i64 Rotate the 64-bit value right by n bits.
i32
bitReverse32(x : i32) : i32 Reverse the order of all 32 bits.
i64
bitReverse64(x : i64) : i64 Reverse the order of all 64 bits.
bool
isPowerOfTwo32(x : i32) : bool True when x is a positive power of two.
bool
isPowerOfTwo64(x : i64) : bool True when x is a positive power of two.
i32
nextPowerOfTwo32(x : i32) : i32 Smallest power of two ≥ x. nextPowerOfTwo*(0) = 1 by
convention. Saturates at the type's max power of two (no overflow
promotion to a wider type).
i64
nextPowerOfTwo64(x : i64) : i64 64-bit counterpart of nextPowerOfTwo32.

Method detail

#popcount32

popcount32(x : i32) : i32

Count of set bits in the 32-bit operand.

Parameters
x raw 32-bit pattern whose set bits are counted (sign ignored)

#popcount64

popcount64(x : i64) : i32

Count of set bits in the 64-bit operand.

Parameters
x raw 64-bit pattern whose set bits are counted (sign ignored)

#leadingZeros32

leadingZeros32(x : i32) : i32

Number of leading zero bits in the 32-bit operand.

Parameters
x raw 32-bit pattern scanned from the MSB; 0 yields 32

#leadingZeros64

leadingZeros64(x : i64) : i32

Number of leading zero bits in the 64-bit operand.

Parameters
x raw 64-bit pattern scanned from the MSB; 0 yields 64

#trailingZeros32

trailingZeros32(x : i32) : i32

Number of trailing zero bits in the 32-bit operand.

Parameters
x raw 32-bit pattern scanned from the LSB; 0 yields 32

#trailingZeros64

trailingZeros64(x : i64) : i32

Number of trailing zero bits in the 64-bit operand.

Parameters
x raw 64-bit pattern scanned from the LSB; 0 yields 64

#byteSwap32

byteSwap32(x : i32) : i32

Reverse the byte order of a 32-bit value.

Parameters
x 4-byte pattern whose bytes are reversed (b0↔b3, b1↔b2)

#byteSwap64

byteSwap64(x : i64) : i64

Reverse the byte order of a 64-bit value.

Parameters
x 8-byte pattern whose bytes are reversed (b0↔b7 … b3↔b4)

#rotateLeft32

rotateLeft32(x : i32, n : i32) : i32

Rotate the 32-bit value left by n bits.

Parameters
x 32-bit pattern to rotate
n shift amount, taken modulo 32 (n & 31)

#rotateLeft64

rotateLeft64(x : i64, n : i32) : i64

Rotate the 64-bit value left by n bits.

Parameters
x 64-bit pattern to rotate
n shift amount, taken modulo 64 (n & 63)

#rotateRight32

rotateRight32(x : i32, n : i32) : i32

Rotate the 32-bit value right by n bits.

Parameters
x 32-bit pattern to rotate
n shift amount, taken modulo 32 (n & 31)

#rotateRight64

rotateRight64(x : i64, n : i32) : i64

Rotate the 64-bit value right by n bits.

Parameters
x 64-bit pattern to rotate
n shift amount, taken modulo 64 (n & 63)

#bitReverse32

bitReverse32(x : i32) : i32

Reverse the order of all 32 bits.

Parameters
x 32-bit pattern whose bit i moves to position 31 - i

#bitReverse64

bitReverse64(x : i64) : i64

Reverse the order of all 64 bits.

Parameters
x 64-bit pattern whose bit i moves to position 63 - i

#isPowerOfTwo32

isPowerOfTwo32(x : i32) : bool

True when x is a positive power of two.

Parameters
x 32-bit value tested via x > 0 && (x & (x-1)) == 0

#isPowerOfTwo64

isPowerOfTwo64(x : i64) : bool

True when x is a positive power of two.

Parameters
x 64-bit value tested via x > 0 && (x & (x-1)) == 0

#nextPowerOfTwo32

nextPowerOfTwo32(x : i32) : i32

Smallest power of two ≥ x. nextPowerOfTwo*(0) = 1 by
convention. Saturates at the type's max power of two (no overflow
promotion to a wider type).

Parameters
x 32-bit value; x <= 1 returns 1, else rounds up to a power of two

#nextPowerOfTwo64

nextPowerOfTwo64(x : i64) : i64

64-bit counterpart of nextPowerOfTwo32.

Parameters
x 64-bit value; x <= 1 returns 1, else rounds up to a power of two