Main Content

Avoid Multiword Operations in Generated Code

This example shows how to avoid multiword operations in generated code by using theaccumposfunction instead of simple addition in your MATLAB®algorithm. Similarly, you can useaccumnegfor subtraction.

This example requires aMATLAB Coder™license.

Write a simple MATLAB algorithm that adds two numbers and returns the result.

functiony = my_add1(a, b) y = a+b;

Write a second MATLAB algorithm that adds two numbers usingaccumposand returns the result.

functiony = my_add2(a, b) y = accumpos(a, b);% floor, wrap

accumposaddsaandbusing the data type ofa.b抛的数据类型吗a. Ifais afiobject, by default,accumpossets the rounding mode to'Floor'and the overflow action to'Wrap'. It ignores thefimathproperties ofaandb.

Compare the outputs of the two functions in MATLAB.

a = fi(1.25, 1, 32,5); b = fi(0.125, 0, 32);%%y1 = my_add1(a, b) y2 = my_add2(a, b)
y1 = 1.3750 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 62 FractionLength: 34 y2 = 1.3750 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 5

For the simple addition, the word length grows but usingaccumpos, the word length of the result is the same as that ofa.

Generate C code for the functionmy_add1. First, disable use of thelong longdata type because it is not usually supported by the target hardware.

hw = coder.HardwareImplementation;hw。ProdHWDeviceType ='Generic->32-bit Embedded Processor'; hw.ProdLongLongMode = false; hw.ProdBitPerLong = 32; cfg = coder.config('lib'); cfg.HardwareImplementation = hw; codegenmy_add1-args{a,b}-report-configcfg

MATLAB Codergenerates a C static library and provides a link to the code generation report.

View the generated code for the simple addition. Click theView reportlink to open the code generation report and then scroll to the code for themy_add1function.

/* Function Declarations */ static void MultiWordAdd(const unsigned long u1[], const unsigned long u2[], unsigned long y[], int n); static void MultiWordSignedWrap(const unsigned long u1[], int n1, unsigned int n2, unsigned long y[]); static void sLong2MultiWord(long u, unsigned long y[], int n); static void sMultiWord2MultiWord(const unsigned long u1[], int n1, unsigned long y[], int n); static void sMultiWord2sMultiWordSat(const unsigned long u1[], int n1, unsigned long y[], int n); static void sMultiWordShl(const unsigned long u1[], int n1, unsigned int n2, unsigned long y[], int n); static void sMultiWordShr(const unsigned long u1[], int n1, unsigned int n2, unsigned long y[], int n); static void uLong2MultiWord(unsigned long u, unsigned long y[], int n);

The generated C code contains multiple multiword operations.

Generate C code for the functionmy_add2.

codegenmy_add2-args{a,b}-report-configcfg

View the generated code for the addition usingaccumpos. Click theView reportlink to open the code generation report and then scroll to the code for themy_add2function.

int my_add2(int a, unsigned int b) { int y; y = a + (int)(b >> 29); /* floor, wrap */ return y; }

For this function, the generated code contains no multiword operations.