Hi,
I see in the include file that mpz_t is defined as follow:

typedef __mpz_struct mpz_t[1];

and in turn __mpz_struct is:

typedef struct
{
  int _mp_alloc;
  int _mp_size;
  mp_limb_t *_mp_d;
} __mpz_struct;

with mp_lib_t defined in three different ways, according to the value of a
macro, defined at compile time.
I wrote a C program which outputs the value of sizeof(mp_limb_t). In my
case (Ubuntu Linux) that value is 8, so mp_limb_t definition is:

typedef unsigned long int mp_limb_t;

On the Raku side, the mpz_t definition would be:

class mpz_t is repr('CStruct') is export {
  has int32 _mp_alloc;
  has int32 _mp_size;
  has CArray[uint64] _mp_d;
}

I don't know the library at all, so I guessed that _mp_d would be an array.
If it's more useful to consider it just a pointer, declare it as
Pointer[uint64].
As you can see it's very easy and intuitive. The documentation (
https://docs.raku.org/language/nativecall) is very useful. If you need more
examples, look at the code of some of the modules that use the NativeCall
interface (https://modules.raku.org/t/NATIVECALL).

I've been using the NativeCall interface in many modules, see for example
this interface to the interpolation section of the GNU Scientific Library
https://github.com/frithnanth/raku-Math-Libgsl-Interpolation/blob/master/lib/Math/Libgsl/Raw/Interpolation.rakumod

On Sun, May 2, 2021 at 1:30 PM sisyphus <sisyphus...@gmail.com> wrote:

> On Wed, Apr 28, 2021 at 5:38 PM Fernando Santagata <
> nando.santag...@gmail.com> wrote:
>
>> ,
>> If you've found the Perl XS interface easy to work with, give Raku's
>> NativeCall a try, you will find it amazing. Since you already know the C
>> libraries, I guess you can cook an interface in no time.
>>
>>
> Thanks for the pointer, Fernando.
> I did take a look at "NativeCall".
> The basics are pretty straightforward, and worked well enough on my
> Windows 7 box:
>
> ###########################
> use NativeCall;
> sub mpfr_get_default_prec() returns int32 is native('libmpfr-6') { * }
> say "# ", mpfr_get_default_prec();
>
> sub __gmpf_get_default_prec() returns int32 is native('libgmp-10') { * }
> say "# ", __gmpf_get_default_prec();
>
> # Correctly outputs:
> # 53
> # 64
> ###########################
>
> The tricky bit is that nearly all of the gmp library functions want to
> take either an mpz_t, mpq_t, mpn_t or mpf_t argument, but NativeCall knows
> nothing about these types.
> Similarly most of the mpfr library functions take an mpfr_t argument,
> which is also a type that's unknown to NativeCall.
>
> I guess it might be achievable via passing pointers and/or structs, both
> of which are documented in the NativeCall docs.
> However, I haven't knuckled down to working my way through it - and I'm
> not even sure that it's do-able.
>
> It would be much easier (and much more likely to happen ;-) if there was
> some publicly available demo code to light the way to accessing the gmp (or
> mpfr) library functions using raku's NativeCall capacity.
>
> Cheers,
> Rob
>


-- 
Fernando Santagata

Reply via email to