Yes, I eventually got around to passing $obj->{value}, but there's probably still something I'm missing. The following works for me - but it avoids a lot of the pre-processor stuff that Math::BigInt::GMP's GMP.xs contains, and I therefore wonder about its portability
use Math::BigInt lib => 'GMP';
use Inline C => Config =>
LIBS => '-lgmp',
USING => 'ParseRegExp',
BUILD_NOISY => 1;
use Inline C => <<'EOC';
#include <gmp.h>
mpz_t * mpz_from_sv_nofail (SV *sv)
{
MAGIC *mg;
if (!sv_derived_from(sv, "Math::BigInt::GMP"))
croak("not of type Math::BigInt::GMP");
mg = SvMAGIC(SvRV(sv));
return (mpz_t *)mg->mg_ptr;
}
mpz_t * mpz_from_sv (SV *sv)
{
mpz_t *mpz;
if (!(mpz = mpz_from_sv_nofail(sv)))
croak("failed to fetch mpz pointer");
return mpz;
}
void my_mul(SV * bi_rop, SV * bi_op, SV * si) {
mpz_t *t1, *t2;
t1 = mpz_from_sv(bi_rop);
t2 = mpz_from_sv(bi_op);
mpz_mul_si(*t1, *t2, SvIV(si) * 10);
}
EOC
my $rop = Math::BigInt->new(0);
my $op = Math::BigInt->new(123456);
my $x = -110;
my_mul($rop->{value}, $op->{value}, $x);
print $rop, "\n";
__END__
Outputs:
-13580160
I also wonder:
1) How is the change of sign being effected by my_mul() - given that it should be working only with 'value';
(
Update: Setting $op to -123456 and multiplying by -110 still results in an output of -13580160. So 'sign' is *not* being dealt with correctly ... which is what I expected. )
2) Why do I have to multiply the 3rd arg of my_mul() by 10 in order to have it produce the correct value in the Math::BigInt::GMP object.
I'm sure there's a very good (and likely very simple) explanation for both.
Cheers,
Rob
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.