| Simulink® Fixed Point™ | ![]() |
| On this page… |
|---|
Prepare for Code Generation Overview Identify blocks that generate expensive saturation and rounding code |
Identify settings that might lead to nonoptimal results in code generation.
All tasks in this folder require a Real-Time Workshop license.
Disables unnecessary signal logging.
Disabling unnecessary signal logging avoids declaring extra signal memory in generated code.
| Conditions | Recommended Action |
|---|---|
| Signals are logged. | Disable signal logging on all signals. |
Clicking Modify All disables signal logging on all logged signals.
This task requires a Real-Time Workshop license.
Check for blocks that generate expensive saturation or rounding code.
Setting the Saturate on integer overflow parameter can produce condition-checking code that your application might not require.
Generated rounding code is inefficient because of Round integer calculations toward parameter setting.
| Conditions | Recommended Action |
|---|---|
| Blocks generate expensive saturation code. | Check each block to ensure that your application requires setting Function Block Parameters > Signal Attributes > Saturate on integer overflow. Otherwise, clear the Saturate on integer overflow parameter to ensure the most efficient implementation of the block in the generated code. |
| Generated code is inefficient. | Set the Function Block Parameters > Round integer calculations toward parameter to the recommended value. |
This task requires a Real-Time Workshop license.
Identify fixed-point operations that can lead to nonoptimal results.
The following operations can lead to nonoptimal results:
Division
The rounding behavior of signed integer division is not fully specified by C language standards. Therefore, the generated code for division is large to ensure bit-true agreement between simulation and code generation.
Integer division generated code contains protection against arithmetic exceptions such as division by zero, INT_MIN/-1, and LONG_MIN/-1. If you construct models making it impossible for exception triggering input combinations to reach a division operation, the protection code generated as part of the division operation is redundant.
The index search method Evenly-spaced points requires a division operation, which can be computationally expensive.
Multiplication
Product blocks are configured to do more than one division operation. Multiplying all the denominator terms together first, and then computing only one division operation improves accuracy and speed in floating-point and fixed-point calculations.
Product blocks are configured to do more than one multiplication or division operation. Using several blocks, with each block performing one multiplication or one division operation, allows you to control the data type and scaling used for intermediate calculations. The choice of data types for intermediate calculations affects precision, range errors, and efficiency.
Blocks that have the Saturate on integer overflow parameter selected, and have an ideal multiplication product with a larger integer size than the target integer size, must determine the ideal product in generated C code. The C code required to do this multiplication is large and slow.
Blocks with relative scaling of inputs and outputs must determine the ideal product in the generated C code. The C code required to do this multiplication is large and slow.
Blocks that multiply signals with nonzero bias require extra steps to implement the multiplication. Inserting Data Type Conversion blocks remove the biases, and allow you to control data type and scaling for intermediate calculations. The conversion is done once and all blocks in the subsystem benefit from simpler, bias-free math.
Blocks are multiplying signals with mismatched slope adjustment factors. This mismatch causes the overall operation to involve two multiply instructions.
the Real-Time Workshop software generates a reciprocal operation followed by a multiply operation for Product blocks that have a divide operation for the first input, and a multiply operation for the second input. If you reverse the inputs so that the multiplication occurs first and the division occurs second, the Real-Time Workshop software generates a single division operation for both inputs.
An input with an invariant constant value is used as the denominator in an online division operation. If the operation is changed to multiplication, and the invariant input is replaced by its reciprocal, then the division is done offline and the online operation is multiplication. This leads to faster and smaller generated code.
Addition
Sum blocks can have a range error when the input range exceeds the output range.
A Sum block has an input with a slope adjustment factor that does not equal the slope adjustment factor of the output. This mismatch requires the Sum block to do one or more multiplication operations.
The net sum of the Sum block input biases does not equal the bias of the output. The generated code includes one extra addition or subtraction instruction to correctly account for the net bias adjustment. For better accuracy and efficiency, nonzero bias terms are collected into a single net bias correction term. The ranges given for the input and output exclude their biases.
Using Relational Operator blocks
The data types of the Relational Operator block inputs are not the same. A conversion operation is required every time the block is executed. If one of the inputs is invariant, then changing the data type and scaling of the invariant input to match the other input improves the efficiency of the model.
The Relational Operator block inputs have different ranges, resulting in a range error when casting, and a precision loss each time a conversion is performed. You can insert Data Type Conversion blocks before the Relational Operator block to convert both inputs to a common data type that has sufficient range and precision to represent each input, making the relational operation error-free.
The inputs of the Relational Operator block have different slope adjustment factors. The mismatch causes the Relational Operator block to require a multiply operation each time the input with lesser positive range is converted to the data type and scaling of the input with greater positive range.
Using MinMax blocks
The input and output of the MinMax block have different data types. A conversion operation is required every time the block is executed. The model is more efficient with the same data types.
The input of the MinMax block is converted to the data type and scaling of the output before performing a relational operation, resulting in a range error when casting, or a precision loss each time a conversion is performed.
The input of the MinMax block has a different slope adjustment factor than the output. This mismatch causes the MinMax block to require a multiply operation each time the input is converted to the data type and scaling of the output.
Discrete-Time Integerator blocks have a complicated initial condition setting. The initial condition for the Discrete-Time Integrator blocks are used to initialize the state and output. As a result, the output equation generates excessive code and an extra global variable is required.
| Conditions | Recommended Action |
|---|---|
| Integer division generated code is large. | Set the Configuration Parameters > Hardware Implementation > Signed integer division rounds to parameter to the recommended value. |
| Protection code generated as part of the division operation is redundant. | Verify that your model cannot cause exceptions in division operations and then remove redundant protection code by setting the Configuration Parameters > Optimization > Remove code that protects against division arithmetic exceptions parameter. |
| Generated code is inefficient. | Set the Function Block Parameters > Round integer calculations toward parameter to the recommended value. |
| Lookup table input data is not evenly spaced. | If the data is nontunable, adjust the table to be evenly spaced. See fixpt_look1_func_approx. |
| Lookup table input data is not evenly spaced when quantized, but it is very close to being evenly spaced. | If the data is nontunable, adjust the table to be evenly spaced. See fixpt_evenspace_cleanup. |
| Lookup table input data is evenly spaced, but the spacing is not a power of 2. | If the data is nontunable, reimplement the table with even power-of-2 spacing. See fixpt_look1_func_approx. |
| Index search method is set to Evenly-spaced points. | Specify a different Function Block Parameters > Index search method to avoid the division operation. |
| Blocks require cumbersome multiplication. | Restrict multiplication operations:
|
| Blocks multiply signals with nonzero bias. | Insert a Data Type Conversion block before and after the block containing the multiplication operation. |
| Product blocks are multiplying signals with mismatched slope adjustment factors. | Change the scaling of the output so that its slope adjustment factor is the product of the input slope adjustment factors. |
| Product blocks are configured to do multiple division operations. | Multiply all the denominator terms together, and then do a single division using cascading Product blocks. |
| Product blocks are configured to do many multiplication or division operations. | Split the operations across several blocks, with each block performing one multiplication or one division operation. |
| Product blocks are configured with a divide operation for the first input and a multiply operation for the second input. | Reverse the inputs so the multiply operation occurs first and the division operation occurs second. |
| An input with an invariant constant value is used as the denominator in an online division operation. | Change the operation to multiplication, and replace the invariant input by its reciprocal. |
| The data type range of the inputs of Sum blocks exceeds the data type range of the output, which can cause overflow or saturation. | Change the output and accumulator data types so the range equals or exceeds all input ranges. For example, if the model has two inputs
The data type range of the output and accumulator must equal or exceed –128 to 255. A int16 (–32768 to 32767) data type meets this condition. |
| A Sum block has an input with a slope adjustment factor that does not equal the slope adjustment factor of the output. | Change the data types so the inputs, outputs, and accumulator have the same slope adjustment factor. |
| The net sum of the Sum block input biases does not equal the bias of the output. | Change the bias of the output scaling, making the net bias adjustment zero. |
| The inputs of the Relational Operator block have different data types. |
|
| The inputs of the Relational Operator block have different slope adjustment factors. | Change the scaling of either input. |
| The input and output of the MinMax block have different data types. | Change the data type of the input or output. |
| The input of the MinMax block has a different slope adjustment factor than the output. | Change the scaling of the input or the output. |
| The initial condition of the Discrete-Time Integrator block is used to initialize both the state and the output. | Set the Function Block Parameters > Use initial condition as initial and reset value for parameter to State only (most efficient). |
This task requires Real-Time Workshop and Simulink Fixed Point licenses.
![]() | Perform Data Typing and Scaling | Function Reference | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |