1 /* parsepari.c -- wrapper for integer I/O in pari/gp
2  *
3  * Copyright (C) 2012, 2018 INRIA
4  *
5  * This file is part of CMH.
6  *
7  * CMH is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * CMH is distributed in the hope that it will be useful, but WITHOUT ANY
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see http://www.gnu.org/licenses/ .
19  */
20 
21 #include <stdio.h>
22 #include <assert.h>
23 #include "parsepari.h"
24 
25 
26 FILE* file;
27 mpz_t z;
28 
29 /****************************************************************************/
30 /*                                                                          */
31 /* Functions converting between pari and mpz; the following two functions   */
32 /* are taken from PARITWINE.                                                */
33 /*                                                                          */
34 /****************************************************************************/
35 
mpz_set_GEN(mpz_ptr z,GEN x)36 void mpz_set_GEN (mpz_ptr z, GEN x)
37    /* Sets z to x, which needs to be of type t_INT. */
38 
39 {
40    const long lx = lgefint (x) - 2;
41    const long sign = signe (x);
42    int i;
43 
44    assert (sizeof (long) == sizeof (mp_limb_t));
45 
46    if (typ (x) != t_INT)
47       pari_err_TYPE ("mpz_set_GEN", x);
48 
49    if (sign == 0)
50       mpz_set_ui (z, 0);
51    else {
52       mpz_realloc2 (z, lx * BITS_IN_LONG);
53       z->_mp_size = sign * lx;
54       for (i = 0; i < lx; i++)
55          (z->_mp_d) [i] = *int_W (x, i);
56    }
57 }
58 
59 /****************************************************************************/
60 
mpz_get_GEN(mpz_srcptr z)61 GEN mpz_get_GEN (mpz_srcptr z)
62    /* Returns the GEN of type t_INT corresponding to z. */
63 
64 {
65    const long lz = z->_mp_size;
66    const long lx = labs (lz);
67    const long lx2 = lx + 2;
68    int i;
69    GEN x = cgeti (lx2);
70 
71    assert (sizeof (long) == sizeof (mp_limb_t));
72 
73    x [1] = evalsigne ((lz > 0 ? 1 : (lz < 0 ? -1 : 0))) | evallgefint (lx2);
74    for (i = 0; i < lx; i++)
75       *int_W (x, i) = (z->_mp_d) [i];
76 
77    return x;
78 }
79 
80 /*****************************************************************************/
81 
parifopen(const char * filename)82 void parifopen (const char * filename)
83 {
84    /* opens the file "filename" for reading */
85    file = fopen (filename, "r");
86    mpz_init (z);
87 }
88 
89 /*****************************************************************************/
90 
parifclose(void)91 void parifclose (void)
92 {
93    /* closes the file */
94    fclose (file);
95    mpz_clear (z);
96 }
97 
98 /*****************************************************************************/
99 
parifreadint()100 GEN parifreadint ()
101 {
102    /* reads the next integer from the file */
103    mpz_inp_str (z, file, 10);
104    return mpz_get_GEN (z);
105 }
106 
107 /*****************************************************************************/
108 
parifreadmod(GEN p)109 GEN parifreadmod (GEN p)
110 {
111    /* reads the next integer from the file and reduces it mod p */
112    pari_sp av = avma;
113    return gerepileuptoint (av, modii (parifreadint (), p));
114 }
115 
116 /*****************************************************************************/
117 
parifskipint(long n)118 void parifskipint (long n)
119 {
120    /* skips over n integers from the file */
121    long i;
122 
123    for (i = 0; i < n; i++)
124       mpz_inp_str (z, file, 10);
125 }
126 
127 /*****************************************************************************/
128