coder.ignoreConst - Prevent use of constant value of expression for function

specializations - MATLAB (original) (raw)

Use coder.ignoreConst to prevent function specializations for a function that is called with constant values.

Write the function call_myfn, which calls myfcn.

function [x, y] = call_myfcn(n) %#codegen x = myfcn(n, 'mode1'); y = myfcn(n, 'mode2'); end

function y = myfcn(n,mode) coder.inline('never'); if strcmp(mode,'mode1') y = n; else y = -n; end end

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

codegen -config:lib call_myfcn -args {1} -report

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

This image shows the function call_myfcn and the function specializations in the code generation report.

The code generator creates call_myfcn>myfcn>1 for mode with a value of 'mode1'. It creates call_myfcn>myfcn>2 for mode with a value of 'mode2'.

In the generated C code, you see the specializations my_fcn and b_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 the mode argument are constant.

function [x, y] = call_myfcn(n) %#codegen x = myfcn(n, coder.ignoreConst('mode1')); y = myfcn(n, coder.ignoreConst('mode2')); end

function y = myfcn(n,mode) coder.inline('never'); if strcmp(mode,'mode1') y = n; else y = -n; end end

Generate the C code.

codegen -config:lib call_myfcn -args {1} -report

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

This image shows the function call_myfcn after instructing the code generator to ignore that the values of the mode argument are constant in the code generation report.

In the generated C code, you see one function for my_fcn.