Main Content

Optimize Generated Code Using memset Function

This example shows how to optimize the generated code by using the memset function to clear the internal storage. When you select the model configuration parameter Use memset to initialize floats and doubles to 0.0, the memset function clears internal storage, to the integer bit pattern 0 (that is, all bits are off).

If your compiler and target CPU both represent floating-point zero with the integer bit pattern 0, consider setting this parameter to gain execution and ROM efficiency.

NOTE: The command-line values are the reverse of the settings values. 'on' in the command line corresponds to clearing the setting. 'off' in the command line corresponds to selecting the setting.

This optimization:

  • Reduces ROM consumption.

  • Improves execution speed.

Example Model

Consider the model MemsetOptimization .

model = 'MemsetOptimization';
open_system(model);

Generate Code

The code generator uses a loop to initialize the Constant block values. Build the model.

slbuild(model)
### Starting build procedure for: MemsetOptimization
### Successful completion of build procedure for: MemsetOptimization

Build Summary

Top model targets built:

Model               Action                        Rebuild Reason                                    
====================================================================================================
MemsetOptimization  Code generated and compiled.  Code generation information file does not exist.  

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

View the generated code without the optimization. These lines of code are in MemsetOptimization.c.

cfile = fullfile('MemsetOptimization_grt_rtw','MemsetOptimization.c');
coder.example.extractLines(cfile,'/* Model initialize function */',...
    '/* Model terminate function */',1,0);
/* Model initialize function */
void MemsetOptimization_initialize(void)
{
  /* Registration code */

  /* initialize error status */
  rtmSetErrorStatus(MemsetOptimization_M, (NULL));

  /* external outputs */
  {
    int32_T i;
    for (i = 0; i < 50; i++) {
      MemsetOptimization_Y.Out1[i] = 0.0;
    }
  }

  {
    int32_T i;

    /* ConstCode for Outport: '<Root>/Out1' */
    for (i = 0; i < 50; i++) {
      MemsetOptimization_Y.Out1[i] = 56.0;
    }

    /* End of ConstCode for Outport: '<Root>/Out1' */
  }
}

Enable Optimization

  1. Open the Configuration Parameters dialog box.

  2. In the Configuration Parameter dialog box select the Use memset to initialize floats and doubles to 0.0 parameter. Alternatively, you can use the command-line API to enable the optimization:

set_param(model,'InitFltsAndDblsToZero','off');

Generate Code with Optimization

The code generator uses the memset function to initialize the Constant block values.

Build the model.

slbuild(model)
### Starting build procedure for: MemsetOptimization
### Successful completion of build procedure for: MemsetOptimization

Build Summary

Top model targets built:

Model               Action                        Rebuild Reason                   
===================================================================================
MemsetOptimization  Code generated and compiled.  Generated code was out of date.  

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

View the generated code with the optimization. These lines of code are in MemsetOptimization.c.

coder.example.extractLines(cfile,'/* Model initialize function */',...
    '/* Model terminate function */',1,0);
/* Model initialize function */
void MemsetOptimization_initialize(void)
{
  /* Registration code */

  /* initialize error status */
  rtmSetErrorStatus(MemsetOptimization_M, (NULL));

  /* external outputs */
  (void)memset(&MemsetOptimization_Y, 0, sizeof(ExtY_MemsetOptimization_T));

  {
    int32_T i;

    /* ConstCode for Outport: '<Root>/Out1' */
    for (i = 0; i < 50; i++) {
      MemsetOptimization_Y.Out1[i] = 56.0;
    }

    /* End of ConstCode for Outport: '<Root>/Out1' */
  }
}

See Also

Related Topics