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
/*
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))
/*
Compute wn-limb fixed-point number w, a number of ulps error, and
an exponent q such that x = (w + xi * error) - q * log(2)
where 0 <= w < log(2) and |xi| <= 1.
Returns 0 if unsuccessful (high enough precision for log(2) to be available)
and 1 if successful.
Assumes nonspecial x; its exponent must not be an mpz.
Error analysis:
We first set
n = |x| - e1
d = log(2) - e2
where 0 <= e1, e2 <= 1 ulp are the errors resulting from truncating
to fixed-point numbers (note that the value of log(2) is correctly rounded).
Next, we compute q, r such that 0 <= r < d and q*d + r = n (this
is just integer division with remainder, done exactly).
The real number we want to approximate is r_exact = |x| - q * log(2).
The approximation r satisfies
|r_exact - r| = |(|x| - q * log(2)) - (n - q * d)|
= ||x| - q * log(2) - (|x| - e1) + q * (log(2) - e2)|
= |e1 - q * e2|
<= (q + 1) ulp.
We select the working precision so that (q + 1) ulp is at most
1 ulp in the target precision.
It is sufficient to use tn extra limbs where (q + 1) <= 2^(FLINT_BITS * tn).
Note that q + 1 <= n / d + 1 <= |x| * 1.5 + 1 < 2^(exp+2). So it is
sufficient to choose tn = ceil((exp+2)/FLINT_BITS).
Now we round the result to the final precision: w = r - e3.
This can add 1 more ulp of error, so the error may be 2 ulp.
Finally, if x < 0, we correct the sign by setting,x = log(2) - x,
q = -(q+1). This adds 1 more ulp (from the approximation of log(2)),
for a total of 3 ulp.
*/
int
_arb_get_mpn_fixed_mod_log2(nn_ptr w, fmpz_t q, ulong * error,
const arf_t x, slong wn)
{
nn_srcptr xp;
slong xn;
int negative;
slong exp;
ARF_GET_MPN_READONLY(xp, xn, x);
exp = ARF_EXP(x);
negative = ARF_SGNBIT(x);
if (exp <= -1)
{
/* todo: just zero top */
flint_mpn_zero(w, wn);
*error = _arf_get_integer_mpn(w, xp, xn, exp + wn * FLINT_BITS);
if (!negative)
{
fmpz_zero(q);
}
else
{
if (wn > ARB_LOG_TAB2_LIMBS)
return 0;
mpn_sub_n(w, arb_log_log2_tab + ARB_LOG_TAB2_LIMBS - wn, w, wn);
*error += 1; /* log(2) has 1 ulp error */
fmpz_set_si(q, -1);
}
return 1; /* success */
}
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_LOG_TAB2_LIMBS)
return 0;
TMP_START;
alloc = qn + rn + nn;
qp = TMP_ALLOC_LIMBS(alloc);
rp = qp + qn;
np = rp + rn;
dp = arb_log_log2_tab + ARB_LOG_TAB2_LIMBS - dn;
/* todo: prove that zeroing is unnecessary */
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);
if (!negative)
{
flint_mpn_copyi(w, rp + tn, wn);
*error = 2;
}
else
{
if (mpn_add_1(qp, qp, qn, 1))
{
/* I believe this cannot happen (should prove it) */
flint_throw(FLINT_ERROR, "mod log(2): unexpected carry\n");
}
mpn_sub_n(w, dp + tn, rp + tn, wn);
*error = 3;
}
/* read the exponent */
while (qn > 1 && qp[qn-1] == 0)
qn--;
if (qn == 1)
{
if (!negative)
fmpz_set_ui(q, qp[0]);
else
fmpz_neg_ui(q, qp[0]);
}
else
{
fmpz_set_mpn_large(q, qp, qn, negative);
}
TMP_END;
return 1;
}
}