ptitSeb / box64

Box64 - Linux Userspace x86_64 Emulator with a twist, targeted at ARM64 Linux devices
https://box86.org
MIT License
3.73k stars 267 forks source link

Mask `rs2` when using `bext` instruction if `rex.w` is not set #1653

Closed Coekjan closed 2 months ago

Coekjan commented 2 months ago

Fixes #1652 .

ptitSeb commented 2 months ago

Mmmm, is that a fix or a workaround?

doesn't BEXT works on 32bits input?

Coekjan commented 2 months ago

I think this is a fix and not a workaround.

bext on rv64 does not handle 32bits input. This pseudo code describes how it works:

let index = X(rs2) & (XLEN - 1);
X(rd) = (X(rs1) >> index) & 1;

In the first step, X(rs2) & (XLEN - 1) is actually X(rs2) & (64 - 1) in rv64. When rex.w is not set, what we need to do is X(rs2) & (32 - 1) instead.

ptitSeb commented 2 months ago

Then why not using the 's0' scratch reg to do the mask and still use BEXT, that would still be 1 opcode less then the fallback?

Coekjan commented 2 months ago

Then why not using the 's0' scratch reg to do the mask and still use BEXT, that would still be 1 opcode less then the fallback?

This can be better. I will push a new version and then please review the changes again. Thanks!