From khansen at stat.Berkeley.EDU  Sun Apr 16 10:43:03 2006
From: khansen at stat.Berkeley.EDU (Kasper Daniel Hansen)
Date: Sun, 16 Apr 2006 01:43:03 -0700
Subject: [Rd] c++ code on amd64
Message-ID: <EA2A797B-2427-4777-BD91-2E7217FCD318@stat.berkeley.edu>

Hi

Brief synopsis:

I am having a rather peculiar problem regarding a C++ library. It  
seems that functions from this library behave differently when  
compiled using R as opposed to being compiled directly from the  
command line. The problem is only seen on the amd64 platform (using  
gcc 4.0.2) and not on either of Solaris (both 32 bit and 64 bit), Mac  
OS and Windows.

A bit more detail:

Basically the c++ library contains a class and methods for parsing  
text files in specific formats. These files contains integers as well  
floats.

If I write a stand-alone c++ program with a line like

   ....
   cout << "x: " cel.GetIndexToX(1) << " intensity: " <<  
cel.GetIntensity(1) << endl;
   ....

(here cel is pointing to a specific file while GetIndexToX returns an  
integer (in what is essentially the first row), while GetIntensity  
returns a float), it works fine: the two numbers are printed to stdout.

If I instead embed the code inside R like

extern "C" {
    ....
    Rprintf("x: %d intensity: %f", cel.GetIndexToX(1),  
cel.GetIntensity(1)):
   ...
}

and do a R CMD INSTALL, I am able to read the integer from the file,  
but not the float. The float always returns 0.00000. This is very  
strange considering that the code is basically identical in the two  
cases (except for the extern part and inclusion of the R header  
files), and that the integer number is being read perfectly!

I would say that the fact that the stand-alone program works is  
indicating that the C++ library actually works. Further indication  
that this is the case is the fact that our R package works fine on  
Mac G4, Solaris and Windows.

The only real difference I can see is that the amd64 platform is the  
only little-endian 64-bit platform.

My working hypothesis is that the float is being read and then  
truncated.

I am extremely baffled by this. Do anyone have an idea on where I  
should start looking?

System details:

uname -a
Linux shadowfax.berkeley.edu 2.6.12-1-amd64-k8-smp #1 SMP Wed Sep 28  
02:57:49 CEST 2005 x86_64 GNU/Linux

gcc --version
gcc (GCC) 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu9)

R is version 2.2.1 build using a standard ./configure, make step.


/Kasper



[Rd] c++ code on amd64
Kasper Daniel Hansen