What causes the following?
;;; Code:
(define (disp . xs) (for-each display xs) (newline))
(define-syntax show (syntax-rules () ((_ x) (begin (write 'x) (display " => ") (write x) (newline)))))
(define try-as-u8 (c-lambda (scheme-object) size_t "___return(___U8VECTORLENGTH(___arg1));"))
(define try-as-u16 (c-lambda (scheme-object) size_t "___return(___U16VECTORLENGTH(___arg1));"))
(define try-as-u32 (c-lambda (scheme-object) size_t "___return(___U32VECTORLENGTH(___arg1));"))
(define try-as-u64 (c-lambda (scheme-object) size_t "___return(___U64VECTORLENGTH(___arg1));"))
(define n 1024) (disp "u8vector of " n " elements") (let ((v (make-u8vector n))) (show (try-as-u8 v)) (show (try-as-u16 v)) (show (try-as-u32 v)) (show (try-as-u64 v))) (newline) (disp "u16vector of " n " elements") (let ((v (make-u16vector n))) (show (try-as-u8 v)) (show (try-as-u16 v)) (show (try-as-u32 v)) (show (try-as-u64 v))) (newline) (disp "u32vector of " n " elements") (let ((v (make-u32vector n))) (show (try-as-u8 v)) (show (try-as-u16 v)) (show (try-as-u32 v)) (show (try-as-u64 v)))
;;; Output:
u8vector of 1024 elements (try-as-u8 v) => 4096 (try-as-u16 v) => 2048 (try-as-u32 v) => 1024 (try-as-u64 v) => 512
u16vector of 1024 elements (try-as-u8 v) => 8192 (try-as-u16 v) => 4096 (try-as-u32 v) => 2048 (try-as-u64 v) => 1024
u32vector of 1024 elements (try-as-u8 v) => 16384 (try-as-u16 v) => 8192 (try-as-u32 v) => 4096 (try-as-u64 v) => 2048
Afficher les réponses par date
You are using the `___U8VECTORLENGTH` macro and friends incorrectly. They return a fixnum but your `c-lambda`s return `size_t`… so you want to say `___INT(___U8VECTORLENGTH(___arg1))` which converts the fixnum to an `int`, or use `scheme-object` instead of `size_t` as the return type of the `c-lambda`s.
Marc
On Aug 9, 2020, at 10:09 AM, Lassi Kortela lassi@lassi.io wrote:
What causes the following?
;;; Code:
(define (disp . xs) (for-each display xs) (newline))
(define-syntax show (syntax-rules () ((_ x) (begin (write 'x) (display " => ") (write x) (newline)))))
(define try-as-u8 (c-lambda (scheme-object) size_t "___return(___U8VECTORLENGTH(___arg1));"))
(define try-as-u16 (c-lambda (scheme-object) size_t "___return(___U16VECTORLENGTH(___arg1));"))
(define try-as-u32 (c-lambda (scheme-object) size_t "___return(___U32VECTORLENGTH(___arg1));"))
(define try-as-u64 (c-lambda (scheme-object) size_t "___return(___U64VECTORLENGTH(___arg1));"))
(define n 1024) (disp "u8vector of " n " elements") (let ((v (make-u8vector n))) (show (try-as-u8 v)) (show (try-as-u16 v)) (show (try-as-u32 v)) (show (try-as-u64 v))) (newline) (disp "u16vector of " n " elements") (let ((v (make-u16vector n))) (show (try-as-u8 v)) (show (try-as-u16 v)) (show (try-as-u32 v)) (show (try-as-u64 v))) (newline) (disp "u32vector of " n " elements") (let ((v (make-u32vector n))) (show (try-as-u8 v)) (show (try-as-u16 v)) (show (try-as-u32 v)) (show (try-as-u64 v)))
;;; Output:
u8vector of 1024 elements (try-as-u8 v) => 4096 (try-as-u16 v) => 2048 (try-as-u32 v) => 1024 (try-as-u64 v) => 512
u16vector of 1024 elements (try-as-u8 v) => 8192 (try-as-u16 v) => 4096 (try-as-u32 v) => 2048 (try-as-u64 v) => 1024
u32vector of 1024 elements (try-as-u8 v) => 16384 (try-as-u16 v) => 8192 (try-as-u32 v) => 4096 (try-as-u64 v) => 2048
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://mailman.iro.umontreal.ca/cgi-bin/mailman/listinfo/gambit-list
You are using the `___U8VECTORLENGTH` macro and friends incorrectly. They return a fixnum but your `c-lambda`s return `size_t`… so you want to say `___INT(___U8VECTORLENGTH(___arg1))` which converts the fixnum to an `int`, or use `scheme-object` instead of `size_t` as the return type of the `c-lambda`s.
Thanks! I didn't notice the fixnum tagging.
How does one build lists in a c-lambda? This:
(pp ((c-lambda () scheme-object "___return(___CONS(___FIX(1), ___CONS(FIX(2), ___NUL)));")))
gives errors about:
/Users/lassi/.local/include/gambit.h:2459:30: error: '___hp' undeclared (first use in this function); did you mean '___fp'? 2459 | #define ___BEGIN_ALLOC_PAIR()___hp[0] = ___MAKE_HD_WORDS(___PAIR_SIZE,___sPAIR) | ^~~~~
The `___CONS` macro is meant to be used in C code generated by the gsc compiler (it does the allocation in an optimized way, it allocates a movable object and does not include the check for heap overflow).
You want to call `___make_pair` to allocate a still object with `___EXT(___make_pair) (___ps, x_scmobj, y_scmobj)` and you need to check for heap overflow explicitly with `___FIXNUMP(result)`.
For a full example, look at the definition of `STRINGS_to_SCMOBJ` in the Gambit manual (page 237).
Marc
On Aug 9, 2020, at 12:39 PM, Lassi Kortela lassi@lassi.io wrote:
You are using the `___U8VECTORLENGTH` macro and friends incorrectly. They return a fixnum but your `c-lambda`s return `size_t`… so you want to say `___INT(___U8VECTORLENGTH(___arg1))` which converts the fixnum to an `int`, or use `scheme-object` instead of `size_t` as the return type of the `c-lambda`s.
Thanks! I didn't notice the fixnum tagging.
How does one build lists in a c-lambda? This:
(pp ((c-lambda () scheme-object "___return(___CONS(___FIX(1), ___CONS(FIX(2), ___NUL)));")))
gives errors about:
/Users/lassi/.local/include/gambit.h:2459:30: error: '___hp' undeclared (first use in this function); did you mean '___fp'? 2459 | #define ___BEGIN_ALLOC_PAIR()___hp[0] = ___MAKE_HD_WORDS(___PAIR_SIZE,___sPAIR) | ^~~~~
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://mailman.iro.umontreal.ca/cgi-bin/mailman/listinfo/gambit-list