Use coder.noImplicitExpansionInFunction
to disable implicit expansion in the code generated for a MATLAB function by calling it in within the required function. Disable implicit expansion to apply binary operations and functions without:
In this example, the functions DisableImpExpinFunction
and ImpExpinFunction
add two operands of compatible sizes with identical code but the former disables implicit expansion. The code generated for DisableImpExpinFunction
does not contain additional code to apply implicit expansion. The code generated for ImpExpinFunction
contains loops that must be executed if the operands are of compatible size and can be implicitly expanded at run-time.
In the code generation report for the two functions, the sizes of the expression assigning the out
values are different.
Define a function DisableImpExpinFunction
that calculates the addition of two operands without implicit expansion by calling coder.noImplicitExpansionInFunction
within the required function.
function out = DisableImpExpinFunction(a,b)
coder.noImplicitExpansionInFunction;
out = a + b;
end
Define a function ImpExpinFunction
that calculates the addition of two operands with implicit expansion enabled.
function out = ImpExpinFunction(a,b)
out = a + b;
end
Define a fixed-size and variable-size input type for these functions.
Generate code for the functions by using these commands:
Code generation successful: To view the report, open('codegen/lib/DisableImpExpinFunction/html/report.mldatx')
Code generation successful: To view the report, open('codegen/lib/ImpExpinFunction/html/report.mldatx')
Compare Generated Code for Functions
To apply implicit expansion, the generated code introduces additional code to automatically expand the operands. The difference between the generated code for the two functions is shown in this code.
//
// File: DisableImpExpinFunction.cpp
//
// MATLAB Coder version : 24.2
// C/C++ source code generated on : 05-Sep-2024 13:46:24
//
// Include Files
#include "DisableImpExpinFunction.h"
#include "coder_array.h"
// Function Definitions
//
// Arguments : const double a[2]
// const coder::array<double, 2U> &b
// double out[2]
// Return Type : void
//
void DisableImpExpinFunction(const double a[2],
const coder::array<double, 2U> &b, double out[2])
{
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
}
//
// File trailer for DisableImpExpinFunction.cpp
//
// [EOF]
//
//
// File: ImpExpinFunction.cpp
//
// MATLAB Coder version : 24.2
// C/C++ source code generated on : 05-Sep-2024 13:46:29
//
// Include Files
#include "ImpExpinFunction.h"
#include "coder_array.h"
#include <emmintrin.h>
// Function Definitions
//
// Arguments : const double a[2]
// const coder::array<double, 2U> &b
// coder::array<double, 2U> &out
// Return Type : void
//
void ImpExpinFunction(const double a[2], const coder::array<double, 2U> &b,
coder::array<double, 2U> &out)
{
int loop_ub;
loop_ub = b.size(1);
out.set_size(2, b.size(1));
for (int i{0}; i < loop_ub; i++) {
_mm_storeu_pd(&out[2 * i],
_mm_add_pd(_mm_loadu_pd(&a[0]), _mm_loadu_pd(&b[2 * i])));
}
}
//
// File trailer for ImpExpinFunction.cpp
//
// [EOF]
//
When implicit expansion is enabled, the generated code includes additional code to change the size of the operands, as seen in ImpExpinFunction
. If implicit expansion is disabled, the generated code does not include additional code to apply any size changes to the operands, as seen in DisableImpExpinFunction
.
Compare the size of the output variables in the code generation report. The expression assigning the out
variable in DisableImpExpinFunction
is of the size 2x1
.
The expression assigning the out
variable in ImpExpinFunction
is of the size 2x:?
.