Browse the docs

silk.num.usize.

Pointer-width unsigned integers for lengths, indices, capacities, and allocation sizes.

When to use#

Use usize for values that index target memory or describe its layout. Use a fixed-width integer for serialized data, protocols, and persistent identifiers whose range must not change between 32-bit and 64-bit targets.

Details#

Each arithmetic operation has four forms with different overflow rules:

  • The plain form, such as add, traps on overflow. Use it when overflow is a program error.

  • The checked form, such as checkedAdd, returns None on overflow. Use it when input data controls the operands.

  • The wrapping form, such as wrappingAdd, computes the result modulo two to the power of BITS.

  • The saturating form, such as saturatingAdd, returns MIN or MAX on overflow.

Division and remainder by zero trap. checkedDivide and checkedRemainder return None. The shift count must be in the range 0 to BITS - 1. Otherwise, the program traps. A rotate reduces its count modulo BITS, so each count is valid. A right shift inserts zero bits.

A to conversion, such as toU8, traps if the value is out of the range of the destination type. A checkedTo conversion returns None instead. Conversion to f32 or f64 rounds to the nearest value if the integer has more significant bits than the float.

A wrappingTo conversion, such as wrappingToI16, never traps. It keeps the low bits of the two's complement form of the value, like a cast in C or Rust, so it truncates, sign-extends, or reinterprets bits.

countOnes, leadingZeros, and trailingZeros count the bits of the value. For zero, both zero counts return BITS.

BITS, MIN, and MAX depend on the target: 32 bits on wasm32 and 64 bits on the other targets.

Use ZERO and ONE where a literal has no type context, because a literal without context is an i32.

parse reads decimal text. A sign is not valid. To write a value as text, use silk.text.format.Format.display or Format.displayWith with a Writer. Neither needs an Allocator.

Gotchas#

A value valid as usize on a 64-bit target can be out of range on a 32-bit target.

Examples#

Keep a count within the target range#

silk
import silk.core.option { Option }
import silk.num.usize

pub fn main() -> i32 {
  let next = usize.checkedAdd(usize.MAX, usize.ONE)
  let recovered = move next |> Option<usize>.unwrapOr(42)
  let result = usize.toI32(recovered)
  if result != 42 {
    return 1
  }
  return 0
}

Parse decimal text and reject invalid text#

silk
import silk.text.format { ParseError }
import silk.core.result { Result }
import silk.num.usize

pub fn main() -> i32 {
  let parsed = usize.parse("42") |> Result<usize, ParseError>.unwrapOr(0)
  let invalid = usize.parse("4x2") |> Result<usize, ParseError>.unwrapOr(0)
  if parsed != 42 || invalid != 0 {
    return 1
  }
  return 0
}

Import as usize with import silk.num.usize.

Public declarations: 72.

MAX#

silk
pub const MAX: usize

The largest usize value for the compilation target.

Details#

This is 4294967295 on a 32-bit target and 18446744073709551615 on a 64-bit target. Checked arithmetic rejects results above it.

MIN#

silk
pub const MIN: usize

The smallest usize value, which is zero at each pointer width.

BITS#

silk
pub const BITS: u32

The width of usize in bits, which is the compilation target's pointer width.

ZERO#

silk
pub const ZERO: usize

The usize zero value for a count without a typed context.

ONE#

silk
pub const ONE: usize

The usize value one for a step or count that has no other type context.

toU8#

silk
pub fn toU8(value: usize) -> u8

Converts value to u8. Traps if value is outside the u8 range. Use this function when an out-of-range value is a program error.

checkedToU8#

silk
pub fn checkedToU8(value: usize) -> Option<u8>

Converts value to u8, or returns None if value is outside the u8 range. Use this function when an out-of-range value is input data.

toU16#

silk
pub fn toU16(value: usize) -> u16

Converts value to u16. Traps if value is outside the u16 range. Use this function when an out-of-range value is a program error.

checkedToU16#

silk
pub fn checkedToU16(value: usize) -> Option<u16>

Converts value to u16, or returns None if value is outside the u16 range. Use this function when an out-of-range value is input data.

toU32#

silk
pub fn toU32(value: usize) -> u32

