主要内容

生成代码中的定点多字操作

这个例子展示了如何在生成的代码中控制多字操作的生成。

在这个例子中,你会学到:

  • 如何使用多字操作为大数据类型生成代码

  • 如何防止多字码时,它不是预期

简单多字操作

这个模型展示了在生成的C代码中,整数和定点运算如何变成多字运算。多字码通常由使用数据类型大于C 'long'的参数或信号触发。

open_system (“fxpdemo_multiword_example1”);set_param (“fxpdemo_multiword_example1”,“SimulationCommand”,“更新”);

为模型生成代码:

evalc (“slbuild(“fxpdemo_multiword_example1”),“);抑制输出

在生成的代码中,使用函数实现多字操作。这些函数的名称中有“MultiWord”。

回顾生成的一个多字函数:MultiWordAdd ()

Fid = fopen(“fxpdemo_multiword_example1_grt_rtw / fxpdemo_multiword_example1.c”);Ctext = fread(fid,“*字符”)”;文件关闭(fid);匹配= regexp(ctext,“void MultiWordAdd。* ?\ n \} ',“匹配”);disp(匹配{1});
void MultiWordAdd(const uint32_T u1[], const uint32_T u2[], uint32_T y[], int32_T n) {int32_T i;uint32_T carry = 0U;uint32_T u1i;uint32_T易;For (i = 0;I < n;I ++) {u1i = u1[I];Yi = (u1i + u2[i]) +进位;Y [i] = yi;= 0U ? (uint32_T)(yi <= u1i) : (uint32_T)(yi < u1i); } }

这个函数用c语言实现了多字加法。两个操作数和结果都有相同的字数,并且每次执行一个字。

close_system (“fxpdemo_multiword_example1”, 0);

关系运算符块

在下面的关系运算符块示例中,您将期望在生成的代码中获得多字函数。这两个输入数据类型是uint32而且ufix32_En3.一个比较好的类型是ufix35_En3,因为该类型可以表示两个操作数的所有实际值。

我们期望的是数据类型ufix35_En3使用64位两字数据类型实现。

open_system (“fxpdemo_multiword_example2”);set_param (“fxpdemo_multiword_example2”,“SimulationCommand”,“更新”);

适用于32位C型长CPU。64位数据类型将是多字类型。

get_param (bdroot“ProdBitPerLong”
Ans = 32

生成模型代码并检查:

evalc (“slbuild(“fxpdemo_multiword_example2”),“);抑制输出Fid = fopen(“fxpdemo_multiword_example2_grt_rtw / fxpdemo_multiword_example2.c”);Ctext = fread(fid,“*字符”)”;文件关闭(fid);匹配= regexp(ctext,“void fxpdemo_multiword_example2_step。* ?\ n \} ',“匹配”);disp(匹配{1});
void fxpdemo_multiword_example2_step(void) {/* RelationalOperator: '<根>/LessThan'合并:*输入:'<根>/In1' *输入:'<根>/In2' */ Y = (U1 < (uint32_T)((U2 & 4U) != 0U) + (U2 >> 3));}

没有生成多字代码。此代码是单字的,并使用比较数据类型uint32.因此,比较时可能出现精度损失。

金宝appSimulink平衡了内部数据类型的需求,以便进行比较。在本例中,由于所有数据类型都是单个单词,因此它实现了一种高效的数据类型,可以生成较小、快速的代码,而不是更精确、更麻烦的计算。

close_system (“fxpdemo_multiword_example2”, 0);

为了提高计算的精度,请执行以下步骤之一:

  • 选择可以使用单个单词比较类型进行完全精度比较的输入数据类型。例如,16位类型,或者两个相同的类型。

  • 通过为至少一金宝app个输入指定多字类型,强制Simulink使用多字类型(在模拟和代码生成期间)。这向Simulink发出信金宝app号,表明您想对这个块使用多字操作。

  • 为64位系统配置模型。

MATLAB函数块

下面的MATLAB函数块示例展示了一个全单字计算。不需要多字代码。

open_system (“fxpdemo_multiword_example3”);set_param (“fxpdemo_multiword_example3”,“SimulationCommand”,“更新”);

MFB = get_param“fxpdemo_multiword_example3 / MATLAB函数”,“MATLABFunctionConfiguration”);地铁消防队。FunctionScript
Ans = '函数y = fcn(u1, u2) %#代码原y = fi(u1 * u2, 0,32,0);'

