生成定点 C 代码
注意
要从 MATLAB® 生成定点代码,您必须同时拥有 Fixed-Point Designer™ 产品和 MATLAB Coder™ 产品。您还必须有 C 编译器。
此示例说明如何为一个简单函数生成代码,该函数将两个输入值相乘并累加。这是可以嵌入到外部硬件中的一种代码。函数为
function acc = mult_acc(x,a,acc)
acc = accumpos(acc,x*a);
以下代码定义测试平台输入,设置必需的代码生成属性并生成代码。测试平台输入被指定为定点数。x 输入为随机数,a 为 0.9,累加器 acc 初始化为 0。coder.HardwareImplementation 对象指定影响生成代码的外部硬件的属性。这些示例指定一个 40 位累加器。coder.CodeConfig 对象具有直接影响代码生成的属性。codegen 命令将函数、配置对象作为输入参量并生成可嵌入的 C 代码。
x = fi(rand,true,16,15); a = fi(0.9,true,16,15); acc = fi(0,true,40,30); %% hi = coder.HardwareImplementation; hi. ProdHWDeviceType = 'Generic->Custom' hi. TargetHWDeviceType = 'Generic->Custom' hi.TargetBitPerLong = 40; hi.ProdBitPerLong = 40; hc = coder.config('lib'); hc.HardwareImplementation = hi; hc.GenerateReport = true; codegen mult_acc -config hc -args {x,a,acc}
生成的 C 代码为:
/* Include Files */
#include "mult_acc.h"
/* Function Definitions */
/*
* Arguments : short x
* short a
* long *acc
* Return Type : void
*/
void mult_acc(short x, short a, long *acc)
{
*acc += x * a;
}注意
有关支持代码生成的函数的列表,请参阅 C/C++ 代码生成支持的函数和对象。