diff options
author | Zack Buhman <zack@buhman.org> | 2024-04-05 15:17:39 -1000 |
---|---|---|
committer | Michael Tokarev <mjt@tls.msk.ru> | 2024-04-10 20:32:12 +0300 |
commit | 77e03229ca4eaa3d9cf44ec34ebf887213d11dc7 (patch) | |
tree | cf0107b7cb304358a0adb05536aea573895120d3 /target | |
parent | 7fc1bcb65fb66b59b9cf8c2b1584713ef2db8741 (diff) |
target/sh4: Fix mac.l with saturation enabled
The saturation arithmetic logic in helper_macl is not correct.
I tested and verified this behavior on a SH7091.
Signed-off-by: Zack Buhman <zack@buhman.org>
Message-Id: <20240404162641.27528-2-zack@buhman.org>
[rth: Reformat helper_macl, add a test case.]
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
(cherry picked from commit c97e8977dcacb3fa8362ee28bcee75ceb01fceaa)
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
Diffstat (limited to 'target')
-rw-r--r-- | target/sh4/helper.h | 2 | ||||
-rw-r--r-- | target/sh4/op_helper.c | 23 |
2 files changed, 14 insertions, 11 deletions
diff --git a/target/sh4/helper.h b/target/sh4/helper.h index 8d792f6b55..64056e4a39 100644 --- a/target/sh4/helper.h +++ b/target/sh4/helper.h @@ -11,7 +11,7 @@ DEF_HELPER_3(movcal, void, env, i32, i32) DEF_HELPER_1(discard_movcal_backup, void, env) DEF_HELPER_2(ocbi, void, env, i32) -DEF_HELPER_3(macl, void, env, i32, i32) +DEF_HELPER_3(macl, void, env, s32, s32) DEF_HELPER_3(macw, void, env, i32, i32) DEF_HELPER_2(ld_fpscr, void, env, i32) diff --git a/target/sh4/op_helper.c b/target/sh4/op_helper.c index 54d390fe1f..c96c6008a1 100644 --- a/target/sh4/op_helper.c +++ b/target/sh4/op_helper.c @@ -160,20 +160,23 @@ void helper_ocbi(CPUSH4State *env, uint32_t address) } } -void helper_macl(CPUSH4State *env, uint32_t arg0, uint32_t arg1) +void helper_macl(CPUSH4State *env, int32_t arg0, int32_t arg1) { + const int64_t min = -(1ll << 47); + const int64_t max = (1ll << 47) - 1; + int64_t mul = (int64_t)arg0 * arg1; + int64_t mac = env->mac; int64_t res; - res = ((uint64_t) env->mach << 32) | env->macl; - res += (int64_t) (int32_t) arg0 *(int64_t) (int32_t) arg1; - env->mach = (res >> 32) & 0xffffffff; - env->macl = res & 0xffffffff; - if (env->sr & (1u << SR_S)) { - if (res < 0) - env->mach |= 0xffff0000; - else - env->mach &= 0x00007fff; + if (!(env->sr & (1u << SR_S))) { + res = mac + mul; + } else if (sadd64_overflow(mac, mul, &res)) { + res = mac < 0 ? min : max; + } else { + res = MIN(MAX(res, min), max); } + + env->mac = res; } void helper_macw(CPUSH4State *env, uint32_t arg0, uint32_t arg1) |