public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/18275] New: Copying float that is nan to another variable twiddles bit
@ 2004-11-02 15:20 dsracic at gmail dot com
  2004-11-02 15:23 ` [Bug c/18275] " pinskia at gcc dot gnu dot org
  2004-11-02 15:28 ` dsracic at gmail dot com
  0 siblings, 2 replies; 3+ messages in thread
From: dsracic at gmail dot com @ 2004-11-02 15:20 UTC (permalink / raw)
  To: gcc-bugs

[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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/* 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 int    iword;
    float           fword;
  } eq;

  eq.fword = *word;

  temp[0] = eq.iword & 0x000000ff;
  temp[1] = (eq.iword & 0x0000ff00) >> 8;
  temp[2] = (eq.iword & 0x00ff0000) >> 16;
  temp[3] = (eq.iword & 0xff000000) >> 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


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

* [Bug c/18275] Copying float that is nan to another variable twiddles bit
  2004-11-02 15:20 [Bug c/18275] New: Copying float that is nan to another variable twiddles bit dsracic at gmail dot com
@ 2004-11-02 15:23 ` pinskia at gcc dot gnu dot org
  2004-11-02 15:28 ` dsracic at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-11-02 15:23 UTC (permalink / raw)
  To: gcc-bugs


------- 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


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

* [Bug c/18275] Copying float that is nan to another variable twiddles bit
  2004-11-02 15:20 [Bug c/18275] New: Copying float that is nan to another variable twiddles bit dsracic at gmail dot com
  2004-11-02 15:23 ` [Bug c/18275] " pinskia at gcc dot gnu dot org
@ 2004-11-02 15:28 ` dsracic at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: dsracic at gmail dot com @ 2004-11-02 15:28 UTC (permalink / raw)
  To: gcc-bugs


------- 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 -0000, pinskia at gcc dot gnu dot org
<gcc-bugzilla@gcc.gnu.org> 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


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

end of thread, other threads:[~2004-11-02 15:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-02 15:20 [Bug c/18275] New: Copying float that is nan to another variable twiddles bit dsracic at gmail dot com
2004-11-02 15:23 ` [Bug c/18275] " pinskia at gcc dot gnu dot org
2004-11-02 15:28 ` dsracic at gmail dot com

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).