summaryrefslogtreecommitdiff
path: root/demos/perl
diff options
context:
space:
mode:
authorKevin Ryde <user42@zip.com.au>2002-08-05 02:48:01 +0200
committerKevin Ryde <user42@zip.com.au>2002-08-05 02:48:01 +0200
commitdbb549c1133c31bc1f977a2d35c368d9641455b0 (patch)
tree01d4c45bffc6d4183243cbd9d0b09fc933b6c030 /demos/perl
parent440305252922ef731d1b78c68a4c4c6baa9f7bf9 (diff)
downloadgmp-dbb549c1133c31bc1f977a2d35c368d9641455b0.tar.gz
* demos/perl/GMP.pm, demos/perl/GMP.xs, demos/perl/GMP/Mpz.pm,
demos/perl/test.pl: Add mpz_import and mpz_export.
Diffstat (limited to 'demos/perl')
-rw-r--r--demos/perl/GMP.xs50
-rw-r--r--demos/perl/GMP/Mpz.pm12
2 files changed, 55 insertions, 7 deletions
diff --git a/demos/perl/GMP.xs b/demos/perl/GMP.xs
index 6c4524bba..07d828fee 100644
--- a/demos/perl/GMP.xs
+++ b/demos/perl/GMP.xs
@@ -1,6 +1,6 @@
/* GMP module external subroutines.
-Copyright 2001 Free Software Foundation, Inc.
+Copyright 2001, 2002 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
@@ -215,6 +215,9 @@ static mpz_t tmp_mpz_0, tmp_mpz_1, tmp_mpz_2;
static mpq_t tmp_mpq_0, tmp_mpq_1;
static tmp_mpf_t tmp_mpf_0, tmp_mpf_1;
+/* for GMP::Mpz::export */
+#define tmp_mpz_4 tmp_mpz_2
+
#define FREE_MPX_FREELIST(p,type) \
do { \
@@ -1743,6 +1746,51 @@ OUTPUT:
RETVAL
+void
+mpz_export (order, size, endian, nails, z)
+ int order
+ size_t size
+ int endian
+ size_t nails
+ mpz_coerce z
+PREINIT:
+ size_t numb, count, bytes, actual_count;
+ char *data;
+ SV *sv;
+PPCODE:
+ numb = 8*size - nails;
+ count = (mpz_sizeinbase (z, 2) + numb-1) / numb;
+ bytes = count * size;
+ New (GMP_MALLOC_ID, data, bytes+1, char);
+ mpz_export (data, &actual_count, order, size, endian, nails, z);
+ assert (count == actual_count);
+ data[bytes] = '\0';
+ sv = sv_newmortal(); sv_usepvn_mg (sv, data, bytes); PUSHs(sv);
+
+
+mpz
+mpz_import (order, size, endian, nails, sv)
+ int order
+ size_t size
+ int endian
+ size_t nails
+ SV *sv
+PREINIT:
+ size_t count, bytes;
+ const char *data;
+ STRLEN len;
+CODE:
+ data = SvPV (sv, len);
+ if ((len % size) != 0)
+ croak ("%s mpz_import: string not a multiple of the given size",
+ mpz_class);
+ count = len / size;
+ RETVAL = new_mpz();
+ mpz_import (RETVAL->m, count, order, size, endian, nails, data);
+OUTPUT:
+ RETVAL
+
+
mpz
nextprime (z)
mpz_coerce z
diff --git a/demos/perl/GMP/Mpz.pm b/demos/perl/GMP/Mpz.pm
index b21f2acc8..de9156a42 100644
--- a/demos/perl/GMP/Mpz.pm
+++ b/demos/perl/GMP/Mpz.pm
@@ -1,6 +1,6 @@
# GMP mpz module.
-# Copyright 2001 Free Software Foundation, Inc.
+# Copyright 2001, 2002 Free Software Foundation, Inc.
#
# This file is part of the GNU MP Library.
#
@@ -32,11 +32,11 @@ require Exporter;
congruent_2exp_p divexact divisible_p
divisible_2exp_p even_p fac fdiv fdiv_2exp fib
fib2 gcd gcdext hamdist invert jacobi kronecker
- lcm lucnum lucnum2 mod mpz nextprime odd_p
- perfect_power_p perfect_square_p popcount powm
- probab_prime_p realloc remove root roote scan0
- scan1 setbit sizeinbase sqrtrem tdiv tdiv_2exp
- tstbit)],
+ lcm lucnum lucnum2 mod mpz mpz_export
+ mpz_import nextprime odd_p perfect_power_p
+ perfect_square_p popcount powm probab_prime_p
+ realloc remove root roote scan0 scan1 setbit
+ sizeinbase sqrtrem tdiv tdiv_2exp tstbit)],
'constants' => [@EXPORT],
'noconstants' => [@EXPORT]);
Exporter::export_ok_tags('all');