From c878a7e73523154a4f7da52bde9633d05b715c56 Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Wed, 3 Jun 2026 11:24:30 +0100 Subject: [PATCH] Optimize VL_ONEHOT Equivalent to `__builtin_popcount(_) == 1` and a few instructions shorter. --- include/verilated_funcs.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/verilated_funcs.h b/include/verilated_funcs.h index 5a1e2b442..db4c39e5c 100644 --- a/include/verilated_funcs.h +++ b/include/verilated_funcs.h @@ -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;