为模型生成代码:

evalc (“slbuild(“fxpdemo_multiword_example3”),“);抑制输出Fid = fopen(“fxpdemo_multiword_example3_grt_rtw / fxpdemo_multiword_example3.c”);Ctext = fread(fid,“*字符”)”;文件关闭(fid);匹配= regexp(ctext,“void fxpdemo_multiword_example3_step。* ?\ n \} ',“匹配”);disp(匹配{1});
void fxpdemo_multiword_example3_step(void) {uint64m_T tmp;uint64m_T tmp_0;/* MATLAB函数:'<根>/MATLAB函数'合并:*输入:'<根>/In1' *输入:'<根>/In2' */ /* MATLAB函数'MATLAB函数':':1' */ /* :1:4' */ uMultiWordMul(&U1, 1, &U2, 1, &tmp_0。块u] [0, 2);uMultiWordShrNear (&tmp_0。块[0U], 2,3u, &tmp。块u] [0, 2);/*输出端口:'<根>/Out1'包含:* MATLAB函数:'<根>/MATLAB函数' */ fxpdemo_multiword_example3_Y。Out1 = uMultiWord2uLongSat(&tmp. Out1 = uMultiWord2uLongSat)块u] [0, 2);}
close_system (“fxpdemo_multiword_example3”, 0);

尽管模型中的所有数据类型都是单字,但仍然有三次对多字函数的调用,以及两个多字变量。

MATLAB函数块中的定点操作由fimath属性设置控制。

fimath
ans =四舍四入方法:最近溢出动作:饱和ProductMode: FullPrecision SumMode: FullPrecision

这个fimath指定完全精度ProductMode.因此,在执行乘法时,要尽可能地保持精度。产品数据类型,即uint64_En3,被实现为一个多字类型。

您可以通过操纵fimath来控制MATLAB代码生成中的多字。例如:

  • 调整fimath属性以满足高效的代码需求。本例中为set“ProductMode”“KeepLSB”而且“OverflowAction”“包装”

  • 在MATLAB函数块中定义为特定计算量身定制的局部fimath,而不依赖于全局fimath。

load_system (“fxpdemo_multiword_example4”);不需要展示这个型号。只显示MATLAB代码。MFB = get_param“fxpdemo_multiword_example4 / MATLAB函数”,“MATLABFunctionConfiguration”);地铁消防队。FunctionScript
ans = '函数y = fcn(u1, u2) %#代码原F = fimath('ProductMode',' keepplsb ',…“ProductWordLength”,32岁的……“OverflowAction”、“包装”);u1 = setfimath(u1,F);u2 = setfimath(u2,F);Y = fi(u1 * u2,0,32,0); '

这个fimath会生成如下代码:

evalc (“slbuild(“fxpdemo_multiword_example4”),“);抑制输出Fid = fopen(“fxpdemo_multiword_example4_grt_rtw / fxpdemo_multiword_example4.c”);Ctext = fread(fid,“*字符”)”;文件关闭(fid);匹配= regexp(ctext,“void fxpdemo_multiword_example4_step。* ?\ n \} ',“匹配”);disp(匹配{1});
void fxpdemo_multiword_example4_step(void) {uint32_T tmp;/ * MATLAB功能:根> < / MATLAB函数的结合:*尺寸:< Root > /三机一体的*尺寸:“根> < / In2”* / / * MATLAB函数的MATLAB函数:“< S1 >: 1 ' */ /* '< S1 >: 1:6 ' */ /* '< S1 >: 1:7 ' */ /* '< S1 >: 1:8 * / tmp = U1 * U2;Y1 = (uint32_T)((tmp & 4U) != 0U) + (tmp >> 3);}
close_system (“fxpdemo_multiword_example4”, 0);
清晰的ctext支撑材匹配地铁消防队清晰的

另请参阅

|