From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: =?ISO-8859-1?Q?Andreas_V=F6gele?= Newsgroups: gmane.lisp.guile.devel Subject: Re: mpz_import Date: Sun, 29 Aug 2004 17:33:45 +0200 Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Message-ID: References: <877jrqr7ni.fsf@zip.com.au> <77888DE4-F4FF-11D8-9F1C-000D93673682@gmx.net> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 (Apple Message framework v619) Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: sea.gmane.org 1093793667 23240 80.91.224.253 (29 Aug 2004 15:34:27 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sun, 29 Aug 2004 15:34:27 +0000 (UTC) Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sun Aug 29 17:34:18 2004 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1C1RhC-0003sW-00 for ; Sun, 29 Aug 2004 17:34:18 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1C1Rly-00053Z-FC for guile-devel@m.gmane.org; Sun, 29 Aug 2004 11:39:14 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.33) id 1C1Rlq-00053B-JW for guile-devel@gnu.org; Sun, 29 Aug 2004 11:39:06 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.33) id 1C1Rlq-00052b-21 for guile-devel@gnu.org; Sun, 29 Aug 2004 11:39:06 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1C1Rlp-00052W-VS for guile-devel@gnu.org; Sun, 29 Aug 2004 11:39:06 -0400 Original-Received: from [213.165.64.20] (helo=mail.gmx.net) by monty-python.gnu.org with smtp (Exim 4.34) id 1C1Rgk-0006sz-Jx for guile-devel@gnu.org; Sun, 29 Aug 2004 11:33:50 -0400 Original-Received: (qmail 18735 invoked by uid 65534); 29 Aug 2004 15:33:49 -0000 Original-Received: from pD9E61EDC.dip.t-dialin.net (EHLO [192.168.1.15]) (217.230.30.220) by mail.gmx.net (mp012) with SMTP; 29 Aug 2004 17:33:49 +0200 X-Authenticated: #14729429 In-Reply-To: Original-To: guile-devel@gnu.org X-Mailer: Apple Mail (2.619) X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: main.gmane.org gmane.lisp.guile.devel:4053 X-Report-Spam: http://spam.gmane.org/gmane.lisp.guile.devel:4053 Marius Vollmer writes: > Andreas V=F6gele writes: > >> Marius Vollmer writes: >> >>> [...] I was thinking whether we should export something like >>> mpz_import and mpz_export for our integers in general. Such an >>> operation is obviously needed sometimes. >> >> I think that's a good idea. I could, for example, use such wrappers >> to convert message digests into bigints. Currently, I use scm_ash and >> scm_sum in a loop to convert MD5 hashes into numbers. > > Yes, that sounds the prototypical application. Maybe you have a > concrete suggestion of how Guile should offer these functions (i.e., > documented function prototypes)? That would be great. I'd include in libguile/numbers.h and provide two C functions: void scm_to_mpz_t (SCM val, mpz_t result); SCM scm_from_mpz_t (mpz_t val); Here's an example that converts a digest buffer into a Scheme number: mpz_t z; mpz_init (z); mpz_import (z, digestlen, 1, 1, 0, 0, digest); SCM n =3D scm_from_mpz_t (z); mpz_clear (z); ... Another example that converts a Scheme number into an mpz_t number: mpz_t z; mpz_init (z); SCM n =3D scm_from_int (0); scm_to_mpz_t (n, z); ... mpz_add_ui (z, z, 100); ... SCM res =3D scm_from_mpz_t (z); mpz_clear (z); return res; Another option would be to initialize a new mpz_t variable in=20 scm_to_mpz_t() and to return that value. But I don't like this approach=20= very much since the initialisation happens in the library whereas the=20 memory must be freed by the user: mpz_t z =3D scm_to_mpz_t (n); mpz_clear (z); The first approach also allows users to reuse variables of type mpz_t.=20= This might be useful in loops, for example: mpz_t z; mpz_init (z); for (walk =3D list; SCM_CONSP (walk); walk =3D SCM_CDR (walk)) { elt =3D SCM_CAR (walk); scm_to_mpz_t (elt, z); ... } mpz_clear (z); I'm not sure about the order of arguments in scm_to_mpz_t(). All the=20 assignment functions provided by GNU MP expect the result parameter as=20= the first argument, e.g. mpz_set_ui (result, 100) and mp_add_ui=20 (result, op, 100). So maybe the arguments ought to be reversed in the=20 following documentation and code. Documentation for api-data.texi: @deftypefn {C Function} void scm_to_mpz_t (SCM val, mpz_t rop) Assign @var{val} to the multiple precision integer @var{rop}. @var{val} must be an exact integer, otherwise a `wrong-type-arg' error will be signalled. @var{rop} must have been initialized with @code{mpz_init} before this function is called. When @var{rop} is no longer needed the occupied space must be freed with @code{mpz_clear}. @xref{Initializing Integers,,, gmp, GNU MP Manual}, for details. @end deftypefn @deftypefn {C Function} SCM scm_from_mpz_t (mpz_t val) Return the @code{SCM} value that represents @var{val}. @end deftypefn Implementation: void scm_to_mpz_t (SCM val, mpz_t rop) { if (SCM_I_INUMP (val)) mpz_set_si (rop, SCM_I_INUM (val)); else if (SCM_BIGP (val)) mpz_set (rop, SCM_I_BIG_MPZ (val)); else scm_wrong_type_arg (NULL, 0, val); } SCM scm_from_mpz_t (mpz_t val) { SCM z =3D scm_double_cell (scm_tc16_big, 0, 0, 0); mpz_init_set (SCM_I_BIG_MPZ (z), val); return scm_i_normbig (z); } _______________________________________________ Guile-devel mailing list Guile-devel@gnu.org http://lists.gnu.org/mailman/listinfo/guile-devel