Optimize VL_ONEHOT

Equivalent to `__builtin_popcount(_) == 1` and a few instructions
shorter.
This commit is contained in:
Geza Lore 2026-06-03 11:24:30 +01:00
parent 7b45b3ee8a
commit c878a7e735
1 changed files with 4 additions and 2 deletions

View File

@ -803,10 +803,12 @@ static inline IData VL_COUNTBITS_W(int lbits, int words, WDataInP const lwp, IDa
}
static inline IData VL_ONEHOT_I(IData lhs) VL_PURE {
return (((lhs & (lhs - 1)) == 0) & (lhs != 0));
const IData y = lhs - 1;
return y < (lhs ^ y);
}
static inline IData VL_ONEHOT_Q(QData lhs) VL_PURE {
return (((lhs & (lhs - 1)) == 0) & (lhs != 0));
const QData y = lhs - 1;
return y < (lhs ^ y);
}
static inline IData VL_ONEHOT_W(int words, WDataInP const lwp) VL_PURE {
EData one = 0;