harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit d199daf6edb87676240177d7f0dd574f12d993d4
parent cee199ce90d574664b349d82c7abd8b97e9e4b2c
Author: Thomas Bracht Laumann Jespersen <t@laumann.xyz>
Date:   Wed, 21 Apr 2021 23:48:03 +0200

gen: Make ">>" respect signedness

Make both ">>" and ">>=" respect the signedness of the shift-expression.
The spec needs to be updated (section 6.6.35.3) to reflect this
behaviour.

Fixes https://todo.sr.ht/~sircmpwn/hare/402

Diffstat:
Msrc/qinstr.c | 2+-
Mtests/10-binarithms.ha | 23+++++++++++++++++++++++
2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/qinstr.c b/src/qinstr.c @@ -92,7 +92,7 @@ binarithm_for_op(enum binarithm_operator op, case BIN_LSHIFT: return Q_SHL; case BIN_RSHIFT: - return Q_SHR; + return is_signed ? Q_SAR : Q_SHR; case BIN_LEQUAL: switch (stype) { case Q_WORD: diff --git a/tests/10-binarithms.ha b/tests/10-binarithms.ha @@ -28,7 +28,30 @@ fn andor() void = { assert(x == 42); }; +fn sar_shr() void = { + assert(-12697259629065987i64 >> 26 == -189203913); + + let x = 1i64; + x <<= 63; + assert(x == -9223372036854775808); + x >>= 63; + assert(x == -1); + + let y = 1u64; + y <<= 63; + assert(y == 9223372036854775808); + y >>= 63; + assert(y == 1); + + assert(-4i32 >> 1 == -2); + + let h0 = -12697259629065987i64; + let h1 = (h0 + (1i64 << 25)) >> 26; + assert(h1 == -189203912); +}; + export fn main() void = { // TODO: other binarithms andor(); + sar_shr(); };