Forums

Unexpected compiler result for maths on a long data type

Started by dtw_aerie January 11, 2004
Hi, I wonder if someone can help me.

I'm pretty new to Codewarrior and the 56000, but am an experienced C
programmer so I'm not completely clueless. However, I'm getting some
behaviour from the compiler which seems a bit odd to me.

I have a structure, one member of which is a long. I'm setting this
to an initialisation value near the start of my program. This value
is a constant multiplied by a Scaling factor, which I'm currently
experimenting with. The scaling value is set by use of a #define in
a header file. Its value is currently set to 250. So I have a line
of code as follows:

controls.slRelease = 200 * SCALEFACTOR;

I was expecting the value of controls.slRelease to be 50,000, but it
isn't - it's -15536.

Looking at the assembly produced by the compiler, I get:

0x0000004C 0x86F40000C350 movei #-15536,X:0x0000
0x0000004F 0x86F40000FFFF movei #-1,X:0x0000

Looking at how longs are stored in memory, the first line of this,
setting the low order word, is correct. The high order word,
however, has been set to 0xFFFF, which isn't what I was expecting at
all.

To check, I got rid of the #define:

controls.slRelease = 200 * 250;

which produces the same compiler output. As a further test I wrote
the value to the variable directly:

controls.slRelease = 50000;

The compiler produced the following:

0x00000052 0x86F40000C350 movei #-15536,X:0x0000
0x00000055 0x86F400000000 movei #0,X:0x0000

whch shows up as my expected value of 50,000.

It seems that the compiler is treating the high order word in these
cases differently. Is this what is happening, and if so why - what
have I missed?

Or have I found a bug?

Thanks for your time

Dave Falconer



Hi Dave

Is it possible that you have mis-matched declarations? Once I had somewhat
similar behavior because I had defined a variable as unsigned long in one
file and then (foolishly) had typed an extern declaration of the same
variable as an unsigned short in another file.

I used to think that a linker was guaranteed to catch such a bug and treat
it as an error, but no.

The resulting code was not what I had expected. If I recall, I was doing a
modulus on the long (%) but it was being treated as a short.

Rick Corey -----Original Message-----
From: dtw_aerie [mailto:]
Sent: Sunday, January 11, 2004 4:58 PM
To:
Subject: [motoroladsp] Unexpected compiler result for maths on a long
data type Hi, I wonder if someone can help me.

I'm pretty new to Codewarrior and the 56000, but am an experienced C
programmer so I'm not completely clueless. However, I'm getting some
behaviour from the compiler which seems a bit odd to me.

I have a structure, one member of which is a long. I'm setting this
to an initialisation value near the start of my program. This value
is a constant multiplied by a Scaling factor, which I'm currently
experimenting with. The scaling value is set by use of a #define in
a header file. Its value is currently set to 250. So I have a line
of code as follows:

controls.slRelease = 200 * SCALEFACTOR;

I was expecting the value of controls.slRelease to be 50,000, but it
isn't - it's -15536.

Looking at the assembly produced by the compiler, I get:

0x0000004C 0x86F40000C350 movei #-15536,X:0x0000
0x0000004F 0x86F40000FFFF movei #-1,X:0x0000

Looking at how longs are stored in memory, the first line of this,
setting the low order word, is correct. The high order word,
however, has been set to 0xFFFF, which isn't what I was expecting at
all.

To check, I got rid of the #define:

controls.slRelease = 200 * 250;

which produces the same compiler output. As a further test I wrote
the value to the variable directly:

controls.slRelease = 50000;

The compiler produced the following:

0x00000052 0x86F40000C350 movei #-15536,X:0x0000
0x00000055 0x86F400000000 movei #0,X:0x0000

whch shows up as my expected value of 50,000.

It seems that the compiler is treating the high order word in these
cases differently. Is this what is happening, and if so why - what
have I missed?

Or have I found a bug?

Thanks for your time

Dave Falconer _____________________________________
Note: If you do a simple "reply" with your email client, only the author of
this message will receive your answer. You need to do a "reply all" if you
want your answer to be distributed to the entire group.

_____________________________________
About this discussion group:

To Join:

To Post:

To Leave:

Archives: http://www.yahoogroups.com/group/motoroladsp

More Groups: http://www.dsprelated.com/groups.php3



Hi Dave,

You must use:
controls.slRelease = (long) 200 * SCALEFACTOR;
or
controls.slRelease = 200 * (long) SCALEFACTOR;
otherwise the compiler thinks that you mean:
controls.slRelease = (long) (int) 200 * SCALEFACTOR;
that delivers: (long) (int) 50000
that is: (long) -15536

