From 07471d111aada1bbdb10a10081b076e1b47beb50 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 14 Dec 2014 19:01:39 -0500 Subject: [PATCH] armemu: Implement USAD8 and USADA8 --- src/core/arm/interpreter/armemu.cpp | 33 +++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/core/arm/interpreter/armemu.cpp b/src/core/arm/interpreter/armemu.cpp index 825955ade..cdc945036 100644 --- a/src/core/arm/interpreter/armemu.cpp +++ b/src/core/arm/interpreter/armemu.cpp @@ -5888,8 +5888,37 @@ L_stm_s_takeabort: case 0x75: printf ("Unhandled v6 insn: smmla/smmls/smmul\n"); break; - case 0x78: - printf ("Unhandled v6 insn: usad/usada8\n"); + case 0x78: // USAD8 and USADA8 + { + const u8 rm_idx = BITS(8, 11); + const u8 rn_idx = BITS(0, 3); + const u8 rd_idx = BITS(16, 19); + + const u32 rm_val = state->Reg[rm_idx]; + const u32 rn_val = state->Reg[rn_idx]; + + // Equivalent of https://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs for 8-bit. + auto branchlessAbs = [&](u8 shift) { + const int diff = ((rn_val >> shift) & 0xFF) - ((rm_val >> shift) & 0xFF); + const int mask = diff >> (CHAR_BIT - 1); + + return (diff + mask) ^ mask; + }; + + u32 finalDif = 0; + finalDif += branchlessAbs(0); + finalDif += branchlessAbs(8); + finalDif += branchlessAbs(16); + finalDif += branchlessAbs(24); + + // Op is USADA8 if true. + const u8 ra_idx = BITS(12, 15); + if (ra_idx != 15) + finalDif += state->Reg[ra_idx]; + + state->Reg[rd_idx] = finalDif; + return 1; + } break; #if 0 case 0x7a: