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: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
.
Generate Code
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; } } }
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,
(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; }