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


Relevant Pages

  • Re: Binary file from C++ to Matlab
    ... You need an effeicient mapping between a hardware byte and a C char. ... They are not a part of the ANSI C specification. ... What is the relation between ANSI C and ISO C? ...
    (comp.soft-sys.matlab)
  • Re: (void) pointer arithmetic
    ... void pointer arithmetic is not allowed by ISO C90/C99. ... (and ANSI C89) ... sizeof(unsigned sometype) == sizeof ... wide enough to store a char value. ...
    (comp.lang.c)
  • Re: A Type Conversion Question
    ... > What does ISO C say about how the conversions in the ... and added to `acc'. ... two array elements are not promoted, ...
    (comp.lang.c)
  • Re: scsiinfo for Solaris 10
    ... > Char size of 16 would be an interesting exercise to port since sizeof ... ISO 9899 section 3.6 says that the number of bits of a byte ...
    (comp.unix.solaris)
  • Re: malloc - How it should be??
    ... by the official definition of the ISO C language (C89 and ... A char /can be/ more than 8 bits long. ... worth noting that the current UNIX/POSIX standard requires char to ...
    (comp.unix.programmer)