Converts value to u32. Traps if value is outside the u32 range. Use this function when an out-of-range value is a program error.

checkedToU32#

silk
pub fn checkedToU32(value: usize) -> Option<u32>

Converts value to u32, or returns None if value is outside the u32 range. Use this function when an out-of-range value is input data.

toU64#

silk
pub fn toU64(value: usize) -> u64

Converts value exactly to u64. Every usize value is representable.

checkedToU64#

silk
pub fn checkedToU64(value: usize) -> Option<u64>

Converts value exactly to u64 and returns Some. Every usize value is representable.

toUsize#

silk
pub fn toUsize(value: usize) -> usize

Returns value unchanged as usize. Use this function when generic conversion code can select usize as both source and destination.

checkedToUsize#

silk
pub fn checkedToUsize(value: usize) -> Option<usize>

Returns Some with value unchanged as usize. Use this function when generic checked-conversion code can select the same source and destination type.

toI8#

silk
pub fn toI8(value: usize) -> i8

Converts value to i8. Traps if value is outside the i8 range. Use this function when an out-of-range value is a program error.

checkedToI8#

silk
pub fn checkedToI8(value: usize) -> Option<i8>

Converts value to i8, or returns None if value is outside the i8 range. Use this function when an out-of-range value is input data.

toI16#

silk
pub fn toI16(value: usize) -> i16

Converts value to i16. Traps if value is outside the i16 range. Use this function when an out-of-range value is a program error.

checkedToI16#

silk
pub fn checkedToI16(value: usize) -> Option<i16>

Converts value to i16, or returns None if value is outside the i16 range. Use this function when an out-of-range value is input data.

toI32#

silk
pub fn toI32(value: usize) -> i32

Converts value to i32. Traps if value is outside the i32 range. Use this function when an out-of-range value is a program error.

checkedToI32#

silk
pub fn checkedToI32(value: usize) -> Option<i32>

Converts value to i32, or returns None if value is outside the i32 range. Use this function when an out-of-range value is input data.

toI64#

silk
pub fn toI64(value: usize) -> i64

Converts value to i64. Traps if value is outside the i64 range. Use this function when an out-of-range value is a program error.

checkedToI64#

silk
pub fn checkedToI64(value: usize) -> Option<i64>

Converts value to i64, or returns None if value is outside the i64 range. Use this function when an out-of-range value is input data.

toIsize#

silk
pub fn toIsize(value: usize) -> isize

Converts value to isize. Traps if value is outside the isize range. Use this function when an out-of-range value is a program error.

checkedToIsize#

silk
pub fn checkedToIsize(value: usize) -> Option<isize>

Converts value to isize, or returns None if value is outside the isize range. Use this function when an out-of-range value is input data.

toF32#

silk
pub fn toF32(value: usize) -> f32

Converts value to the nearest f32 value, with ties to even. Only magnitudes below 2^24 are exactly representable, so a larger usize rounds.

toF64#

silk
pub fn toF64(value: usize) -> f64

Converts value to the nearest f64 value, with ties to even. Only magnitudes below 2^53 are exactly representable, so a larger usize rounds. Every usize is exact on a 32-bit target.

wrappingToU8#

silk
pub fn wrappingToU8(value: usize) -> u8

Converts value to u8 modulo 2^8. Keeps the low 8 bits of the two's complement form of value and reads them as u8, so it never traps. Use this function for deliberate truncation, sign extension, or bit reinterpretation, such as reading a signed field.

wrappingToU16#

silk
pub fn wrappingToU16(value: usize) -> u16

Converts value to u16 modulo 2^16. Keeps the low 16 bits of the two's complement form of value and reads them as u16, so it never traps. Use this function for deliberate truncation, sign extension, or bit reinterpretation, such as reading a signed field.

wrappingToU32#

silk
pub fn wrappingToU32(value: usize) -> u32

Converts value to u32 modulo 2^32. Keeps the low 32 bits of the two's complement form of value and reads them as u32, so it never traps. Use this function for deliberate truncation, sign extension, or bit reinterpretation, such as reading a signed field.

wrappingToU64#

silk
pub fn wrappingToU64(value: usize) -> u64

