Return IEEE 754-2008 context for a given bit width.

    The IEEE 754 standard specifies binary interchange formats with bitwidths
    16, 32, 64, 128, and all multiples of 32 greater than 128.  This function
    returns the context corresponding to the interchange format for the given
    bitwidth.

    See section 3.6 of IEEE 754-2008 or the bigfloat source for more details.

def IEEEContext(bitwidth):
    """
    Return IEEE 754-2008 context for a given bit width.

    The IEEE 754 standard specifies binary interchange formats with bitwidths
    16, 32, 64, 128, and all multiples of 32 greater than 128.  This function
    returns the context corresponding to the interchange format for the given
    bitwidth.

    See section 3.6 of IEEE 754-2008 or the bigfloat source for more details.

    """
    try:
        precision = {16: 11, 32: 24, 64: 53, 128: 113}[bitwidth]
    except KeyError:
        if not (bitwidth >= 128 and bitwidth % 32 == 0):
            raise ValueError("nonstandard bitwidth: bitwidth should be "
                             "16, 32, 64, 128, or k*32 for some k >= 4")
        # The formula for the precision involves rounding 4*log2(width) to the
        # nearest integer. We have:
        #
        #   round(4*log2(width)) == round(log2(width**8)/2)
        #                        == floor((log2(width**8) + 1)/2)
        #                        == (width**8).bit_length() // 2
        #
        # (Note that 8*log2(width) can never be an odd integer, so we
        # don't care which way half-way cases round in the 'round'
        # operation.)
        precision = bitwidth - _bit_length(bitwidth ** 8) // 2 + 13

    emax = 1 << bitwidth - precision - 1
    return Context(
        precision=precision,
        emin=4 - emax - precision,
        emax=emax,
        subnormalize=True,
    )