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
|
/*
Copyright (C) 2012, 2013 Fredrik Johansson
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/>.
*/
#include "fmpr.h"
#define ADD_STACK_ALLOC 40
#define ADD_TLS_ALLOC 1000
TLS_PREFIX mp_ptr __add_tmp = NULL;
TLS_PREFIX slong __add_alloc = 0;
void _add_tmp_cleanup(void)
{
flint_free(__add_tmp);
__add_tmp = NULL;
__add_alloc = 0;
}
#define ADD_TMP_ALLOC \
if (alloc <= ADD_STACK_ALLOC) \
{ \
tmp = tmp_stack; \
} \
else if (alloc <= ADD_TLS_ALLOC) \
{ \
if (__add_alloc < alloc) \
{ \
if (__add_alloc == 0) \
{ \
flint_register_cleanup_function(_add_tmp_cleanup); \
} \
__add_tmp = flint_realloc(__add_tmp, sizeof(mp_limb_t) * alloc); \
__add_alloc = alloc; \
} \
tmp = __add_tmp; \
} \
else \
{ \
tmp = flint_malloc(sizeof(mp_limb_t) * alloc); \
}
#define ADD_TMP_FREE \
if (alloc > ADD_TLS_ALLOC) \
flint_free(tmp);
/* computes x + y * 2^shift (optionally negated) */
slong
_fmpr_add_mpn(fmpr_t z,
mp_srcptr xman, mp_size_t xn, int xsign, const fmpz_t xexp,
mp_srcptr yman, mp_size_t yn, int ysign, const fmpz_t yexp,
slong shift, slong prec, fmpr_rnd_t rnd)
{
slong tn, zn, alloc, ret, shift_bits, shift_limbs;
int negative;
mp_limb_t tmp_stack[ADD_STACK_ALLOC];
mp_limb_t cy;
mp_ptr tmp, tmp2;
shift_limbs = shift / FLINT_BITS;
shift_bits = shift % FLINT_BITS;
/* x does not overlap with y or the result -- outcome is
equivalent to adding/subtracting a small number to/from y
and rounding */
if (shift > xn * FLINT_BITS &&
prec != FMPR_PREC_EXACT &&
xn * FLINT_BITS + prec - (FLINT_BITS * (yn - 1)) < shift)
{
zn = (prec + FLINT_BITS - 1) / FLINT_BITS;
zn = FLINT_MAX(zn, yn) + 2;
shift_limbs = zn - yn;
alloc = zn;
ADD_TMP_ALLOC
flint_mpn_zero(tmp, shift_limbs);
flint_mpn_copyi(tmp + shift_limbs, yman, yn);
if (xsign == ysign)
{
tmp[0] = 1;
}
else
{
mpn_sub_1(tmp, tmp, zn, 1);
while (tmp[zn-1] == 0)
zn--;
}
ret = _fmpr_set_round_mpn(&shift, fmpr_manref(z), tmp, zn, ysign, prec, rnd);
shift -= shift_limbs * FLINT_BITS;
fmpz_add_si_inline(fmpr_expref(z), yexp, shift);
ADD_TMP_FREE
return ret;
}
if (xsign == ysign)
{
negative = xsign;
/* certainly no overlap between x and y * 2^shift
XXXXX
YYYYY
|-------|
shift_limbs
todo: handle huge case efficiently!
*/
if (shift_limbs >= xn)
{
alloc = shift_limbs + yn + 1;
ADD_TMP_ALLOC
flint_mpn_copyi(tmp, xman, xn);
flint_mpn_zero(tmp + xn, shift_limbs - xn);
if (shift_bits == 0)
{
flint_mpn_copyi(tmp + shift_limbs, yman, yn);
zn = shift_limbs + yn;
}
else
{
cy = mpn_lshift(tmp + shift_limbs, yman, yn, shift_bits);
tmp[shift_limbs + yn] = cy;
zn = shift_limbs + yn + (cy != 0);
}
}
else /* may have overlap between x and y * 2^shift */
{
slong alloc1, alloc2;
alloc2 = yn + 1; /* shifted value of y */
alloc1 = FLINT_MAX(alloc2 + shift_limbs, xn) + 1; /* space for sum */
alloc = alloc1 + alloc2;
ADD_TMP_ALLOC
tmp2 = tmp + alloc1;
flint_mpn_copyi(tmp, xman, shift_limbs);
/* {t, tn} = shift of y (ignoring shift_limbs zero limbs) */
if (shift_bits == 0)
{
flint_mpn_copyi(tmp2, yman, yn);
tn = yn;
}
else
{
cy = mpn_lshift(tmp2, yman, yn, shift_bits);
tmp2[yn] = cy;
tn = yn + (cy != 0);
}
if (tn + shift_limbs == xn)
{
/* XXXXXXXX
TTTTT */
cy = mpn_add_n(tmp + shift_limbs, xman + shift_limbs, tmp2, tn);
tmp[xn] = cy;
zn = xn + (cy != 0);
}
else if (tn + shift_limbs < xn)
{
/* XXXXXXXX
TTTTT */
cy = mpn_add_n(tmp + shift_limbs, xman + shift_limbs, tmp2, tn);
cy = mpn_add_1(tmp + shift_limbs + tn, xman + shift_limbs + tn, xn - tn - shift_limbs, cy);
tmp[xn] = cy;
zn = xn + (cy != 0);
}
else
{
/* XXXXXXXX
TTTTTTT */
cy = mpn_add_n(tmp + shift_limbs, xman + shift_limbs, tmp2, xn - shift_limbs);
cy = mpn_add_1(tmp + xn, tmp2 + xn - shift_limbs, tn + shift_limbs - xn, cy);
tmp[tn + shift_limbs] = cy;
zn = tn + shift_limbs + (cy != 0);
}
}
}
else
{
/* certainly no overlap between x and y * 2^shift
XXXXX
YYYYY
|-------|
shift_limbs
todo: handle huge case efficiently!
*/
if (shift_limbs >= xn)
{
alloc = shift_limbs + yn + 1;
ADD_TMP_ALLOC
mpn_neg_n(tmp, xman, xn);
/* since x is nonzero, there is certainly carry (borrow) */
flint_mpn_store(tmp + xn, shift_limbs - xn, (mp_limb_t) -1);
if (shift_bits == 0)
{
flint_mpn_copyi(tmp + shift_limbs, yman, yn);
zn = shift_limbs + yn;
}
else
{
cy = mpn_lshift(tmp + shift_limbs, yman, yn, shift_bits);
tmp[shift_limbs + yn] = cy;
zn = shift_limbs + yn + (cy != 0);
}
mpn_sub_1(tmp + shift_limbs, tmp + shift_limbs, zn - shift_limbs, 1);
/* y has larger absolute value, and determines the sign */
negative = ysign;
}
else /* may have overlap between x and y * 2^shift */
{
slong alloc1, alloc2;
alloc2 = yn + 1; /* shifted value of y */
alloc1 = FLINT_MAX(alloc2 + shift_limbs, xn); /* space for difference */
alloc = alloc1 + alloc2;
ADD_TMP_ALLOC
tmp2 = tmp + alloc1;
/* {t, tn} = shift of y (ignoring shift_limbs zero limbs) */
if (shift_bits == 0)
{
flint_mpn_copyi(tmp2, yman, yn);
tn = yn;
}
else
{
cy = mpn_lshift(tmp2, yman, yn, shift_bits);
tmp2[yn] = cy;
tn = yn + (cy != 0);
}
if (tn + shift_limbs == xn)
{
int comp;
/* XXXXXXXX
TTTTT */
comp = mpn_cmp(xman + shift_limbs, tmp2, tn);
if (comp == 0)
{
slong i;
for (i = 0; i < shift_limbs; i++)
{
if (xman[i] != 0)
{
comp = 1;
break;
}
}
/* the result is exactly zero */
if (comp == 0)
{
fmpr_zero(z);
ret = FMPR_RESULT_EXACT;
goto cleanup;
}
}
/* x is larger */
if (comp > 0)
{
flint_mpn_copyi(tmp, xman, shift_limbs);
mpn_sub_n(tmp + shift_limbs, xman + shift_limbs, tmp2, tn);
negative = xsign;
}
else
{
mpn_sub_n(tmp + shift_limbs, tmp2, xman + shift_limbs, tn);
if (shift_limbs != 0)
{
cy = mpn_neg_n(tmp, xman, shift_limbs);
mpn_sub_1(tmp + shift_limbs, tmp + shift_limbs, tn, cy);
}
negative = ysign;
}
zn = xn;
}
else if (tn + shift_limbs < xn)
{
/* XXXXXXXX
TTTTT */
flint_mpn_copyi(tmp, xman, shift_limbs);
cy = mpn_sub_n(tmp + shift_limbs, xman + shift_limbs, tmp2, tn);
mpn_sub_1(tmp + shift_limbs + tn, xman + shift_limbs + tn, xn - tn - shift_limbs, cy);
zn = xn;
negative = xsign;
}
else
{
/* XXXXXXXX
TTTTTTT */
cy = mpn_sub_n(tmp + shift_limbs, tmp2, xman + shift_limbs, xn - shift_limbs);
mpn_sub_1(tmp + xn, tmp2 + xn - shift_limbs, tn + shift_limbs - xn, cy);
if (shift_limbs != 0)
{
cy = mpn_neg_n(tmp, xman, shift_limbs);
mpn_sub_1(tmp + shift_limbs, tmp + shift_limbs, tn, cy);
}
zn = shift_limbs + tn;
negative = ysign;
}
}
/* there might have been cancellation */
while (tmp[zn-1] == 0)
zn--;
}
ret = _fmpr_set_round_mpn(&shift, fmpr_manref(z), tmp, zn, negative, prec, rnd);
fmpz_add_si_inline(fmpr_expref(z), xexp, shift);
cleanup:
ADD_TMP_FREE
return ret;
}
|