A Type Conversion Question

From: Randy Yates (randy.yates_at_sonyericsson.com)
Date: 03/04/05


Date: 04 Mar 2005 13:40:48 -0500

Consider the following code:

#include "dsptypes.h"

/* definitions */

#define VECTOR_LENGTH 64

/* local variables */

/* local function prototypes */

/* function definitions */

int main(int margc, char **margv)
{
   UINT16_T n;
   INT16_T x[VECTOR_LENGTH];
   INT16_T y[VECTOR_LENGTH];
   INT32_T acc;
   INT16_T result;

   acc = 0;
   for (n = 0; n < VECTOR_LENGTH; n++)
   {
      x[n] = n;
      y[n] = VECTOR_LENGTH - n - 1;
   }

   acc = 0;
   for (n = 0; n < VECTOR_LENGTH; n++)
   {
      acc += x[n] * y[n];
   }

   result = (INT16_T)(acc >> 16);

   return result;
}

What does ISO C say about how the conversions in the

      acc += x[n] * y[n];

line are to be done? Specifically, is the multiply to be done with 16
bits, then the result promoted to 32 bits, or are x and y first to be
promoted to 32 bits before the multiply? Does the standard specify?

-- 
Randy Yates
Sony Ericsson Mobile Communications
Research Triangle Park, NC, USA
randy.yates@sonyericsson.com, 919-472-1124