Converts value to u64 modulo 2^64. Keeps the low 64 bits of the two's complement form of value and reads them as u64, so it never traps. Use this function for deliberate truncation, sign extension, or bit reinterpretation, such as reading a signed field.

wrappingToUsize#

silk
pub fn wrappingToUsize(value: usize) -> usize

Returns value unchanged. Use this function when generic wrapping-conversion code can select usize as both source and destination.

wrappingToI8#

silk
pub fn wrappingToI8(value: usize) -> i8

Converts value to i8 modulo 2^8. Keeps the low 8 bits of the two's complement form of value and reads them as i8, so it never traps. Use this function for deliberate truncation, sign extension, or bit reinterpretation, such as reading a signed field.

wrappingToI16#

silk
pub fn wrappingToI16(value: usize) -> i16

Converts value to i16 modulo 2^16. Keeps the low 16 bits of the two's complement form of value and reads them as i16, so it never traps. Use this function for deliberate truncation, sign extension, or bit reinterpretation, such as reading a signed field.

wrappingToI32#

silk
pub fn wrappingToI32(value: usize) -> i32

Converts value to i32 modulo 2^32. Keeps the low 32 bits of the two's complement form of value and reads them as i32, so it never traps. Use this function for deliberate truncation, sign extension, or bit reinterpretation, such as reading a signed field.

wrappingToI64#

silk
pub fn wrappingToI64(value: usize) -> i64

Converts value to i64 modulo 2^64. Keeps the low 64 bits of the two's complement form of value and reads them as i64, so it never traps. Use this function for deliberate truncation, sign extension, or bit reinterpretation, such as reading a signed field.

wrappingToIsize#

silk
pub fn wrappingToIsize(value: usize) -> isize

Converts value to isize modulo 2^isize.BITS. Keeps the low isize.BITS bits of the two's complement form of value and reads them as isize, so it never traps. Use this function for deliberate truncation, sign extension, or bit reinterpretation, such as reading a signed field.

add#

silk
pub fn add(left: usize, right: usize) -> usize

Returns left + right and traps if the result is outside the usize range. Use this function when overflow is a program error.

subtract#

silk
pub fn subtract(left: usize, right: usize) -> usize

Returns left - right and traps if the result is outside the usize range. Use this function when overflow is a program error.

multiply#

silk
pub fn multiply(left: usize, right: usize) -> usize

Returns left * right and traps if the result is outside the usize range. Use this function when overflow is a program error.

divide#

silk
pub fn divide(left: usize, right: usize) -> usize

Returns left / right. Traps if right is zero. Use this function when a zero divisor is a program error.

remainder#

silk
pub fn remainder(left: usize, right: usize) -> usize

Returns the remainder of left / right. Traps if right is zero. Use this function when a zero divisor is a program error.

bitAnd#

silk
pub fn bitAnd(left: usize, right: usize) -> usize

Returns the bitwise AND of left and right.

bitOr#

silk
pub fn bitOr(left: usize, right: usize) -> usize

Returns the bitwise OR of left and right.

bitXor#

silk
pub fn bitXor(left: usize, right: usize) -> usize

Returns the bitwise exclusive OR of left and right.

bitNot#

silk
pub fn bitNot(value: usize) -> usize

Returns value with each bit inverted.

shiftLeft#

silk
pub fn shiftLeft(left: usize, right: usize) -> usize

Shifts left bits left by right positions. Traps if right is not less than BITS.

shiftRight#

silk
pub fn shiftRight(left: usize, right: usize) -> usize

Shifts left bits right by right positions and inserts zero bits. Traps if right is not less than BITS.

rotateLeft#

silk
pub fn rotateLeft(left: usize, right: usize) -> usize

Rotates the bits of left left by right positions. The count is reduced modulo BITS, so each count is valid.

rotateRight#

silk
pub fn rotateRight(left: usize, right: usize) -> usize

Rotates the bits of left right by right positions. The count is reduced modulo BITS, so each count is valid.

countOnes#

silk
pub fn countOnes(value: usize) -> u32

Returns the number of one bits in value.

leadingZeros#

silk
pub fn leadingZeros(value: usize) -> u32

