Main Content

coder.ignoreConst

防止使用常数赋值的表达式nction specializations

Description

example

coder.ignoreConst(expression)prevents the code generator from using the constant value ofexpressionto createfunction specializations.coder.ignoreConst(expression)returns the value ofexpression.

Examples

collapse all

Usecoder.ignoreConstto prevent function specializations for a function that is called with constant values.

Write the functioncall_myfn, which callsmyfcn.

function[x, y] = call_myfcn(n)%#codegenx = myfcn(n,'mode1'); y = myfcn(n,'mode2');endfunctiony = myfcn (n,mode) coder.inline('never');ifstrcmp(mode,'mode1') y = n;elsey = -n;endend

Generate standalone C code. For example, generate a static library. Enable the code generation report.

codegen-config:libcall_myfcn-args{1}-report

In the code generation report, you see two function specializations forcall_myfcn.

The code generator createscall_myfcn>myfcn>1formodewith a value of'mode1'. It createscall_myfcn>myfcn>2formodewith a value of'mode2'.

In the generated C code, you see the specializationsmy_fcnandb_my_fcn.

static double b_myfcn(double n) { return -n; } static double myfcn(double n) { return n; }

To prevent the function specializations, instruct the code generator to ignore that values of themodeargument are constant.

function[x, y] = call_myfcn(n)%#codegenx = myfcn(n, coder.ignoreConst('mode1')); y = myfcn(n, coder.ignoreConst('mode2'));endfunctiony = myfcn (n,mode) coder.inline('never');ifstrcmp(mode,'mode1') y = n;elsey = -n;endend

Generate the C code.

codegen-config:libcall_myfcn-args{1}-report

In the code generation report, you do not see multiple function specializations.

In the generated C code, you see one function formy_fcn.

Input Arguments

collapse all

Expression whose value is to be treated as a nonconstant, specified as a MATLAB expression.

More About

collapse all

Function Specialization

Version of a function in which an input type, size, complexity, or value is customized for a particular invocation of the function.

Function specialization produces efficient C code at the expense of code duplication. The code generation report shows all MATLAB function specializations that the code generator creates. However, the specializations might not appear in the generated C/C++ code due to later transformations or optimizations.

Tips

  • For some recursive function calls, you can usecoder.ignoreConstto force run-time recursion. SeeForce Code Generator to Use Run-Time Recursion.

  • coder.ignoreConst(expression)prevents the code generator from using the constant value ofexpressionto create function specializations. It does not prevent other uses of the constant value during code generation.

Introduced in R2017a