BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:
UPDATE: Never mind. Fixed it.
Why does this IC code produce different output each time given the same input?
#! perl -slw use Config; package F128; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => 'F128', CLEAN_AFTER_BUILD =>0, TYP +EMAPS => './F128.typemap'; #include "../C/mytypes.h" typedef union { struct { unsigned sign : 1; unsigned exponent :11; U64 mantissa :52; }; double d; U64 u; } DFields; void test( double d ) { U32 i; DFields *df = (DFields*)&d; for( i = 0; i < 64; ++i ) printf( "%u", ( df->u & ( 1ull<<i ) ) >> + i ); printf( "\n" ); printf( "%.16f mantissa:%I64u exponent:%d sign:%u\n", df->d, df->m +antissa, df->exponent -1023, df->sign ); return; } END_C package main; F128::test( 0.45e-10 );
typemap:
TYPEMAP const char * T_PV F128 * O_OBJECT U64 T_UV I64 T_IV U8 T_UV U8 * T_PV double T_NV INPUT O_OBJECT if( sv_isobject($arg) && ( SvTYPE( SvRV($arg) ) == SVt_PVMG ) ) $var = INT2PTR( $type, SvIV( (SV*)SvRV( $arg ) ) ); else{ warn( \"${Package}::$func_name() -- $var is not a blessed +SV reference\" ); XSRETURN_UNDEF; } OUTPUT # The Perl object is blessed into 'CLASS', which should be a # char* having the name of the package for the blessing. O_OBJECT sv_setref_pv( $arg, (char *)CLASS, (void*)$var );
Results of consequestive runs:
0001010010001001000101011011101111110100101111010001001110111100 0.0000000000450000 mantissa:2403864 exponent:-875 sign:0 C:\test\mandlebrot>F128.pl 0001010010001001000101011011101111110100101111010001001110111100 0.0000000000450000 mantissa:2141720 exponent:-875 sign:0 C:\test\mandlebrot>F128.pl 0001010010001001000101011011101111110100101111010001001110111100 0.0000000000450000 mantissa:1093144 exponent:-875 sign:0 C:\test\mandlebrot>F128.pl 0001010010001001000101011011101111110100101111010001001110111100 0.0000000000450000 mantissa:2338328 exponent:-875 sign:0
The double passed doesn't change; the binary dumped via the union's U64 doesn't change; but somehow the value of the mantissa field varies?? Why gives?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: IC problem.[Solved, (feel free to reap)]
by cavac (Prior) on Jun 12, 2015 at 11:54 UTC | |
by BrowserUk (Patriarch) on Jun 12, 2015 at 12:08 UTC | |
by cavac (Prior) on Jun 12, 2015 at 12:58 UTC | |
by BrowserUk (Patriarch) on Jun 12, 2015 at 14:04 UTC | |
by cavac (Prior) on Jun 12, 2015 at 14:11 UTC | |
|