主要内容

coder.ignoreConst

防止将表达式的常量值用于函数特化

说明

coder.ignoreConst(expression) 防止代码生成器使用 expression 的常量值来创建函数特化coder.ignoreConst(expression) 返回 expression 的值。

示例

示例

全部折叠

使用 coder.ignoreConst 来防止使用常量值调用的函数的函数特化。

编写函数 call_myfn,该函数调用 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

生成独立的 C 代码。例如,生成静态库。启用代码生成报告。

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

在代码生成报告中,您可以看到 call_myfcn 的两个函数特化。

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

代码生成器使用值 'mode1'mode 创建 call_myfcn>myfcn>1。它使用值 'mode2'mode 创建 call_myfcn>myfcn>2

在生成的 C 代码中,您会看到特化 my_fcnb_my_fcn

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

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

为了防止函数特化,请指示代码生成器忽略 mode 参量的值是常量。

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

生成 C 代码。

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

在代码生成报告中,您看不到多个函数特化。

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.

在生成的 C 代码中,您会看到 my_fcn 的一个函数。

输入参数

全部折叠

其值被视为非常量的表达式,指定为 MATLAB 表达式。

详细信息

全部折叠

提示

  • 对于一些递归函数调用,您可以使用 coder.ignoreConst 来强制运行时递归。请参阅Force Code Generator to Use Run-Time Recursion

  • coder.ignoreConst(expression) 防止代码生成器使用常量值 expression 来创建函数特化。它并不阻止在代码生成过程中常量值的其他用途。

扩展功能

全部展开

C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。

GPU 代码生成
使用 GPU Coder™ 为 NVIDIA® GPU 生成 CUDA® 代码。

版本历史记录

在 R2017a 中推出