## Abstract

Basic mathematical modules and functions.

## Synopsis

module type from_prim = {
 type t val i8 : i8 -> t val i16 : i16 -> t val i32 : i32 -> t val i64 : i64 -> t val u8 : u8 -> t val u16 : u16 -> t val u32 : u32 -> t val u64 : u64 -> t val f32 : f32 -> t val f64 : f64 -> t val bool : bool -> t
}
module type numeric = {
 include from_prim val + : t -> t -> t val - : t -> t -> t val * : t -> t -> t val / : t -> t -> t val % : t -> t -> t val ** : t -> t -> t val to_i64 : t -> i64 val == : t -> t -> bool val < : t -> t -> bool val > : t -> t -> bool val <= : t -> t -> bool val >= : t -> t -> bool val != : t -> t -> bool val negate : t -> t val max : t -> t -> t val min : t -> t -> t val abs : t -> t val sgn : t -> t val highest : t val lowest : t val sum [n]: [n]t -> t val product [n]: [n]t -> t val maximum [n]: [n]t -> t val minimum [n]: [n]t -> t
}
module type integral = {
 include numeric val // : t -> t -> t val %% : t -> t -> t val & : t -> t -> t val | : t -> t -> t val ^ : t -> t -> t val ! : t -> t val << : t -> t -> t val >> : t -> t -> t val >>> : t -> t -> t val num_bits : i32 val get_bit : i32 -> t -> i32 val set_bit : i32 -> t -> i32 -> t val popc : t -> i32 val mul_hi : (x: t) -> (y: t) -> t val mad_hi : (a: t) -> (b: t) -> (c: t) -> t val clz : t -> i32
}
module type size = {
 include integral val iota : t -> *[]t val replicate v: t -> v -> *[]v
}
module type real = {
 include numeric val from_fraction : i32 -> i32 -> t val to_i32 : t -> i32 val to_i64 : t -> i64 val to_f64 : t -> f64 val sqrt : t -> t val exp : t -> t val sin : t -> t val cos : t -> t val tan : t -> t val asin : t -> t val acos : t -> t val atan : t -> t val sinh : t -> t val cosh : t -> t val tanh : t -> t val asinh : t -> t val acosh : t -> t val atanh : t -> t val atan2 : t -> t -> t val gamma : t -> t val lgamma : t -> t val lerp : t -> t -> t -> t val log : t -> t val log2 : t -> t val log10 : t -> t val ceil : t -> t val floor : t -> t val trunc : t -> t val mad : (a: t) -> (b: t) -> (c: t) -> t val fma : (a: t) -> (b: t) -> (c: t) -> t val round : t -> t val isinf : t -> bool val isnan : t -> bool val inf : t val nan : t val pi : t val e : t
}
module type float = {
 include real type int_t val from_bits : int_t -> t val to_bits : t -> int_t val num_bits : i32 val get_bit : i32 -> t -> i32 val set_bit : i32 -> t -> i32 -> t
}
module bool: from_prim with t = bool
module i8: (size with t = i8)
module i16: (size with t = i16)
module i32: (size with t = i32)
module i64: (size with t = i64)
module u8: (size with t = u8)
module u16: (size with t = u16)
module u32: (size with t = u32)
module u64: (size with t = u64)
module f64: (float with t = f64 with int_t = u64)
module f32: (float with t = f32 with int_t = u32)

## Description

module type from_prim

Describes types of values that can be created from the primitive numeric types (and bool).

type t
val i8: i8 -> t
val i16: i16 -> t
val i32: i32 -> t
val i64: i64 -> t
val u8: u8 -> t
val u16: u16 -> t
val u32: u32 -> t
val u64: u64 -> t
val f32: f32 -> t
val f64: f64 -> t
val bool: bool -> t
module type numeric

A basic numeric module type that can be implemented for both integers and rational numbers.

include from_prim
val +: t -> t -> t
val -: t -> t -> t
val *: t -> t -> t
val /: t -> t -> t
val %: t -> t -> t
val **: t -> t -> t
val to_i64: t -> i64
val ==: t -> t -> bool
val <: t -> t -> bool
val >: t -> t -> bool
val <=: t -> t -> bool
val >=: t -> t -> bool
val !=: t -> t -> bool
val negate: t -> t
val max: t -> t -> t
val min: t -> t -> t
val abs: t -> t
val sgn: t -> t
val highest: t

