math::checked
math::checked provides safe integer arithmetic functions that check for overflow.
The functions add*, sub* and mul* perform wrapping integer arithmetic, with the same semantics as the +, -, and * operators.
const (res, overflow) = math::addi32(types::I32_MAX, 1);
assert(res == types::I32_MIN);
assert(overflow);
The functions sat_* perform saturating integer arithmetic, which clamp the result value to the range of the type.
const res = math::sat_addi32(types::I32_MAX, 1);
assert(res == types::I32_MAX);
Index
Functions
fn addi(a: int, b: int) (int, bool);
fn addi16(a: i16, b: i16) (i16, bool);
fn addi32(a: i32, b: i32) (i32, bool);
fn addi64(a: i64, b: i64) (i64, bool);
fn addi8(a: i8, b: i8) (i8, bool);
fn addu(a: uint, b: uint) (uint, bool);
fn addu16(a: u16, b: u16) (u16, bool);
fn addu32(a: u32, b: u32) (u32, bool);
fn addu64(a: u64, b: u64) (u64, bool);
fn addu8(a: u8, b: u8) (u8, bool);
fn addz(a: size, b: size) (size, bool);
fn muli(a: int, b: int) (int, bool);
fn muli16(a: i16, b: i16) (i16, bool);
fn muli32(a: i32, b: i32) (i32, bool);
fn muli64(a: i64, b: i64) (i64, bool);
fn muli8(a: i8, b: i8) (i8, bool);
fn mulu(a: uint, b: uint) (uint, bool);
fn mulu16(a: u16, b: u16) (u16, bool);
fn mulu32(a: u32, b: u32) (u32, bool);
fn mulu64(a: u64, b: u64) (u64, bool);
fn mulu8(a: u8, b: u8) (u8, bool);
fn mulz(a: size, b: size) (size, bool);
fn sat_addi(a: int, b: int) int;
fn sat_addi16(a: i16, b: i16) i16;
fn sat_addi32(a: i32, b: i32) i32;
fn sat_addi64(a: i64, b: i64) i64;
fn sat_addi8(a: i8, b: i8) i8;
fn sat_addu(a: uint, b: uint) uint;
fn sat_addu16(a: u16, b: u16) u16;
fn sat_addu32(a: u32, b: u32) u32;
fn sat_addu64(a: u64, b: u64) u64;
fn sat_addu8(a: u8, b: u8) u8;
fn sat_addz(a: size, b: size) size;
fn sat_muli(a: int, b: int) int;
fn sat_muli16(a: i16, b: i16) i16;
fn sat_muli32(a: i32, b: i32) i32;
fn sat_muli64(a: i64, b: i64) i64;
fn sat_muli8(a: i8, b: i8) i8;
fn sat_mulu(a: uint, b: uint) uint;
fn sat_mulu16(a: u16, b: u16) u16;
fn sat_mulu32(a: u32, b: u32) u32;
fn sat_mulu64(a: u64, b: u64) u64;
fn sat_mulu8(a: u8, b: u8) u8;
fn sat_mulz(a: size, b: size) size;
fn sat_subi(a: int, b: int) int;
fn sat_subi16(a: i16, b: i16) i16;
fn sat_subi32(a: i32, b: i32) i32;
fn sat_subi64(a: i64, b: i64) i64;
fn sat_subi8(a: i8, b: i8) i8;
fn sat_subu(a: uint, b: uint) uint;
fn sat_subu16(a: u16, b: u16) u16;
fn sat_subu32(a: u32, b: u32) u32;
fn sat_subu64(a: u64, b: u64) u64;
fn sat_subu8(a: u8, b: u8) u8;
fn sat_subz(a: size, b: size) size;
fn subi(a: int, b: int) (int, bool);
fn subi16(a: i16, b: i16) (i16, bool);
fn subi32(a: i32, b: i32) (i32, bool);
fn subi64(a: i64, b: i64) (i64, bool);
fn subi8(a: i8, b: i8) (i8, bool);
fn subu(a: uint, b: uint) (uint, bool);
fn subu16(a: u16, b: u16) (u16, bool);
fn subu32(a: u32, b: u32) (u32, bool);
fn subu64(a: u64, b: u64) (u64, bool);
fn subu8(a: u8, b: u8) (u8, bool);
fn subz(a: size, b: size) (size, bool);
Functions
fn addi
fn addi(a: int, b: int) (int, bool);
Adds 'a' and 'b', returning the result and whether overflow occurred.
fn addi16
fn addi16(a: i16, b: i16) (i16, bool);
Adds 'a' and 'b', returning the result and whether overflow occurred.
fn addi32
fn addi32(a: i32, b: i32) (i32, bool);
Adds 'a' and 'b', returning the result and whether overflow occurred.
fn addi64
fn addi64(a: i64, b: i64) (i64, bool);
Adds 'a' and 'b', returning the result and whether overflow occurred.
fn addi8
fn addi8(a: i8, b: i8) (i8, bool);
Adds 'a' and 'b', returning the result and whether overflow occurred.
fn addu
fn addu(a: uint, b: uint) (uint, bool);
Adds 'a' and 'b', returning the result and whether overflow occurred.
fn addu16
fn addu16(a: u16, b: u16) (u16, bool);
Adds 'a' and 'b', returning the result and whether overflow occurred.
fn addu32
fn addu32(a: u32, b: u32) (u32, bool);
Adds 'a' and 'b', returning the result and whether overflow occurred.
fn addu64
fn addu64(a: u64, b: u64) (u64, bool);
Adds 'a' and 'b', returning the result and whether overflow occurred.
fn addu8
fn addu8(a: u8, b: u8) (u8, bool);
Adds 'a' and 'b', returning the result and whether overflow occurred.
fn addz
fn addz(a: size, b: size) (size, bool);
Adds 'a' and 'b', returning the result and whether overflow occurred.
fn muli
fn muli(a: int, b: int) (int, bool);
Multiplies 'a' and 'b' returning the result and whether overflow occurred.
fn muli16
fn muli16(a: i16, b: i16) (i16, bool);
Multiplies 'a' and 'b' returning the result and whether overflow occurred.
fn muli32
fn muli32(a: i32, b: i32) (i32, bool);
Multiplies 'a' and 'b' returning the result and whether overflow occurred.
fn muli64
fn muli64(a: i64, b: i64) (i64, bool);
Multiplies 'a' and 'b' returning the result and whether overflow occurred.
fn muli8
fn muli8(a: i8, b: i8) (i8, bool);
Multiplies 'a' and 'b' returning the result and whether overflow occurred.
fn mulu
fn mulu(a: uint, b: uint) (uint, bool);
Multiplies 'a' and 'b' returning the result and whether overflow occurred.
fn mulu16
fn mulu16(a: u16, b: u16) (u16, bool);
Multiplies 'a' and 'b' returning the result and whether overflow occurred.
fn mulu32
fn mulu32(a: u32, b: u32) (u32, bool);
Multiplies 'a' and 'b' returning the result and whether overflow occurred.
fn mulu64
fn mulu64(a: u64, b: u64) (u64, bool);
Multiplies 'a' and 'b' returning the result and whether overflow occurred.
fn mulu8
fn mulu8(a: u8, b: u8) (u8, bool);
Multiplies 'a' and 'b' returning the result and whether overflow occurred.
fn mulz
fn mulz(a: size, b: size) (size, bool);
Multiplies 'a' and 'b' returning the result and whether overflow occurred.
fn sat_addi
fn sat_addi(a: int, b: int) int;
Computes the saturating addition of 'a' and 'b'.
fn sat_addi16
fn sat_addi16(a: i16, b: i16) i16;
Computes the saturating addition of 'a' and 'b'.
fn sat_addi32
fn sat_addi32(a: i32, b: i32) i32;
Computes the saturating addition of 'a' and 'b'.
fn sat_addi64
fn sat_addi64(a: i64, b: i64) i64;
Computes the saturating addition of 'a' and 'b'.
fn sat_addi8
fn sat_addi8(a: i8, b: i8) i8;
Computes the saturating addition of 'a' and 'b'.
fn sat_addu
fn sat_addu(a: uint, b: uint) uint;
Computes the saturating addition of 'a' and 'b'.
fn sat_addu16
fn sat_addu16(a: u16, b: u16) u16;
Computes the saturating addition of 'a' and 'b'.
fn sat_addu32
fn sat_addu32(a: u32, b: u32) u32;
Computes the saturating addition of 'a' and 'b'.
fn sat_addu64
fn sat_addu64(a: u64, b: u64) u64;
Computes the saturating addition of 'a' and 'b'.
fn sat_addu8
fn sat_addu8(a: u8, b: u8) u8;
Computes the saturating addition of 'a' and 'b'.
fn sat_addz
fn sat_addz(a: size, b: size) size;
Computes the saturating addition of 'a' and 'b'.
fn sat_muli
fn sat_muli(a: int, b: int) int;
Computes the saturating multiplication of 'a' and 'b'.
fn sat_muli16
fn sat_muli16(a: i16, b: i16) i16;
Computes the saturating multiplication of 'a' and 'b'.
fn sat_muli32
fn sat_muli32(a: i32, b: i32) i32;
Computes the saturating multiplication of 'a' and 'b'.
fn sat_muli64
fn sat_muli64(a: i64, b: i64) i64;
Computes the saturating multiplication of 'a' and 'b'.
fn sat_muli8
fn sat_muli8(a: i8, b: i8) i8;
Computes the saturating multiplication of 'a' and 'b'.
fn sat_mulu
fn sat_mulu(a: uint, b: uint) uint;
Computes the saturating multiplication of 'a' and 'b'.
fn sat_mulu16
fn sat_mulu16(a: u16, b: u16) u16;
Computes the saturating multiplication of 'a' and 'b'.
fn sat_mulu32
fn sat_mulu32(a: u32, b: u32) u32;
Computes the saturating multiplication of 'a' and 'b'.
fn sat_mulu64
fn sat_mulu64(a: u64, b: u64) u64;
Computes the saturating multiplication of 'a' and 'b'.
fn sat_mulu8
fn sat_mulu8(a: u8, b: u8) u8;
Computes the saturating multiplication of 'a' and 'b'.
fn sat_mulz
fn sat_mulz(a: size, b: size) size;
Computes the saturating multiplication of 'a' and 'b'.
fn sat_subi
fn sat_subi(a: int, b: int) int;
Computes the saturating subtraction of 'b' from 'a'.
fn sat_subi16
fn sat_subi16(a: i16, b: i16) i16;
Computes the saturating subtraction of 'b' from 'a'.
fn sat_subi32
fn sat_subi32(a: i32, b: i32) i32;
Computes the saturating subtraction of 'b' from 'a'.
fn sat_subi64
fn sat_subi64(a: i64, b: i64) i64;
Computes the saturating subtraction of 'b' from 'a'.
fn sat_subi8
fn sat_subi8(a: i8, b: i8) i8;
Computes the saturating subtraction of 'b' from 'a'.
fn sat_subu
fn sat_subu(a: uint, b: uint) uint;
Computes the saturating subtraction of 'b' from 'a'.
fn sat_subu16
fn sat_subu16(a: u16, b: u16) u16;
Computes the saturating subtraction of 'b' from 'a'.
fn sat_subu32
fn sat_subu32(a: u32, b: u32) u32;
Computes the saturating subtraction of 'b' from 'a'.
fn sat_subu64
fn sat_subu64(a: u64, b: u64) u64;
Computes the saturating subtraction of 'b' from 'a'.
fn sat_subu8
fn sat_subu8(a: u8, b: u8) u8;
Computes the saturating subtraction of 'b' from 'a'.
fn sat_subz
fn sat_subz(a: size, b: size) size;
Computes the saturating subtraction of 'b' from 'a'.
fn subi
fn subi(a: int, b: int) (int, bool);
Subtracts 'b' from 'a', returning the result and whether overflow occurred.
fn subi16
fn subi16(a: i16, b: i16) (i16, bool);
Subtracts 'b' from 'a', returning the result and whether overflow occurred.
fn subi32
fn subi32(a: i32, b: i32) (i32, bool);
Subtracts 'b' from 'a', returning the result and whether overflow occurred.
fn subi64
fn subi64(a: i64, b: i64) (i64, bool);
Subtracts 'b' from 'a', returning the result and whether overflow occurred.
fn subi8
fn subi8(a: i8, b: i8) (i8, bool);
Subtracts 'b' from 'a', returning the result and whether overflow occurred.
fn subu
fn subu(a: uint, b: uint) (uint, bool);
Subtracts 'b' from 'a', returning the result and whether overflow occurred.
fn subu16
fn subu16(a: u16, b: u16) (u16, bool);
Subtracts 'b' from 'a', returning the result and whether overflow occurred.
fn subu32
fn subu32(a: u32, b: u32) (u32, bool);
Subtracts 'b' from 'a', returning the result and whether overflow occurred.
fn subu64
fn subu64(a: u64, b: u64) (u64, bool);
Subtracts 'b' from 'a', returning the result and whether overflow occurred.
fn subu8
fn subu8(a: u8, b: u8) (u8, bool);
Subtracts 'b' from 'a', returning the result and whether overflow occurred.
fn subz
fn subz(a: size, b: size) (size, bool);
Subtracts 'b' from 'a', returning the result and whether overflow occurred.