[gambit-list] Returning homogeneous numeric vectors from C to Scheme

Lassi Kortela lassi at lassi.io
Sat Jul 18 07:10:34 EDT 2020


> ___SCMOBJ result = ___EXT(___alloc_scmobj) (NULL, ___sF64VECTOR, 
> count*sizeof(___F64));
> double *ptr = ___CAST(double*,___BODY(result));
> 
> ...
> 
> ___EXT(___release_scmobj)(result);
> return result;

Nice. Thanks for the super-quick reply!

(define get-test-s64vector
   (c-lambda () scheme-object #<<c-lambda-end
     static ___S64 c_ints[32];
     size_t nbytes = sizeof(c_ints);
     size_t n = nbytes / sizeof(___S64);
     size_t i;
     for (i = 0; i < n; i += 2) c_ints[i] = i;
     for (i = 1; i < n; i += 2) c_ints[i] = -i;
     ___SCMOBJ result = ___EXT(___alloc_scmobj)(NULL, ___sS64VECTOR, 
nbytes);
     ___S64 *scheme_ints = ___CAST(___S64 *, ___BODY(result));
     for (i = 0; i < n; i++) scheme_ints[i] = c_ints[i];
     ___EXT(___release_scmobj)(result);
     ___return(result);
c-lambda-end
))

$ gsc test.scm
$ gsi test.o1 -e "(pp (get-test-s64vector))"
#s64(0 -1 2 -3 4 -5 6 -7 8 -9 10 -11 12 -13 14 -15 16 -17 18 -19 20 -21 
22 -23 24 -25 26 -27 28 -29 30 -31)




More information about the Gambit-list mailing list