Fixed-Point Optimizations Using Specified Minimum and Maximum Values
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.
Benefits of optimizing the generated code include:
Reducing the ROM and RAM consumption.
Improving the execution speed.
Note: You must ensure that the specified minimum and maximum values are accurate and trustworthy. Otherwise, optimization might result in numerical mismatch with simulation.
Open and Inspect Model
Open the fxpdemo_min_max_optimization
model.
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. To view the optimization configuration, open the Configuration Parameters dialog and select Code Generation > Optimization.
Generate and Inspect Code Without Optimization
Generate code for the model without using the specified minimum and maximum values.
slbuild('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: Model Build Reason Status Build Duration ================================================================================================================== fxpdemo_min_max_optimization Information cache folder or artifacts were missing. Code generated. 0h 0m 11.296s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 12.021s
The 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 C/C++ Code > Navigate to C/C++ 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.
Enable Optimization
Open the Configuration Parameters dialog.
Under Code Generation > Optimization, enable the option Optimize using the specified minimum and maximum values.
set_param('fxpdemo_min_max_optimization','UseSpecifiedMinMax','on');
Generate and Inspect Code with Optimization
Regenerate the code using the specified minimum and maximum values.
slbuild('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: Model Build Reason Status Build Duration ============================================================================================== fxpdemo_min_max_optimization Generated code was out of date. Code generated. 0h 0m 9.6209s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 10.558s
Right-click on block Product with reduced fraction length output data type and select C/C++ Code > Navigate to C/C++ 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
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.