Main Content

Optimize Generated Code for Fixed-Point Data Operations

This example shows how the code generator optimizes fixed-point operations by replacing expensive division operations with highly efficient product operations. This optimization improves execution speed.

Example Model

In the model FixedPointOperations, two fixed point signals connect to a Divide block. The Number of inputs parameter has the value /*.


Generate Code

Build the model.

### Starting build procedure for: FixedPointOperations
### Successful completion of code generation for: FixedPointOperations

Build Summary

Top model targets built:

Model                 Action           Rebuild Reason                                    
FixedPointOperations  Code generated.  Code generation information file does not exist.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 46.347s

View the generated code. Here is a portion of FixedPointOperations.c.

cfile = fullfile('FixedPointOperations_ert_rtw','FixedPointOperations.c');
coder.example.extractLines(cfile,'/* Model step','/* Model initialize', 1, 0);
/* Model step function */
void FixedPointOperations_step(void)
  /* Outport: '<Root>/Out1' incorporates:
   *  Inport: '<Root>/In1'
   *  Product: '<Root>/Divide'
  rtY.Out1 = (int16_T)(rtU.In1 >> 3);

The generated code contains a highly efficient right shift operation instead of an expensive division operation. The generated code also contains the precomputed value for the constant input to the Product block.

Note that the resulting operation also includes the adjustment in signal scaling from 2^-3 to 2^-5.

Related Topics