in your special case (there will yet be no overflow) you can also use (but I
don't advice it):
controls.slRelease = (long) (unsigned) (200 * SCALEFACTOR);

kind regards,
Wim de Haan

Exendis B.V.
W.J. de Haan
P.O.box 56, 6710 BB Ede
Keesomstraat 4, 6716 AB Ede
The Netherlands.
Tel: +31-(0)318 - 676305
Fax: +31-(0)318 - 676319
mailto:
URL: http://www.exendis.com <http://www.exendis.com/ -----Original Message-----
From: dtw_aerie [mailto:]
Sent: zondag 11 januari 2004 22:58
To:
Subject: [motoroladsp] Unexpected compiler result for maths on a long data
type Hi, I wonder if someone can help me.

I'm pretty new to Codewarrior and the 56000, but am an experienced C
programmer so I'm not completely clueless. However, I'm getting some
behaviour from the compiler which seems a bit odd to me.

I have a structure, one member of which is a long. I'm setting this
to an initialisation value near the start of my program. This value
is a constant multiplied by a Scaling factor, which I'm currently
experimenting with. The scaling value is set by use of a #define in
a header file. Its value is currently set to 250. So I have a line
of code as follows:

controls.slRelease = 200 * SCALEFACTOR;

I was expecting the value of controls.slRelease to be 50,000, but it
isn't - it's -15536.

Looking at the assembly produced by the compiler, I get:

0x0000004C 0x86F40000C350 movei #-15536,X:0x0000
0x0000004F 0x86F40000FFFF movei #-1,X:0x0000

Looking at how longs are stored in memory, the first line of this,
setting the low order word, is correct. The high order word,
however, has been set to 0xFFFF, which isn't what I was expecting at
all.

To check, I got rid of the #define:

controls.slRelease = 200 * 250;

which produces the same compiler output. As a further test I wrote
the value to the variable directly:

controls.slRelease = 50000;

The compiler produced the following:

0x00000052 0x86F40000C350 movei #-15536,X:0x0000
0x00000055 0x86F400000000 movei #0,X:0x0000

whch shows up as my expected value of 50,000.

It seems that the compiler is treating the high order word in these
cases differently. Is this what is happening, and if so why - what
have I missed?

Or have I found a bug?

Thanks for your time

Dave Falconer
_____________________________________
Note: If you do a simple "reply" with your email client, only the author of
this message will receive your answer. You need to do a "reply all" if you
want your answer to be distributed to the entire group.

_____________________________________
About this discussion group:

To Join:

To Post:

To Leave:

Archives: http://www.yahoogroups.com/group/motoroladsp
<http://www.yahoogroups.com/group/motoroladsp>

More Groups: http://www.dsprelated.com/groups.php3
<http://www.dsprelated.com/groups.php3
<http://rd.yahoo.com/SIGc4hkk48/M&7637.4116732.5333197.1261774/D=egroup
web/S05771855:HM/EXP73997778/A53619/R=0/*http://www.netflix.com/Def
ault?mqso`178356&partidA16732> click here

<http://us.adserver.yahoo.com/l?M&7637.4116732.5333197.1261774/D=egroupmai
l/S=:HM/A53619/rand1026647 _____

> .


Hi Dave,

You must use:
controls.slRelease = (long) 200 * SCALEFACTOR;
or
controls.slRelease = 200 * (long) SCALEFACTOR;
otherwise the compiler thinks that you mean:
controls.slRelease = (long) (int) (200 * SCALEFACTOR) ; // Sorry I
forgot ( ) in my first E-mail
that delivers: (long) (int) 50000
that is: (long) -15536

in your special case (there will yet be no overflow) you can also use (but I
don't advice it):
controls.slRelease = (long) (unsigned) (200 * SCALEFACTOR);

kind regards,
Wim de Haan

Exendis B.V.
W.J. de Haan
P.O.box 56, 6710 BB Ede
Keesomstraat 4, 6716 AB Ede
The Netherlands.
Tel: +31-(0)318 - 676305
Fax: +31-(0)318 - 676319
mailto:
URL: http://www.exendis.com <http://www.exendis.com/ -----Original Message-----
From: dtw_aerie [mailto:]
Sent: zondag 11 januari 2004 22:58
To:
Subject: [motoroladsp] Unexpected compiler result for maths on a long data
type Hi, I wonder if someone can help me.

I'm pretty new to Codewarrior and the 56000, but am an experienced C
programmer so I'm not completely clueless. However, I'm getting some
behaviour from the compiler which seems a bit odd to me.

I have a structure, one member of which is a long. I'm setting this
to an initialisation value near the start of my program. This value
is a constant multiplied by a Scaling factor, which I'm currently
experimenting with. The scaling value is set by use of a #define in
a header file. Its value is currently set to 250. So I have a line
of code as follows:

controls.slRelease = 200 * SCALEFACTOR;

I was expecting the value of controls.slRelease to be 50,000, but it
isn't - it's -15536.

Looking at the assembly produced by the compiler, I get:

0x0000004C 0x86F40000C350 movei #-15536,X:0x0000
0x0000004F 0x86F40000FFFF movei #-1,X:0x0000

Looking at how longs are stored in memory, the first line of this,
setting the low order word, is correct. The high order word,
however, has been set to 0xFFFF, which isn't what I was expecting at
all.

To check, I got rid of the #define:

controls.slRelease = 200 * 250;

which produces the same compiler output. As a further test I wrote
the value to the variable directly:

controls.slRelease = 50000;

The compiler produced the following:

0x00000052 0x86F40000C350 movei #-15536,X:0x0000
0x00000055 0x86F400000000 movei #0,X:0x0000

whch shows up as my expected value of 50,000.

It seems that the compiler is treating the high order word in these
cases differently. Is this what is happening, and if so why - what
have I missed?

Or have I found a bug?

Thanks for your time

Dave Falconer