unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* scm_from_ipv6
@ 2004-08-23  0:59 Kevin Ryde
  2004-08-23 10:27 ` scm_from_ipv6 Marius Vollmer
  0 siblings, 1 reply; 8+ messages in thread
From: Kevin Ryde @ 2004-08-23  0:59 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 302 bytes --]

A simplification,

        * socket.c (scm_from_ipv6): Just use mpz_import.  Don't bother trying
        to fit scm_from_ulong_long, since that uses mpz_import anyway.  Don't
        bother trying to fit scm_from_ulong, not really worth the trouble if
        addresses are more than 4 bytes usually.


[-- Attachment #2: socket.c.ipv6.diff --]
[-- Type: text/plain, Size: 1808 bytes --]

--- socket.c.~1.107.~	2004-08-20 11:00:38.000000000 +1000
+++ socket.c	2004-08-22 13:31:48.000000000 +1000
@@ -278,55 +278,7 @@
 static SCM
 scm_from_ipv6 (const scm_t_uint8 *src)
 {
-  int i = 0;
-  const scm_t_uint8 *ptr = src;
-  int num_zero_bytes = 0;
-  scm_t_uint8 addr[16];
-
-  /* count leading zeros (since we know it's bigendian, they'll be first) */ 
-  while (i < 16)
-    {
-      if (*ptr) break;
-      num_zero_bytes++;
-      i++;
-    }
-
-  if (SCM_SIZEOF_UNSIGNED_LONG_LONG != 0) /* compiler should optimize this */
-    {
-      if ((16 - num_zero_bytes) <= sizeof (unsigned long long))
-        {
-          /* it fits */
-          unsigned long long x;
-          
-          FLIPCPY_NET_HOST_128(addr, src);
-#ifdef WORDS_BIGENDIAN
-          memcpy (&x, addr + (16 - sizeof (x)), sizeof (x));
-#else
-          memcpy (&x, addr, sizeof (x));
-#endif
-          return scm_from_ulong_long (x);
-        }
-    }
-  else
-    {
-      if ((16 - num_zero_bytes) <= sizeof (unsigned long))
-        {
-          /* this is just so that we use INUM where possible.  */
-          unsigned long x;
-
-          FLIPCPY_NET_HOST_128(addr, src);          
-#ifdef WORDS_BIGENDIAN
-          memcpy (&x, addr + (16 - sizeof (x)), sizeof (x));
-#else
-          memcpy (&x, addr, sizeof (x));
-#endif
-          return scm_from_ulong (x);
-        }
-    }
-  /* otherwise get the big hammer */
-  {
     SCM result = scm_i_mkbig ();
-    
     mpz_import (SCM_I_BIG_MPZ (result),
                 1, /* chunk */
                 1, /* big-endian chunk ordering */
@@ -335,7 +287,6 @@
                 0, /* "nails" -- leading unused bits per chunk */
                 src);
     return scm_i_normbig (result);
-  }
 }  
 
 /* convert a host ordered SCM integer to a 128 bit IPv6 address in

[-- Attachment #3: Type: text/plain, Size: 143 bytes --]

_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: scm_from_ipv6
  2004-08-23  0:59 scm_from_ipv6 Kevin Ryde
@ 2004-08-23 10:27 ` Marius Vollmer
  2004-08-23 12:25   ` mpz_import (was: scm_from_ipv6) Andreas Vögele
  0 siblings, 1 reply; 8+ messages in thread
From: Marius Vollmer @ 2004-08-23 10:27 UTC (permalink / raw)


Kevin Ryde <user42@zip.com.au> writes:

> A simplification,
>
>         * socket.c (scm_from_ipv6): Just use mpz_import.  Don't bother trying
>         to fit scm_from_ulong_long, since that uses mpz_import anyway.  Don't
>         bother trying to fit scm_from_ulong, not really worth the trouble if
>         addresses are more than 4 bytes usually.

Nice.  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.  What do you think?


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 8+ messages in thread

* mpz_import (was: scm_from_ipv6)
  2004-08-23 10:27 ` scm_from_ipv6 Marius Vollmer
@ 2004-08-23 12:25   ` Andreas Vögele
  2004-08-23 13:19     ` mpz_import Marius Vollmer
  0 siblings, 1 reply; 8+ messages in thread
From: Andreas Vögele @ 2004-08-23 12:25 UTC (permalink / raw)


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.



_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: mpz_import
  2004-08-23 12:25   ` mpz_import (was: scm_from_ipv6) Andreas Vögele
@ 2004-08-23 13:19     ` Marius Vollmer
  2004-08-29 15:33       ` mpz_import Andreas Vögele
  0 siblings, 1 reply; 8+ messages in thread
From: Marius Vollmer @ 2004-08-23 13:19 UTC (permalink / raw)
  Cc: guile-devel

Andreas Vögele <voegelas@gmx.net> 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.


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: mpz_import
  2004-08-23 13:19     ` mpz_import Marius Vollmer
