File: latte_gmp.cpp

package info (click to toggle)
latte-int 1.7.6%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 38,260 kB
  • sloc: cpp: 32,231; sh: 4,413; makefile: 811; perl: 300
file content (102 lines) | stat: -rw-r--r-- 2,359 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* latte_gmp.cpp -- Interface between GMP and NTL integers

   Copyright 2006 Matthias Koeppe

   This file is part of LattE.
   
   LattE is free software; you can redistribute it and/or modify it
   under the terms of the version 2 of the GNU General Public License
   as published by the Free Software Foundation.

   LattE is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with LattE; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

#include "latte_gmp.h"
#include <cassert>

mpz_class
convert_ZZ_to_mpz(const ZZ &zz)
{
  mpz_class mpz;
  convert_ZZ_to_mpz(zz, mpz);
  return mpz;
}

void
convert_ZZ_to_mpz(const ZZ &zz, mpz_class &mpz)
{
  long size = NumBytes(zz);
  unsigned char *data = new unsigned char[size];
  int sig = sign(zz);
  BytesFromZZ(data, zz, size);

  mpz_import(mpz.get_mpz_t(), size, -1, 1, 1, 0, data);
  if (sig == -1)
    mpz = -mpz;
  delete[] data;
}

ZZ
convert_mpz_to_ZZ(const mpz_class &mpz)
{
  size_t count;
  int sig = sgn(mpz);
  int size = 1;
  int nail = 0;
  int numb = 8*size - nail;
  count = (mpz_sizeinbase (mpz.get_mpz_t(), 2) + numb-1) / numb;
  unsigned char *data = new unsigned char[count * size];
  mpz_export(data, &count,
	     /*order:*/ -1, size, /*endian:*/ 1, nail,
	     mpz.get_mpz_t());
  ZZ result = ZZFromBytes(data, count);
  if (sig == -1)
    result = -result;
  delete[] data;
  return result;
}

mpq_class
convert_ZZ_to_mpq(const ZZ &zz)
{
  return mpq_class(convert_ZZ_to_mpz(zz));
}

ZZ
convert_mpq_to_ZZ(mpq_t mpq)
{
  mpq_class elt(mpq);
  assert(elt.get_den() == 1);
  return convert_mpz_to_ZZ(elt.get_num());
}

ZZ
convert_mpq_to_ZZ(mpq_class elt)
{
  assert(elt.get_den() == 1);
  return convert_mpz_to_ZZ(elt.get_num());
}

mpz_vector
convert_vec_ZZ_to_mpz_vector(const vec_ZZ &vec)
{
  mpz_vector result(vec.length());
  convert_vec_ZZ_to_mpz_vector(vec, result);
  return result;   
}

void
convert_vec_ZZ_to_mpz_vector(const vec_ZZ &vec, mpz_vector &result)
{
  int j;
  for (j = 0; j<vec.length(); j++) {
    result[j] = convert_ZZ_to_mpz(vec[j]);
  }
}