armemu: Implement USAD8 and USADA8

This commit is contained in:
Lioncash 2014-12-14 19:01:39 -05:00
parent af1cd769e7
commit 07471d111a

View file

@ -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: