memset
FunctionThis 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.
Consider the model matlab:rtwdemo_memset.
model = 'rtwdemo_memset';
open_system(model);
The code generator uses a loop to initialize the Constant
block values.
Create a temporary folder (in your system temporary folder) for the build and inspection process.
currentDir = pwd; [~,cgDir] = rtwdemodir();
Build the model.
rtwbuild(model)
### Starting build procedure for: rtwdemo_memset ### Successful completion of build procedure for: rtwdemo_memset
View the generated code without the optimization. These lines of code are in rtwdemo_memset.c
.
cfile = fullfile(cgDir,'rtwdemo_memset_grt_rtw','rtwdemo_memset.c'); rtwdemodbtype(cfile,'/* Model initialize function */',... '/* Model terminate function */',1,0);
/* Model initialize function */ void rtwdemo_memset_initialize(void) { /* Registration code */ /* initialize error status */ rtmSetErrorStatus(rtwdemo_memset_M, (NULL)); /* external outputs */ { int32_T i; for (i = 0; i < 50; i++) { rtwdemo_memset_Y.Out1[i] = 0.0; } } { int32_T i; /* ConstCode for Outport: '<Root>/Out1' */ for (i = 0; i < 50; i++) { rtwdemo_memset_Y.Out1[i] = 56.0; } /* End of ConstCode for Outport: '<Root>/Out1' */ } }
Open the Configuration Parameters dialog box.
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'); % # Open the Configuration Parameters dialog box.
The code generator uses the memset
function to initialize the Constant
block values.
Build the model.
rtwbuild(model)
### Starting build procedure for: rtwdemo_memset ### Successful completion of build procedure for: rtwdemo_memset
View the generated code with the optimization. These lines of code are in rtwdemo_memset.c
.
rtwdemodbtype(cfile,'/* Model initialize function */',... '/* Model terminate function */',1,0);
/* Model initialize function */ void rtwdemo_memset_initialize(void) { /* Registration code */ /* initialize error status */ rtmSetErrorStatus(rtwdemo_memset_M, (NULL)); /* external outputs */ (void) memset(&rtwdemo_memset_Y.Out1[0], 0, 50U*sizeof(real_T)); { int32_T i; /* ConstCode for Outport: '<Root>/Out1' */ for (i = 0; i < 50; i++) { rtwdemo_memset_Y.Out1[i] = 56.0; } /* End of ConstCode for Outport: '<Root>/Out1' */ } }
Close the model and the code generation report.
bdclose(model) rtwdemoclean; cd(currentDir)
Use memset to initialize floats and doubles to 0.0