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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
/* mini-mpq, a minimalistic implementation of a GNU GMP subset.
Contributed to the GNU project by Marco Bodrato
Acknowledgment: special thanks to Bradley Lucier for his comments
to the preliminary version of this code.
Copyright 2018-2022 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.
or both in parallel, as here.
The GNU MP Library 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 copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library. If not,
see https://www.gnu.org/licenses/. */
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mini-mpq.h"
#ifndef GMP_LIMB_HIGHBIT
/* Define macros and static functions already defined by mini-gmp.c */
#define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
#define GMP_LIMB_HIGHBIT ((mp_limb_t) 1 << (GMP_LIMB_BITS - 1))
#define GMP_LIMB_MAX ((mp_limb_t) ~ (mp_limb_t) 0)
#define GMP_NEG_CAST(T,x) (-((T)((x) + 1) - 1))
#define GMP_MIN(a, b) ((a) < (b) ? (a) : (b))
static mpz_srcptr
mpz_roinit_normal_n (mpz_t x, mp_srcptr xp, mp_size_t xs)
{
x->_mp_alloc = 0;
x->_mp_d = (mp_ptr) xp;
x->_mp_size = xs;
return x;
}
static void
gmp_die (const char *msg)
{
fprintf (stderr, "%s\n", msg);
abort();
}
#endif
^L
/* MPQ helper functions */
static mpq_srcptr
mpq_roinit_normal_nn (mpq_t x, mp_srcptr np, mp_size_t ns,
mp_srcptr dp, mp_size_t ds)
{
mpz_roinit_normal_n (mpq_numref(x), np, ns);
mpz_roinit_normal_n (mpq_denref(x), dp, ds);
return x;
}
static mpq_srcptr
mpq_roinit_zz (mpq_t x, mpz_srcptr n, mpz_srcptr d)
{
return mpq_roinit_normal_nn (x, n->_mp_d, n->_mp_size,
d->_mp_d, d->_mp_size);
}
static void
mpq_nan_init (mpq_t x)
{
mpz_init (mpq_numref (x));
mpz_init (mpq_denref (x));
}
void
mpq_init (mpq_t x)
{
mpz_init (mpq_numref (x));
mpz_init_set_ui (mpq_denref (x), 1);
}
void
mpq_clear (mpq_t x)
{
mpz_clear (mpq_numref (x));
mpz_clear (mpq_denref (x));
}
static void
mpq_canonical_sign (mpq_t r)
{
mp_size_t ds = mpq_denref (r)->_mp_size;
if (ds <= 0)
{
if (ds == 0)
gmp_die("mpq: Fraction with zero denominator.");
mpz_neg (mpq_denref (r), mpq_denref (r));
mpz_neg (mpq_numref (r), mpq_numref (r));
}
}
static void
mpq_helper_canonicalize (mpq_t r, const mpz_t num, const mpz_t den)
{
if (num->_mp_size == 0)
mpq_set_ui (r, 0, 1);
else
{
mpz_t g;
mpz_init (g);
mpz_gcd (g, num, den);
mpz_tdiv_q (mpq_numref (r), num, g);
mpz_tdiv_q (mpq_denref (r), den, g);
mpz_clear (g);
mpq_canonical_sign (r);
}
}
void
mpq_canonicalize (mpq_t r)
{
mpq_helper_canonicalize (r, mpq_numref (r), mpq_denref (r));
}
void
mpq_swap (mpq_t a, mpq_t b)
{
mpz_swap (mpq_numref (a), mpq_numref (b));
mpz_swap (mpq_denref (a), mpq_denref (b));
}
^L
/* MPQ assignment and conversions. */
void
mpz_set_q (mpz_t r, const mpq_t q)
{
mpz_tdiv_q (r, mpq_numref (q), mpq_denref (q));
}
void
mpq_set (mpq_t r, const mpq_t q)
{
mpz_set (mpq_numref (r), mpq_numref (q));
mpz_set (mpq_denref (r), mpq_denref (q));
}
void
mpq_set_ui (mpq_t r, unsigned long n, unsigned long d)
{
mpz_set_ui (mpq_numref (r), n);
mpz_set_ui (mpq_denref (r), d);
}
void
mpq_set_si (mpq_t r, signed long n, unsigned long d)
{
mpz_set_si (mpq_numref (r), n);
mpz_set_ui (mpq_denref (r), d);
}
void
mpq_set_z (mpq_t r, const mpz_t n)
{
mpz_set_ui (mpq_denref (r), 1);
mpz_set (mpq_numref (r), n);
}
void
mpq_set_num (mpq_t r, const mpz_t z)
{
mpz_set (mpq_numref (r), z);
}
void
mpq_set_den (mpq_t r, const mpz_t z)
{
mpz_set (mpq_denref (r), z);
}
void
mpq_get_num (mpz_t r, const mpq_t q)
{
mpz_set (r, mpq_numref (q));
}
void
mpq_get_den (mpz_t r, const mpq_t q)
{
mpz_set (r, mpq_denref (q));
}
^L
/* MPQ comparisons and the like. */
int
mpq_cmp (const mpq_t a, const mpq_t b)
{
mpz_t t1, t2;
int res;
mpz_init (t1);
mpz_init (t2);
mpz_mul (t1, mpq_numref (a), mpq_denref (b));
mpz_mul (t2, mpq_numref (b), mpq_denref (a));
res = mpz_cmp (t1, t2);
mpz_clear (t1);
mpz_clear (t2);
return res;
}
int
mpq_cmp_z (const mpq_t a, const mpz_t b)
{
mpz_t t;
int res;
mpz_init (t);
mpz_mul (t, b, mpq_denref (a));
res = mpz_cmp (mpq_numref (a), t);
mpz_clear (t);
return res;
}
int
mpq_equal (const mpq_t a, const mpq_t b)
{
return (mpz_cmp (mpq_numref (a), mpq_numref (b)) == 0) &&
(mpz_cmp (mpq_denref (a), mpq_denref (b)) == 0);
}
int
mpq_cmp_ui (const mpq_t q, unsigned long n, unsigned long d)
{
mpq_t t;
assert (d != 0);
if (ULONG_MAX <= GMP_LIMB_MAX) {
mp_limb_t nl = n, dl = d;
return mpq_cmp (q, mpq_roinit_normal_nn (t, &nl, n != 0, &dl, 1));
} else {
int ret;
mpq_nan_init (t);
mpq_set_ui (t, n, d);
ret = mpq_cmp (q, t);
mpq_clear (t);
return ret;
}
}
int
mpq_cmp_si (const mpq_t q, signed long n, unsigned long d)
{
assert (d != 0);
if (n >= 0)
return mpq_cmp_ui (q, n, d);
else
{
mpq_t t;
if (ULONG_MAX <= GMP_LIMB_MAX)
{
mp_limb_t nl = GMP_NEG_CAST (unsigned long, n), dl = d;
return mpq_cmp (q, mpq_roinit_normal_nn (t, &nl, -1, &dl, 1));
}
else
{
unsigned long l_n = GMP_NEG_CAST (unsigned long, n);
mpq_roinit_normal_nn (t, mpq_numref (q)->_mp_d, - mpq_numref (q)->_mp_size,
mpq_denref (q)->_mp_d, mpq_denref (q)->_mp_size);
return - mpq_cmp_ui (t, l_n, d);
}
}
}
int
mpq_sgn (const mpq_t a)
{
return mpz_sgn (mpq_numref (a));
}
^L
/* MPQ arithmetic. */
void
mpq_abs (mpq_t r, const mpq_t q)
{
mpz_abs (mpq_numref (r), mpq_numref (q));
mpz_set (mpq_denref (r), mpq_denref (q));
}
void
mpq_neg (mpq_t r, const mpq_t q)
{
mpz_neg (mpq_numref (r), mpq_numref (q));
mpz_set (mpq_denref (r), mpq_denref (q));
}
void
mpq_add (mpq_t r, const mpq_t a, const mpq_t b)
{
mpz_t t;
mpz_init (t);
mpz_gcd (t, mpq_denref (a), mpq_denref (b));
if (mpz_cmp_ui (t, 1) == 0)
{
mpz_mul (t, mpq_numref (a), mpq_denref (b));
mpz_addmul (t, mpq_numref (b), mpq_denref (a));
mpz_mul (mpq_denref (r), mpq_denref (a), mpq_denref (b));
mpz_swap (mpq_numref (r), t);
}
else
{
mpz_t x, y;
mpz_init (x);
mpz_init (y);
mpz_tdiv_q (x, mpq_denref (b), t);
mpz_tdiv_q (y, mpq_denref (a), t);
mpz_mul (x, mpq_numref (a), x);
mpz_addmul (x, mpq_numref (b), y);
mpz_gcd (t, x, t);
mpz_tdiv_q (mpq_numref (r), x, t);
mpz_tdiv_q (x, mpq_denref (b), t);
mpz_mul (mpq_denref (r), x, y);
mpz_clear (x);
mpz_clear (y);
}
mpz_clear (t);
}
void
mpq_sub (mpq_t r, const mpq_t a, const mpq_t b)
{
mpq_t t;
mpq_roinit_normal_nn (t, mpq_numref (b)->_mp_d, - mpq_numref (b)->_mp_size,
mpq_denref (b)->_mp_d, mpq_denref (b)->_mp_size);
mpq_add (r, a, t);
}
void
mpq_div (mpq_t r, const mpq_t a, const mpq_t b)
{
mpq_t t;
mpq_mul (r, a, mpq_roinit_zz (t, mpq_denref (b), mpq_numref (b)));
}
void
mpq_mul (mpq_t r, const mpq_t a, const mpq_t b)
{
mpq_t t;
mpq_nan_init (t);
if (a != b) {
mpq_helper_canonicalize (t, mpq_numref (a), mpq_denref (b));
mpq_helper_canonicalize (r, mpq_numref (b), mpq_denref (a));
a = r;
b = t;
}
mpz_mul (mpq_numref (r), mpq_numref (a), mpq_numref (b));
mpz_mul (mpq_denref (r), mpq_denref (a), mpq_denref (b));
mpq_clear (t);
}
static void
mpq_helper_2exp (mpz_t rn, mpz_t rd, const mpz_t qn, const mpz_t qd, mp_bitcnt_t e)
{
mp_bitcnt_t z = mpz_scan1 (qd, 0);
z = GMP_MIN (z, e);
mpz_mul_2exp (rn, qn, e - z);
mpz_tdiv_q_2exp (rd, qd, z);
}
void
mpq_div_2exp (mpq_t r, const mpq_t q, mp_bitcnt_t e)
{
mpq_helper_2exp (mpq_denref (r), mpq_numref (r), mpq_denref (q), mpq_numref (q), e);
}
void
mpq_mul_2exp (mpq_t r, const mpq_t q, mp_bitcnt_t e)
{
mpq_helper_2exp (mpq_numref (r), mpq_denref (r), mpq_numref (q), mpq_denref (q), e);
}
void
mpq_inv (mpq_t r, const mpq_t q)
{
mpq_set (r, q);
mpz_swap (mpq_denref (r), mpq_numref (r));
mpq_canonical_sign (r);
}
^L
/* MPQ to/from double. */
void
mpq_set_d (mpq_t r, double x)
{
mpz_set_ui (mpq_denref (r), 1);
/* x != x is true when x is a NaN, and x == x * 0.5 is true when x is
zero or infinity. */
if (x == x * 0.5 || x != x)
mpq_numref (r)->_mp_size = 0;
else
{
double B;
mp_bitcnt_t e;
B = 4.0 * (double) (GMP_LIMB_HIGHBIT >> 1);
for (e = 0; x != x + 0.5; e += GMP_LIMB_BITS)
x *= B;
mpz_set_d (mpq_numref (r), x);
mpq_div_2exp (r, r, e);
}
}
double
mpq_get_d (const mpq_t u)
{
mp_bitcnt_t ne, de, ee;
mpz_t z;
double B, ret;
ne = mpz_sizeinbase (mpq_numref (u), 2);
de = mpz_sizeinbase (mpq_denref (u), 2);
ee = CHAR_BIT * sizeof (double);
if (de == 1 || ne > de + ee)
ee = 0;
else
ee = (ee + de - ne) / GMP_LIMB_BITS + 1;
mpz_init (z);
mpz_mul_2exp (z, mpq_numref (u), ee * GMP_LIMB_BITS);
mpz_tdiv_q (z, z, mpq_denref (u));
ret = mpz_get_d (z);
mpz_clear (z);
B = 4.0 * (double) (GMP_LIMB_HIGHBIT >> 1);
for (B = 1 / B; ee != 0; --ee)
ret *= B;
return ret;
}
^L
/* MPQ and strings/streams. */
char *
mpq_get_str (char *sp, int base, const mpq_t q)
{
char *res;
char *rden;
size_t len;
res = mpz_get_str (sp, base, mpq_numref (q));
if (res == NULL || mpz_cmp_ui (mpq_denref (q), 1) == 0)
return res;
len = strlen (res) + 1;
rden = sp ? sp + len : NULL;
rden = mpz_get_str (rden, base, mpq_denref (q));
assert (rden != NULL);
if (sp == NULL) {
void * (*gmp_reallocate_func) (void *, size_t, size_t);
void (*gmp_free_func) (void *, size_t);
size_t lden;
mp_get_memory_functions (NULL, &gmp_reallocate_func, &gmp_free_func);
lden = strlen (rden) + 1;
res = (char *) gmp_reallocate_func (res, len, (lden + len) * sizeof (char));
memcpy (res + len, rden, lden);
gmp_free_func (rden, lden);
}
res [len - 1] = '/';
return res;
}
size_t
mpq_out_str (FILE *stream, int base, const mpq_t x)
{
char * str;
size_t len, n;
void (*gmp_free_func) (void *, size_t);
str = mpq_get_str (NULL, base, x);
if (!str)
return 0;
len = strlen (str);
n = fwrite (str, 1, len, stream);
mp_get_memory_functions (NULL, NULL, &gmp_free_func);
gmp_free_func (str, len + 1);
return n;
}
int
mpq_set_str (mpq_t r, const char *sp, int base)
{
const char *slash;
slash = strchr (sp, '/');
if (slash == NULL) {
mpz_set_ui (mpq_denref(r), 1);
return mpz_set_str (mpq_numref(r), sp, base);
} else {
char *num;
size_t numlen;
int ret;
void * (*gmp_allocate_func) (size_t);
void (*gmp_free_func) (void *, size_t);
mp_get_memory_functions (&gmp_allocate_func, NULL, &gmp_free_func);
numlen = slash - sp;
num = (char *) gmp_allocate_func (numlen + 1);
memcpy (num, sp, numlen);
num[numlen] = '\0';
ret = mpz_set_str (mpq_numref(r), num, base);
gmp_free_func (num, numlen + 1);
if (ret != 0)
return ret;
return mpz_set_str (mpq_denref(r), slash + 1, base);
}
}
|