According to the C standard, when you apply the unary operator - to x, x
is first promoted to an integer.
I think the difference between the PC and the MSP430 is that on the
MSP430, the variable is promoted to 16bits which is the same size as the
short. On the PC, x is first promoted to 32bits.

This is a common annoyance of mine on the msp430 when dealing with 32bit
types. On any line giving a 32bit result, try casting all the operands
to long (or unsigned long) before you work on them:

short x = -32768;
unsigned long  y = -((long)x);

does this fix your problem?

- Wayne

-----Original Message-----
From: Carl [mailto:[email protected]] 
Sent: Wednesday, 24 February 2010 10:01 AM
To: GCC for MSP430 - http://mspgcc.sf.net
Subject: Re: [Mspgcc-users] problem converting signed shortto abs
unsigned long

On Tue, 2010-02-23 at 23:37 +0000, Grant Edwards wrote:
> On 2010-02-23, Carl <[email protected]> wrote:
> > On Tue, 2010-02-23 at 23:08 +0000, Grant Edwards wrote:
> >> The values being passed to printf and then displayed are
> >> "unsigned".  On the '430, that's 16 bits.  On the PC that's 32
> >> bits.
> >
> > A short on both my system and 430 is 16 bits. A long on both my
> system
> > and the 430 are both 32 bits. I can inspect the memory with gdb. The
> > problem is the upper 16 bits of y on the 430 are set. Why does it do
> > this?
> >
> > On PC:
> >
> > (gdb) p sizeof(y)
> > $1 = 4
> > (gdb) p /x y
> > $2 = 0x8000
> >
> > On 430:
> >
> > (gdb) p sizeof(y)
> > $1 = 4
> > (gdb) p /x y
> > $2 = 0xffff8000
> 
> Unless you provide the code that was run up to that point,
> there's no way to answer your question.
> 
> But, I suspect that on the msp430, the value in question was an
> "int" at some point and then got converted to a long. 
> 
> On the MSP430, that resulted in the value being sign-extended
> when it got converted from 16 to 32 bits.  On a PC, an "int"
> and "long" are both 32 bits, so there is no sign extension when
> a value is converted from an int to a long.


Sorry, here is the code for the msp430:

#include <io.h>
int main(void)
{
    short x = -32768;
    unsigned long  y = -x;
    LPM0;
}

And same code as my previous posting for the PC:

#include <stdio.h>
int main()
{
    short          x = -32768;
    unsigned long  y = -x;
    printf("sizeof short =%d, x=%d, y=%lu\n", sizeof(short), x, y);
}

As you can see there is no int type used.

Carl




------------------------------------------------------------------------
------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Mspgcc-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users

Reply via email to