Skip to content

Commit 51284da

Browse files
committed
Use branchless way to get rv_hi/rv_lo result
We can avoid using conditional branch via bitwise operation. The value of `((val & (1 << 11)) << 1)` is exactly 4096 if `(val & (1 << 11))` is true.
1 parent 6615b6c commit 51284da

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

src/riscv.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,12 @@ int rv_extract_bits(int imm, int i_start, int i_end, int d_start, int d_end)
112112

113113
int rv_hi(int val)
114114
{
115-
if ((val & (1 << 11)) != 0)
116-
return val + 4096;
117-
return val;
115+
return val + ((val & (1 << 11)) << 1);
118116
}
119117

120118
int rv_lo(int val)
121119
{
122-
if ((val & (1 << 11)) != 0)
123-
return (val & 0xFFF) - 4096;
124-
return val & 0xFFF;
120+
return (val & 0xFFF) - ((val & (1 << 11)) << 1);
125121
}
126122

127123
int rv_encode_R(rv_op op, rv_reg rd, rv_reg rs1, rv_reg rs2)

0 commit comments

Comments
 (0)