## Handle Integer Overflow for Chart Data

### When Integer Overflow Can Occur

For some arithmetic operations, a processor may need to take an
* n*-bit fixed-point value and store it in

*bits, where*

`m`

*≠*

`m`

*. If*

`n`

*<*

`m`

*, the reduced range of the value can cause an overflow for an arithmetic operation. Some processors identify this overflow as*

`n`

`Inf`

or `NaN`

. Other processors, especially digital signal
processors (DSPs), handle overflows by saturating or wrapping the value.For more information about saturation and wrapping for integer overflow, see Saturation and Wrapping (Fixed-Point Designer).

### Support for Handling Integer Overflow in Charts

For Stateflow^{®} charts in Simulink^{®} models, you can control whether or not saturation occurs for integer overflow. To
control overflow handling, set the **Saturate on integer overflow** chart
property, as described in Specify Properties for Stateflow Charts.

Chart Property Setting | When to Use This Setting | Overflow Handling | Example of the Result |
---|---|---|---|

Selected | Overflow is possible for data in your chart and you want explicit saturation protection in the generated code. | Overflows saturate to either the minimum or maximum value that the data type can represent. | An overflow associated with a signed 8-bit integer saturates to –128 or +127 in the generated code. |

Cleared | You want to optimize efficiency of the generated code. | The handling of overflows depends on the C compiler that you use for generating code. | The number 130 does not fit in a signed 8-bit integer and wraps to –126 in the generated code. |

Arithmetic operations for which you can enable saturation protection are:

Unary minus: –

`a`

Binary operations:

`a + b`

,`a`

–`b`

,`a * b`

,`a / b`

,`a ^ b`

Assignment operations:

`a += b`

,`a`

–`=b`

,`a *= b`

,`a /= b`

In C charts, increment and decrement operations:

`++`

,`--`

When you select **Saturate on integer overflow**, be aware that:

Saturation applies to all intermediate operations, not just the output or final result.

The code generator can detect some cases when overflow is not possible. In these cases, the generated code does not include saturation protection.

To determine whether clearing the **Saturate on integer overflow** check
box is a safe option, perform a careful analysis of your logic, including simulation if
necessary. If saturation is necessary in only some sections of the logic, encapsulate that logic
in atomic subcharts or MATLAB functions and define a different set of saturation settings for
those units.

### Effect of Integer Promotion Rules on Saturation

Charts use ANSI^{®} C rules for integer promotion.

All arithmetic operations use a data type that has the same word length as the target word size. Therefore, the intermediate data type in a chained arithmetic operation can be different from the data type of the operands or the final result.

For operands with integer types smaller than the target word size, promotion to a larger type of the same word length as the target size occurs. This implicit cast occurs before any arithmetic operations take place.

For example, when the target word size is 32 bits, an implicit cast to

`int32`

occurs for operands with a type of`uint8`

,`uint16`

,`int8`

, or`int16`

before any arithmetic operations occur.

Suppose that you have the following expression, where `y`

,
`u1`

, `u2`

, and `u3`

are of
`uint8`

type:

y = (u1 + u2) - u3;

Based on integer promotion rules, that expression is equivalent to the following statements:

uint8_T u1, u2, u3, y; int32_T tmp, result; tmp = (int32_T) u1 + (int32_T) u2; result = tmp - (int32_T) u3; y = (uint8_T) result;

For each calculation, the following data types and saturation limits apply.

Calculation | Data Type | Saturation Limits |
---|---|---|

`tmp` | `int32` | `(MIN_INT32, MAX_INT32)` |

`result` | `int32` | `(MIN_INT32, MAX_INT32)` |

`y` | `uint8` | `(MIN_UINT8, MAX_UINT8)` |

Suppose that `u1`

, `u2`

, and `u3`

are
equal to 200. Because the saturation limits depend on the intermediate data types and not the
operand types, you get the following values:

`tmp`

is 400.`result`

is 200.`y`

is 200.

### Impact of Saturation on Error Checks

If you enable the chart property **Saturate on integer overflow** and set
the configuration parameter **Wrap on overflow** to `error`

or `warning`

, the Stateflow chart does not flag cases of integer overflow during simulation. However, the
chart continues to flag division-by-zero operations and out-of-range data violations based on
minimum and maximum range checks.