Contenuto principale

Use Simplest Rounding for Efficient Generated Code

The simplest rounding mode attempts to reduce or eliminate the need for extra rounding code in your generated code using a combination of techniques. In nearly all cases, the simplest rounding mode produces the most efficient generated code. This rounding mode is only available in Simulink®.

For a specialized case of division that meets three specific criteria, rounding to floor might be more efficient:

  • Fixed-point/integer signed division

  • Denominator is an invariant constant

  • Denominator is an exact power of two

For this case, set the rounding mode to Floor. In the Configuration Parameters dialog box, select Hardware Implementation. Under Device details, set the option Signed integer division rounds to to the option that describes the rounding behavior of your production target.

Optimize Rounding for Casts

In Simulink, you can use the Data Type Conversion block to cast a signal with one data type to another data type. When the block casts the signal to a data type with a shorter word length than the original data type, precision is lost and rounding occurs. The simplest rounding mode automatically chooses the best rounding for these cases based on these rules:

  • When casting from one integer or fixed-point data type to another, the simplest mode rounds toward floor.

  • When casting from a floating-point data type to an integer or fixed-point data type, the simplest mode rounds toward zero.

Optimize Rounding for High-Level Arithmetic Operations

The simplest rounding mode chooses the best rounding for each high-level arithmetic operation. For example, consider the operation y = u1 × u2 / u3 implemented using a Product block.

A Product block configured to implement the above arithmetic operation.

As stated in the C standard, the most efficient rounding mode for multiplication operations is always floor. However, the C standard does not specify the rounding mode for division in cases where at least one of the operands is negative. Therefore, the most efficient rounding mode for a divide operation with signed data types can be floor or zero, depending on your production target.

The simplest rounding mode:

  • Rounds to floor for all non-division operations.

  • Rounds to zero or floor for division, depending on the setting of the Signed integer division rounds to parameter.

To get the most efficient code, you must set the Signed integer division rounds to model parameter to specify whether your production target rounds to zero or to floor for integer division. Most production targets round to zero for integer division operations. Note that the Simplest rounding mode enables mixed-mode rounding for such cases, as it rounds to floor for multiplication and to zero for division.

If the Signed integer division rounds to model parameter is set to Undefined, the simplest rounding mode might not be able to produce the most efficient code. The simplest mode rounds to zero for division for this case, but it cannot rely on your production target to perform the rounding, because the parameter value is Undefined. Therefore, you need additional rounding code to ensure rounding to zero behavior.

Note

For signed fixed-point division where the denominator is an invariant constant power of 2, the simplest rounding mode does not generate the most efficient code. In this case, set the rounding mode to floor.

Optimize Rounding for Intermediate Arithmetic Operations

For fixed-point arithmetic with nonzero slope and bias, the simplest rounding mode also chooses the best rounding for each intermediate arithmetic operation. For example, consider the operation y = u1 / u2 implemented using a Product block, where u1 and u2 are fixed-point quantities.

A Product block configured to implement the above arithmetic operation.

Each fixed-point quantity is calculated using its slope, bias, and stored integer. Fore more information, see Data Types and Scaling in Digital Hardware. In this example, the high-level divide operation specified by the block results in intermediate addition and multiplication operations:

y=u1u2=S1Q1+B1S2Q2+B2

The simplest rounding mode performs the best rounding for each of these operations, high-level and intermediate, to produce the most efficient code. This process enables mixed-mode rounding, with rounding toward floor as the most common mode for additions, subtractions, and multiplies, and rounding toward zero as the most common mode for divides.

Remember that generating the most efficient code using the simplest rounding mode requires you to set the Signed integer division rounds to model parameter to the option that describes the rounding behavior of your production target.

Note

For signed fixed-point division where the denominator is an invariant constant power of 2, the simplest rounding mode does not generate the most efficient code. In this case, set the rounding mode to floor.

See Also

Topics