主要内容

定点多字操作生成的代码

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

在这个例子中,您将学习:

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

  • 如何防止多字代码时不会

简单的多字操作

这个模型显示了大整数和定点操作如何成为多字生成的C代码。多字代码通常是由使用参数或信号比C数据类型更广泛的“长”。

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

生成代码的模型:

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

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

复习一个多字生成函数:MultiWordAdd ()

fid = fopen (“fxpdemo_multiword_example1_grt_rtw / fxpdemo_multiword_example1.c”);ctext =从文件中读(fid,“*字符”)”;文件关闭(fid);= regexp匹配(ctext“空白MultiWordAdd * ?。\ n \} ',“匹配”);disp(匹配{1});
空白MultiWordAdd (const uint32_T u1 [], const uint32_T u2 [], uint32_T y [], int32_T n) {int32_T我;uint32_T携带u = 0;uint32_T u1i;uint32_T易;(我= 0;我< n;我+ +){u1i = u1[我];易= (u1i + u2[我])+携带;[我]y =易;携带=携带! = 0 u ? (uint32_T)(yi <= u1i) : (uint32_T)(yi < u1i); } }

这个函数实现了多字除了在c两个操作数和结果都有相同数量的话说,以及一次执行一个词。

close_system (“fxpdemo_multiword_example1”,0);

关系操作符块

关系操作符块在下面的例子中,你会期望得到多字函数生成的代码。两个输入数据类型uint32ufix32_En3。为比较好的类型ufix35_En3,因为这种可以代表所有现实世界两个操作数的值。

我们期望的数据类型ufix35_En3实现使用一个64位的两个单词数据类型。

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

这个模型与32位CPU配置C类型长。一个64位的数据类型将是一个多字类型。

get_param (bdroot“ProdBitPerLong”)
ans = 32

生成的代码模型和审查:

evalc (“slbuild (“fxpdemo_multiword_example2”),“);%抑制输出fid = fopen (“fxpdemo_multiword_example2_grt_rtw / fxpdemo_multiword_example2.c”);ctext =从文件中读(fid,“*字符”)”;文件关闭(fid);= regexp匹配(ctext“空白fxpdemo_multiword_example2_step * ?。\ n \} ',“匹配”);disp(匹配{1});
空白fxpdemo_multiword_example2_step (void) {/ * RelationalOperator:“< Root > /小于”包含:*尺寸:“< Root > / In1”*尺寸:' < Root > / In2 * / Y = (U1 < (uint32_T) ((U2 & 4 u) ! = 0 u) + (U2 > > 3));}

多字不是生成的代码。这段代码是单字原图和使用比较的数据类型uint32。因此,精度损失可能发生的比较。

万博1manbetx仿真软件平衡内部的要求比较的数据类型。在这种情况下,因为所有数据类型是单身的话,它实现了一个高效的数据类型产生小,快的代码而不是更精确,繁琐的计算。

close_system (“fxpdemo_multiword_example2”,0);

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

  • 选择输入数据类型,可以使用一个词而完整的精度比较的类型。例如,16位类型,或两个相同的类型。

  • 力模型使用一万博1manbetx个多字类型(在模拟和代码生成)通过指定一个多字类型的至少一个输入。这信号仿真软件,你想要使用万博1manbetx多字操作这一块。

  • 配置为一个64位的系统模型。

MATLAB功能块

MATLAB函数块在下面的例子展示了一个all-single-word计算。多字代码是出乎意料的时候。

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

地铁消防队= get_param (“fxpdemo_multiword_example3 / MATLAB函数”,“MATLABFunctionConfiguration”);mfb.FunctionScript
ans = '函数y = fcn (u1, u2) % # codegen y = fi(32岁的u1 * u2, 0 0);”

生成代码的模型:

evalc (“slbuild (“fxpdemo_multiword_example3”),“);%抑制输出fid = fopen (“fxpdemo_multiword_example3_grt_rtw / fxpdemo_multiword_example3.c”);ctext =从文件中读(fid,“*字符”)”;文件关闭(fid);= regexp匹配(ctext“空白fxpdemo_multiword_example3_step * ?。\ n \} ',“匹配”);disp(匹配{1});
空白fxpdemo_multiword_example3_step (void) {uint64m_T tmp;uint64m_T tmp_0;/ * MATLAB功能:“< Root > / MATLAB函数”包含:*尺寸:“< Root > / In1”*尺寸:“< Root > / In2”* / / * MATLAB函数的MATLAB函数:“< S1 >: 1”* / / * < S1 >: 1:4的* / uMultiWordMul (u1, u2, 1 &tmp_0。块u] [0, 2);uMultiWordShrNear (&tmp_0。块u] [0 2 3 u, tmp。块u] [0, 2);/ *输出港:“< Root > /着干活”包含:* MATLAB函数:‘< Root > / MATLAB函数* / fxpdemo_multiword_example3_Y。着干活= uMultiWord2uLongSat (tmp。块u] [0, 2);}
close_system (“fxpdemo_multiword_example3”,0);

尽管在模型中所有的数据类型是单字原图,你仍然有三个调用多字函数,和两个多字变量。

MATLAB中定点操作功能块由fimath属性设置控制。

fimath
ans = RoundingMethod:最近的OverflowAction:饱和ProductMode: FullPrecision SumMode: FullPrecision

这个fimath指定完整的精度ProductMode。因此执行乘法的方式保存尽可能多的精度。的产品数据类型uint64_En3,作为一个多字实现类型。

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

  • 满足高效的代码要求调整fimath属性。在这个例子中,集“ProductMode”“KeepLSB”“OverflowAction”“包装”

  • 定义本地fimaths MATLAB功能块中针对特定的计算,而不是依靠全球fimath。

load_system (“fxpdemo_multiword_example4”);%不需要显示这个模型。只显示了MATLAB代码。地铁消防队= get_param (“fxpdemo_multiword_example4 / MATLAB函数”,“MATLABFunctionConfiguration”);mfb.FunctionScript
ans = '函数y = fcn (u1, u2) % # codegen F = fimath (‘ProductMode’,‘KeepLSB’,……“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 =从文件中读(fid,“*字符”)”;文件关闭(fid);= regexp匹配(ctext“空白fxpdemo_multiword_example4_step * ?。\ n \} ',“匹配”);disp(匹配{1});
空白fxpdemo_multiword_example4_step (void) {uint32_T tmp;/ * MATLAB功能:“< Root > / MATLAB函数”包含:*尺寸:“< Root > / In1”*尺寸:“< Root > / In2”* / / * MATLAB函数的MATLAB函数:“< S1 >: 1”* / / * < S1 >: 1:6的* / / * < S1 >: 1:7的* / / * < S1 >: 1:8的* / tmp = U1 * U2;日元= (uint32_T) ((tmp & 4 u) ! = 0 u) + (tmp > > 3);}
close_system (“fxpdemo_multiword_example4”,0);
清晰的ctext支撑材匹配地铁消防队清晰的

另请参阅

|