Skip to content

Commit

Permalink
Replace ND_SIGN_EX with less branchy version
Browse files Browse the repository at this point in the history
  • Loading branch information
turol committed Aug 6, 2024
1 parent 3c10d54 commit 5a87ef9
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions inc/bdx86_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,15 +347,11 @@ typedef ND_UINT32 ND_REG_SIZE;
// Misc macros.
//

// Sign extend 8 bit to 64 bit.
#define ND_SIGN_EX_8(x) (((x) & 0x00000080) ? (0xFFFFFFFFFFFFFF00 | (x)) : ((x) & 0xFF))
// Sign extend 16 bit to 64 bit.
#define ND_SIGN_EX_16(x) (((x) & 0x00008000) ? (0xFFFFFFFFFFFF0000 | (x)) : ((x) & 0xFFFF))
// Sign extend 32 bit to 64 bit.
#define ND_SIGN_EX_32(x) (((x) & 0x80000000) ? (0xFFFFFFFF00000000 | (x)) : ((x) & 0xFFFFFFFF))
// Wrapper for for ND_SIGN_EX_8/ND_SIGN_EX_16/ND_SIGN_EX_32. Sign extend sz bytes to 64 bits.
#define ND_SIGN_EX(sz, x) ((sz) == 1 ? ND_SIGN_EX_8(x) : (sz) == 2 ? ND_SIGN_EX_16(x) : \
(sz) == 4 ? ND_SIGN_EX_32(x) : (x))
// Sign extend to 64 bit, with minimal branches
#define ND_SIGN_EX(sz, x) ( ( (x) & (0x00000080 << ((sz - 1) * 8)) ) \
? ((~( (1ULL << (sz * 8)) - 1) ) | (x)) \
: ((x) & ( (1ULL << (sz * 8)) - 1) ) )

// Trim 64 bits to sz bytes.
#define ND_TRIM(sz, x) ((sz) == 1 ? (x) & 0xFF : (sz) == 2 ? (x) & 0xFFFF : \
(sz) == 4 ? (x) & 0xFFFFFFFF : (x))
Expand Down

0 comments on commit 5a87ef9

Please sign in to comment.