commit 62c4357d7aba7223c18cf032cc8005d30414ea44 parent 05b599b8e6fbabb1fed2562989af9564f8fbff61 Author: Thomas Bracht Laumann Jespersen <t@laumann.xyz> Date: Tue, 6 Dec 2022 15:34:20 +0100 eval: Fix itrunc for signed integers Fixes: https://todo.sr.ht/~sircmpwn/hare/708 Diffstat:
M | src/eval.c | | | 6 | +++--- |
M | tests/10-binarithms.ha | | | 12 | +++++++++++- |
2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/src/eval.c b/src/eval.c @@ -109,11 +109,11 @@ itrunc(const struct type *type, uintmax_t val) case STORAGE_U64: return (uint64_t)val; case STORAGE_I8: - return (int8_t)((val >> 24) | (val & 0x7F)); + return (int8_t)val; case STORAGE_I16: - return (int16_t)((val >> 16) | (val & 0x7FF)); + return (int16_t)val; case STORAGE_I32: - return (int32_t)((val >> 8) | (val & 0x7FFFF)); + return (int32_t)val; case STORAGE_I64: return (int64_t)val; case STORAGE_INT: diff --git a/tests/10-binarithms.ha b/tests/10-binarithms.ha @@ -94,11 +94,21 @@ def FLOAT: f64 = 6.0 * 7.0; def I8: i8 = 127 * 2; def U8: u8 = 128 * 2; def ALIAS: testmod::enum_alias = 1: testmod::_enum: testmod::enum_alias + 1: testmod::enum_alias; +let a: i8 = 3i8 - (-128i8); +let b: i8 = 3i8 + (-128i8); +def A: i8 = 3i8 - (-128i8); +def B: i8 = 3i8 + (-128i8); +def I32: i32 = 3 - (-2147483648i32); fn eval() void = { assert(FLOAT == 42.0); - assert(I8 == 126); + assert(I8 == -2i8); assert(U8 == 0); + assert(a == -125i8); + assert(b == -125i8); + assert(A == -125i8); + assert(B == -125i8); + assert(I32 == -2147483645i32); assert(ALIAS == 2); };