[Bug c/18275] New: Copying float that is nan to another variable twiddles bit

2004-11-02 Thread dsracic at gmail dot com
[Bug does not appear in 2.96/3.x and forward]

'gcc -v'
Reading specs from /usr/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/specs
gcc version 2.95.3 20010315 (release)

To duplicate bug, build the following (gcc -o test main.c)

The program attempts to take t, print it bit-by-bit, byte swap it, print that 
swapped value, then swap it back to its original value.  The swapped value 
becomes corrupt.  This bug shows up in 2.95 and prior, but 2.96/3.x and higher 
do not have this problem.

/* main.c */
#include 
#include 
#include 

/* some magic values that will demonstrate the bug. */
/* 
float t = 119.774410;
float t = 117.774410;
float t = 110.774410;
 */

void swap_float(float *word);
void print_char(char);
void print_bytes(void *, int);

int main(int argc, char** argv) {
  float t = 119.774410;

  printf("t = %f\n", t);
  print_bytes( (void *)&t, sizeof(t) );
  printf("\n");

  swap_float(&t);
  printf("swapped t = %f\n", t);
  print_bytes( (void *)&t, sizeof(t) );
  printf("\n");

  swap_float(&t);
  printf("unswapped t = %f\n", t);
  print_bytes( (void *)&t, sizeof(t) );
  printf("\n");

  return 0;
}

void print_char(char x) {
  int b;
  char test[256];

  for (b = 0; b < sizeof (x) * 8; b++) {
if (x & 01) test[(sizeof(x) * 8) - b -1] = '1';
  else test[(sizeof(x) * 8) - b -1] = '0';
x >>= 1;
  }
  test[sizeof(x) * 8] = '\0';
  printf("%s", test);
}

void print_bytes(void *startBytes, int numBytes) {
  int i = 0;
  char *curChar;

  curChar = startBytes;

  for (i = 0; i < numBytes; i++) {
print_char( *curChar );
printf(" | ");
curChar++;
  }
}

void swap_float(float *word) {
  unsigned int temp[4];

  union {
unsigned intiword;
float   fword;
  } eq;

  eq.fword = *word;

  temp[0] = eq.iword & 0x00ff;
  temp[1] = (eq.iword & 0xff00) >> 8;
  temp[2] = (eq.iword & 0x00ff) >> 16;
  temp[3] = (eq.iword & 0xff00) >> 24;

  eq.iword = (temp[0] << 24) + (temp[1] << 16) + (temp[2] << 8) + temp[3];

  *word = eq.fword;
}
/* End main.c */

-- 
   Summary: Copying float that is nan to another variable twiddles
bit
   Product: gcc
       Version: 2.95.3
Status: UNCONFIRMED
  Severity: normal
  Priority: P1
 Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: dsracic at gmail dot com
CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18275


[Bug c/18275] Copying float that is nan to another variable twiddles bit

2004-11-02 Thread dsracic at gmail dot com

--- Additional Comments From dsracic at gmail dot com  2004-11-02 15:28 ---
Subject: Re:  Copying float that is nan to another variable twiddles bit

It'd be nice if we could upgrade compilers on production systems to a
new major revision, but alas, that's not an option; this platform has
been defined for years, and won't get updated with anything except
minor revisions.  We've worked around this issue, but I figured that I
might as well report it so someone can figure out why it was happening
and not re-introduce the problem into 3.x by accident.


On 2 Nov 2004 15:23:07 -, pinskia at gcc dot gnu dot org
<[EMAIL PROTECTED]> wrote:
> 
> --- Additional Comments From pinskia at gcc dot gnu dot org  2004-11-02 15:23 
> ---
> What can I say other than if it is fixed in 3.0 and above just use 3.0 or above. 
> There is not going to be
> another release of 2.95.x, 3.0.x, 3.1.x, or 3.2.x at all.
> 
> --
>   What|Removed |Added
> 
> Status|UNCONFIRMED |RESOLVED
> Resolution||FIXED
>   Target Milestone|--- |3.0.x
> 
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18275
> 
> --- You are receiving this mail because: ---
> You reported the bug, or are watching the reporter.
>


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18275