Contenuto principale

Simplify array indexing

Replace multiply operations in array indices when accessing arrays in a loop

Model Configuration Pane: Code Generation / Optimization

Description

The Simplify array indexing parameter instructs the code generator whether to replace multiply operations in array indices when accessing arrays in a loop.

Settings

off (default) | on

Default: Off

On

In array indices, replace multiply operations with add operations when accessing arrays in a loop in the generated code. When the original signal is multidimensional, the Embedded Coder® generates one-dimensional arrays, resulting in multiply operations in the array indices. Using this setting eliminates costly multiply operations when accessing arrays in a loop in the C/C++ program. This optimization (commonly referred to as strength reduction) is particularly useful if the C/C++ compiler on the target platform does not have similar functionality. The absence of multiply operations in the C/C++ program does not imply that the C/C++ compiler does not generate multiply instructions.

Off

Leave multiply operations in array indices when accessing arrays in a loop.

Examples

expand all

The generated code might have multiply operations when indexing an element of an array. You can select the optimization parameter Simplify array indexing to replace multiply operations in the array index with a temporary variable. This optimization can improve execution speed by reducing the number of times the multiply operation executes.

If you have the following model:

Model containing a Concatenate block that concatenates three input array signals.

The Constant blocks have the following Constant value:

  • Const1: reshape(1:120,[1 2 3 4 5])

  • Const2: reshape(1:120,[1 2 3 4 5])

  • Const3: reshape(1:120,[1 2 3 4 5])

The Concatenate block parameter Mode is set to Multidimensional array. The Constant blocks Sample time parameter is set to –1.

Building the model with the Simplify array indexing parameter turned off generates the following code:

  real_T pooled1;
  int32_T Out1_tmp;
  int32_T Out1_tmp_0;
  int32_T i;
  int32_T i_0;
  int32_T i_1;
  int32_T pooled1_tmp;
  int32_T pooled1_tmp_0;

  for (i_1 = 0; i_1 < 5; i_1++) {
    for (i_0 = 0; i_0 < 4; i_0++) {
      for (i = 0; i < 3; i++) {
        pooled1_tmp = i << 1;
        pooled1_tmp_0 = (6 * i_0 + pooled1_tmp) + 24 * i_1;
        pooled1 = ex_arrayindex_ConstP.pooled1[pooled1_tmp_0];
        pooled1_tmp = (18 * i_0 + pooled1_tmp) + 72 * i_1;
        ex_arrayindex_Y.Out1[pooled1_tmp] = pooled1;

        Out1_tmp = (((i + 3) << 1) + 18 * i_0) + 72 * i_1;
        ex_arrayindex_Y.Out1[Out1_tmp] = pooled1;

        Out1_tmp_0 = (((i + 6) << 1) + 18 * i_0) + 72 * i_1;
        ex_arrayindex_Y.Out1[Out1_tmp_0] = pooled1;

        pooled1 = ex_arrayindex_ConstP.pooled1[pooled1_tmp_0 + 1];
        ex_arrayindex_Y.Out1[pooled1_tmp + 1] = pooled1;
        ex_arrayindex_Y.Out1[Out1_tmp + 1] = pooled1;
        ex_arrayindex_Y.Out1[Out1_tmp_0 + 1] = pooled1;
      }
    }
  }

Open the Configuration Parameters dialog box and select the Simplify array indexing parameter. Build the model again. In the generated code, (tmp_3 + tmp_2) + tmp_0 replaces a multiply operation in the array index. The generated code is now:

  real_T pooled1;
  int32_T Out1_tmp;
  int32_T i;
  int32_T i_0;
  int32_T i_1;
  int32_T pooled1_tmp;
  int32_T tmp;
  int32_T tmp_0;
  int32_T tmp_1;
  int32_T tmp_2;
  int32_T tmp_3;

  tmp = 0;
  tmp_0 = 0;
  for (i_1 = 0; i_1 < 5; i_1++) {
    tmp_1 = 0;
    tmp_2 = 0;
    for (i_0 = 0; i_0 < 4; i_0++) {
      tmp_3 = 0;
      for (i = 0; i < 3; i++) {
        pooled1_tmp = (tmp_3 + tmp_1) + tmp;
        pooled1 = ex_arrayindex_ConstP.pooled1[pooled1_tmp];
        Out1_tmp = (tmp_3 + tmp_2) + tmp_0;
        ex_arrayindex_Y.Out1[Out1_tmp] = pooled1;
        ex_arrayindex_Y.Out1[Out1_tmp + 6] = pooled1;
        ex_arrayindex_Y.Out1[Out1_tmp + 12] = pooled1;
        pooled1 = ex_arrayindex_ConstP.pooled1[pooled1_tmp + 1];
        ex_arrayindex_Y.Out1[Out1_tmp + 1] = pooled1;
        ex_arrayindex_Y.Out1[Out1_tmp + 7] = pooled1;
        ex_arrayindex_Y.Out1[Out1_tmp + 13] = pooled1;
        tmp_3 += 2;
      }

      tmp_1 += 6;
      tmp_2 += 18;
    }

    tmp += 24;
    tmp_0 += 72;
  }

Recommended Settings

ApplicationSetting
DebuggingNo impact
TraceabilityNo impact
EfficiencyOn (execution speed)
Safety precautionNo impact

Programmatic Use

Parameter: StrengthReduction
Type: character vector
Value: 'on' | 'off'
Default: 'off'

Version History

Introduced in R2009a