-
Notifications
You must be signed in to change notification settings - Fork 140
/
arf.h
1324 lines (1092 loc) · 32.6 KB
/
arf.h
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2014 Fredrik Johansson
2x2 mul code taken from MPFR 2.3.0
(Copyright (C) 1991-2007 Free Software Foundation, Inc.)
This file is part of Arb.
Arb is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version. See <http://www.gnu.org/licenses/>.
*/
#ifndef ARF_H
#define ARF_H
#ifdef ARF_INLINES_C
#define ARF_INLINE
#else
#define ARF_INLINE static __inline__
#endif
#include <stdio.h>
#include <math.h>
#include "flint/flint.h"
#include "fmpr.h"
#include "mag.h"
#ifndef flint_abort
#if __FLINT_RELEASE <= 20502
#define flint_abort abort
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
#define arf_rnd_t fmpr_rnd_t
#define ARF_RND_DOWN FMPR_RND_DOWN
#define ARF_RND_UP FMPR_RND_UP
#define ARF_RND_FLOOR FMPR_RND_FLOOR
#define ARF_RND_CEIL FMPR_RND_CEIL
#define ARF_RND_NEAR FMPR_RND_NEAR
ARF_INLINE int
arf_rounds_down(arf_rnd_t rnd, int sgnbit)
{
if (rnd == ARF_RND_DOWN) return 1;
if (rnd == ARF_RND_UP) return 0;
if (rnd == ARF_RND_FLOOR) return !sgnbit;
return sgnbit;
}
ARF_INLINE int
arf_rounds_up(arf_rnd_t rnd, int sgnbit)
{
if (rnd == ARF_RND_DOWN) return 0;
if (rnd == ARF_RND_UP) return 1;
if (rnd == ARF_RND_FLOOR) return sgnbit;
return !sgnbit;
}
ARF_INLINE mpfr_rnd_t
arf_rnd_to_mpfr(arf_rnd_t rnd)
{
if (rnd == ARF_RND_DOWN) return MPFR_RNDZ;
else if (rnd == ARF_RND_UP) return MPFR_RNDA;
else if (rnd == ARF_RND_FLOOR) return MPFR_RNDD;
else if (rnd == ARF_RND_CEIL) return MPFR_RNDU;
else return MPFR_RNDN;
}
/* Allow 'infinite' precision for operations where a result can be computed exactly. */
#define ARF_PREC_EXACT WORD_MAX
#define ARF_PREC_ADD(prec,extra) ((prec) == ARF_PREC_EXACT ? ARF_PREC_EXACT : (prec) + (extra))
#define ARF_RESULT_EXACT 0
#define ARF_RESULT_INEXACT 1
/* Range where we can skip fmpz overflow checks for exponent manipulation. */
#define ARF_MAX_LAGOM_EXP MAG_MAX_LAGOM_EXP
#define ARF_MIN_LAGOM_EXP MAG_MIN_LAGOM_EXP
/* Exponent values used to encode special values. */
#define ARF_EXP_ZERO 0
#define ARF_EXP_NAN COEFF_MIN
#define ARF_EXP_POS_INF (COEFF_MIN+1)
#define ARF_EXP_NEG_INF (COEFF_MIN+2)
/* Direct access to the exponent. */
#define ARF_EXP(x) ((x)->exp)
#define ARF_EXPREF(x) (&(x)->exp)
/* Finite and with lagom big exponents. */
#define ARF_IS_LAGOM(x) (ARF_EXP(x) >= ARF_MIN_LAGOM_EXP && \
ARF_EXP(x) <= ARF_MAX_LAGOM_EXP)
/* More than two limbs (needs pointer). */
#define ARF_HAS_PTR(x) ((x)->size > ((2 << 1) + 1))
/* Raw size field (encodes both limb size and sign). */
#define ARF_XSIZE(x) ((x)->size)
/* Construct size field value from size in limbs and sign bit. */
#define ARF_MAKE_XSIZE(size, sgnbit) ((((mp_size_t) size) << 1) | (sgnbit))
/* The limb size, and the sign bit. */
#define ARF_SIZE(x) (ARF_XSIZE(x) >> 1)
#define ARF_SGNBIT(x) (ARF_XSIZE(x) & 1)
/* Assumes non-special value */
#define ARF_NEG(x) (ARF_XSIZE(x) ^= 1)
/* Note: may also be hardcoded in a few places. */
#define ARF_NOPTR_LIMBS 2
/* Direct access to the limb data. */
#define ARF_NOPTR_D(x) ((x)->d.noptr.d)
#define ARF_PTR_D(x) ((x)->d.ptr.d)
#define ARF_PTR_ALLOC(x) ((x)->d.ptr.alloc)
/* Encoding for special values. */
#define ARF_IS_SPECIAL(x) (ARF_XSIZE(x) == 0)
/* Value is +/- a power of two */
#define ARF_IS_POW2(x) (ARF_SIZE(x) == 1) && (ARF_NOPTR_D(x)[0] == LIMB_TOP)
/* To set a special value, first call this and then set the exponent. */
#define ARF_MAKE_SPECIAL(x) \
do { \
fmpz_clear(ARF_EXPREF(x)); \
ARF_DEMOTE(x); \
ARF_XSIZE(x) = 0; \
} while (0)
typedef struct
{
mp_limb_t d[ARF_NOPTR_LIMBS];
}
mantissa_noptr_struct;
typedef struct
{
mp_size_t alloc;
mp_ptr d;
}
mantissa_ptr_struct;
typedef union
{
mantissa_noptr_struct noptr;
mantissa_ptr_struct ptr;
}
mantissa_struct;
typedef struct
{
fmpz exp;
mp_size_t size;
mantissa_struct d;
}
arf_struct;
typedef arf_struct arf_t[1];
typedef arf_struct * arf_ptr;
typedef const arf_struct * arf_srcptr;
void _arf_promote(arf_t x, mp_size_t n);
void _arf_demote(arf_t x);
/* Warning: does not set size! -- also doesn't demote exponent. */
#define ARF_DEMOTE(x) \
do { \
if (ARF_HAS_PTR(x)) \
_arf_demote(x); \
} while (0)
/* Get mpn pointer and size (xptr, xn) for read-only use. */
#define ARF_GET_MPN_READONLY(xptr, xn, x) \
do { \
xn = ARF_SIZE(x); \
if (xn <= ARF_NOPTR_LIMBS) \
xptr = ARF_NOPTR_D(x); \
else \
xptr = ARF_PTR_D(x); \
} while (0)
/* Assumes non-special! */
#define ARF_GET_TOP_LIMB(lmb, x) \
do { \
mp_srcptr __xptr; \
mp_size_t __xn; \
ARF_GET_MPN_READONLY(__xptr, __xn, (x)); \
(lmb) = __xptr[__xn - 1]; \
} while (0)
/* Get mpn pointer xptr for writing *exactly* xn limbs to x. */
#define ARF_GET_MPN_WRITE(xptr, xn, x) \
do { \
mp_size_t __xn = (xn); \
if ((__xn) <= ARF_NOPTR_LIMBS) \
{ \
ARF_DEMOTE(x); \
xptr = ARF_NOPTR_D(x); \
} \
else \
{ \
if (!ARF_HAS_PTR(x)) \
{ \
_arf_promote(x, __xn); \
} \
else if (ARF_PTR_ALLOC(x) < (__xn)) \
{ \
ARF_PTR_D(x) = (mp_ptr) \
flint_realloc(ARF_PTR_D(x), \
(xn) * sizeof(mp_limb_t)); \
ARF_PTR_ALLOC(x) = (__xn); \
} \
xptr = ARF_PTR_D(x); \
} \
ARF_XSIZE(x) = ARF_MAKE_XSIZE(__xn, 0); \
} while (0)
ARF_INLINE void
arf_init(arf_t x)
{
fmpz_init(ARF_EXPREF(x));
ARF_XSIZE(x) = 0;
}
void arf_clear(arf_t x);
arf_ptr _arf_vec_init(slong n);
void _arf_vec_clear(arf_ptr v, slong n);
ARF_INLINE void
arf_zero(arf_t x)
{
ARF_MAKE_SPECIAL(x);
ARF_EXP(x) = ARF_EXP_ZERO;
}
ARF_INLINE void
arf_pos_inf(arf_t x)
{
ARF_MAKE_SPECIAL(x);
ARF_EXP(x) = ARF_EXP_POS_INF;
}
ARF_INLINE void
arf_neg_inf(arf_t x)
{
ARF_MAKE_SPECIAL(x);
ARF_EXP(x) = ARF_EXP_NEG_INF;
}
ARF_INLINE void
arf_nan(arf_t x)
{
ARF_MAKE_SPECIAL(x);
ARF_EXP(x) = ARF_EXP_NAN;
}
ARF_INLINE int
arf_is_special(const arf_t x)
{
return ARF_IS_SPECIAL(x);
}
ARF_INLINE int
arf_is_zero(const arf_t x)
{
return ARF_IS_SPECIAL(x) && (ARF_EXP(x) == ARF_EXP_ZERO);
}
ARF_INLINE int
arf_is_pos_inf(const arf_t x)
{
return ARF_IS_SPECIAL(x) && (ARF_EXP(x) == ARF_EXP_POS_INF);
}
ARF_INLINE int
arf_is_neg_inf(const arf_t x)
{
return ARF_IS_SPECIAL(x) && (ARF_EXP(x) == ARF_EXP_NEG_INF);
}
ARF_INLINE int
arf_is_nan(const arf_t x)
{
return ARF_IS_SPECIAL(x) && (ARF_EXP(x) == ARF_EXP_NAN);
}
ARF_INLINE int
arf_is_normal(const arf_t x)
{
return !ARF_IS_SPECIAL(x);
}
ARF_INLINE int
arf_is_finite(const arf_t x)
{
return !ARF_IS_SPECIAL(x) || (ARF_EXP(x) == ARF_EXP_ZERO);
}
ARF_INLINE int
arf_is_inf(const arf_t x)
{
return ARF_IS_SPECIAL(x) && (ARF_EXP(x) == ARF_EXP_POS_INF ||
ARF_EXP(x) == ARF_EXP_NEG_INF);
}
ARF_INLINE void
arf_one(arf_t x)
{
fmpz_clear(ARF_EXPREF(x));
ARF_DEMOTE(x);
ARF_EXP(x) = 1;
ARF_XSIZE(x) = ARF_MAKE_XSIZE(1, 0);
ARF_NOPTR_D(x)[0] = LIMB_TOP;
}
ARF_INLINE int
arf_is_one(const arf_t x)
{
return (ARF_EXP(x) == 1) && (ARF_XSIZE(x) == ARF_MAKE_XSIZE(1, 0))
&& ARF_NOPTR_D(x)[0] == LIMB_TOP;
}
ARF_INLINE int
arf_sgn(const arf_t x)
{
if (arf_is_special(x))
{
if (arf_is_zero(x) || arf_is_nan(x))
return 0;
return arf_is_pos_inf(x) ? 1 : -1;
}
else
{
return ARF_SGNBIT(x) ? -1 : 1;
}
}
int arf_cmp(const arf_t x, const arf_t y);
int arf_cmpabs(const arf_t x, const arf_t y);
int arf_cmpabs_ui(const arf_t x, ulong y);
int arf_cmpabs_d(const arf_t x, double y);
int arf_cmp_si(const arf_t x, slong y);
int arf_cmp_ui(const arf_t x, ulong y);
int arf_cmp_d(const arf_t x, double y);
ARF_INLINE void
arf_swap(arf_t y, arf_t x)
{
if (x != y)
{
arf_struct t = *x;
*x = *y;
*y = t;
}
}
ARF_INLINE void
arf_set(arf_t y, const arf_t x)
{
if (x != y)
{
/* Fast path */
if (!COEFF_IS_MPZ(ARF_EXP(x)) && !COEFF_IS_MPZ(ARF_EXP(y)))
ARF_EXP(y) = ARF_EXP(x);
else
fmpz_set(ARF_EXPREF(y), ARF_EXPREF(x));
/* Fast path */
if (!ARF_HAS_PTR(x))
{
ARF_DEMOTE(y);
(y)->d = (x)->d;
}
else
{
mp_ptr yptr;
mp_srcptr xptr;
mp_size_t n;
ARF_GET_MPN_READONLY(xptr, n, x);
ARF_GET_MPN_WRITE(yptr, n, y);
flint_mpn_copyi(yptr, xptr, n);
}
/* Important. */
ARF_XSIZE(y) = ARF_XSIZE(x);
}
}
ARF_INLINE void
arf_neg(arf_t y, const arf_t x)
{
arf_set(y, x);
if (arf_is_special(y))
{
if (arf_is_pos_inf(y))
arf_neg_inf(y);
else if (arf_is_neg_inf(y))
arf_pos_inf(y);
}
else
{
ARF_NEG(y);
}
}
ARF_INLINE void
arf_init_set_ui(arf_t x, ulong v)
{
if (v == 0)
{
ARF_EXP(x) = ARF_EXP_ZERO;
ARF_XSIZE(x) = 0;
}
else
{
unsigned int c;
count_leading_zeros(c, v);
ARF_EXP(x) = FLINT_BITS - c;
ARF_NOPTR_D(x)[0] = v << c;
ARF_XSIZE(x) = ARF_MAKE_XSIZE(1, 0);
}
}
/* FLINT_ABS is unsafe for x = WORD_MIN */
#define UI_ABS_SI(x) (((slong)(x) < 0) ? (-(ulong)(x)) : ((ulong)(x)))
ARF_INLINE void
arf_init_set_si(arf_t x, slong v)
{
arf_init_set_ui(x, UI_ABS_SI(v));
if (v < 0)
ARF_NEG(x);
}
ARF_INLINE void
arf_set_ui(arf_t x, ulong v)
{
ARF_DEMOTE(x);
_fmpz_demote(ARF_EXPREF(x));
if (v == 0)
{
ARF_EXP(x) = ARF_EXP_ZERO;
ARF_XSIZE(x) = 0;
}
else
{
unsigned int c;
count_leading_zeros(c, v);
ARF_EXP(x) = FLINT_BITS - c;
ARF_NOPTR_D(x)[0] = v << c;
ARF_XSIZE(x) = ARF_MAKE_XSIZE(1, 0);
}
}
ARF_INLINE void
arf_set_si(arf_t x, slong v)
{
arf_set_ui(x, UI_ABS_SI(v));
if (v < 0)
ARF_NEG(x);
}
ARF_INLINE void
arf_init_set_shallow(arf_t z, const arf_t x)
{
*z = *x;
}
ARF_INLINE void
arf_init_neg_shallow(arf_t z, const arf_t x)
{
*z = *x;
arf_neg(z, z);
}
ARF_INLINE void
arf_init_set_mag_shallow(arf_t y, const mag_t x)
{
mp_limb_t t = MAG_MAN(x);
ARF_XSIZE(y) = ARF_MAKE_XSIZE(t != 0, 0);
ARF_EXP(y) = MAG_EXP(x);
ARF_NOPTR_D(y)[0] = t << (FLINT_BITS - MAG_BITS);
}
ARF_INLINE void
arf_init_neg_mag_shallow(arf_t z, const mag_t x)
{
arf_init_set_mag_shallow(z, x);
arf_neg(z, z);
}
ARF_INLINE int
arf_cmpabs_mag(const arf_t x, const mag_t y)
{
arf_t t;
arf_init_set_mag_shallow(t, y); /* no need to free */
return arf_cmpabs(x, t);
}
ARF_INLINE int
arf_mag_cmpabs(const mag_t x, const arf_t y)
{
arf_t t;
arf_init_set_mag_shallow(t, x); /* no need to free */
return arf_cmpabs(t, y);
}
/* Assumes xn > 0, x[xn-1] != 0. */
/* TBD: 1, 2 limb versions */
void arf_set_mpn(arf_t y, mp_srcptr x, mp_size_t xn, int sgnbit);
ARF_INLINE void
arf_set_mpz(arf_t y, const mpz_t x)
{
slong size = x->_mp_size;
if (size == 0)
arf_zero(y);
else
arf_set_mpn(y, x->_mp_d, FLINT_ABS(size), size < 0);
}
ARF_INLINE void
arf_set_fmpz(arf_t y, const fmpz_t x)
{
if (!COEFF_IS_MPZ(*x))
arf_set_si(y, *x);
else
arf_set_mpz(y, COEFF_TO_PTR(*x));
}
int _arf_set_round_ui(arf_t x, ulong v, int sgnbit, slong prec, arf_rnd_t rnd);
int _arf_set_round_uiui(arf_t z, slong * fix, mp_limb_t hi, mp_limb_t lo, int sgnbit, slong prec, arf_rnd_t rnd);
int
_arf_set_round_mpn(arf_t y, slong * exp_shift, mp_srcptr x, mp_size_t xn,
int sgnbit, slong prec, arf_rnd_t rnd);
ARF_INLINE int
arf_set_round_ui(arf_t x, ulong v, slong prec, arf_rnd_t rnd)
{
return _arf_set_round_ui(x, v, 0, prec, rnd);
}
ARF_INLINE int
arf_set_round_si(arf_t x, slong v, slong prec, arf_rnd_t rnd)
{
return _arf_set_round_ui(x, UI_ABS_SI(v), v < 0, prec, rnd);
}
ARF_INLINE int
arf_set_round_mpz(arf_t y, const mpz_t x, slong prec, arf_rnd_t rnd)
{
int inexact;
slong size = x->_mp_size;
slong fix;
if (size == 0)
{
arf_zero(y);
return 0;
}
inexact = _arf_set_round_mpn(y, &fix, x->_mp_d, FLINT_ABS(size),
(size < 0), prec, rnd);
_fmpz_demote(ARF_EXPREF(y));
ARF_EXP(y) = FLINT_ABS(size) * FLINT_BITS + fix;
return inexact;
}
ARF_INLINE int
arf_set_round_fmpz(arf_t y, const fmpz_t x, slong prec, arf_rnd_t rnd)
{
if (!COEFF_IS_MPZ(*x))
return arf_set_round_si(y, *x, prec, rnd);
else
return arf_set_round_mpz(y, COEFF_TO_PTR(*x), prec, rnd);
}
int arf_set_round(arf_t y, const arf_t x, slong prec, arf_rnd_t rnd);
int arf_neg_round(arf_t y, const arf_t x, slong prec, arf_rnd_t rnd);
void arf_get_fmpr(fmpr_t y, const arf_t x);
void arf_set_fmpr(arf_t y, const fmpr_t x);
int arf_get_mpfr(mpfr_t x, const arf_t y, mpfr_rnd_t rnd);
void arf_set_mpfr(arf_t x, const mpfr_t y);
int arf_equal(const arf_t x, const arf_t y);
int arf_equal_si(const arf_t x, slong y);
int arf_equal_ui(const arf_t x, ulong y);
int arf_equal_d(const arf_t x, double y);
ARF_INLINE void
arf_min(arf_t z, const arf_t a, const arf_t b)
{
if (arf_cmp(a, b) <= 0)
arf_set(z, a);
else
arf_set(z, b);
}
ARF_INLINE void
arf_max(arf_t z, const arf_t a, const arf_t b)
{
if (arf_cmp(a, b) > 0)
arf_set(z, a);
else
arf_set(z, b);
}
ARF_INLINE void
arf_abs(arf_t y, const arf_t x)
{
if (arf_sgn(x) < 0)
arf_neg(y, x);
else
arf_set(y, x);
}
ARF_INLINE slong
arf_bits(const arf_t x)
{
if (arf_is_special(x))
return 0;
else
{
mp_srcptr xp;
mp_size_t xn;
slong c;
ARF_GET_MPN_READONLY(xp, xn, x);
count_trailing_zeros(c, xp[0]);
return xn * FLINT_BITS - c;
}
}
ARF_INLINE void
arf_bot(fmpz_t e, const arf_t x)
{
if (arf_is_special(x))
fmpz_zero(e);
else
fmpz_sub_si(e, ARF_EXPREF(x), arf_bits(x));
}
int arf_is_int(const arf_t x);
int arf_is_int_2exp_si(const arf_t x, slong e);
int arf_cmp_2exp_si(const arf_t x, slong e);
int arf_cmpabs_2exp_si(const arf_t x, slong e);
ARF_INLINE void
arf_set_si_2exp_si(arf_t x, slong man, slong exp)
{
arf_set_si(x, man);
if (man != 0)
fmpz_add_si_inline(ARF_EXPREF(x), ARF_EXPREF(x), exp);
}
ARF_INLINE void
arf_set_ui_2exp_si(arf_t x, ulong man, slong exp)
{
arf_set_ui(x, man);
if (man != 0)
fmpz_add_si_inline(ARF_EXPREF(x), ARF_EXPREF(x), exp);
}
ARF_INLINE void
arf_mul_2exp_si(arf_t y, const arf_t x, slong e)
{
arf_set(y, x);
if (!arf_is_special(y))
fmpz_add_si_inline(ARF_EXPREF(y), ARF_EXPREF(y), e);
}
ARF_INLINE void
arf_mul_2exp_fmpz(arf_t y, const arf_t x, const fmpz_t e)
{
arf_set(y, x);
if (!arf_is_special(y))
fmpz_add_inline(ARF_EXPREF(y), ARF_EXPREF(y), e);
}
ARF_INLINE int
arf_set_round_fmpz_2exp(arf_t y, const fmpz_t x, const fmpz_t exp, slong prec, arf_rnd_t rnd)
{
if (fmpz_is_zero(x))
{
arf_zero(y);
return 0;
}
else
{
int r = arf_set_round_fmpz(y, x, prec, rnd);
fmpz_add_inline(ARF_EXPREF(y), ARF_EXPREF(y), exp);
return r;
}
}
ARF_INLINE void
arf_abs_bound_lt_2exp_fmpz(fmpz_t b, const arf_t x)
{
if (arf_is_special(x))
fmpz_zero(b);
else
fmpz_set(b, ARF_EXPREF(x));
}
ARF_INLINE void
arf_abs_bound_le_2exp_fmpz(fmpz_t b, const arf_t x)
{
if (arf_is_special(x))
fmpz_zero(b);
else if (ARF_IS_POW2(x))
fmpz_sub_ui(b, ARF_EXPREF(x), 1);
else
fmpz_set(b, ARF_EXPREF(x));
}
slong arf_abs_bound_lt_2exp_si(const arf_t x);
void arf_frexp(arf_t man, fmpz_t exp, const arf_t x);
void arf_get_fmpz_2exp(fmpz_t man, fmpz_t exp, const arf_t x);
int _arf_get_integer_mpn(mp_ptr y, mp_srcptr x, mp_size_t xn, slong exp);
int _arf_set_mpn_fixed(arf_t z, mp_srcptr xp, mp_size_t xn,
mp_size_t fixn, int negative, slong prec, arf_rnd_t rnd);
int arf_get_fmpz(fmpz_t z, const arf_t x, arf_rnd_t rnd);
slong arf_get_si(const arf_t x, arf_rnd_t rnd);
int arf_get_fmpz_fixed_fmpz(fmpz_t y, const arf_t x, const fmpz_t e);
int arf_get_fmpz_fixed_si(fmpz_t y, const arf_t x, slong e);
ARF_INLINE void
arf_set_fmpz_2exp(arf_t x, const fmpz_t man, const fmpz_t exp)
{
arf_set_fmpz(x, man);
if (!arf_is_zero(x))
fmpz_add_inline(ARF_EXPREF(x), ARF_EXPREF(x), exp);
}
void arf_floor(arf_t z, const arf_t x);
void arf_ceil(arf_t z, const arf_t x);
void arf_debug(const arf_t x);
char * arf_get_str(const arf_t x, slong d);
void arf_fprint(FILE * file, const arf_t x);
void arf_fprintd(FILE * file, const arf_t y, slong d);
ARF_INLINE void
arf_print(const arf_t x)
{
arf_fprint(stdout, x);
}
ARF_INLINE void
arf_printd(const arf_t y, slong d)
{
arf_fprintd(stdout, y, d);
}
void arf_randtest(arf_t x, flint_rand_t state, slong bits, slong mag_bits);
void arf_randtest_not_zero(arf_t x, flint_rand_t state, slong bits, slong mag_bits);
void arf_randtest_special(arf_t x, flint_rand_t state, slong bits, slong mag_bits);
void arf_urandom(arf_t x, flint_rand_t state, slong bits, arf_rnd_t rnd);
#define MUL_MPFR_MIN_LIMBS 25
#define MUL_MPFR_MAX_LIMBS 10000
#define nn_mul_2x1(r2, r1, r0, a1, a0, b0) \
do { \
mp_limb_t t1; \
umul_ppmm(r1, r0, a0, b0); \
umul_ppmm(r2, t1, a1, b0); \
add_ssaaaa(r2, r1, r2, r1, 0, t1); \
} while (0)
#define nn_mul_2x2(r3, r2, r1, r0, a1, a0, b1, b0) \
do { \
mp_limb_t t1, t2, t3; \
umul_ppmm(r1, r0, a0, b0); \
umul_ppmm(r2, t1, a1, b0); \
add_ssaaaa(r2, r1, r2, r1, 0, t1); \
umul_ppmm(t1, t2, a0, b1); \
umul_ppmm(r3, t3, a1, b1); \
add_ssaaaa(r3, t1, r3, t1, 0, t3); \
add_ssaaaa(r2, r1, r2, r1, t1, t2); \
r3 += r2 < t1; \
} while (0)
/* todo: use mpn_extras.h when available */
#define ARF_USE_FFT_MUL(_xn) ((_xn) > 32000)
/* from flint/fft.h */
void flint_mpn_mul_fft_main(mp_ptr r1, mp_srcptr i1, mp_size_t n1, mp_srcptr i2, mp_size_t n2);
#define ARF_MPN_MUL(_z, _x, _xn, _y, _yn) \
if ((_xn) == (_yn)) \
{ \
if ((_xn) == 1) \
{ \
umul_ppmm((_z)[1], (_z)[0], (_x)[0], (_y)[0]); \
} \
else if ((_xn) == 2) \
{ \
mp_limb_t __arb_x1, __arb_x0, __arb_y1, __arb_y0; \
__arb_x0 = (_x)[0]; \
__arb_x1 = (_x)[1]; \
__arb_y0 = (_y)[0]; \
__arb_y1 = (_y)[1]; \
nn_mul_2x2((_z)[3], (_z)[2], (_z)[1], (_z)[0], __arb_x1, __arb_x0, __arb_y1, __arb_y0); \
} \
else if (ARF_USE_FFT_MUL(_xn)) \
flint_mpn_mul_fft_main((_z), (_x), (_xn), (_y), (_yn)); \
else if ((_x) == (_y)) \
mpn_sqr((_z), (_x), (_xn)); \
else \
mpn_mul_n((_z), (_x), (_y), (_xn)); \
} \
else if ((_xn) > (_yn)) \
{ \
if ((_yn) == 1) \
(_z)[(_xn) + (_yn) - 1] = mpn_mul_1((_z), (_x), (_xn), (_y)[0]); \
else if (ARF_USE_FFT_MUL(_yn)) \
flint_mpn_mul_fft_main((_z), (_x), (_xn), (_y), (_yn)); \
else \
mpn_mul((_z), (_x), (_xn), (_y), (_yn)); \
} \
else \
{ \
if ((_xn) == 1) \
(_z)[(_xn) + (_yn) - 1] = mpn_mul_1((_z), (_y), (_yn), (_x)[0]); \
else if (ARF_USE_FFT_MUL(_xn)) \
flint_mpn_mul_fft_main((_z), (_y), (_yn), (_x), (_xn)); \
else \
mpn_mul((_z), (_y), (_yn), (_x), (_xn)); \
}
#define ARF_MUL_STACK_ALLOC 40
#define ARF_MUL_TLS_ALLOC 1000
extern TLS_PREFIX mp_ptr __arf_mul_tmp;
extern TLS_PREFIX slong __arf_mul_alloc;
ARB_DLL extern void _arf_mul_tmp_cleanup(void);
#define ARF_MUL_TMP_DECL \
mp_limb_t tmp_stack[ARF_MUL_STACK_ALLOC]; \
#define ARF_MUL_TMP_ALLOC(tmp, alloc) \
if (alloc <= ARF_MUL_STACK_ALLOC) \
{ \
tmp = tmp_stack; \
} \
else if (alloc <= ARF_MUL_TLS_ALLOC) \
{ \
if (__arf_mul_alloc < alloc) \
{ \
if (__arf_mul_alloc == 0) \
{ \
flint_register_cleanup_function(_arf_mul_tmp_cleanup); \
} \
__arf_mul_tmp = flint_realloc(__arf_mul_tmp, sizeof(mp_limb_t) * alloc); \
__arf_mul_alloc = alloc; \
} \
tmp = __arf_mul_tmp; \
} \
else \
{ \
tmp = flint_malloc(sizeof(mp_limb_t) * alloc); \
}
#define ARF_MUL_TMP_FREE(tmp, alloc) \
if (alloc > ARF_MUL_TLS_ALLOC) \
flint_free(tmp);
void arf_mul_special(arf_t z, const arf_t x, const arf_t y);
int arf_mul_via_mpfr(arf_t z, const arf_t x, const arf_t y, slong prec, arf_rnd_t rnd);
int arf_mul_rnd_any(arf_ptr z, arf_srcptr x, arf_srcptr y, slong prec, arf_rnd_t rnd);
int arf_mul_rnd_down(arf_ptr z, arf_srcptr x, arf_srcptr y, slong prec);
#define arf_mul(z, x, y, prec, rnd) \
((rnd == FMPR_RND_DOWN) \
? arf_mul_rnd_down(z, x, y, prec) \
: arf_mul_rnd_any(z, x, y, prec, rnd))
ARF_INLINE int
arf_neg_mul(arf_t z, const arf_t x, const arf_t y, slong prec, arf_rnd_t rnd)
{
if (arf_is_special(y))
{
arf_mul(z, x, y, prec, rnd);
arf_neg(z, z);
return 0;
}
else
{
arf_t t;
*t = *y;
ARF_NEG(t);
return arf_mul(z, x, t, prec, rnd);
}
}
ARF_INLINE int
arf_mul_ui(arf_ptr z, arf_srcptr x, ulong y, slong prec, arf_rnd_t rnd)
{
arf_t t;
arf_init_set_ui(t, y); /* no need to free */
return arf_mul(z, x, t, prec, rnd);
}
ARF_INLINE int
arf_mul_si(arf_ptr z, arf_srcptr x, slong y, slong prec, arf_rnd_t rnd)
{
arf_t t;
arf_init_set_si(t, y); /* no need to free */
return arf_mul(z, x, t, prec, rnd);
}
int arf_mul_mpz(arf_ptr z, arf_srcptr x, const mpz_t y, slong prec, arf_rnd_t rnd);
ARF_INLINE int
arf_mul_fmpz(arf_ptr z, arf_srcptr x, const fmpz_t y, slong prec, arf_rnd_t rnd)
{
if (!COEFF_IS_MPZ(*y))
return arf_mul_si(z, x, *y, prec, rnd);
else
return arf_mul_mpz(z, x, COEFF_TO_PTR(*y), prec, rnd);
}
#define ARF_ADD_STACK_ALLOC 40
#define ARF_ADD_TLS_ALLOC 1000
extern TLS_PREFIX mp_ptr __arf_add_tmp;
extern TLS_PREFIX slong __arf_add_alloc;
ARB_DLL extern void _arf_add_tmp_cleanup(void);
#define ARF_ADD_TMP_DECL \
mp_limb_t tmp_stack[ARF_ADD_STACK_ALLOC]; \
#define ARF_ADD_TMP_ALLOC(tmp, alloc) \
if (alloc <= ARF_ADD_STACK_ALLOC) \
{ \
tmp = tmp_stack; \
} \
else if (alloc <= ARF_ADD_TLS_ALLOC) \
{ \
if (__arf_add_alloc < alloc) \
{ \
if (__arf_add_alloc == 0) \
{ \
flint_register_cleanup_function(_arf_add_tmp_cleanup); \
} \
__arf_add_tmp = flint_realloc(__arf_add_tmp, sizeof(mp_limb_t) * alloc); \