in reply to accessing mpz_t * of Math::BigInt::GMP from XS
I feel that it should also be do-able with Math::BigInt::GMP, too - but I'm currently stuck on trying to work out how that module's mpz_from_sv_nofail() passes its own sv_derived_from() check.use Math::GMP; use Math::GMPz; use Inline C => Config => LIBS => '-lgmp', BUILD_NOISY => 1, USING => ParseRecDescent; use Inline C => <<'EOC'; #include <gmp.h> void my_mul_si(SV * rop, SV * op, SV * si) { mpz_mul_si (*(INT2PTR(mpz_t *, SvIV(SvRV(rop)))), *(INT2PTR(mpz_t *, SvIV(SvRV(op)))), SvIV(si)); } EOC my $ x = -10; my $g_rop = Math::GMP->new(); my $g_op = Math::GMP->new(1234567); my $z_rop = Math::GMPz->new(); my $z_op = Math::GMPz->new(1234567); my_mul_si($g_rop, $g_op, $x); my_mul_si($z_rop, $z_op, $x); my_mul_si($g_op, $g_op, $x); my_mul_si($z_op, $z_op, $x); print $g_rop, "\n", $z_rop, "\n", $g_op, "\n", $z_op, "\n"; __END__ Outputs: -12345670 -12345670 -12345670 -12345670
What's the trick ?use Math::BigInt lib => 'GMP'; use warnings; use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'EOC'; void derivation(SV * obj) { if(sv_derived_from(obj, "Math::BigInt::GMP")) printf("A Math::BigInt::GMP object\n"); else printf("Not a Math::BigInt::GMP object\n"); if(sv_derived_from(obj, "Math::BigInt")) printf("A Math::BigInt object\n"); else printf("Not a Math::BigInt object\n"); } EOC my $o = Math::BigInt->new(1234); derivation($o); __END__ Outputs: Not a Math::BigInt::GMP object A Math::BigInt object
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: accessing mpz_t * of Math::BigInt::GMP from XS
by dkg (Acolyte) on Mar 18, 2011 at 20:25 UTC | |
|
Re^2: accessing mpz_t * of Math::BigInt::GMP from XS
by dkg (Acolyte) on Mar 18, 2011 at 20:50 UTC | |
by syphilis (Archbishop) on Mar 18, 2011 at 22:55 UTC | |
by syphilis (Archbishop) on Mar 19, 2011 at 04:34 UTC |