aGrUM  0.16.0
lrslong.h
Go to the documentation of this file.
1 #ifndef DOXYGEN_SHOULD_SKIP_THIS
2 
3 /* lrslong.h (lrs long integer arithmetic library */
4 /* Copyright: David Avis 2000, avis@cs.mcgill.ca */
5 /* Version 4.0, February 17, 2000 */
6 
7 /* This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
20  */
21 /******************************************************************************/
22 /* See http://cgm.cs.mcgill.ca/~avis/C/lrs.html for lrs usage instructions */
23 /******************************************************************************/
24 /* This package contains the extended precision routines used by lrs
25  and some other miscellaneous routines. The maximum precision depends on
26  the parameter MAX_DIGITS defined below, with usual default of 255L. This
27  gives a maximum of 1020 decimal digits on 32 bit machines. The procedure
28  lrs_mp_init(dec_digits) may set a smaller number of dec_digits, and this
29  is useful if arrays or matrices will be used.
30  */
31 
32 
33 #ifdef PLRS
34 #include <string>
35 using namespace std;
36 #endif
37 
38 
39 /***********/
40 /* defines */
41 /***********/
42 /*
43  this is number of longwords. Increasing this won't cost you that much
44  since only variables other than the A matrix are allocated this size.
45  Changing affects running time in small but not very predictable ways.
46  */
47 
48 #define MAX_DIGITS 255L
49 
50 /*
51  this is in decimal digits, you pay in memory if you increase this,
52  unless you override by a line with
53  digits n
54  before the begin line of your file.
55  */
56 #define DEFAULT_DIGITS 100L
57 
58 
59 // 64 bits for windows (long is 32 bits)
60 #ifdef _MSC_VER
61 typedef __int64 int64_t;
62 typedef unsigned __int64 uint64_t;
63 #else
64 #include <stdint.h>
65 #endif
66 
67 /**********MACHINE DEPENDENT CONSTANTS***********/
68 /* MAXD is 2^(k-1)-1 where k=16,32,64 word size */
69 /* MAXD must be at least 2*BASE^2 */
70 /* If BASE is 10^k, use "%k.ku" for FORMAT */
71 /* INTSIZE is number of bytes for integer */
72 /* 32/64 bit machines */
73 /***********************************************/
74 #ifdef B32
75 /*32 bit machines */
76 #define FORMAT "%4.4u"
77 #define MAXD 2147483647L
78 #define BASE 10000L
79 #define BASE_DIG 4
80 #define INTSIZE 8L
81 #define BIT "32bit"
82 #else
83 /* 64 bit machines */
84 #define MAXD 9223372036854775807L
85 #define BASE 1000000000L
86 #define FORMAT "%9.9u"
87 #define BASE_DIG 9
88 #define INTSIZE 16L
89 #define BIT "64bit"
90 #endif
91 
92 #define MAXINPUT 1000 /*max length of any input rational */
93 
94 #define POS 1L
95 #define NEG -1L
96 #ifndef TRUE
97 #define TRUE 1L
98 #endif
99 #ifndef FALSE
100 #define FALSE 0L
101 #endif
102 #define ONE 1L
103 #define TWO 2L
104 #define ZERO 0L
105 
106 /**********************************/
107 /* MACROS */
108 /* dependent on mp implementation */
109 /**********************************/
110 
111 #define addint(a, b, c) *(c) = *(a) + *(b)
112 #define changesign(a) (*(a) = -*(a))
113 #define copy(a, b) ((a)[0] = (b)[0])
114 #define decint(a, b) *(a) = *(a) - *(b)
115 #define divint(a, b, c) \
116  *(c) = *(a) / *(b); \
117  *(a) = *(a) % *(b)
118 #define exactdivint(a, b, c) *(c) = *(a) / *(b);
119 #define mp_greater(a, b) (*(a) > *(b))
120 #define itomp(in, a) *(a) = in
121 #define linint(a, ka, b, kb) *(a) = *(a)*ka + *(b)*kb
122 #define mulint(a, b, c) *(c) = *(a) * *(b)
123 #define one(a) (*(a) == 1)
124 #define negative(a) (*(a) < 0)
125 #define normalize(a) ()0
126 #define positive(a) (*(a) > 0)
127 #define sign(a) (*(a) < 0 ? NEG : POS)
128 #define storesign(a, sa) (*(a) = labs(*(a)) * sa)
129 #define subint(a, b, c) *(c) = *(a) - *(b)
130 #define iszero(a) (*(a) == 0)
131 
132 
133 /*
134  * convert between decimal and machine (longword digits). Notice lovely
135  * implementation of ceiling function :-)
136  */
137 #define DEC2DIG(d) ((d) % BASE_DIG ? (d) / BASE_DIG + 1 : (d) / BASE_DIG)
138 #define DIG2DEC(d) ((d)*BASE_DIG)
139 
140 #ifndef OMIT_SIGNALS
141 #include <signal.h>
142 #include <stdlib.h> /* labs */
143 #include <unistd.h>
144 #define errcheck(s, e) \
145  if ((int64_t)(e) == -1L) { \
146  perror(s); \
147  exit(1); \
148  }
149 #endif
150 
151 #define CALLOC(n, s) xcalloc(n, s, __LINE__, __FILE__)
152 
153 /*************/
154 /* typedefs */
155 /*************/
156 
157 typedef int64_t lrs_mp[1]; /* type lrs_mp holds one long integer */
158 typedef int64_t* lrs_mp_t;
159 typedef int64_t** lrs_mp_vector;
160 typedef int64_t*** lrs_mp_matrix;
161 
162 /*********************/
163 /*global variables */
164 /*********************/
165 
166 extern int64_t lrs_digits; /* max permitted no. of digits */
167 extern int64_t lrs_record_digits; /* this is the biggest acheived so far. */
168 
169 extern FILE* lrs_ifp; /* input file pointer */
170 extern FILE* lrs_ofp; /* output file pointer */
171 
172 /*********************************************************/
173 /* Initialization and allocation procedures - must use! */
174 /******************************************************* */
175 
176 int64_t lrs_mp_init(int64_t dec_digits,
177  FILE* lrs_ifp,
178  FILE* lrs_ofp); /* max number of decimal digits, fps */
179 
180 #define lrs_alloc_mp(a)
181 #define lrs_clear_mp(a)
182 
183 lrs_mp_t lrs_alloc_mp_t(); /* dynamic allocation of lrs_mp */
184 lrs_mp_vector
185 lrs_alloc_mp_vector(int64_t n); /* allocate lrs_mp_vector for n+1 lrs_mp numbers */
186 lrs_mp_matrix
187 lrs_alloc_mp_matrix(int64_t m,
188  int64_t n); /* allocate lrs_mp_matrix for m+1 x n+1 lrs_mp */
189 
190 void lrs_clear_mp_vector(lrs_mp_vector a, int64_t n);
191 void lrs_clear_mp_matrix(lrs_mp_matrix a, int64_t m, int64_t n);
192 
193 
194 /*********************************************************/
195 /* Core library functions - depend on mp implementation */
196 /******************************************************* */
197 void atomp(const char s[], lrs_mp a); /* convert string to lrs_mp integer */
198 int64_t compare(lrs_mp a, lrs_mp b); /* a ? b and returns -1,0,1 for <,=,> */
199 void gcd(lrs_mp u, lrs_mp v); /* returns u=gcd(u,v) destroying v */
200 void mptodouble(lrs_mp a, double* x); /* convert lrs_mp to double */
201 int64_t mptoi(lrs_mp a); /* convert lrs_mp to long integer */
202 #ifdef PLRS
203 string pmp(char name[], lrs_mp a); /* print the long precision integer a */
204 string prat(char name[], lrs_mp Nt, lrs_mp Dt); /* reduce and print Nt/Dt */
205 char* cprat(char name[], lrs_mp Nt, lrs_mp Dt); /* C version of prat */
206 int64_t
207 plrs_readrat(lrs_mp Na,
208  lrs_mp Da,
209  const char* rat); /* take a rational number and convert to lrs_mp */
210 #else
211 void pmp(char name[], lrs_mp a); /* print the long precision integer a */
212 void prat(char name[], lrs_mp Nt, lrs_mp Dt); /* reduce and print Nt/Dt */
213 #endif
214 void readmp(lrs_mp a); /* read an integer and convert to lrs_mp */
215 int64_t readrat(lrs_mp Na,
216  lrs_mp Da); /* read a rational or int and convert to lrs_mp */
217 void reduce(lrs_mp Na, lrs_mp Da); /* reduces Na Da by gcd(Na,Da) */
218 
219 /*********************************************************/
220 /* Standard arithmetic & misc. functions */
221 /* should be independent of mp implementation */
222 /******************************************************* */
223 
224 void atoaa(const char in[],
225  char num[],
226  char den[]); /* convert rational string in to num/den strings */
227 int64_t atos(char s[]); /* convert s to integer */
228 int64_t comprod(lrs_mp Na,
229  lrs_mp Nb,
230  lrs_mp Nc,
231  lrs_mp Nd); /* +1 if Na*Nb > Nc*Nd,-1 if Na*Nb > Nc*Nd else 0 */
232 void divrat(lrs_mp Na, lrs_mp Da, lrs_mp Nb, lrs_mp Db, lrs_mp Nc, lrs_mp Dc);
233 /* computes Nc/Dc = (Na/Da) /( Nb/Db ) and reduce */
234 void getfactorial(lrs_mp factorial, int64_t k); /* compute k factorial in lrs_mp */
235 void linrat(lrs_mp Na,
236  lrs_mp Da,
237  int64_t ka,
238  lrs_mp Nb,
239  lrs_mp Db,
240  int64_t kb,
241  lrs_mp Nc,
242  lrs_mp Dc);
243 void lcm(lrs_mp a, lrs_mp b); /* a = least common multiple of a, b; b is saved */
244 void mulrat(lrs_mp Na, lrs_mp Da, lrs_mp Nb, lrs_mp Db, lrs_mp Nc, lrs_mp Dc);
245 /* computes Nc/Dc=(Na/Da)*(Nb/Db) and reduce */
246 int64_t myrandom(int64_t num,
247  int64_t nrange); /* return a random number in range 0..nrange-1 */
248 void notimpl(char s[]); /* bail out - help! */
249 void rattodouble(lrs_mp a,
250  lrs_mp b,
251  double* x); /* convert lrs_mp rational to double */
252 void reduceint(lrs_mp Na, lrs_mp Da); /* divide Na by Da and return it */
253 void reducearray(lrs_mp_vector p,
254  int64_t n); /* find gcd of p[0]..p[n-1] and divide through by */
255 void scalerat(lrs_mp Na, lrs_mp Da, int64_t ka); /* scales rational by ka */
256 
257 /**********************************/
258 /* Miscellaneous functions */
259 /******************************** */
260 
261 void lrs_getdigits(int64_t* a, int64_t* b); /* send digit information to user */
262 
263 void stringcpy(char* s, char* t); /* copy t to s pointer version */
264 
265 void* calloc();
266 void* malloc();
267 void* xcalloc(int64_t n, int64_t s, int64_t l, char* f);
268 
269 void lrs_default_digits_overflow();
270 
271 /* end of lrs_mp.h (vertex enumeration using lexicographic reverse search) */
272 
273 #endif // DOXYGEN_SHOULD_SKIP_THIS
STL namespace.