Documentation Center

  • Trial Software
  • Product Updates

Avoid Multiword Operations in Generated Code

This example shows how to avoid multiword operations in generated code by using the accumpos function instead of simple addition in your MATLAB® algorithm. Similarly, you can use accumneg for subtraction.

This example requires a MATLAB Coder™ license.

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

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

Write a second MATLAB algorithm that adds two numbers using accumpos and returns the result.

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

accumpos adds a and b using the data type of a. b is cast into the data type of a. If a is a fi object, by default, accumpos sets the rounding mode to 'Floor' and the overflow action to 'Wrap'. It ignores the fimath properties of a and b.

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 using accumpos, the word length of the result is the same as that of a.

Generate C code for the function my_add1. First, disable use of the long long data 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;
codegen my_add1 -args {a,b} -report  -config cfg

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

View the generated code for the simple addition. Click the View report link to open the code generation report and then scroll to the code for the my_add1 function.

/* 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 function my_add2.

codegen my_add2 -args {a,b} -report -config cfg

View the generated code for the addition using accumpos. Click the View report link to open the code generation report and then scroll to the code for the my_add2 function.

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.

Was this topic helpful?