@ 2004-08-29 15:33       ` Andreas Vögele
  2004-08-31  7:47         ` mpz_import Andreas Vögele
  2004-09-21  0:28         ` mpz_import Marius Vollmer
  0 siblings, 2 replies; 8+ messages in thread
From: Andreas Vögele @ 2004-08-29 15:33 UTC (permalink / raw)


Marius Vollmer writes:

> Andreas Vögele <voegelas@gmx.net> 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 <gmp.h> 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 = 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 = scm_from_int (0);
   scm_to_mpz_t (n, z);
   ...
   mpz_add_ui (z, z, 100);
   ...
   SCM res = scm_from_mpz_t (z);
   mpz_clear (z);
   return res;

Another option would be to initialize a new mpz_t variable in 
scm_to_mpz_t() and to return that value. But I don't like this approach 
very much since the initialisation happens in the library whereas the 
memory must be freed by the user:

mpz_t z = scm_to_mpz_t (n);
mpz_clear (z);

The first approach also allows users to reuse variables of type mpz_t. 
This might be useful in loops, for example:

mpz_t z;
mpz_init (z);
for (walk = list; SCM_CONSP (walk); walk = SCM_CDR (walk)) {
   elt = 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 
assignment functions provided by GNU MP expect the result parameter as 
the first argument, e.g. mpz_set_ui (result, 100) and mp_add_ui 
(result, op, 100). So maybe the arguments ought to be reversed in the 
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 = 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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: mpz_import
  2004-08-29 15:33       ` mpz_import Andreas Vögele
@ 2004-08-31  7:47         ` Andreas Vögele
  2004-09-20 23:21           ` mpz_import Marius Vollmer
  2004-09-21  0:28         ` mpz_import Marius Vollmer
  1 sibling, 1 reply; 8+ messages in thread
From: Andreas Vögele @ 2004-08-31  7:47 UTC (permalink / raw)


Andreas Vögele wrote:

> Marius Vollmer writes:
>
>> Andreas Vögele <voegelas@gmx.net> 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 <gmp.h> in libguile/numbers.h [...]

I've just encountered problems with gmp.h (from GNU MP 4.1.2) when 
building C++ programs with GCC 2.95. For me it's more important to be 
able to build C++ programs with GCC 2.95 than to be able to convert 
variables of type mpz_t directly into Scheme numbers. I can live with 
scm_sum and scm_ash. Let's forget about scm_from_mpz_t and scm_to_mpz_t 
for the time being.



_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: mpz_import
  2004-08-31  7:47         ` mpz_import Andreas Vögele
@ 2004-09-20 23:21           ` Marius Vollmer
  0 siblings, 0 replies; 8+ messages in thread
From: Marius Vollmer @ 2004-09-20 23:21 UTC (permalink / raw)
  Cc: guile-devel

Andreas Vögele <voegelas@gmx.net> writes:

> I've just encountered problems with gmp.h (from GNU MP 4.1.2) when
> building C++ programs with GCC 2.95. For me it's more important to be
> able to build C++ programs with GCC 2.95 than to be able to convert
> variables of type mpz_t directly into Scheme numbers. I can live with
> scm_sum and scm_ash. Let's forget about scm_from_mpz_t and
> scm_to_mpz_t for the time being.

Hmm, that would be unfortunate.  Maybe GMP can be fixed?

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: mpz_import
  2004-08-29 15:33       ` mpz_import Andreas Vögele
  2004-08-31  7:47         ` mpz_import Andreas Vögele
@ 2004-09-21  0:28         ` Marius Vollmer
  1 sibling, 0 replies; 8+ messages in thread
From: Marius Vollmer @ 2004-09-21  0:28 UTC (permalink / raw)
  Cc: guile-devel

Andreas Vögele <voegelas@gmx.net> writes:

> I'd include <gmp.h> 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);

Excellent, I like this a lot.  I was thinking about providing
scm_export_intgeror something like that, duplicating the mpz
functions, but this is much nicer.  It _does_ expose the use of GMP,
but we will be tied to that for a the time being anyway.

I will install your code and documentation.  We can use it without any
paper work since it is, while elegant, short enough.

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2004-09-21  0:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-08-23  0:59 scm_from_ipv6 Kevin Ryde
2004-08-23 10:27 ` scm_from_ipv6 Marius Vollmer
2004-08-23 12:25   ` mpz_import (was: scm_from_ipv6) Andreas Vögele
2004-08-23 13:19     ` mpz_import Marius Vollmer
2004-08-29 15:33       ` mpz_import Andreas Vögele
2004-08-31  7:47         ` mpz_import Andreas Vögele
2004-09-20 23:21           ` mpz_import Marius Vollmer
2004-09-21  0:28         ` mpz_import Marius Vollmer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).