Returns the number of zero bits above the highest one bit of value. Returns BITS for zero.

trailingZeros#

silk
pub fn trailingZeros(value: usize) -> u32

Returns the number of zero bits below the lowest one bit of value. Returns BITS for zero.

wrappingAdd#

silk
pub fn wrappingAdd(left: usize, right: usize) -> usize

Returns left + right, wrapped to the usize range. Use this function for deliberate modulo arithmetic.

wrappingSubtract#

silk
pub fn wrappingSubtract(left: usize, right: usize) -> usize

Returns left - right, wrapped to the usize range. Use this function for deliberate modulo arithmetic.

wrappingMultiply#

silk
pub fn wrappingMultiply(left: usize, right: usize) -> usize

Returns left * right, wrapped to the usize range. Use this function for deliberate modulo arithmetic.

saturatingAdd#

silk
pub fn saturatingAdd(left: usize, right: usize) -> usize

Returns left + right, clamped to MIN or MAX. Use this function when a boundary value is the required overflow result.

saturatingSubtract#

silk
pub fn saturatingSubtract(left: usize, right: usize) -> usize

Returns left - right, clamped to MIN or MAX. Use this function when a boundary value is the required overflow result.

saturatingMultiply#

silk
pub fn saturatingMultiply(left: usize, right: usize) -> usize

Returns left * right, clamped to MIN or MAX. Use this function when a boundary value is the required overflow result.

multiplyHigh#

silk
pub fn multiplyHigh(left: usize, right: usize) -> usize

Returns the high half of the exact double-width product left * right. It never overflows.

When to use#

Use this function with wrappingMultiply, which returns the low half, when an algorithm needs the full product, such as multi-word arithmetic, multiply-shift hashing, or division by a precomputed reciprocal.

Details#

The width is the target word: the high 64 bits of a 128-bit product on a 64-bit target, and the high 32 bits of a 64-bit product on a 32-bit target.

checkedAdd#

silk
pub fn checkedAdd(left: usize, right: usize) -> Option<usize>

Returns Some with left + right, or None if the result is outside the usize range. Use this function when overflow is input data.

checkedSubtract#

silk
pub fn checkedSubtract(left: usize, right: usize) -> Option<usize>

Returns Some with left - right, or None if the result is outside the usize range. Use this function when overflow is input data.

checkedMultiply#

silk
pub fn checkedMultiply(left: usize, right: usize) -> Option<usize>

Returns Some with left * right, or None if the result is outside the usize range. Use this function when overflow is input data.

checkedDivide#

silk
pub fn checkedDivide(left: usize, right: usize) -> Option<usize>

Returns Some with left / right, or None if right is zero. Use this function when a zero divisor is input data.

checkedRemainder#

silk
pub fn checkedRemainder(left: usize, right: usize) -> Option<usize>

Returns Some with the remainder, or None if right is zero. Use this function when a zero divisor is input data.

equals#

silk
pub fn equals(left: usize, right: usize) -> bool

Returns true if left and right are equal.

notEquals#

silk
pub fn notEquals(left: usize, right: usize) -> bool

Returns true if left and right are not equal.

lessThan#

silk
pub fn lessThan(left: usize, right: usize) -> bool

Returns true if left is less than right.

lessOrEqual#

silk
pub fn lessOrEqual(left: usize, right: usize) -> bool

Returns true if left is less than or equal to right.

greaterThan#

silk
pub fn greaterThan(left: usize, right: usize) -> bool

Returns true if left is greater than right.

greaterOrEqual#

silk
pub fn greaterOrEqual(left: usize, right: usize) -> bool

Returns true if left is greater than or equal to right.

parse#

silk
pub fn parse(text: string) -> Result<usize, ParseError>

Returns the usize value of the decimal text text, or a ParseError.

Details#

The complete text must be one decimal number. This function does not skip spaces and does not accept digit separators.

Errors#

  • ParseError with reason silk.text.format.NotANumber if text is empty, has a sign, contains a byte that is not a digit, or has bytes after the number. offset gives the byte position where the read stopped.

  • ParseError with reason silk.text.format.OutOfRange if the number is outside the range of usize on the target.

Category: Numbers.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.

View the source of this page