This example shows how to optimize the generated code by packing Boolean data into bitfields. When you select the model configuration parameter Pack Boolean data into bitfields, Embedded Coder® packs the Boolean signals into 1-bit bitfields, reducing RAM consumption. By default, the optimization is enabled. This optimization reduces the RAM consumption. Be aware that this optimization can potentially increase code size and execution speed.
Consider the model rtwdemo_pack_boolean.
model = 'rtwdemo_pack_boolean';
open_system(model);
Open the Configuration Parameters dialog box.
On the Optimization > Signals and Parameters pane, clear Pack Boolean data into bitfields.
Alternatively, you can use the command-line API to disable the optimization:
set_param(model,'BooleansAsBitfields','off');
Create a temporary folder (in your system temporary folder) for the build and inspection process.
currentDir = pwd; [~,cgDir] = rtwdemodir();
Build the model using Embedded Coder®.
rtwbuild(model)
### Starting build procedure for: rtwdemo_pack_boolean ### Successful completion of build procedure for: rtwdemo_pack_boolean Build Summary Top model targets built: Model Action Rebuild Reason ===================================================================================================== rtwdemo_pack_boolean 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 9.5877s
View the generated code without the optimization. These lines of code are in rtwdemo_pack_boolean.h
.
hfile = fullfile(cgDir,'rtwdemo_pack_boolean_ert_rtw','rtwdemo_pack_boolean.h'); rtwdemodbtype(hfile,'/* Block signals and states','/* External inputs',1,0);
/* Block signals and states (default storage) for system '<Root>' */ typedef struct { boolean_T LogicalOp1; /* '<Root>/Logical Op1' */ boolean_T LogicalOp2; /* '<Root>/Logical Op2' */ boolean_T LogicalOp5; /* '<Root>/Logical Op5' */ boolean_T LogicalOp3; /* '<Root>/Logical Op3' */ boolean_T LogicalOp4; /* '<Root>/Logical Op4' */ boolean_T RelationalOperator; /* '<Root>/Relational Operator' */ boolean_T UnitDelay_DSTATE; /* '<Root>/Unit Delay' */ } DW;
Open the Configuration Parameters dialog box.
On the Optimization > Signals and Parameters pane, select Pack Boolean data into bitfields.
Alternatively, you can use the command-line API to enable the optimization:
set_param(model,'BooleansAsBitfields','on');
Build the model using Embedded Coder®.
rtwbuild(model)
### Starting build procedure for: rtwdemo_pack_boolean ### Successful completion of build procedure for: rtwdemo_pack_boolean Build Summary Top model targets built: Model Action Rebuild Reason ==================================================================================== rtwdemo_pack_boolean 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 9.1745s
View the generated code with the optimization. These lines of code are in rtwdemo_pack_boolean.h
.
hfile = fullfile(cgDir,'rtwdemo_pack_boolean_ert_rtw','rtwdemo_pack_boolean.h'); rtwdemodbtype(hfile,'/* Block signals and states','/* External inputs',1,0);
/* Block signals and states (default storage) for system '<Root>' */ typedef struct { struct { uint_T LogicalOp1:1; /* '<Root>/Logical Op1' */ uint_T LogicalOp2:1; /* '<Root>/Logical Op2' */ uint_T LogicalOp5:1; /* '<Root>/Logical Op5' */ uint_T LogicalOp3:1; /* '<Root>/Logical Op3' */ uint_T LogicalOp4:1; /* '<Root>/Logical Op4' */ uint_T RelationalOperator:1; /* '<Root>/Relational Operator' */ uint_T UnitDelay_DSTATE:1; /* '<Root>/Unit Delay' */ } bitsForTID0; } DW;
Selecting Pack Boolean data into bitfields enables model configuration parameter Bitfield declarator type specifier. To optimize your code further, select uchar_t
. However, the optimization benefit of the Bitfield declarator type specifier setting depends on your choice of target.
Close the model and code generation report.
bdclose(model) rtwdemoclean; cd(currentDir)
Pack Boolean data into bitfields