chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] u8vector to numbers bignum


From: chi
Subject: Re: [Chicken-users] u8vector to numbers bignum
Date: Fri, 29 May 2015 19:42:08 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0

> (define (bignum->u8vector/host-byte-order num)
>   (unless (bignum? num) (error "Argument is not a bignum!" num))
> 
>   (let* ((word-size (cond-expand (64bit 8) (else 4))) ; Is there a better way?
>          (bignum-contents (##sys#slot num 1))   ; Digits preceded by sign word
>          (bignum-digits (substring bignum-contents word-size)))
>     (blob->u8vector/shared (string->blob bignum-digits))))

That's pretty fascinating. I wonder if you could even just C_copy_memory the
bignum-contents+word into an existing blob of large enough size, instead of that
substring/string->blob. Assuming you know your number is of fixed but >64 bit
size, that is. (1024 bit for instance). If you didn't want native order, instead
of C_copy_memory you could do a word based converter to network byte order
called I dunno, C_copy_flipwords.

Incidentally, chicken.h defines C_WORD_SIZE, and I think it's a fairly safe
assumption even though the core never explicitly exports that value into scheme.
You could probably do this pretty confidently:

(define word-octets (/ (foreign-value "C_WORD_SIZE" integer) 8)



reply via email to

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