Mavryk_raw_protocol_001_PtAtLas.Saturation_reprThis module provides saturated arithmetic between 0 and 2^62 - 1.
This means that the arithmetic operations provided by this module do not overflow. If an operation would produce an integer x greater than 2 ^ 62 - 1, it is saturated to this value. Similarly, if an operation would produce a negative integer, it outputs zero instead.
This saturation arithmetic is used to monitor gas levels. While the gas model can produce values beyond 2^62 - 1, there is no point in distinguishing these values from 2^62 - 1 because the amount of gas available is significantly lower than this limit.
Notice that most saturation arithmetic operations do not behave as their standard counterparts when one of their operands is saturated. For instance,
(saturated + saturated) - saturated = 0
For more information about saturation arithmetic, take a look at:
https://en.wikipedia.org/wiki/Saturation_arithmetic
An integer of type 'a t is between 0 and saturated.
The type parameter 'a is mul_safe if the integer is known not to overflow when multiplied with another mul_safe t.
The type parameter 'a is may_saturate if the integer is not known to be sufficiently small to prevent overflow during multiplication.
val may_saturate : _ t -> may_saturate tval to_int : 'a t -> intto_int x returns the underlying integer representing x.
val zero : _ t0
val one : _ t1
val saturated : may_saturate t2^62 - 1
val (>!) : _ t -> int -> boola >! b is a > b. Avoids using to_int.
val numbits : 'a t -> intnumbits x returns the number of bits used in the binary representation of x.
shift_right x y behaves like a logical shift of x by y bits to the right. y must be between 0 and 63.
shift_left x y behaves like a logical shift of x by y bits to the left. y must be between 0 and 63. In cases where x lsl y is overflowing, shift_left x y is saturated.
val mul : _ t -> _ t -> may_saturate tmul x y behaves like multiplication between native integers as long as its result stay below saturated. Otherwise, mul returns saturated.
mul_safe x returns a mul_safe t only if x does not trigger overflows when multiplied with another mul_safe t. More precisely, x is safe for fast multiplications if x < 2147483648.
mul_fast x y exploits the fact that x and y are known not to provoke overflows during multiplication to perform a mere multiplication.
val scale_fast : mul_safe t -> _ t -> may_saturate tscale_fast x y exploits the fact that x is known not to provoke overflows during multiplication to perform a multiplication faster than mul.
val add : _ t -> _ t -> may_saturate tadd x y behaves like addition between native integers as long as its result stay below saturated. Otherwise, add returns saturated.
val succ : _ t -> may_saturate tsucc x is like add one x
sub x y behaves like subtraction between native integers as long as its result stay positive. Otherwise, sub returns zero. This function assumes that x is not saturated.
sub_opt x y behaves like subtraction between native integers as long as its result stay positive. Otherwise, sub returns None.
ediv x y returns x / y. This operation never saturates, hence it is exactly the same as its native counterpart. y is supposed to be strictly greater than 0, otherwise this function raises Division_by_zero.
erem x y returns x mod y. y is supposed to be strictly greater than 0, otherwise this function raises Division_by_zero.
val of_int_opt : int -> may_saturate t optionof_int_opt x returns Some x if x >= 0 and x < saturated, and None otherwise.
val of_z_opt :
Mavryk_protocol_environment_001_PtAtLas.Z.t ->
may_saturate t optionof_z_opt x returns Some x if x >= 0 and x < saturated, and None otherwise.
val mul_safe_exn : may_saturate t -> mul_safe tWhen a saturated integer is sufficiently small (i.e. strictly less than 2147483648), we can assign it the type mul_safe S.t to use it within fast multiplications, named S.scale_fast and S.mul_fast.
The following function allows such type assignment but may raise an exception if the assumption is wrong. Therefore, mul_safe_exn should only be used to define toplevel values, so that these exceptions can only occur during startup.
mul_safe_of_int_exn x is the composition of of_int_opt and mul_safe in the option monad. This function raises Invalid_argument if x is not safe. This function should be used on integer literals that are obviously mul_safe.
val safe_z : Mavryk_protocol_environment_001_PtAtLas.Z.t -> may_saturate tsafe_z z is of_z_opt x |> saturate_if_undef.
val safe_int : int -> may_saturate tsafe_int x is of_int_opt x |> saturate_if_undef.
val to_z : _ t -> Mavryk_protocol_environment_001_PtAtLas.Z.tto_z z is Z.of_int.
val z_encoding : _ t Mavryk_protocol_environment_001_PtAtLas.Data_encoding.tEncoding for t through the encoding for z integers.
val n_encoding : _ t Mavryk_protocol_environment_001_PtAtLas.Data_encoding.tEncoding for t through the encoding for non-negative integers.
val pp :
Mavryk_protocol_environment_001_PtAtLas.Format.formatter ->
_ t ->
unitA pretty-printer for native integers.
module Syntax : sig ... endSyntax for simple representations.