This example shows how to optimize fixed-point operations in generated code using minimum and maximum values that you specify in a model.
Minimum and maximum values can represent environmental limits or mechanical limits, such as the output ranges of sensors. The code generation software can use these values to create more efficient code by eliminating unreachable code branches and unnecessary utility functions.
Note: You must ensure that the specified minimum and maximum values are accurate and trustworthy. Otherwise, optimization might result in numerical mismatch with simulation.
The benefits of optimizing the generated code are:
Reducing the ROM and RAM consumption.
Improving the execution speed.
Open the model fxpdemo_min_max_optimization.
open_system('fxpdemo_min_max_optimization');
In this model, there are minimum and maximum values specified at the input ports upstream of the various fixed-point blocks. By utilizing these values, every fixed-point operation in the model is optimized in some way.
First, without using the specified minimum and maximum values, generate code for this model. Double-click the blue button.
rtwbuild('fxpdemo_min_max_optimization');
### Starting build procedure for: fxpdemo_min_max_optimization ### Successful completion of code generation for: fxpdemo_min_max_optimization Build Summary Top model targets built: Model Action Rebuild Reason ================================================================================================ fxpdemo_min_max_optimization Code generated Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 27.819s
An HTML Code Generation Report opens. Examine the code corresponding to the block "Product with reduced fraction length output data type". Right-click on this block and select Code Generation > Navigate to code....
rtwtrace('fxpdemo_min_max_optimization/Product with reduced fraction length output data type');
The generated code is:
rtY.Out1 = mul_u32_u32_u32_sr10(rtU.In1, rtU.In2);
To implement this fixed-point multiplication operation, the code generation software must generate a utility function mul_u32_u32_u32_sr10
. Also, to implement mul_u32_u32_u32_sr10
, it must generate a second utility function, mul_wide_u32
. These functions consist of many lines of code and require several temporary variables.
Double-click the yellow button to open the Configuration Parameters dialog box.
In this dialog box, under Code generation, select Optimize using specified minimum and maximum values.
set_param('fxpdemo_min_max_optimization', 'UseSpecifiedMinMax', 'on');
Now regenerate the code using the specified minimum and maximum values.
Double-click the blue button.
rtwbuild('fxpdemo_min_max_optimization');
### Starting build procedure for: fxpdemo_min_max_optimization ### Successful completion of code generation for: fxpdemo_min_max_optimization Build Summary Top model targets built: Model Action Rebuild Reason =============================================================================== fxpdemo_min_max_optimization Code generated Generated code was out of date. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 12.106s
Right-click on block "Product with reduced fraction length output data type" and select Code Generation > Navigate to code....
rtwtrace('fxpdemo_min_max_optimization/Product with reduced fraction length output data type');
The generated code is:
rtY.Out1 = rtU.In1 * rtU.In2 >> 10;
Using the specified minimum and maximum values, the code generation software determines that it can safely implement the reduced fraction length at the output with a right shift, and does not generate utility functions.
Examine other operations in the generated code to see how the code generation software uses the specified minimum and maximum values. The code generation software now implements each fixed-point operation with simple C operations and eliminates unnecessary helper functions and code branches.