summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/01_arithmetic/05_overflow
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-12-13 12:58:18 +0100
committerJo-Philipp Wich <jo@mein.io>2022-01-04 15:53:36 +0100
commitb605dbfcf04f310e08634b52507da7a4155bfce1 (patch)
tree04397dab9be96a5978e08366299671a8aa507267 /tests/custom/01_arithmetic/05_overflow
parent8907ce41a36f8d42097d884550fb3cfbba62e6c5 (diff)
treewide: rework numeric value handling
- Parse integer literals as unsigned numeric values in order to be able to represent the entire unsigned 64bit value range - Stop parsing minus-prefixed integer literals as negative numbers but treat them as separate minus operator followed by a positive integer instead - Only store unsigned numeric constants in bytecode - Rework numeric comparison logic to be able to handle full 64bit unsigned integers - If possible, yield unsigned 64 bit results for additions - Simplify numeric value conversion API - Compile code with -fwrapv for defined signed overflow semantics Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/custom/01_arithmetic/05_overflow')
-rw-r--r--tests/custom/01_arithmetic/05_overflow54
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/custom/01_arithmetic/05_overflow b/tests/custom/01_arithmetic/05_overflow
new file mode 100644
index 0000000..8c2f214
--- /dev/null
+++ b/tests/custom/01_arithmetic/05_overflow
@@ -0,0 +1,54 @@
+For integers, the ucode VM tries to perform unsigned 64bit arithmetic internally
+if both operands are positive or if the result is guaranteed to be positive.
+
+In all other cases, calculations are performed using signed 64bit arithmetic
+with wrap arounds using twos-complement representation.
+
+Due to this, the minimum and maximum representable values depend on the values
+of the involved operands.
+
+-- Testcase --
+Unsigned additions roll over back to zero:
+{{ 18446744073709551615 + 1 }}
+
+Unsigned multiplications roll over back to zero:
+{{ 9223372036854775808 * 2 }}
+
+Signed additions roll over at INT64_MIN/INT64_MAX:
+{{ -9223372036854775808 + -1 }}
+
+Signed multiplications roll over back to INT64_MIN:
+{{ 18446744073709551615 * -1 }}
+
+Multiplicating two negative operands yields an unsigned result.
+{{ -9223372036854775807 * -2 }}
+
+Signed calculations yielding positive results are promoted to unsigned.
+{{ -9223372036854775808 + 9223372036854775808 + -9223372036854775807 * -2 }}
+
+Substractions roll over to INT64_MAX on underflow:
+{{ 0 - 9223372036854775809 }}
+-- End --
+
+-- Expect stdout --
+Unsigned additions roll over back to zero:
+0
+
+Unsigned multiplications roll over back to zero:
+0
+
+Signed additions roll over at INT64_MIN/INT64_MAX:
+9223372036854775807
+
+Signed multiplications roll over back to INT64_MIN:
+-9223372036854775807
+
+Multiplicating two negative operands yields an unsigned result.
+18446744073709551614
+
+Signed calculations yielding positive results are promoted to unsigned.
+18446744073709551614
+
+Substractions roll over to INT64_MAX on underflow:
+9223372036854775807
+-- End --