public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* cast of float to unsigned char bug on ARM with 3.4.5 - when was it fixed?
@ 2008-01-09 16:29 Sam Ravnborg
  2008-01-10 15:23 ` John Love-Jensen
  0 siblings, 1 reply; 3+ messages in thread
From: Sam Ravnborg @ 2008-01-09 16:29 UTC (permalink / raw)
  To: gcc-help

The following sample code produces different result depending on
the gcc version and target:

gcc version    target           output
3.4.5          i386             n1: 226 n2: -30.33
4.1.2 (RedHat) i386             n1: 226 n2: -30.33
3.4.5          arm-softfloat    n1: 0 n2: -30.33
4.1.1          arm              n1: 226 n2: -30.33

From the sample code above it is obvious that gcc 3.4.5
on arm-softfloat differs from the rest.

The sample code:
====================
#include <stdio.h>

int main(void)
{
   unsigned char n1;
   float n2;

   n2 = -30.33F;

   n1 = (unsigned char)n2;

   printf("n1: %d n2: %3.2f\n", n1, n2);

  return 0;
}
====================


I have not yet tried with a arm-softfloat gcc > 3.4.5 because Dan Kegel's
crosstool do not have it out-of-the box.

I assume this is a know bug that got fixed in later gcc's.
I have tried searching the gcc bug database with no luck.

Can anyone give a hint of when it was fixed or how I can better look it
up myself?

Thanks in advance,

	Sam

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: cast of float to unsigned char bug on ARM with 3.4.5 - when  was it fixed?
  2008-01-09 16:29 cast of float to unsigned char bug on ARM with 3.4.5 - when was it fixed? Sam Ravnborg
@ 2008-01-10 15:23 ` John Love-Jensen
  2008-01-10 19:57   ` Sam Ravnborg
  0 siblings, 1 reply; 3+ messages in thread
From: John Love-Jensen @ 2008-01-10 15:23 UTC (permalink / raw)
  To: Sam Ravnborg, MSX to GCC

Hi Sam,

> I assume this is a know bug that got fixed in later gcc's.
> I have tried searching the gcc bug database with no luck.

It's not a GCC bug.  Your code a C bug (if I may be so presumptuous to
describe using undefined C behavior as a "bug").

Since your code is performing undefined behavior, the output of all four
examples is correct.

From "The C Programming Language" A.6.3 "The result is undefined if the
value will not fit in the space provided".  After truncation, -30 does not
fit in 0 ... 255 range (assuming your unsigned char is 8-bit), hence
undefined behavior.

One way you could fix your code to have more consistent behavior is to use
an intermediate int.

The sample code:
====================
#include <stdio.h>

int main(void)
{
  int i;
  unsigned char n1;
  float n2;

  n2 = -30.33F;
  i = (int)n2;
  n1 = (unsigned char)i;

  printf("n1: %d n2: %3.2f\n", n1, n2);

  return 0;
}
====================

As long as an int is the same bit-size and is 2's complement represented on
the platforms being compared, that should product the same output.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: cast of float to unsigned char bug on ARM with 3.4.5 - when  was it fixed?
  2008-01-10 15:23 ` John Love-Jensen
@ 2008-01-10 19:57   ` Sam Ravnborg
  0 siblings, 0 replies; 3+ messages in thread
From: Sam Ravnborg @ 2008-01-10 19:57 UTC (permalink / raw)
  To: John Love-Jensen; +Cc: MSX to GCC

On Wed, Jan 09, 2008 at 07:51:49AM -0600, John Love-Jensen wrote:
> Hi Sam,
> 
> > I assume this is a know bug that got fixed in later gcc's.
> > I have tried searching the gcc bug database with no luck.
> 
> It's not a GCC bug.  Your code a C bug (if I may be so presumptuous to
> describe using undefined C behavior as a "bug").

I would name it a bug relying on undefined behaviour.
Thanks for clarifyng this!

	Sam

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-01-09 15:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-09 16:29 cast of float to unsigned char bug on ARM with 3.4.5 - when was it fixed? Sam Ravnborg
2008-01-10 15:23 ` John Love-Jensen
2008-01-10 19:57   ` Sam Ravnborg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).