Main Content

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

  1. Open the Configuration Parameters dialog box.

  2. 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 built:

Model            Action                        Rebuild Reason                                    
=================================================================================================
PackBooleanData  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.336s

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

  1. Open the Configuration Parameters dialog box.

  2. 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 built:

Model            Action                        Rebuild Reason                   
================================================================================
PackBooleanData  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 19.708s

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

Related Topics