Bug 37849 - double to array conversion
double to array conversion
Status: RESOLVED DUPLICATE of bug 21920
Product: gcc
Classification: Unclassified
Component: c
4.3.2
: P3 major
: ---
Assigned To: Not yet assigned to anyone
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2008-10-16 13:00 UTC by Artur Kornacki
Modified: 2008-10-16 13:30 UTC (History)
27 users (show)

See Also:
Host: x86_64-linux-gnu
Target: x86_64-linux-gnu
Build: x86_64-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Artur Kornacki 2008-10-16 13:00:32 UTC
Double conversion to memory and from memory array is broken with O3 optimization enabled. Both functions conv1 ( conversion from double to uint16_t[4] array ) and conv2 ( conversion from uint16_t array to double ) are broken. 
To my knowlege the problem also exists in previous versions of gcc/g++ ( I've used gcc 4.2.4 and 4.1.x on my previous machine (32 bit environment)- and I've had problems with HPA ( high precision arithmetic ) library with O3 enabled ). 

Correct test program output ( with -O0 ) is
r=(0,0,0,16420)   <- double converted to array
d=10.000000       <- double reconverted from array

Enabling optimization -O3 gives 
r=(1600,64,0,0)
d=0.000000

Using g++ with -O3 gives
r=(1840,64,0,0)
d=0.000000

The following code shows the base of the problem I found in the HPA library.

------------- test5.c -------------
#include <stdio.h>
#include <stdint.h>

// move double to array
void conv1( double y, uint16_t* pr )
{
    uint16_t*	py;
    int		i;
    
    py = (uint16_t*)&y;    
    for(i=0;i<4;i++) pr[i] = py[i];
}

// get double from array
double conv2( uint16_t* pr )
{
    uint16_t	pa[4];
    int		i;
    
    for(i=0;i<4;i++) pa[i] = pr[i];
    
    return *((double*)pa);
}

int main( int argc, char* argv[] )
{
    uint16_t	r[4];
    double	d;
    int		i;
        
    conv1(10.,r);    

    printf("r=(%i,%i,%i,%i)\n", (int)r[0], (int)r[1], (int)r[2], (int)r[3]);
    
    d = conv2(r);
    
    printf("d=%lf\n", d);
}
------------- test5.c -------------

Result of gcc -v -save-temps -O3 test5.c

Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.3.2-1ubuntu10' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext--enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.3.2 (Ubuntu 4.3.2-1ubuntu10)
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-mtune=generic'
 /usr/lib/gcc/x86_64-linux-gnu/4.3.2/cc1 -E -quiet -v test5.c -D_FORTIFY_SOURCE=2 -mtune=generic -O3 -fpch-preprocess -o test5.i
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../x86_64-linux-gnu/include"
ignoring nonexistent directory "/usr/include/x86_64-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.3.2/include
 /usr/lib/gcc/x86_64-linux-gnu/4.3.2/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-mtune=generic'
 /usr/lib/gcc/x86_64-linux-gnu/4.3.2/cc1 -fpreprocessed test5.i -quiet -dumpbase test5.c -mtune=generic -auxbase test5 -O3 -version -fstack-protector -o test5.s
GNU C (Ubuntu 4.3.2-1ubuntu10) version 4.3.2 (x86_64-linux-gnu)
        compiled by GNU C version 4.3.2, GMP version 4.2.2, MPFR version 2.3.1.
warning: MPFR header version 2.3.1 differs from library version 2.3.2.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 9fb216f9df26c4d658fb4fb0c8a8291a
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-mtune=generic'
 as -V -Qy -o test5.o test5.s
GNU assembler version 2.18.93 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.18.93.20081009
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.3.2/:/usr/lib/gcc/x86_64-linux-gnu/4.3.2/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.3.2/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.3.2/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.3.2/:/usr/lib/gcc/x86_64-linux-gnu/4.3.2/:/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-mtune=generic'
 /usr/lib/gcc/x86_64-linux-gnu/4.3.2/collect2 --eh-frame-hdr -m elf_x86_64 --hash-style=both -dynamic-linker /lib64/ld-linux-x86-64.so.2 -zrelro /usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.3.2/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.3.2 -L/usr/lib/gcc/x86_64-linux-gnu/4.3.2 -L/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../.. test5.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.3.2/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/crtn.o
Comment 1 Andreas Schwab 2008-10-16 13:30:00 UTC

*** This bug has been marked as a duplicate of 21920 ***