Optimize Generated Code by Packing Boolean Data into Bitfields
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 time.
Example Model
Consider the model PackBooleanData
.
model = 'PackBooleanData';
open_system(model);
Disable Optimization
Open the Configuration Parameters dialog box.
On the Optimization pane, clear Pack Boolean data into bitfields.
Alternatively, you can use the command-line API to disable the optimization:
set_param(model,'BooleansAsBitfields','off');
Generate Code Without Optimization
Build the model using Embedded Coder.
slbuild(model)
### Starting build procedure for: PackBooleanData ### Successful completion of build procedure for: PackBooleanData Build Summary Top model targets: Model Build Reason Status Build Duration ================================================================================================================== PackBooleanData Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 23.968s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 28.757s
View the generated code without the optimization. These lines of code are in PackBooleanData.h
.
hfile = fullfile('PackBooleanData_ert_rtw','PackBooleanData.h'); coder.example.extractLines(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;
Enable Optimization
Open the Configuration Parameters dialog box.
On the Optimization pane, select Pack Boolean data into bitfields.
Alternatively, you can use the command-line API to enable the optimization:
set_param(model,'BooleansAsBitfields','on');
Generate Code with Optimization
Build the model using Embedded Coder.
slbuild(model)
### Starting build procedure for: PackBooleanData ### Successful completion of build procedure for: PackBooleanData Build Summary Top model targets: Model Build Reason Status Build Duration ============================================================================================== PackBooleanData Generated code was out of date. Code generated and compiled. 0h 0m 9.7821s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 10.844s
View the generated code with the optimization. These lines of code are in PackBooleanData.h
.
hfile = fullfile('PackBooleanData_ert_rtw','PackBooleanData.h'); coder.example.extractLines(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)
See Also
Pack Boolean data into bitfields