Main Content

Handle Integer Overflow for Chart Data

When Integer Overflow Can Occur

For some arithmetic operations, a processor may need to take ann-bit fixed-point value and store it inmbits, wheremn. Ifm<n, the reduced range of the value can cause an overflow for an arithmetic operation. Some processors identify this overflow asInforNaN. 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, seeSaturation 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 theSaturate on integer overflowchart property, as described inSpecify 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,ab,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 selectSaturate 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 theSaturate on integer overflowcheck 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 useANSI®Crules 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 toint32occurs for operands with a type ofuint8,uint16,int8, orint16before any arithmetic operations occur.

假设您有以下表达式,wherey,u1,u2, andu3are ofuint8type:

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 thatu1,u2, andu3are equal to 200. Because the saturation limits depend on the intermediate data types and not the operand types, you get the following values:

  • tmpis 400.

  • resultis 200.

  • yis 200.

Impact of Saturation on Error Checks

If you enable the chart propertySaturate on integer overflow并设置配置参数eterWrap on overflowtoerrororwarning, 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.