Main Content

Replace boolean with Specific Integer Data Type

Depending on the architecture of the processor that your production hardware uses, you can improve the execution speed of the generated code. Select a specific integer data type for the built-in type boolean. Using data type replacement, in the generated code you can replace the boolean built-in data type with one of these integer types:

  • int8

  • uint8

  • intn

To match the integer word size for the production hardware, replace n with 8, 16, or 32.

Example Model

The model ex_bool contains two blocks that output boolean values and two blocks that take boolean values as inputs. This example shows how to replace the data type boolean with the integer data type int32 in the code generated for a 32-bit hardware target.

Generate Code That Contains the Default boolean Data Type

View the generated file rtwtypes.h. The typedef statements contain the generic type definition of boolean_T, which is the code generation name for boolean.

/*===========================================================================*
 * Generic type definitions: boolean_T, char_T, byte_T, int_T, uint_T,       *
 *                           real_T, time_T, ulong_T.                        *
 *===========================================================================*/
typedef double real_T;
typedef double time_T;
typedef unsigned char boolean_T;
typedef int int_T;
typedef unsigned int uint_T;
typedef unsigned long ulong_T;
typedef char char_T;
typedef unsigned char uchar_T;
typedef char_T byte_T;

View the generated file ex_bool.c. The code declares boolean variables by using the type boolean_T.

/* External inputs (root inport signals with auto storage) */
typedef struct {
  real_T In1;                          /* '<Root>/In1' */
  real_T In2;                          /* '<Root>/In2' */
  boolean_T In3;                       /* '<Root>/In3' */
  boolean_T In4;                       /* '<Root>/In4' */
  real_T In5;                          /* '<Root>/In5' */
} ExtU_ex_bool_T;

/* External outputs (root outports fed by signals with auto storage) */
typedef struct {
  boolean_T Out1;                      /* '<Root>/Out1' */
  boolean_T Out2;                      /* '<Root>/Out2' */
  real_T Out3;                         /* '<Root>/Out3' */
} ExtY_ex_bool_T;

Generate Code That Contains the Target boolean Data Type

  1. Define a Simulink.AliasType object with a base type of int32. Name the object using the replacement name that you want to appear in the generated code.

    mybool = Simulink.AliasType;
    mybool.BaseType = 'int32';

  2. In the Configuration Parameters dialog box, specify the Replacement Name field for the data type boolean as mybool.

View the generated file rtwtypes.h. The code maps the identifier mybool to the native integer type of the target hardware by creating typedef statements.

/* Generic type definitions ... */
...
typedef int boolean_T;
 ...
/* Define Simulink Coder replacement data types. */
typedef boolean_T mybool;    /* User defined replacement datatype for boolean_T */

View the generated file ex_bool.c. The code declares boolean variables that use the type mybool.

  /* External inputs (root inport signals with auto storage) */
typedef struct {
  real_T In1;                          /* '<Root>/In1' */
  real_T In2;                          /* '<Root>/In2' */
  mybool In3;                          /* '<Root>/In3' */
  mybool In4;                          /* '<Root>/In4' */
  real_T In5;                          /* '<Root>/In5' */
} ExtU_ex_bool_T;

/* External outputs (root outports fed by signals with auto storage) */
typedef struct {
  mybool Out1;                         /* '<Root>/Out1' */
  mybool Out2;                         /* '<Root>/Out2' */
  real_T Out3;                         /* '<Root>/Out3' */
} ExtY_ex_bool_T;

See Also

Related Topics