# GSoC #7: Overflow Modes

**URL:** <https://discourse.myhdl.org/t/gsoc-7-overflow-modes/211>\
**Category:** Synopsis\
**Created:** [August 1, 2017, 1:51pm UTC](https://discourse.myhdl.org/t/gsoc-7-overflow-modes/211 "2017-08-01T13:51:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![qrqiuren](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/qrqiuren/32/58_2.png) [@qrqiuren](https://discourse.myhdl.org/u/qrqiuren)\
**Post date:** [August 1, 2017, 1:51pm UTC](https://discourse.myhdl.org/t/gsoc-7-overflow-modes/211/1 "2017-08-01T13:51:06Z")

</div>

The original blog post is [here](http://qren.logdown.com/posts/2017/08/01/2127496).

* * *

Comparing to round modes I introduced in [the last post](http://discourse.myhdl.org/t/gsoc-6-round-modes-and-their-behavior/209), 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:

```auto
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

```
