Simplify Multiply Operations in Array Indexing
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.
Example Model
If you have the following model:
The Constant blocks have the following Constant value:
Const1
:reshape(1:30,[1 5 3 2])
Const2
:reshape(1:20,[1 5 2 2])
Const3
:reshape(1:90,[1 5 9 2])
The Concatenate block parameter Mode is set to
Multidimensional array
. The Constant blocks
Sample time parameter is set to –1
.
Generate Code
Building the model with the Simplify array indexing parameter turned off generates the following code:
int32_T i; int32_T i_0; int32_T i_1; for (i = 0; i < 2; i++) { for (i_1 = 0; i_1 < 3; i_1++) { for (i_0 = 0; i_0 < 5; i_0++) { ex_arrayindex_Y.Out[(i_0 + 5 * i_1) + 70 * i] = ex_arrayindex_P.Constant1_Value[(5 * i_1 + i_0) + 15 * i]; } } } for (i = 0; i < 2; i++) { for (i_1 = 0; i_1 < 2; i_1++) { for (i_0 = 0; i_0 < 5; i_0++) { ex_arrayindex_Y.Out[(i_0 + 5 * (i_1 + 3)) + 70 * i] = ex_arrayindex_P.Constant2_Value[(5 * i_1 + i_0) + 10 * i]; } } } for (i = 0; i < 2; i++) { for (i_1 = 0; i_1 < 9; i_1++) { for (i_0 = 0; i_0 < 5; i_0++) { ex_arrayindex_Y.Out[(i_0 + 5 * (i_1 + 5)) + 70 * i] = ex_arrayindex_P.Constant3_Value[(5 * i_1 + i_0) + 45 * i]; } } }
Generate Code with Optimization
Open the Configuration Parameters dialog box and select the Simplify array
indexing parameter. Build the model again. In the generated code,
[(i_0 + tmp_1) + tmp]
replaces a multiply operation in the array index,
[(i_0 + 5 * i_1) + 70 * i]
. The generated code is
now:
int32_T i; int32_T i_0; int32_T i_1; int32_T tmp; int32_T tmp_0; int32_T tmp_1; tmp = 0; tmp_0 = 0; for (i = 0; i < 2; i++) { tmp_1 = 0; for (i_1 = 0; i_1 < 3; i_1++) { for (i_0 = 0; i_0 < 5; i_0++) { ex_arrayindex_Y.Out[(i_0 + tmp_1) + tmp] = ex_arrayindex_P.Constant1_Value[(i_0 + tmp_1) + tmp_0]; } tmp_1 += 5; } tmp += 70; tmp_0 += 15; } tmp = 0; tmp_0 = 0; for (i = 0; i < 2; i++) { tmp_1 = 0; for (i_1 = 0; i_1 < 2; i_1++) { for (i_0 = 0; i_0 < 5; i_0++) { ex_arrayindex_Y.Out[((i_0 + tmp_1) + tmp) + 15] = ex_arrayindex_P.Constant2_Value[(i_0 + tmp_1) + tmp_0]; } tmp_1 += 5; } tmp += 70; tmp_0 += 10; } tmp = 0; tmp_0 = 0; for (i = 0; i < 2; i++) { tmp_1 = 0; for (i_1 = 0; i_1 < 9; i_1++) { for (i_0 = 0; i_0 < 5; i_0++) { ex_arrayindex_Y.Out[((i_0 + tmp_1) + tmp) + 25] = ex_arrayindex_P.Constant3_Value[(i_0 + tmp_1) + tmp_0]; } tmp_1 += 5; } tmp += 70; tmp_0 += 45; }