From 0b4e7d60f9b5ca7068a6e77afaa0517902e54fa5 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 6 Aug 2023 21:33:00 -0700 Subject: [PATCH] riscv: Implement ReverseBits in jit. --- Core/MIPS/RiscV/RiscVCompALU.cpp | 36 +++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/Core/MIPS/RiscV/RiscVCompALU.cpp b/Core/MIPS/RiscV/RiscVCompALU.cpp index e2f28683dc..c1939e9867 100644 --- a/Core/MIPS/RiscV/RiscVCompALU.cpp +++ b/Core/MIPS/RiscV/RiscVCompALU.cpp @@ -232,7 +232,41 @@ void RiscVJitBackend::CompIR_Bits(IRInst inst) { switch (inst.op) { case IROp::ReverseBits: - CompIR_Generic(inst); + if (cpu_info.RiscV_Zbb) { + gpr.MapDirtyIn(inst.dest, inst.src1); + // Start by reversing bytes (note: this puts in upper 32 of XLEN.) + REV8(gpr.R(inst.dest), gpr.R(inst.src1)); + + // Swap nibbles. + LI(SCRATCH1, (s32)0xF0F0F0F0); + SRLI(SCRATCH2, gpr.R(inst.dest), XLEN - 32 - 4); + AND(SCRATCH2, SCRATCH2, SCRATCH1); + if (XLEN >= 64) + SRLI(gpr.R(inst.dest), gpr.R(inst.dest), XLEN - 28); + else + SLLI(gpr.R(inst.dest), gpr.R(inst.dest), 4); + SRLIW(SCRATCH1, SCRATCH1, 4); + AND(gpr.R(inst.dest), gpr.R(inst.dest), SCRATCH1); + OR(gpr.R(inst.dest), gpr.R(inst.dest), SCRATCH2); + + // Now the consecutive pairs. + LI(SCRATCH1, (s32)0x33333333); + SRLI(SCRATCH2, gpr.R(inst.dest), 2); + AND(SCRATCH2, SCRATCH2, SCRATCH1); + AND(gpr.R(inst.dest), gpr.R(inst.dest), SCRATCH1); + SLLIW(gpr.R(inst.dest), gpr.R(inst.dest), 2); + OR(gpr.R(inst.dest), gpr.R(inst.dest), SCRATCH2); + + // And finally the even and odd bits. + LI(SCRATCH1, (s32)0x55555555); + SRLI(SCRATCH2, gpr.R(inst.dest), 1); + AND(SCRATCH2, SCRATCH2, SCRATCH1); + AND(gpr.R(inst.dest), gpr.R(inst.dest), SCRATCH1); + SLLIW(gpr.R(inst.dest), gpr.R(inst.dest), 1); + OR(gpr.R(inst.dest), gpr.R(inst.dest), SCRATCH2); + } else { + CompIR_Generic(inst); + } break; case IROp::BSwap16: