[gambit-list] How to return multiple "double" values from a C function ?

Marc Feeley feeley at iro.umontreal.ca
Sun Dec 22 08:03:54 EST 2019


For returning multiple doubles, “res” should be an f64vector, and the third parameter of ___F64VECTORSET is the double value (no conversion required).

Marc



> On Dec 22, 2019, at 7:30 AM, Ryuho Yokoyama <ryuho8 at gmail.com> wrote:
> 
> 
> [gambit-list] FFI C out parameters
> https://mailman.iro.umontreal.ca/pipermail/gambit-list/2015-January/007696.html
> 
> According to the mailing list entry above:
> 
> 
> multiple "int" values:
> ----------
> (c-declare "
> 
> void add_mul(int x, int y, ___SCMOBJ res) {
> 
>    int add = x+y;
>    int mul = x*y;
> 
>    ___VECTORSET(res, ___FIX(0), ___FIX(add))
>    ___VECTORSET(res, ___FIX(1), ___FIX(mul))
> }
> ")
> 
> (define (add-mul x y)
>   (let ((res (vector 0 0)))
>     ((c-lambda (int int scheme-object) void "add_mul") x y res)
>     (values (vector-ref res 0)
>             (vector-ref res 1))))
> 
> (receive (add mul) (add-mul 3 7)
>  (pp (list 'add= add 'mul= mul)))
> 
> ;; prints: (add= 10 mul= 21)
> ----------
> 
> In an example code,  C function is returning multiple "int" values.
> How to write an example code returning multiple "double" values from a C function ?
> 
> 
> multiple "double" values:
> ----------
> (c-declare "
> 
> void add_mul(double x, double y, ___SCMOBJ res) {
> 
>    double add = x+y;
>    double mul = x*y;
> 
>    ___F64VECTORSET(res, ___FIX(0), ___???(add))
>    ___F64VECTORSET(res, ___FIX(1), ___???(mul))
> }
> ")
> 
> (define (add-mul x y)
>   (let ((res (vector 0 0)))
>     ((c-lambda (double double scheme-object) void "add_mul") x y res)
>     (values (vector-ref res 0)
>             (vector-ref res 1))))
> 
> (receive (add mul) (add-mul 3.5 7.0)
>  (pp (list 'add= add 'mul= mul)))
> 
> ;; prints: (add= 10.5 mul= 24.5)
> ----------
> 
>    ___F64VECTORSET(res, ___FIX(0), ___???(add))
>            ↑                         ↑
>           ??                        ??
> 
> 1) Is it possible to use ___F64VECTORSET macro instead of ___VECTORSET macro ?
> 
> 2) Is it possible to define a ___F64(x) macro like a macro ___FIX(x) ?
> 
> 
> Thank you very much in advance.
> 
> 
> _______________________________________________
> Gambit-list mailing list
> Gambit-list at iro.umontreal.ca
> https://mailman.iro.umontreal.ca/cgi-bin/mailman/listinfo/gambit-list





More information about the Gambit-list mailing list