def exact_log2(n):
    """Returns the integer log2 of ``n``, which must be an exact power of two.

    Raises a ``ValueError`` if ``n`` is not a power of two.
    """
    n = operator.index(n)
    if n <= 0 or (n & (n - 1)):
        raise ValueError(f"{n} is not a power of 2")
    return (n - 1).bit_length()
Returns the integer log2 of ``n``, which must be an exact power of two.

    Raises a ``ValueError`` if ``n`` is not a power of two.
    
amaranth/utils.py