主要内容

Use memset to initialize floats and doubles to 0.0

Control generation of code that explicitly initializes floating-point data to 0.0

Model Configuration Pane: Code Generation / Optimization

Description

The Use memset to initialize floats and doubles to 0.0 parameter specifies whether to generate code that explicitly initializes floating-point data to 0.0.

Settings

On (GUI), 'off' (command line) (default) | off (GUI), 'on' (command line)
On

Uses memset to clear internal storage for floating-point data to integer bit pattern 0 (all bits 0), regardless of type. If your compiler and target CPU both represent floating-point zero with the integer bit pattern 0, use this parameter to gain execution and ROM efficiency.

This parameter requires that you turn on the configuration parameter Memcpy threshold to enable the memset functionality. Check that the value of the threshold is set high enough.

Off

Generates code to explicitly initialize storage for data of types float and double to 0.0. The resulting code is slightly less efficient than code generated when you select the option.

Note

The command-line values are reverse of the settings values. Therefore, 'on' in the command line corresponds to the description of “Off” in the settings section, and 'off' in the command line corresponds to the description of “On” in the settings section.

Examples

expand all

This example shows how to optimize the generated code by using the memset function to clear the internal storage.

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)
### Searching for referenced models in model 'MemsetOptimization'.
### Total of 1 models to build.
### Starting build procedure for: MemsetOptimization
### Successful completion of build procedure for: MemsetOptimization

Build Summary

Top model targets:

Model               Build Reason                                         Status                        Build Duration
=====================================================================================================================
MemsetOptimization  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 6.8691s

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

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)
### Searching for referenced models in model 'MemsetOptimization'.
### Total of 1 models to build.
### Starting build procedure for: MemsetOptimization
### Successful completion of build procedure for: MemsetOptimization

Build Summary

Top model targets:

Model               Build Reason                     Status                        Build Duration
=================================================================================================
MemsetOptimization  Generated code was out of date.  Code generated and compiled.  0h 0m 3.8318s

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

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' */
  }
}

Recommended Settings

ApplicationSetting
DebuggingNo impact
TraceabilityNo impact
EfficiencyOn (GUI), 'off' (command line) (execution, ROM), No impact (RAM)
Safety precautionNo impact

Programmatic Use

Parameter: InitFltsAndDblsToZero
Type: character vector
Value: 'on' | 'off'
Default: 'off'

Version History

Introduced before R2006a