GSoC #7: Overflow Modes

The original blog post is here.


Comparing to round modes I introduced in the last post, overflow modes are rather simple.

There are only 3 overflow modes in cfelton’s _resize.py: saturate, ring, and wrap.

saturate means that if overflow occurs, the value will remain maximum (minimum if underflow).

ring and wrap behaves the same. If overflow occurs, it then builds up from minimum. Similarly, if underflow occurs, it goes down from maximum. For binary implementation, it is just reserve the last bits in its width.

We can see clearly in the original implementation:

def _overflow(val, fmt, overflow_mode):
    """handle overflow"""

    assert is_overflow_mode(overflow_mode)
    wl,iwl,fwl = fmt
    mm = 2**(wl-1)
    mmin,mmax = -mm,mm
    #print("    [rsz][ovl]: %f %d %d, %s" % (val, mmin, mmax, fmt))

    if overflow_mode == 'saturate':
        if val >= mmax:
            retval = mmax-1
        elif val <= mmin:
            retval = mmin
        else:
            retval = val
    elif overflow_mode == 'ring' or overflow_mode == 'wrap':
        retval = (val - mmin) % (mmax - mmin) + mmin
    else:
        raise ValueError

    return retval