you've piqued my curiosity :-)
Update: I forgot the package statements. D'oh! (Code updated)
This is what I have so far. Not working yet because of an IC problem that I've encountered before, but can't remember how to fix;
and the math is almost certainly wrong as is; but I threw it together to try and get a starting point:
#! perl -slw use Config; package F128; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<END_C, NAME => 'F128', CLEAN_AFTER_BUILD =>0, TYPEM +APS => './F128.typemap'; #include "../C/mytypes.h" #define CLASS "F128" typedef struct { U64 scale; U64 precision; } F128; F128 *new( I64 scale, U64 precision ) { F128 *n = malloc( sizeof( F128 ) ); n->scale = scale; n->precision = precision; return n; } F128 *add( F128 *a, F128 *b ) { if( a->scale != b->scale ) { while( a->scale > b->scale ) { a->scale >>= 1; a->precision <<= 1; } while( a->scale < b->scale ) { a->scale <<= 1; a->precision >>= 1; } } a->precision += b->precision; return a; } F128 *multiply( F128 *a, F128 *b ) { a->precision *= b->precision; a->scale += b->scale; return a; } SV *toString( F128 *a ) { char *out = malloc( a->scale ); U32 c = sprintf( out, "%I64ue%I64i", a->precision, a->scale ); return newSVpv( out, c ); } void DESTROY( F128 *a ) { free( a ); return; } END_C package main; my $Fa = F128::new( -10, 45 ); my $Fb = F128::new( -10, 45 ); my $Fc = F128::new( -10, 45 ); print $Fa->toString; $Fa->add( $Fb ); print $Fa->toString; $Fc->multiply( $Fb ); print $Fc->toString;
The typemap:
TYPEMAP const char * T_PV F128 * O_OBJECT U64 T_UV I64 T_IV U8 T_UV U8 * T_PV 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 );
I'm currently getting:
45e-10 90e-10 2025e-20
Which is almost there for my requirements.
And I know its a trivial fix, but .... I can't remember what. syphilis Got your ears on?.
In reply to Re^8: Need more precision. (Updated.)(And again)
by BrowserUk
in thread Need more precision.
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |