chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] some questions about easyffi and foreign code


From: Hugo Arregui
Subject: Re: [Chicken-users] some questions about easyffi and foreign code
Date: Sun, 3 Feb 2013 09:49:21 -0300

Hey Kristian,

many thanks for you response!

> Yeah, that example wasn't working for me either. If you put "(use easyffi)"
> at the top of the file though, it should work. Note that easyffi is
> deprecated, use bind instead:
>
> (use bind)
> (bind* "double modf(double x, ___out double *iptr);")
> (let-values ([(frac int) (modf 33.44)])
>     (print frac " " int))

Great!, I'm changing it right now.

> If 64-bit integers are all you need, perhaps you can use the foreign type
> unsigned-integer64? I'm guessing the C compiler will handle that even if
> you're on a 32bit system. I'm not sure how Chicken will handle integer64's
> if you're on a 32bit system though.

Well, I tried that at first (I have a 32bits machine):

(use numbers)
(use bind)

(define double->uint64
  (foreign-lambda* unsigned-integer64 ((double d))
#<<EOS
     union { double d; uint64_t i; } mem;
     mem.d = d;
     C_return(mem.i);
EOS
))

(print (flonum?  (double->uint64 1.3)))

that code prints "#t".

> If you want to continue using your multiword version, you could look into
> the u32vector foreign type. It will give you a nice array on the C-side, and
> a nice vector on the Chicken side:

I exactly do that, but found another problem. u32 uses 31bit, so it's
not working either, let me show you:

(use srfi-4)
(import foreign)

(define double->u32vector
  (foreign-lambda* void ((double d) (nonnull-u32vector v))
#<<EOS
     union { double d; uint64_t i; } mem;
     mem.d = d;
     uint32_t* p = (uint32_t*) &mem.i;
     v[0] = p[0];
     v[1] = p[1];
EOS
))

(let ((out (make-u32vector 2)))
  (double->u32vector 1.3 out)
  (print out))

prints "#u32(3435973837.0 1073007820)"

(Maybe I'm doing something wrong, i don't know).

So finally i have it working with uint8, and there's no problem. But I
felt curious about all this steps.

Thanks again!
Regards,
Hugo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]