Subject: GCC double -> int64_t conversion
To: None <port-arm32@NetBSD.ORG>
From: Simon Levitt <S.Levitt@motiv.co.uk>
List: port-arm32
Date: 12/16/1997 10:54:21
Hiya,

I've come across what I believe must be a problem in part of GCC 
(2.7.2) on RiscBSD 1.2G. 

The internal conversion from a double to a long long (int64_t, quad_t 
or whatever its called today) doesn't appear to work in compiled 
code. It does however work correctly when the operations are checked 
in gdb.

ie. the code:

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

int main(int argc, char **argv) 
{
    double fp = 15.79973527472; // Can't remember the extact number 
                                // I've got but hopefully will still
                                // exhibit the problem
    long long res = (long long) fp;

    fprintf(stdout, "%g -> %li\n", fp, (long) res); 
                                // Convert res into long for display
                                // purposes - should only be 15 in
                                // correct case
}

On solaris works fine. With a number very similar to the above, on 
RiscBSD (compiled with 'gcc -o fred fred.c') produces an answer of a 
few million (may even be billion) which clearly isn't correct.

But trace this program in gdb (again the normal 1.2G release version) 
and type 'p (long long) fp', once fp has been initialised of course!, 
and you get the correct result.

Whats going on? - Have I forgotten a compilation option or something, 
or is this more serious than that?

Does anyone have, or know where I can get hold of a conversion 
function?

Simon.,
----------------------------------------------------------------------
            Simon Levitt, Consultant @ Motiv Systems Ltd., 
      Orwell House,  Cowley Road,  Cambridge,  CB4 4WY,  ENGLAND       
S.Levitt@motiv.co.uk  Ph: +44 (0)1223 576318  Fax: +44 (0)1223 576319        
------- http://www.motiv.co.uk/ ----- http://java.motiv.co.uk/ -------