🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.bug
Article #5993

floating point deviations with -fcaller-saves

#5993
From: Heiko Eissfeldt
Date: Sun, 22 Dec 1996 00:00
117 lines
3753 bytes
Hi dear FSF/GNU people,

I think I have found a GCC 2.7.2.1 floating point bug/flaw.
I have tested this on GCC 2.7.2.1/2.7.2 on GNU/Linux-Intel
with an Intel 486/33 and an AMD 486/133 cpu.
Since I am not following the bug mailing list, I don't know
if this one is known...

Description:
I am generating a series of floating point numbers (doubles)
from a set of three distinct values (0, 1/3, 2/3).
The doubles each can only have one of these three exact
values. They are generated from one function get_real_root().

When I use -O1 and -fcaller-saves for the routine which
_calls_ get_real_root, there are slight deviations
(lower mantisse bits) in one of the values (2/3).
The values become reproducible exact, when I
turn off the option 'caller-saves' (-fno-caller-saves).

The buggy function is create_testcase(). I have made
this part depending on a '#define BUGGY_PART' in order
to compile it with different options from the rest.

This function shall fill an array with distinct doubles.
After one entry has been stored, the next value is generated
from get_real_root() and compared against the stored values
in the array. New values should be generated until no
match occurs with any of the array values. This fails since
the comparisons are done with slightly different values.
In this particular case I rely on exact reproducible values
(I know this is generally not good practise).

This flaw may have something to do with different rounding behavior
at the different places where get_real_root is called
(just a guess).

Here is my minimum (quite ugly) code to show this bug:
---------------------- cut here ----------------------------------
/*
compile with gcc 2.7.2.1 or 2.7.2
 gcc -O1 -Wall -o gcc_bugdemo_f.o -c gcc_bugdemo.c -fcaller-saves -DBUGGY_P=
ART
 gcc -O6 -Wall -o gcc_bugdemo gcc_bugdemo.c gcc_bugdemo_f.o
*/

#include <stdio.h>
#include <stdlib.h>
#define FULL

double get_real_root(int minrange, int maxrange, int divider);

void create_testcase(void);

#ifdef BUGGY_PART

#include <math.h>

void
create_testcase(void)
{
  int rc =3D 0;
  double vals[2];
  double r_val =3D get_real_root(0,1,3);

  for (rc =3D 0; rc < 2; ) {
    int again;
   =20
    vals[rc] =3D r_val;
    rc++;

    /* be sure _not_ to generate a duplicate here */
    again =3D 0;
    do {
      int o;

      r_val =3D get_real_root(0,1,3);

      /* check against generated values */
      for (o =3D 0; o < rc; o++) {

=09if (fabs(r_val-vals[o]) < 0.1)
=09  printf("delta=3D%g, eq=3D%d\n", r_val - vals[o], r_val =3D=3D vals[o])=
;

=09if (r_val =3D=3D vals[o]) {
=09  break;
=09}
      }
      again =3D (o !=3D rc);
    } while (again);
  }
}

#else

double get_real_root(int minrange, int maxrange, int divider) {
  return ((rand() % ((maxrange-minrange)*divider)) + minrange*divider) / (d=
ouble)divider;
}

int main (void)
{
  int i;

  for (i =3D 0; i < 3; i++) {
    create_testcase();
  }
  return 0;
}
#endif
---------------------- cut here ----------------------------------

A reply is appreciated,

best regards and thanks for your work, Heiko Ei=DFfeldt
Technical Details
Message-ID: <Pine.LNX.3.91.961222045605.3545A-100000@colossus.escape.de>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 113d02,b8b4994f2437fd8,start
X-Google-Attributes: gid113d02,public
From: heiko@colossus.escape.de (Heiko Eissfeldt)
Subject: floating point deviations with -fcaller-saves
Date: 1996/12/22
Message-ID: <Pine.LNX.3.91.961222045605.3545A-100000@colossus.escape.de>
X-Deja-AN: 205377599
sender: bug-gcc-request@prep.ai.mit.edu
content-type: TEXT/PLAIN; charset=ISO-8859-1
x-gateway: relay7.UU.NET from bug-gcc to gnu.gcc.bug; Sat, 21 Dec 1996 23:29:38 EST
mime-version: 1.0
newsgroups: gnu.gcc.bug