AW: CodeWarrior Question

"Mark Butcher" <[email protected]> Sat, 13 Sep 2008 21:21:47 +0200
Newsgroups gmane.comp.hardware.motorola.microcontrollers.coldfire
Message-ID <000001c915d5$f7851080$e68f3180$@com>
Hi Bob

You can disable such warning by deactivating "implicit arithmetic
conversions" in "Language Settings+ -> "C/C++ warnings".

However the warning is telling you that you may have a problem - in this
case it is because subtracting a short from a short won't necessarily fit
into a short (I assume that CW is treating shorts as signed shorts - to
avoid ambiguity it is probably better to write "signed short" or "unsigned
short" so that it is clear).
Eg. -32000 - +32000 = -64000  (0x8300 - 0x7d00) = 0xffff0600

Your code would cut the result from int 0xffff0600 to signed short 0x8600
(retaining the sign), which will give -34303 - in a lot of cases it will
work since the cutting wont introduce an error, but in some it will fail -
hence the warning. (Note that a $80'000'000 satellite once had to be
destroyed on launch since it rocket went off course due to such an error
(actually a floating point one but related) so sometimes it is better to
keep the warnings on and only cast when you are also sure that the warning
is not relevant to your case).

In fact I don't actually know why the compiler is converting intermediate
values to int, but ANSI C defines in some detail how it takes place and
someone experienced with compiler development will probably be able to
explain exactly why it is doing this (otherwise do watch the warnings to get
a feel for how it's working internally).

In your specific case I think that it is best to define the edges as
"unsigned short" types (assuming they can only be positive in value). If you
are sure that the "result" will also always be positive (possibly a safety
check is also useful)
if (pRxChannelInput->RiseEdge > pRxChannelInput->FallEdge) {
// unexpected
}
then a cast of the result (unsigned short) is justified.

Regards

Mark

www.uTasker.com



-----Ursprüngliche Nachricht-----
Von: [email protected] [mailto:[email protected]] Im
Auftrag von Robert Ritchey
Gesendet: Samstag, 13. September 2008 17:29
An: [email protected]
Betreff: [ColdFire] CodeWarrior Question

Hi,
I am new to CodeWarrior and pretty green in C so I hope this is not
a totally naive question.  I have some code that takes two shorts and
puts them in a short. I have snipped the code so there is not a lot of
extraneous stuff.  My question is, CodeWarrior always gives me a
warning that I am trying to do an implicit conversion from unsigned int
to short unless I do an explicit cast.  It does this to me in other cases
too, I have to cast to a byte (unsigned char) a lot of places I don't think
I have to. A friend who has written a lot of C thinks I should not be
getting this warning.  I guess I am wondering if I am missing a setting
for CodeWarrior that would make it accept a statement that operates
on two shorts and puts them in a short without having to do a cast.
Thanks,

typedef struct
     {
     short            RiseEdge;                       /* Pulse rising 
edge time                       */
     short            FallEdge;                       /* Pulse 
falling edge time                      */
     } ChanInStruct;

bool TxPulseOffset(short *Offset)
{
     short   PulseWidth; 
                    /* Pulse width                          */
     PulseWidth = (short)(pRxChannelInput->FallEdge - 
pRxChannelInput->RiseEdge);        /* Pulse width is falling minus rising */
}


At 12:04 AM 9/13/2008 -0500, you wrote:


-Bob
--------------------------------------------------------------------
Robert Ritchey
Quest Engineering & Development, Inc.
Suite B/8
6125 South Ash Avenue
Tempe, AZ 85283
Tel: (480) 460-2652
FAX: (480) 460-2653
E-Mail: [email protected]
E-Mail: [email protected]
E-Mail: [email protected]
E-Mail: [email protected]
WWW: www.QuestEngDev.com/
WWW: www.Smart-Fly.com/
-------------------------------------------------------------------- 

---
[email protected]              Send a post to the list.
[email protected]        Join the list.
[email protected]    Join the list in digest mode.
[email protected]     Leave the list.

---
[email protected]              Send a post to the list.
[email protected]        Join the list.
[email protected]    Join the list in digest mode.
[email protected]     Leave the list.