The highest representable number.

val lowest: t

The lowest representable number.

val sum [n]: [n]t -> t

Returns zero on empty input.

val product [n]: [n]t -> t

Returns one on empty input.

val maximum [n]: [n]t -> t

Returns `lowest` on empty input.

val minimum [n]: [n]t -> t

Returns `highest` on empty input.

module type integral

An extension of `numeric` that provides facilities that are only meaningful for integral types.

include numeric
val //: t -> t -> t

Like `/`, but rounds towards zero. This only matters when one of the operands is negative. May be more efficient.

val %%: t -> t -> t

Like `%`, but rounds towards zero. This only matters when one of the operands is negative. May be more efficient.

val &: t -> t -> t
val |: t -> t -> t
val ^: t -> t -> t
val !: t -> t
val <<: t -> t -> t
val >>: t -> t -> t
val >>>: t -> t -> t
val num_bits: i32
val get_bit: i32 -> t -> i32
val set_bit: i32 -> t -> i32 -> t
val popc: t -> i32

Count number of one bits.

val mul_hi: (x: t) -> (y: t) -> t

Computes `x * y` and returns the high half of the product of x and y.

val mad_hi: (a: t) -> (b: t) -> (c: t) -> t

Computes `mul_hi a b + c`, but perhaps in a more efficient way, depending on the target platform.

val clz: t -> i32

Count number of zero bits preceding the most significant set bit.

module type size

An extension of `size` that further includes facilities for constructing arrays where the size is provided as a value of the given integral type.

include integral
val iota: t -> *[]t
val replicate v: t -> v -> *[]v
module type real

Numbers that model real numbers to some degree.

include numeric
val from_fraction: i32 -> i32 -> t
val to_i32: t -> i32
val to_i64: t -> i64
val to_f64: t -> f64
val sqrt: t -> t
val exp: t -> t
val sin: t -> t
val cos: t -> t
val tan: t -> t
val asin: t -> t
val acos: t -> t
val atan: t -> t
val sinh: t -> t
val cosh: t -> t
val tanh: t -> t
val asinh: t -> t
val acosh: t -> t
val atanh: t -> t
val atan2: t -> t -> t
val gamma: t -> t
val lgamma: t -> t
val lerp: t -> t -> t -> t

Linear interpolation. The third argument must be in the range `[0,1]` or the results are unspecified.

val log: t -> t

Natural logarithm.

val log2: t -> t

Base-2 logarithm.

val log10: t -> t

Base-10 logarithm.

val ceil: t -> t
val floor: t -> t
val trunc: t -> t
val mad: (a: t) -> (b: t) -> (c: t) -> t

Computes `a*b+c`. Depending on the compiler backend, this may be fused into a single operation that is faster but less accurate. Do not confuse it with `fma`.

val fma: (a: t) -> (b: t) -> (c: t) -> t

Computes `a*b+c`, with `a*b` being rounded with infinite precision. Rounding of intermediate products shall not occur. Edge case behavior is per the IEEE 754-2008 standard.

val round: t -> t

Round to the nearest integer, with alfway cases rounded to the nearest even integer. Note that this differs from `round()` in C, but matches more modern languages.

val isinf: t -> bool
val isnan: t -> bool
val inf: t
val nan: t
val pi: t
val e: t
module type float

An extension of `real` that further gives access to the bitwise representation of the underlying number. It is presumed that this will be some form of IEEE float.

include real
type int_t

An unsigned integer type containing the same number of bits as 't'.

val from_bits: int_t -> t
val to_bits: t -> int_t
val num_bits: i32
val get_bit: i32 -> t -> i32
val set_bit: i32 -> t -> i32 -> t
module bool

Boolean numbers. When converting from a number to `bool`, 0 is considered `false` and any other value is `true`.

module i8
module i16
module i32
module i64
module u8
module u16
module u32
module u64
module f64
module f32