coder.noImplicitExpansionInFunction - Disable implicit expansion within the specified function in the generated

  code - MATLAB ([original](https://in.mathworks.com/help/coder/ref/coder.noimplicitexpansioninfunction.html)) ([raw](?raw))

Main Content

Disable implicit expansion within the specified function in the generated code

Since R2021b

Syntax

Description

Examples

collapse all

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.

type DisableImpExpinFunction.m

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.

a = coder.typeof(1,[2 1]); b = coder.typeof(1,[2 inf]);

Generate code for the functions by using these commands:

codegen DisableImpExpinFunction -args {a,b} -config:lib -lang:c++ -report

Code generation successful: View report

codegen ImpExpinFunction.m -args {a,b} -config:lib -lang:c++ -report

Code generation successful: View report

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.

type codegen/lib/DisableImpExpinFunction/DisableImpExpinFunction.cpp

// // Prerelease License - for engineering feedback and testing purposes // only. Not for sale. // File: DisableImpExpinFunction.cpp // // MATLAB Coder version : 25.1 // C/C++ source code generated on : 01-Feb-2025 07:28:16 //

// 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] //

type codegen/lib/ImpExpinFunction/ImpExpinFunction.cpp

// // Prerelease License - for engineering feedback and testing purposes // only. Not for sale. // File: ImpExpinFunction.cpp // // MATLAB Coder version : 25.1 // C/C++ source code generated on : 01-Feb-2025 07:28:21 //

// 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:?.

Version History

Introduced in R2021b