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
/*
Copyright (C) 2014 Fredrik Johansson
This file is part of FLINT.
FLINT 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 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "mpn_extras.h"
#include "arb.h"
#define TMP_ALLOC_LIMBS(__n) TMP_ALLOC((__n) * sizeof(ulong))
int
_arb_get_mpn_fixed_mod_pi4(nn_ptr w, fmpz_t q, int * octant,
ulong * error, const arf_t x, slong wn)
{
nn_srcptr xp;
slong xn;
slong exp;
ARF_GET_MPN_READONLY(xp, xn, x);
exp = ARF_EXP(x);
if (exp <= -1)
{
flint_mpn_zero(w, wn);
*error = _arf_get_integer_mpn(w, xp, xn, exp + wn * FLINT_BITS);
*octant = 0;
if (q != NULL)
fmpz_zero(q);
return 1;
}
else if (exp == 0)
{
nn_srcptr dp;
if (wn > ARB_PI4_TAB_LIMBS)
return 0;
flint_mpn_zero(w, wn);
*error = _arf_get_integer_mpn(w, xp, xn, exp + wn * FLINT_BITS);
dp = arb_pi4_tab + ARB_PI4_TAB_LIMBS - wn;
if (mpn_cmp(w, dp, wn) < 0)
{
*octant = 0;
if (q != NULL)
fmpz_zero(q);
}
else
{
*octant = 1;
if (q != NULL)
fmpz_one(q);
mpn_sub_n(w, w, dp, wn);
mpn_sub_n(w, dp, w, wn);
*error += 2;
}
return 1;
}
else
{
nn_ptr qp, rp, np;
nn_srcptr dp;
slong qn, rn, nn, dn, tn, alloc;
TMP_INIT;
tn = ((exp + 2) + FLINT_BITS - 1) / FLINT_BITS;
dn = wn + tn; /* denominator */
nn = wn + 2 * tn; /* numerator */
qn = nn - dn + 1; /* quotient */
rn = dn; /* remainder */
if (dn > ARB_PI4_TAB_LIMBS)
return 0;
TMP_START;
alloc = qn + rn + nn;
qp = TMP_ALLOC_LIMBS(alloc);
rp = qp + qn;
np = rp + rn;
dp = arb_pi4_tab + ARB_PI4_TAB_LIMBS - dn;
flint_mpn_zero(np, nn);
_arf_get_integer_mpn(np, xp, xn, exp + dn * FLINT_BITS);
mpn_tdiv_qr(qp, rp, 0, np, nn, dp, dn);
*octant = qp[0] % 8;
if (*octant % 2 == 0)
{
flint_mpn_copyi(w, rp + tn, wn);
*error = 2;
}
else
{
mpn_sub_n(w, dp + tn, rp + tn, wn);
*error = 3;
}
if (q != NULL)
{
/* read the exponent */
while (qn > 1 && qp[qn-1] == 0)
qn--;
if (qn == 1)
fmpz_set_ui(q, qp[0]);
else
fmpz_set_mpn_large(q, qp, qn, 0);
}
TMP_END;
return 1;
}
}