1 /*
2 Copyright (C) 2013 William Hart
3
4 This file is part of FLINT.
5
6 FLINT is free software: you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version. See <http://www.gnu.org/licenses/>.
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <gmp.h>
15
16 #ifndef GMP_COMPAT_H
17 #define GMP_COMPAT_H
18
19 #define FLINT_MPZ_REALLOC(z, len) \
20 ((len) > ((z)->_mp_alloc) \
21 ? (mp_ptr) _mpz_realloc(z, len) \
22 : ((z)->_mp_d))
23
24 #define FLINT_MPZ_PTR_SWAP(a, b) \
25 do { \
26 mpz_ptr __tmp = (a); \
27 (a) = (b); \
28 (b) = __tmp; \
29 } while (0)
30
31
32 #if defined(__MINGW64__) && !defined(__MPIR_VERSION)
33
34 #define FLINT_MOCK_MPZ_UI(xxx, yyy) \
35 __mpz_struct (xxx)[1] = {{ 1, 0, NULL }}; \
36 do { \
37 (xxx)->_mp_d = (mp_ptr) &(yyy); \
38 if ((yyy) > 0) \
39 (xxx)->_mp_size = 1; \
40 }while (0)
41
42 #define FLINT_MOCK_MPZ_SI(xxx, yyy) \
43 __mpz_struct (xxx)[1] = {{ 1, 0, NULL }}; \
44 do { \
45 (xxx)->_mp_d = (mp_ptr) &(yyy); \
46 if ((yyy) < 0) (xxx)->_mp_size = -1, (yyy) = -(yyy); \
47 else (xxx)->_mp_size = (yyy) != 0; \
48 } while (0)
49
50 #define flint_mpz_get_si(xxx) \
51 ((xxx)->_mp_size == 0 ? (slong) WORD(0) : \
52 ((xxx)->_mp_size > 0 ? (slong) (xxx)->_mp_d[0] : (slong) -(xxx)->_mp_d[0]))
53
54 #define flint_mpz_get_ui(xxx) \
55 ((xxx)->_mp_size == 0 ? UWORD(0) : (xxx)->_mp_d[0])
56
57 static __inline__
flint_mpz_set_si(mpz_ptr r,slong s)58 void flint_mpz_set_si(mpz_ptr r, slong s)
59 {
60 #if __GNU_MP_RELEASE >= 60200
61 if (r->_mp_alloc == 0)
62 {
63 r->_mp_d = (mp_ptr) flint_malloc(sizeof(mp_limb_t));
64 r->_mp_alloc = 1;
65 }
66 #endif
67 if (s < 0) {
68 r->_mp_size = -1;
69 r->_mp_d[0] = -s;
70 } else {
71 r->_mp_size = s != 0;
72 r->_mp_d[0] = s;
73 }
74 }
75
76 static __inline__
flint_mpz_set_ui(mpz_ptr r,ulong u)77 void flint_mpz_set_ui(mpz_ptr r, ulong u)
78 {
79 #if __GNU_MP_RELEASE >= 60200
80 if (r->_mp_alloc == 0)
81 {
82 r->_mp_d = (mp_ptr) flint_malloc(sizeof(mp_limb_t));
83 r->_mp_alloc = 1;
84 }
85 #endif
86 r->_mp_d[0] = u;
87 r->_mp_size = u != 0;
88 }
89
90 static __inline__
flint_mpz_init_set_si(mpz_ptr r,slong s)91 void flint_mpz_init_set_si(mpz_ptr r, slong s)
92 {
93 r->_mp_d = (mp_ptr) flint_malloc(sizeof(mp_limb_t));
94 r->_mp_alloc = 1;
95
96 if (s < 0) {
97 r->_mp_size = -1;
98 r->_mp_d[0] = -s;
99 } else {
100 r->_mp_size = s != 0;
101 r->_mp_d[0] = s;
102 }
103 }
104
105 static __inline__
flint_mpz_init_set_ui(mpz_ptr r,ulong u)106 void flint_mpz_init_set_ui(mpz_ptr r, ulong u)
107 {
108 r->_mp_d = (mp_ptr) flint_malloc(sizeof(mp_limb_t));
109 r->_mp_alloc = 1;
110
111 r->_mp_d[0] = u;
112 r->_mp_size = u != 0;
113 }
114
115 static __inline__
flint_mpz_add_ui(mpz_ptr a,mpz_srcptr b,ulong c)116 void flint_mpz_add_ui(mpz_ptr a, mpz_srcptr b, ulong c)
117 {
118 FLINT_MOCK_MPZ_UI(tc, c);
119
120 mpz_add(a, b, tc);
121 }
122
123 static __inline__
flint_mpz_sub_ui(mpz_ptr a,mpz_srcptr b,ulong c)124 void flint_mpz_sub_ui(mpz_ptr a, mpz_srcptr b, ulong c)
125 {
126 FLINT_MOCK_MPZ_UI(tc, c);
127
128 mpz_sub(a, b, tc);
129 }
130
131 static __inline__
flint_mpz_mul_ui(mpz_ptr a,mpz_srcptr b,ulong c)132 void flint_mpz_mul_ui(mpz_ptr a, mpz_srcptr b, ulong c)
133 {
134 FLINT_MOCK_MPZ_UI(tc, c);
135
136 mpz_mul(a, b, tc);
137 }
138
139 static __inline__
flint_mpz_mul_si(mpz_ptr a,mpz_srcptr b,slong c)140 void flint_mpz_mul_si(mpz_ptr a, mpz_srcptr b, slong c)
141 {
142 FLINT_MOCK_MPZ_SI(tc, c);
143
144 mpz_mul(a, b, tc);
145 }
146
147 static __inline__
flint_mpz_addmul_ui(mpz_ptr a,mpz_srcptr b,ulong c)148 void flint_mpz_addmul_ui(mpz_ptr a, mpz_srcptr b, ulong c)
149 {
150 FLINT_MOCK_MPZ_UI(tc, c);
151
152 mpz_addmul(a, b, tc);
153 }
154
155 static __inline__
flint_mpz_submul_ui(mpz_ptr a,mpz_srcptr b,ulong c)156 void flint_mpz_submul_ui(mpz_ptr a, mpz_srcptr b, ulong c)
157 {
158 FLINT_MOCK_MPZ_UI(tc, c);
159
160 mpz_submul(a, b, tc);
161 }
162
163 static __inline__
flint_mpz_ui_sub(mpz_ptr a,ulong b,mpz_srcptr c)164 void flint_mpz_ui_sub(mpz_ptr a, ulong b, mpz_srcptr c)
165 {
166 FLINT_MOCK_MPZ_UI(tb, b);
167
168 mpz_sub(a, tb, c);
169 }
170
171 static __inline__
flint_mpz_ui_pow_ui(mpz_ptr a,ulong b,ulong c)172 void flint_mpz_ui_pow_ui(mpz_ptr a, ulong b, ulong c)
173 {
174 FLINT_MOCK_MPZ_UI(tb, b);
175
176 mpz_pow_ui(a, tb, c);
177 }
178
179 static __inline__
flint_mpz_divexact_ui(mpz_ptr a,mpz_srcptr b,ulong c)180 void flint_mpz_divexact_ui(mpz_ptr a, mpz_srcptr b, ulong c)
181 {
182 FLINT_MOCK_MPZ_UI(tc, c);
183
184 mpz_divexact(a, b, tc);
185 }
186
187 static __inline__
flint_mpz_divisible_ui_p(mpz_srcptr a,ulong c)188 int flint_mpz_divisible_ui_p(mpz_srcptr a, ulong c)
189 {
190 FLINT_MOCK_MPZ_UI(tc, c);
191
192 return mpz_divisible_p(a, tc);
193 }
194
195 static __inline__
flint_mpz_congruent_ui_p(mpz_srcptr a,ulong b,ulong c)196 int flint_mpz_congruent_ui_p(mpz_srcptr a, ulong b, ulong c)
197 {
198 FLINT_MOCK_MPZ_UI(tc, c);
199 {
200 FLINT_MOCK_MPZ_UI(tb, b);
201
202 return mpz_congruent_p(a, tb, tc);
203 }
204 }
205
206 static __inline__
flint_mpz_cmp_ui(mpz_srcptr a,ulong c)207 int flint_mpz_cmp_ui(mpz_srcptr a, ulong c)
208 {
209 FLINT_MOCK_MPZ_UI(tc, c);
210
211 return mpz_cmp(a, tc);
212 }
213
214 static __inline__
flint_mpz_cmp_si(mpz_srcptr a,slong c)215 int flint_mpz_cmp_si(mpz_srcptr a, slong c)
216 {
217 FLINT_MOCK_MPZ_SI(tc, c);
218
219 return mpz_cmp(a, tc);
220 }
221
222 static __inline__
flint_mpq_cmp_si(mpq_srcptr a,slong b,ulong c)223 int flint_mpq_cmp_si(mpq_srcptr a, slong b, ulong c)
224 {
225 mpq_t tq;
226 int res;
227 FLINT_MOCK_MPZ_SI(tb, b);
228 {
229 FLINT_MOCK_MPZ_UI(tc, c);
230 mpq_init(tq);
231 mpq_set_num(tq, tb);
232 mpq_set_den(tq, tc);
233
234 res = mpq_cmp(a, tq);
235
236 mpq_clear(tq);
237
238 return res;
239 }
240 }
241
242 static __inline__
flint_mpq_cmp_ui(mpq_srcptr a,ulong b,ulong c)243 int flint_mpq_cmp_ui(mpq_srcptr a, ulong b, ulong c)
244 {
245 mpq_t tq;
246 int res;
247 FLINT_MOCK_MPZ_UI(tb, b);
248 {
249 FLINT_MOCK_MPZ_UI(tc, c);
250 mpq_init(tq);
251 mpq_set_num(tq, tb);
252 mpq_set_den(tq, tc);
253
254 res = mpq_cmp(a, tq);
255
256 mpq_clear(tq);
257
258 return res;
259 }
260 }
261
262 static __inline__
flint_mpq_set_si(mpq_ptr a,slong b,ulong c)263 void flint_mpq_set_si(mpq_ptr a, slong b, ulong c)
264 {
265 FLINT_MOCK_MPZ_SI(tb, b);
266 {
267 FLINT_MOCK_MPZ_UI(tc, c);
268
269 mpq_set_num(a, tb);
270 mpq_set_den(a, tc);
271 }
272 }
273
274 static __inline__
flint_mpq_set_ui(mpq_ptr a,ulong b,ulong c)275 void flint_mpq_set_ui(mpq_ptr a, ulong b, ulong c)
276 {
277 FLINT_MOCK_MPZ_UI(tb, b);
278 {
279 FLINT_MOCK_MPZ_UI(tc, c);
280
281 mpq_set_num(a, tb);
282 mpq_set_den(a, tc);
283 }
284 }
285
286 static __inline__
flint_mpz_cdiv_q_ui(mpz_ptr q,mpz_srcptr n,ulong c)287 void flint_mpz_cdiv_q_ui(mpz_ptr q, mpz_srcptr n, ulong c)
288 {
289 FLINT_MOCK_MPZ_UI(tc, c);
290
291 mpz_cdiv_q(q, n, tc);
292 }
293
294 static __inline__
flint_mpz_fdiv_q_ui(mpz_ptr q,mpz_srcptr n,ulong c)295 void flint_mpz_fdiv_q_ui(mpz_ptr q, mpz_srcptr n, ulong c)
296 {
297 FLINT_MOCK_MPZ_UI(tc, c);
298
299 mpz_fdiv_q(q, n, tc);
300 }
301
302 static __inline__
flint_mpz_tdiv_q_ui(mpz_ptr q,mpz_srcptr n,ulong c)303 void flint_mpz_tdiv_q_ui(mpz_ptr q, mpz_srcptr n, ulong c)
304 {
305 FLINT_MOCK_MPZ_UI(tc, c);
306
307 mpz_tdiv_q(q, n, tc);
308 }
309
310 static __inline__
flint_mpz_cdiv_r_ui(mpz_ptr r,mpz_srcptr n,ulong c)311 ulong flint_mpz_cdiv_r_ui(mpz_ptr r, mpz_srcptr n, ulong c)
312 {
313 FLINT_MOCK_MPZ_UI(tc, c);
314
315 mpz_cdiv_r(r, n, tc);
316
317 return flint_mpz_get_ui(r);
318 }
319
320 static __inline__
flint_mpz_fdiv_r_ui(mpz_ptr r,mpz_srcptr n,ulong c)321 ulong flint_mpz_fdiv_r_ui(mpz_ptr r, mpz_srcptr n, ulong c)
322 {
323 FLINT_MOCK_MPZ_UI(tc, c);
324
325 mpz_fdiv_r(r, n, tc);
326
327 return flint_mpz_get_ui(r);
328 }
329
330 static __inline__
flint_mpz_tdiv_r_ui(mpz_ptr r,mpz_srcptr n,ulong c)331 ulong flint_mpz_tdiv_r_ui(mpz_ptr r, mpz_srcptr n, ulong c)
332 {
333 FLINT_MOCK_MPZ_UI(tc, c);
334
335 mpz_tdiv_r(r, n, tc);
336
337 return flint_mpz_get_ui(r);
338 }
339
340 static __inline__
flint_mpz_cdiv_qr_ui(mpz_ptr q,mpz_ptr r,mpz_srcptr n,ulong c)341 ulong flint_mpz_cdiv_qr_ui(mpz_ptr q, mpz_ptr r, mpz_srcptr n, ulong c)
342 {
343 FLINT_MOCK_MPZ_UI(tc, c);
344
345 mpz_cdiv_qr(q, r, n, tc);
346
347 return flint_mpz_get_ui(r);
348 }
349
350 static __inline__
flint_mpz_fdiv_qr_ui(mpz_ptr q,mpz_ptr r,mpz_srcptr n,ulong c)351 ulong flint_mpz_fdiv_qr_ui(mpz_ptr q, mpz_ptr r, mpz_srcptr n, ulong c)
352 {
353 FLINT_MOCK_MPZ_UI(tc, c);
354
355 mpz_fdiv_qr(q, r, n, tc);
356
357 return flint_mpz_get_ui(r);
358 }
359
360 static __inline__
flint_mpz_tdiv_qr_ui(mpz_ptr q,mpz_ptr r,mpz_srcptr n,ulong c)361 ulong flint_mpz_tdiv_qr_ui(mpz_ptr q, mpz_ptr r, mpz_srcptr n, ulong c)
362 {
363 FLINT_MOCK_MPZ_UI(tc, c);
364
365 mpz_tdiv_qr(q, r, n, tc);
366
367 return flint_mpz_get_ui(r);
368 }
369
370 static __inline__
flint_mpz_cdiv_ui(mpz_srcptr n,ulong c)371 ulong flint_mpz_cdiv_ui(mpz_srcptr n, ulong c)
372 {
373 mpz_t r;
374 ulong res;
375
376 mpz_init(r);
377 FLINT_MOCK_MPZ_UI(tc, c);
378
379 mpz_cdiv_r(r, n, tc);
380
381 res = flint_mpz_get_ui(r);
382
383 mpz_clear(r);
384
385 return res;
386 }
387
388 static __inline__
flint_mpz_fdiv_ui(mpz_srcptr n,ulong c)389 ulong flint_mpz_fdiv_ui(mpz_srcptr n, ulong c)
390 {
391 mpz_t r;
392 ulong res;
393
394 mpz_init(r);
395 FLINT_MOCK_MPZ_UI(tc, c);
396
397 mpz_fdiv_r(r, n, tc);
398
399 res = flint_mpz_get_ui(r);
400
401 mpz_clear(r);
402
403 return res;
404 }
405
406 static __inline__
flint_mpz_tdiv_ui(mpz_srcptr n,ulong c)407 ulong flint_mpz_tdiv_ui(mpz_srcptr n, ulong c)
408 {
409 mpz_t r;
410 ulong res;
411
412 mpz_init(r);
413 FLINT_MOCK_MPZ_UI(tc, c);
414
415 mpz_tdiv_r(r, n, tc);
416
417 res = flint_mpz_get_ui(r);
418
419 mpz_clear(r);
420
421 return res;
422 }
423
424 static __inline__
flint_mpz_mod_ui(mpz_ptr r,mpz_srcptr n,ulong c)425 ulong flint_mpz_mod_ui(mpz_ptr r, mpz_srcptr n, ulong c)
426 {
427 FLINT_MOCK_MPZ_UI(tc, c);
428
429 mpz_fdiv_r(r, n, tc);
430
431 return flint_mpz_get_ui(r);
432 }
433
434 static __inline__
flint_mpz_powm_ui(mpz_ptr r,mpz_srcptr b,ulong exp,mpz_srcptr mod)435 void flint_mpz_powm_ui(mpz_ptr r, mpz_srcptr b, ulong exp, mpz_srcptr mod)
436 {
437 FLINT_MOCK_MPZ_UI(texp, exp);
438
439 mpz_powm(r, b, texp, mod);
440 }
441
442 static __inline__
flint_mpz_pow_ui(mpz_ptr r,mpz_srcptr b,ulong exp)443 void flint_mpz_pow_ui(mpz_ptr r, mpz_srcptr b, ulong exp)
444 {
445 if (exp >= (UWORD(1) << 32)) {
446 printf("Exception (flint_mpz_pow_ui). Power too large.\n");
447 flint_abort();
448 }
449
450 mpz_pow_ui(r, b, (unsigned long) exp);
451 }
452
453 static __inline__
flint_mpz_fac_ui(mpz_ptr r,ulong n)454 void flint_mpz_fac_ui(mpz_ptr r, ulong n)
455 {
456 if (n >= (UWORD(1) << 32)) {
457 printf("Exception (flint_mpz_fac_ui). Value n too large.\n");
458 flint_abort();
459 }
460
461 mpz_fac_ui(r, (unsigned long) n);
462 }
463
464 static __inline__
flint_mpz_bin_uiui(mpz_ptr r,ulong n,ulong k)465 void flint_mpz_bin_uiui(mpz_ptr r, ulong n, ulong k)
466 {
467 if (n >= (UWORD(1) << 32)) {
468 printf("Exception (flint_mpz_bin_uiui). Value n too large.\n");
469 flint_abort();
470 }
471
472 if (k >= (UWORD(1) << 32)) {
473 printf("Exception (flint_mpz_bin_uiui). Value k too large.\n");
474 flint_abort();
475 }
476
477 mpz_bin_uiui(r, (unsigned long) n, (unsigned long) k);
478 }
479
480 static __inline__
flint_mpz_fib_ui(mpz_ptr r,ulong n)481 void flint_mpz_fib_ui(mpz_ptr r, ulong n)
482 {
483 if (n >= (UWORD(1) << 32)) {
484 printf("Exception (flint_mpz_fib_ui). Value n too large.\n");
485 flint_abort();
486 }
487
488 mpz_fib_ui(r, (unsigned long) n);
489 }
490
491 /* mpf_set_si() -- Assign a float from a signed int.
492
493 Copyright 1993, 1994, 1995, 2000, 2001, 2002, 2004 Free Software Foundation,
494 Inc.
495
496 Copyright 2015 William Hart.
497
498 This file is part of the GNU MP Library.
499
500 The GNU MP Library is free software; you can redistribute it and/or modify
501 it under the terms of the GNU Lesser General Public License as published by
502 the Free Software Foundation; either version 2.1 of the License, or (at your
503 option) any later version.
504
505 The GNU MP Library is distributed in the hope that it will be useful, but
506 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
507 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
508 License for more details.
509
510 You should have received a copy of the GNU Lesser General Public License
511 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
512 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
513 MA 02110-1301, USA. */
514
515 static __inline__
flint_mpf_set_si(mpf_ptr dest,slong val)516 void flint_mpf_set_si (mpf_ptr dest, slong val)
517 {
518 mp_size_t size;
519 mp_limb_t vl;
520
521 vl = (mp_limb_t) (val >= 0 ? val : -val);
522
523 dest->_mp_d[0] = vl;
524 size = vl != 0;
525
526 dest->_mp_exp = size;
527 dest->_mp_size = val >= 0 ? size : -size;
528 }
529
530 /* mpf_set_ui() -- Assign a float from an unsigned int.
531
532 Copyright 1993, 1994, 1995, 2001, 2002, 2004 Free Software Foundation, Inc.
533
534 Copyright 2015 William Hart
535
536 This file is part of the GNU MP Library.
537
538 The GNU MP Library is free software; you can redistribute it and/or modify
539 it under the terms of the GNU Lesser General Public License as published by
540 the Free Software Foundation; either version 2.1 of the License, or (at your
541 option) any later version.
542
543 The GNU MP Library is distributed in the hope that it will be useful, but
544 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
545 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
546 License for more details.
547
548 You should have received a copy of the GNU Lesser General Public License
549 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
550 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
551 MA 02110-1301, USA. */
552
553 static __inline__
flint_mpf_set_ui(mpf_ptr f,ulong val)554 void flint_mpf_set_ui(mpf_ptr f, ulong val)
555 {
556 mp_size_t size;
557
558 f->_mp_d[0] = val;
559 size = val != 0;
560
561 f->_mp_exp = f->_mp_size = size;
562 }
563
564 /* mpf_get_si -- mpf to long conversion
565
566 Copyright 2001, 2002, 2004 Free Software Foundation, Inc.
567
568 Copyright 2015 William Hart
569
570 This file is part of the GNU MP Library.
571
572 The GNU MP Library is free software; you can redistribute it and/or modify
573 it under the terms of the GNU Lesser General Public License as published by
574 the Free Software Foundation; either version 2.1 of the License, or (at your
575 option) any later version.
576
577 The GNU MP Library is distributed in the hope that it will be useful, but
578 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
579 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
580 License for more details.
581
582 You should have received a copy of the GNU Lesser General Public License
583 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
584 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
585 MA 02110-1301, USA.
586 */
587
588 static __inline__
flint_mpf_get_si(mpf_srcptr f)589 slong flint_mpf_get_si (mpf_srcptr f)
590 {
591 mp_exp_t exp;
592 mp_size_t size, abs_size;
593 mp_srcptr fp;
594 mp_limb_t fl;
595
596 exp = f->_mp_exp;
597 size = f->_mp_size;
598 fp = f->_mp_d;
599
600 /* fraction alone truncates to zero
601 this also covers zero, since we have exp==0 for zero */
602 if (exp <= 0)
603 return WORD(0);
604
605 /* there are some limbs above the radix point */
606
607 fl = 0;
608 abs_size = FLINT_ABS(size);
609 if (abs_size >= exp)
610 fl = fp[abs_size-exp];
611
612 if (size > 0)
613 return fl;
614 else
615 /* this form necessary to correctly handle -0x80..00 */
616 return ~ (fl - 1);
617 }
618
619 /* mpf_cmp_ui -- Compare a float with an unsigned integer.
620
621 Copyright 1993, 1994, 1995, 1999, 2001, 2002 Free Software Foundation, Inc.
622
623 This file is part of the GNU MP Library.
624
625 The GNU MP Library is free software; you can redistribute it and/or modify
626 it under the terms of the GNU Lesser General Public License as published by
627 the Free Software Foundation; either version 2.1 of the License, or (at your
628 option) any later version.
629
630 The GNU MP Library is distributed in the hope that it will be useful, but
631 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
632 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
633 License for more details.
634
635 You should have received a copy of the GNU Lesser General Public License
636 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
637 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
638 MA 02110-1301, USA. */
639
640 static __inline__
flint_mpf_cmp_ui(mpf_srcptr u,ulong vval)641 int flint_mpf_cmp_ui(mpf_srcptr u, ulong vval)
642 {
643 mp_srcptr up;
644 mp_size_t usize;
645 mp_exp_t uexp;
646 mp_limb_t ulimb;
647
648 uexp = u->_mp_exp;
649 usize = u->_mp_size;
650
651 if (usize < 0)
652 return -1;
653
654 if (vval == 0)
655 return usize != 0;
656
657 if (uexp > 1)
658 return 1;
659 if (uexp < 1)
660 return -1;
661
662 up = u->_mp_d;
663
664 ulimb = up[usize - 1];
665 usize--;
666
667 if (ulimb > vval)
668 return 1;
669 else if (ulimb < vval)
670 return -1;
671
672 while (*up == 0)
673 {
674 up++;
675 usize--;
676 }
677
678 if (usize > 0)
679 return 1;
680
681 return 0;
682 }
683
684 /* mpf_fits_s*_p -- test whether an mpf fits a C signed type.
685
686 Copyright 2001, 2002 Free Software Foundation, Inc.
687
688 This file is part of the GNU MP Library.
689
690 The GNU MP Library is free software; you can redistribute it and/or modify
691 it under the terms of the GNU Lesser General Public License as published by
692 the Free Software Foundation; either version 2.1 of the License, or (at your
693 option) any later version.
694
695 The GNU MP Library is distributed in the hope that it will be useful, but
696 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
697 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
698 License for more details.
699
700 You should have received a copy of the GNU Lesser General Public License
701 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
702 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
703 MA 02110-1301, USA. */
704
705 static __inline__
flint_mpf_fits_slong_p(mpf_srcptr f)706 int flint_mpf_fits_slong_p(mpf_srcptr f)
707 {
708 mp_size_t fs, fn;
709 mp_srcptr fp;
710 mp_exp_t exp;
711 mp_limb_t fl;
712
713 fs = f->_mp_size;
714 if (fs == 0)
715 return 1; /* zero fits */
716
717 exp = f->_mp_exp;
718 if (exp < 1)
719 return 1; /* -1 < f < 1 truncates to zero, so fits */
720
721 fp = f->_mp_d;
722 fn = FLINT_ABS(fs);
723
724 if (exp == 1)
725 {
726 fl = fp[fn-1];
727 }
728 else
729 return 0;
730
731 return fl <= (fs >= 0 ? (mp_limb_t) WORD_MAX : - (mp_limb_t) WORD_MIN);
732 }
733
734 /* double mpf_get_d_2exp (signed long int *exp, mpf_t src).
735
736 Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
737
738 This file is part of the GNU MP Library.
739
740 The GNU MP Library is free software; you can redistribute it and/or modify
741 it under the terms of the GNU Lesser General Public License as published by
742 the Free Software Foundation; either version 2.1 of the License, or (at your
743 option) any later version.
744
745 The GNU MP Library is distributed in the hope that it will be useful, but
746 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
747 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
748 License for more details.
749
750 You should have received a copy of the GNU Lesser General Public License
751 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
752 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
753 MA 02110-1301, USA. */
754
755 extern double __gmpn_get_d(mp_limb_t *, size_t, size_t, long);
756
757 static __inline__
flint_mpf_get_d_2exp(slong * exp2,mpf_srcptr src)758 double flint_mpf_get_d_2exp(slong * exp2, mpf_srcptr src)
759 {
760 mp_size_t size, abs_size;
761 mp_limb_t * ptr;
762 int cnt;
763 slong exp;
764
765 size = src->_mp_size;
766 if (size == 0)
767 {
768 *exp2 = 0;
769 return 0.0;
770 }
771
772 ptr = src->_mp_d;
773 abs_size = FLINT_ABS(size);
774 count_leading_zeros (cnt, ptr[abs_size - 1]);
775
776 exp = src->_mp_exp * FLINT_BITS - cnt;
777 *exp2 = exp;
778
779 return __gmpn_get_d (ptr, abs_size, size,
780 (long) - (abs_size * FLINT_BITS - cnt));
781 }
782
783 #else
784
785 #define flint_mpz_get_si mpz_get_si
786 #define flint_mpz_get_ui mpz_get_ui
787 #define flint_mpz_set_si mpz_set_si
788 #define flint_mpz_set_ui mpz_set_ui
789 #define flint_mpz_init_set_si mpz_init_set_si
790 #define flint_mpz_init_set_ui mpz_init_set_ui
791 #define flint_mpz_add_ui mpz_add_ui
792 #define flint_mpz_sub_ui mpz_sub_ui
793 #define flint_mpz_mul_si mpz_mul_si
794 #define flint_mpz_mul_ui mpz_mul_ui
795 #define flint_mpz_addmul_ui mpz_addmul_ui
796 #define flint_mpz_submul_ui mpz_submul_ui
797 #define flint_mpz_ui_sub mpz_ui_sub
798 #define flint_mpz_ui_pow_ui mpz_ui_pow_ui
799 #define flint_mpz_cdiv_q_ui mpz_cdiv_q_ui
800 #define flint_mpz_cdiv_r_ui mpz_cdiv_r_ui
801 #define flint_mpz_cdiv_qr_ui mpz_cdiv_qr_ui
802 #define flint_mpz_cdiv_ui mpz_cdiv_ui
803 #define flint_mpz_fdiv_q_ui mpz_fdiv_q_ui
804 #define flint_mpz_fdiv_r_ui mpz_fdiv_r_ui
805 #define flint_mpz_fdiv_qr_ui mpz_fdiv_qr_ui
806 #define flint_mpz_fdiv_ui mpz_fdiv_ui
807 #define flint_mpz_tdiv_q_ui mpz_tdiv_q_ui
808 #define flint_mpz_tdiv_r_ui mpz_tdiv_r_ui
809 #define flint_mpz_tdiv_qr_ui mpz_tdiv_qr_ui
810 #define flint_mpz_tdiv_ui mpz_tdiv_ui
811 #define flint_mpz_mod_ui mpz_mod_ui
812 #define flint_mpz_divexact_ui mpz_divexact_ui
813 #define flint_mpz_divisible_ui_p mpz_divisible_ui_p
814 #define flint_mpz_congruent_ui_p mpz_congruent_ui_p
815 #define flint_mpz_powm_ui mpz_powm_ui
816 #define flint_mpz_pow_ui mpz_pow_ui
817 #define flint_mpz_fac_ui mpz_fac_ui
818 #define flint_mpz_bin_uiui mpz_bin_uiui
819 #define flint_mpz_fib_ui mpz_fib_ui
820 #define flint_mpz_cmp_si mpz_cmp_si
821 #define flint_mpz_cmp_ui mpz_cmp_ui
822 #define flint_mpq_cmp_si mpq_cmp_si
823 #define flint_mpq_cmp_ui mpq_cmp_ui
824 #define flint_mpq_set_si mpq_set_si
825 #define flint_mpq_set_ui mpq_set_ui
826
827 #define flint_mpf_set_si mpf_set_si
828 #define flint_mpf_set_ui mpf_set_ui
829 #define flint_mpf_get_si mpf_get_si
830 #define flint_mpf_cmp_ui mpf_cmp_ui
831 #define flint_mpf_fits_slong_p mpf_fits_slong_p
832 #define flint_mpf_get_d_2exp mpf_get_d_2exp
833
834 #endif
835
836 #endif
837