MATLAB Examples

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 speed.

Contents

Example Model

Consider the model rtwdemo_pack_boolean.

model = 'rtwdemo_pack_boolean';
open_system(model);

Disable Optimization

  1. Open the Configuration Parameters dialog box.
  2. 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();

Generate Code Without Optimization

Build the model using Embedded Coder®.

rtwbuild(model)
### Starting build procedure for model: rtwdemo_pack_boolean
### Successful completion of build procedure for model: rtwdemo_pack_boolean

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 (auto 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 > 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');

Generate Code with Optimization

Build the model using Embedded Coder®.

rtwbuild(model)
### Starting build procedure for model: rtwdemo_pack_boolean
### Successful completion of build procedure for model: rtwdemo_pack_boolean

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 (auto 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)