Skip to content

Commit 6c21f11

Browse files
asiekierkaJaklyy
andauthored
Use intrinsics to optimize bit counting operations (#2531)
Co-authored-by: Jaklyy <102590697+Jaklyy@users.noreply.github.com>
1 parent 8fa251f commit 6c21f11

File tree

1 file changed

+3
-20
lines changed

1 file changed

+3
-20
lines changed

src/ARMInterpreter_LoadStore.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,7 @@ void A_LDM(ARM* cpu)
400400

401401
if (!(cpu->CurInstr & (1<<23)))
402402
{
403-
for (int i = 0; i < 16; i++)
404-
{
405-
if (cpu->CurInstr & (1<<i))
406-
base -= 4;
407-
}
403+
base -= 4 * __builtin_popcount(cpu->CurInstr & 0xFFFF);
408404

409405
if (cpu->CurInstr & (1<<21))
410406
{
@@ -480,11 +476,7 @@ void A_STM(ARM* cpu)
480476

481477
if (!(cpu->CurInstr & (1<<23)))
482478
{
483-
for (u32 i = 0; i < 16; i++)
484-
{
485-
if (cpu->CurInstr & (1<<i))
486-
base -= 4;
487-
}
479+
base -= 4 * __builtin_popcount(cpu->CurInstr & 0xFFFF);
488480

489481
if (cpu->CurInstr & (1<<21))
490482
cpu->R[baseid] = base;
@@ -701,18 +693,9 @@ void T_LDR_SPREL(ARM* cpu)
701693

702694
void T_PUSH(ARM* cpu)
703695
{
704-
int nregs = 0;
696+
int nregs = __builtin_popcount(cpu->CurInstr & 0x1FF);
705697
bool first = true;
706698

707-
for (int i = 0; i < 8; i++)
708-
{
709-
if (cpu->CurInstr & (1<<i))
710-
nregs++;
711-
}
712-
713-
if (cpu->CurInstr & (1<<8))
714-
nregs++;
715-
716699
u32 base = cpu->R[13];
717700
base -= (nregs<<2);
718701
cpu->R[13] = base;

0 commit comments

Comments
 (0)