From: Jacob Lifshay Date: Tue, 18 Jan 2022 04:57:03 +0000 (-0800) Subject: add log2 pseudo-code helper X-Git-Tag: sv_maxu_works-initial~553 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=4386dd8bb2d41db227263a947bface79d5acaf0c;p=openpower-isa.git add log2 pseudo-code helper --- diff --git a/src/openpower/decoder/helpers.py b/src/openpower/decoder/helpers.py index e37d4777..3dba5567 100644 --- a/src/openpower/decoder/helpers.py +++ b/src/openpower/decoder/helpers.py @@ -360,6 +360,15 @@ def bitrev(val, VL): return result +def log2(val): + """return the base-2 logarithm of `val`. Only works for powers of 2.""" + if isinstance(val, SelectableInt): + val = val.value + retval = val.bit_length() - 1 + assert val == 2 ** retval, "value is not a power of 2" + return retval + + # For these tests I tried to find power instructions that would let me # isolate each of these helper operations. So for instance, when I was # testing the MASK() function, I chose rlwinm and rldicl because if I