package tezos-protocol-alpha
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=c6df840ebbf115e454db949028c595bec558a59a66cade73b52a6d099d6fa4d4
sha512=d8aee903b9fe130d73176bc8ec38b78c9ff65317da3cb4f3415f09af0c625b4384e7498201fdb61aa39086a7d5d409d0ab3423f9bc3ab989a680cf444a79bc13
doc/tezos_raw_protocol_alpha/Tezos_raw_protocol_alpha/Saturation_repr/index.html
Module Tezos_raw_protocol_alpha.Saturation_repr
Source
This 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.
2^62 - 1
numbits 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
.
mul 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.
scale_fast x y
exploits the fact that x
is known not to provoke overflows during multiplication to perform a multiplication faster than mul
.
add x y
behaves like addition between native integers as long as its result stay below saturated
. Otherwise, add
returns saturated
.
succ 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
.
of_int_opt x
returns Some x
if x >= 0
and x < saturated
, and None
otherwise.
of_z_opt x
returns Some x
if x >= 0
and x < saturated
, and None
otherwise.
When 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
.
safe_z z
is of_z_opt x |> saturate_if_undef
.
safe_int x
is of_int_opt x |> saturate_if_undef
.
to_z z
is Z.of_int
.
Encoding for t
through the encoding for z
integers.
Encoding for t
through the encoding for non-negative integers.
A pretty-printer for native integers.