Module Tolk.Dtype
Data types, their constants and their promotion.
A data type names the type of one scalar element. A value with several elements gets them from its shape, and a pointer's address space is a property of the pointer, so neither is part of a data type.
Integer and floating-point literals start weak, as Weak_int and Weak_float: their width is not committed yet, and they take the width of what they are combined with. least_upper combines data types and strong commits a weak one to its default width.
Constants
The type for values. An `Int is a mathematical integer: its data type bounds it only when it is truncated.
The type for constants: a value, or `Invalid, the value of an element that is masked out.
val nan : floatnan is the positive quiet NaN whose payload is zero: the NaN a constant takes when an operation makes one from operands that are not NaNs.
equal_const c0 c1 is true iff c0 and c1 are the same constant: the same constructor with the same payload. Floats are the same when their bits are, so 0.0 and -0.0 differ, and so do NaNs of different bits.
hash_const c is a hash of c, compatible with equal_const.
pp_const formats a constant as a literal: True, False, Invalid, an integer in decimal, and a float in the fewest significant digits that read back as the same float. A float has a .0 if it has no fractional digit, is in exponent notation below 1e-4 and from 1e16 in magnitude (1e-05, 1.5e+16), and is inf, -inf or nan if it is not finite.
Raises Invalid_argument on an integer that Bigint.to_string refuses.
Arithmetic on values
Arithmetic and comparison on values, as on numbers: a `Bool counts as 0 or 1, an operation on two integers gives an exact integer, and one that involves a float gives a float, in which an integer that rounds past the greatest double is the infinity of its sign. The operators compare an integer and a float exactly, without rounding the integer, and a comparison with NaN is false, except <>, which is true.
Address spaces
type addr_space = The type for address spaces: the memory a pointer addresses.
val pp_addr_space : Stdlib.Format.formatter -> addr_space -> unitpp_addr_space formats an address space as AddrSpace.GLOBAL, AddrSpace.LOCAL, AddrSpace.REG or AddrSpace.ALU.
val addr_space_of_string : string -> (addr_space, string) Stdlib.resultaddr_space_of_string s is the address space named s: GLOBAL, LOCAL, REG or ALU, the name pp_addr_space formats after AddrSpace.. The error names s.
Data types
type t = - #
| Void(*The type of no value.
*) - #
| Weak_int(*An integer whose width is not committed yet.
*) - #
| Bool - #
| Int8 - #
| Uint8 - #
| Int16 - #
| Uint16 - #
| Int32 - #
| Uint32 - #
| Int64 - #
| Uint64 - #
| Weak_float(*A float whose width is not committed yet.
*) - #
| Fp8e4m3(*8-bit float with 4 exponent and 3 mantissa bits and no infinities.
*) - #
| Fp8e5m2(*8-bit float with 5 exponent and 2 mantissa bits.
*) - #
| Fp8e4m3fnuz(*8-bit float with 4 exponent and 3 mantissa bits, no infinities, no negative zero and one NaN.
*) - #
| Fp8e5m2fnuz(*8-bit float with 5 exponent and 2 mantissa bits, no infinities, no negative zero and one NaN.
*) - #
| Float16 - #
| Bfloat16(*16-bit float with 8 exponent and 7 mantissa bits.
*) - #
| Float32 - #
| Float64
The type for data types.
itemsize dt is the number of bytes of a dt value, bitsize dt rounded up to a whole byte.
name dt is the short name of dt: "i8" to "i64" for the signed integers, "u8" to "u64" for the unsigned ones, "f16", "bf16", "f32" and "f64", "fp8e4m3", "fp8e5m2", "fp8e4m3fnuz" and "fp8e5m2fnuz" for the 8-bit floats, and "bool", "void", "weakint" and "weakfloat".
fmt dt is the format character of a dt value in a byte string: '?' for Bool, 'b', 'h', 'i' and 'q' for the signed integers, 'B', 'H', 'I' and 'Q' for the unsigned ones, and 'e', 'f' and 'd' for Float16, Float32 and Float64. It is None for the other data types. See storage_fmt.
const dt c is c as a constant of dt. `Invalid is itself. For a float dt it is a `Float, c truncated to dt (truncate), except that a NaN keeps its sign and the payload dt holds, a signalling NaN staying one, in a float of 16 bits or more; an integer is rounded once, from its exact value, as a finite value. For Bool it is `Bool, true iff c is nonzero. Otherwise it is an `Int: a float rounded towards zero, then, for an integer dt of known width, wrapped to it modulo 2bitsize, as its arithmetic does: const Uint8 (`Int (-1)) is `Int 255 and const Int8 (`Int 300) is `Int 44; NaN is 0 of such a dt, and an infinity its least or greatest value. A weak integer keeps the value.
Raises Invalid_argument if c is a NaN or an infinity and dt is Weak_int.
Predicates and groups
is_float dt is true iff dt is in floats or is Weak_float.
Literals
of_const c is the data type of the literal c: Bool for `Bool and `Invalid, Weak_int for `Int, and Weak_float for `Float.
finfo dt is (exponent, mantissa), the number of exponent bits and of explicit mantissa bits of dt.
Raises Invalid_argument if dt is not in floats.
Names and defaults
of_string s is the data type named s, in any case: its name (f32, i8), its constructor's name without underscores (float32, int8, bfloat16, weakint), a C-flavoured alias (half, float, double, char, uchar, short, ushort, int, uint, long, ulong), or default_float and default_int for default_float and default_int. The error names s.
default_float () is the data type named, in any case, by the current value of the setting Setting.default_float, DEFAULT_FLOAT.
Raises Invalid_argument if that is not a float of known width.
default_int () is the data type named, in any case, by the current value of the setting Setting.default_int, DEFAULT_INT.
Raises Invalid_argument if that is not an integer of known width.
strong dt commits dt to a width: Weak_int is default_int (), Weak_float is default_float (), and any other dt is itself.
weak dt is the weak data type of dt's kind: Weak_float for a float, Weak_int for an integer, and dt itself for Bool and Void.
Promotion
The data types other than Void are ordered by promotion: a data type promotes to itself and to those above it. Bool is at the bottom, then Weak_int, then the integers by width, each unsigned integer also promoting to the signed integer of twice its width. The 64-bit integers promote to Weak_float, then come the 8-bit floats, then Float16 and Bfloat16, which each 8-bit float promotes to, then Float32 and Float64.
least_upper dts is the least data type by compare that every data type of dts promotes to. Two different 8-bit floats promote to Float16, the lesser of their two least common bounds, so least_upper is not associative: least_upper [a; b; c] is not always least_upper [least_upper [a; b]; c].
Raises Invalid_argument if dts is empty or holds Void.
least_upper_float dt is the float that dt promotes to: Weak_float for Weak_int, dt itself for a float, and least_upper [dt; default_float ()] otherwise.
Raises Invalid_argument if dt is Void.
can_lossless_cast d0 d1 is true iff a cast from d0 to d1 is known to keep every value, that is iff d0 is d1 or Bool, or:
d1is an integer, weak included, andd0an integer whoseminandmaxlie withind1's;d1is a float of known width andd0an integer ofbbits, less one for a signed one, withbat most one more thand1's mantissa bits (finfo);d1andd0are floats of known width, andd0has no more exponent bits and no more mantissa bits thand1, and fewer of one.
It is false between two floats of the same widths, such as Fp8e4m3 and Fp8e4m3fnuz.
Casts
truncate dt v is v as a value of dt:
- for a float of known width,
vas a float rounded once todt's precision, to nearest with ties to even. A finite value that overflows a 16-bit or wider float is an infinity, and one that overflows an 8-bit float is its greatest finite value of the same sign. An infinity is itself where the format has infinities, and its NaN where it has none. A NaN is quiet, as a conversion makes it: a signalling NaN becomes quiet with the same payload. A 16-bit or wider float keeps its sign and the top of its payload;Fp8e5m2has one NaN of each sign, and the other 8-bit floats one positive NaN. An integer is rounded once, from its exact value, as a finite value: one that rounds past the greatest double is the infinity of its sign inFloat64, and overflows a narrower float. - for an integer of known width,
vwrapped todt's width in two's complement; - for
Bool,trueiffvis nonzero; - for a weak data type,
vitself.
Raises Invalid_argument if dt is Void, or if v is a float and dt an integer of known width.
to_storage_scalar dt v is the value that stores v in storage_fmt: v rounded to Float16, the `Int of the bits that encode v for Bfloat16 and the 8-bit floats, and v itself otherwise. Storage moves a 16-bit NaN's bits and does not quiet it: a NaN keeps its sign and as much of its payload, quiet bit included, as the format holds. An 8-bit NaN is its format's NaN of its sign, the one NaN of an fnuz format.
from_storage_scalar dt s is the value stored by s, the inverse of to_storage_scalar: the `Float that the low bits of s encode for Bfloat16 and the 8-bit floats, and s itself otherwise. A Bfloat16 NaN has the sign and payload of its bits, an Fp8e5m2 NaN the sign of its bits, and the other 8-bit floats' NaN is positive.
Raises Invalid_argument if dt is Bfloat16 or an 8-bit float and s is not an `Int.
bitcast d0 d1 v is the d1 value stored by the bits that store v as a d0 value. Its bits are kept exactly, a signalling NaN's included, so bitcast d1 d0 (bitcast d0 d1 c) = c for every c that d0 stores, but where d1 is an 8-bit float and c's bits are one of its NaN codes, which read as from_storage_scalar reads them.
Raises Invalid_argument if d0 and d1 have different itemsizes, if either has no storage_fmt, if v is a float and d0 an integer, or if v is an integer out of the range of d0's storage.