Integer-arithmetic-operations
Leo integer arithmetic operations.
These are negation (unary), absolute value and wrapped absolute value (unary),
addition, subtraction, multiplication, division, remainder, and exponentiation,
and wrapped versions of those six (all binary ops).
The result has always the same type as the first operand.
The binary operations are currently only allowed
on integers of the same type (there are no automatic conversions),
except that any signed or unsigned integer value
may be raised to the power of any unsigned integer value.
The result type is always the common type,
or the type of the base (i.e. first operand) for exponentiation.
Except for the -wrapped versions of operations,
if the exact mathematical result is not representable in the result type,
it is considered an error in the same way as division by zero.
We defensively return an error indication when these events happen.
For the -wrapped versions of operations,
if the exact mathematical result is not representable in the result type,
then the return value is computed from the mathematical result by taking
the low N bits if positive, or if negative by forming the sign-extended
two's complement bit pattern and taking the low N bits of that.
In either case, the resulting bitvector is then interpreted as an integer
of the result type.
For example 127i8.add_wrapped(127i8) becomes 254, which has the
bit pattern 1111 1110, which represents the returned value -2i8.
for another example, -128i8.add_wrapped(-2i8) becomes -130, which
in two's complement requires 9 bits: 1 0111 1110.
The low 8 bits are 0111 1110, which represents the returned value 126i8.
Subtopics
- Op-int-rem-wrapped
- Leo integer wrapped remainder operation.
- Op-int-div-wrapped
- Leo integer wrapped division operation.
- Op-int-div
- Leo integer division operation.
- Op-int-sub-wrapped
- Leo integer wrapped subtraction operation.
- Op-int-sub
- Leo integer subtraction operation.
- Op-int-mul-wrapped
- Leo integer wrapped multiplication operation.
- Op-int-mul
- Leo integer multiplication operation.
- Op-int-add-wrapped
- Leo integer wrapped addition operation.
- Op-int-add
- Leo integer addition operation.
- Op-int-rem
- Leo integer remainder operation.
- Op-int-pow-wrapped
- Leo integer wrapped exponentiation operation.
- Op-int-pow
- Leo integer exponentiation operation.
- Op-int-abs-wrapped
- Leo integer wrapped absolute value operation.
- Op-int-neg
- Leo integer negation operation.
- Op-int-abs
- Leo integer absolute value operation.