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
|
/* Reference floating point routines.
Copyright 1996, 2001, 2004, 2005 Free Software Foundation, Inc.
This file is part of the GNU MP Library test suite.
The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
The GNU MP Library test suite is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
#include <stdio.h>
#include <stdlib.h>
#include "gmp-impl.h"
#include "tests.h"
void
refmpf_add (mpf_ptr w, mpf_srcptr u, mpf_srcptr v)
{
mp_size_t hi, lo, size;
mp_ptr ut, vt, wt;
int neg;
mp_exp_t exp;
mp_limb_t cy;
TMP_DECL;
TMP_MARK;
if (SIZ (u) == 0)
{
size = ABSIZ (v);
wt = TMP_ALLOC_LIMBS (size + 1);
MPN_COPY (wt, PTR (v), size);
exp = EXP (v);
neg = SIZ (v) < 0;
goto done;
}
if (SIZ (v) == 0)
{
size = ABSIZ (u);
wt = TMP_ALLOC_LIMBS (size + 1);
MPN_COPY (wt, PTR (u), size);
exp = EXP (u);
neg = SIZ (u) < 0;
goto done;
}
if ((SIZ (u) ^ SIZ (v)) < 0)
{
mpf_t tmp;
SIZ (tmp) = -SIZ (v);
EXP (tmp) = EXP (v);
PTR (tmp) = PTR (v);
refmpf_sub (w, u, tmp);
return;
}
neg = SIZ (u) < 0;
/* Compute the significance of the hi and lo end of the result. */
hi = MAX (EXP (u), EXP (v));
lo = MIN (EXP (u) - ABSIZ (u), EXP (v) - ABSIZ (v));
size = hi - lo;
ut = TMP_ALLOC_LIMBS (size + 1);
vt = TMP_ALLOC_LIMBS (size + 1);
wt = TMP_ALLOC_LIMBS (size + 1);
MPN_ZERO (ut, size);
MPN_ZERO (vt, size);
{int off;
off = size + (EXP (u) - hi) - ABSIZ (u);
MPN_COPY (ut + off, PTR (u), ABSIZ (u));
off = size + (EXP (v) - hi) - ABSIZ (v);
MPN_COPY (vt + off, PTR (v), ABSIZ (v));
}
cy = mpn_add_n (wt, ut, vt, size);
wt[size] = cy;
size += cy;
exp = hi + cy;
done:
if (size > PREC (w))
{
wt += size - PREC (w);
size = PREC (w);
}
MPN_COPY (PTR (w), wt, size);
SIZ (w) = neg == 0 ? size : -size;
EXP (w) = exp;
TMP_FREE;
}
/* Add 1 "unit in last place" (ie. in the least significant limb) to f.
f cannot be zero, since that has no well-defined "last place".
This routine is designed for use in cases where we pay close attention to
the size of the data value and are using that (and the exponent) to
indicate the accurate part of a result, or similar. For this reason, if
there's a carry out we don't store 1 and adjust the exponent, we just
leave 100..00. We don't even adjust if there's a carry out of prec+1
limbs, but instead give up in that case (which we intend shouldn't arise
in normal circumstances). */
void
refmpf_add_ulp (mpf_ptr f)
{
mp_ptr fp = PTR(f);
mp_size_t fsize = SIZ(f);
mp_size_t abs_fsize = ABSIZ(f);
mp_limb_t c;
if (fsize == 0)
{
printf ("Oops, refmpf_add_ulp called with f==0\n");
abort ();
}
c = refmpn_add_1 (fp, fp, abs_fsize, CNST_LIMB(1));
if (c != 0)
{
if (abs_fsize >= PREC(f) + 1)
{
printf ("Oops, refmpf_add_ulp carried out of prec+1 limbs\n");
abort ();
}
fp[abs_fsize] = c;
abs_fsize++;
SIZ(f) = (fsize > 0 ? abs_fsize : - abs_fsize);
EXP(f)++;
}
}
/* Fill f with size limbs of the given value, setup as an integer. */
void
refmpf_fill (mpf_ptr f, mp_size_t size, mp_limb_t value)
{
ASSERT (size >= 0);
size = MIN (PREC(f) + 1, size);
SIZ(f) = size;
EXP(f) = size;
refmpn_fill (PTR(f), size, value);
}
/* Strip high zero limbs from the f data, adjusting exponent accordingly. */
void
refmpf_normalize (mpf_ptr f)
{
while (SIZ(f) != 0 && PTR(f)[ABSIZ(f)-1] == 0)
{
SIZ(f) = (SIZ(f) >= 0 ? SIZ(f)-1 : SIZ(f)+1);
EXP(f) --;
}
if (SIZ(f) == 0)
EXP(f) = 0;
}
/* refmpf_set_overlap sets up dst as a copy of src, but with PREC(dst)
unchanged, in preparation for an overlap test.
The full value of src is copied, and the space at PTR(dst) is extended as
necessary. The way PREC(dst) is unchanged is as per an mpf_set_prec_raw.
The return value is the new PTR(dst) space precision, in bits, ready for
a restoring mpf_set_prec_raw before mpf_clear. */
unsigned long
refmpf_set_overlap (mpf_ptr dst, mpf_srcptr src)
{
mp_size_t dprec = PREC(dst);
mp_size_t ssize = ABSIZ(src);
unsigned long ret;
refmpf_set_prec_limbs (dst, (unsigned long) MAX (dprec, ssize));
mpf_set (dst, src);
ret = mpf_get_prec (dst);
PREC(dst) = dprec;
return ret;
}
/* Like mpf_set_prec, but taking a precision in limbs.
PREC(f) ends up as the given "prec" value. */
void
refmpf_set_prec_limbs (mpf_ptr f, unsigned long prec)
{
mpf_set_prec (f, __GMPF_PREC_TO_BITS (prec));
}
void
refmpf_sub (mpf_ptr w, mpf_srcptr u, mpf_srcptr v)
{
mp_size_t hi, lo, size;
mp_ptr ut, vt, wt;
int neg;
mp_exp_t exp;
TMP_DECL;
TMP_MARK;
if (SIZ (u) == 0)
{
size = ABSIZ (v);
wt = TMP_ALLOC_LIMBS (size + 1);
MPN_COPY (wt, PTR (v), size);
exp = EXP (v);
neg = SIZ (v) > 0;
goto done;
}
if (SIZ (v) == 0)
{
size = ABSIZ (u);
wt = TMP_ALLOC_LIMBS (size + 1);
MPN_COPY (wt, PTR (u), size);
exp = EXP (u);
neg = SIZ (u) < 0;
goto done;
}
if ((SIZ (u) ^ SIZ (v)) < 0)
{
mpf_t tmp;
SIZ (tmp) = -SIZ (v);
EXP (tmp) = EXP (v);
PTR (tmp) = PTR (v);
refmpf_add (w, u, tmp);
if (SIZ (u) < 0)
mpf_neg (w, w);
return;
}
neg = SIZ (u) < 0;
/* Compute the significance of the hi and lo end of the result. */
hi = MAX (EXP (u), EXP (v));
lo = MIN (EXP (u) - ABSIZ (u), EXP (v) - ABSIZ (v));
size = hi - lo;
ut = TMP_ALLOC_LIMBS (size + 1);
vt = TMP_ALLOC_LIMBS (size + 1);
wt = TMP_ALLOC_LIMBS (size + 1);
MPN_ZERO (ut, size);
MPN_ZERO (vt, size);
{int off;
off = size + (EXP (u) - hi) - ABSIZ (u);
MPN_COPY (ut + off, PTR (u), ABSIZ (u));
off = size + (EXP (v) - hi) - ABSIZ (v);
MPN_COPY (vt + off, PTR (v), ABSIZ (v));
}
if (mpn_cmp (ut, vt, size) >= 0)
mpn_sub_n (wt, ut, vt, size);
else
{
mpn_sub_n (wt, vt, ut, size);
neg ^= 1;
}
exp = hi;
while (size != 0 && wt[size - 1] == 0)
{
size--;
exp--;
}
done:
if (size > PREC (w))
{
wt += size - PREC (w);
size = PREC (w);
}
MPN_COPY (PTR (w), wt, size);
SIZ (w) = neg == 0 ? size : -size;
EXP (w) = exp;
TMP_FREE;
}
/* Validate got by comparing to want. Return 1 if good, 0 if bad.
The data in got is compared to that in want, up to either PREC(got) limbs
or the size of got, whichever is bigger. Clearly we always demand
PREC(got) of accuracy, but we go further and say that if got is bigger
then any extra must be correct too.
want needs to have enough data to allow this comparison. The size in
want doesn't have to be that big though, if it's smaller then further low
limbs are taken to be zero.
This validation approach is designed to allow some flexibility in exactly
how much data is generated by an mpf function, ie. either prec or prec+1
limbs. We don't try to make a reference function that emulates that same
size decision, instead the idea is for a validation function to generate
at least as much data as the real function, then compare. */
int
refmpf_validate (const char *name, mpf_srcptr got, mpf_srcptr want)
{
int bad = 0;
mp_size_t gsize, wsize, cmpsize, i;
mp_srcptr gp, wp;
mp_limb_t glimb, wlimb;
MPF_CHECK_FORMAT (got);
if (EXP (got) != EXP (want))
{
printf ("%s: wrong exponent\n", name);
bad = 1;
}
gsize = SIZ (got);
wsize = SIZ (want);
if ((gsize < 0 && wsize > 0) || (gsize > 0 && wsize < 0))
{
printf ("%s: wrong sign\n", name);
bad = 1;
}
gsize = ABS (gsize);
wsize = ABS (wsize);
/* most significant limb of respective data */
gp = PTR (got) + gsize - 1;
wp = PTR (want) + wsize - 1;
/* compare limb data */
cmpsize = MAX (PREC (got), gsize);
for (i = 0; i < cmpsize; i++)
{
glimb = (i < gsize ? gp[-i] : 0);
wlimb = (i < wsize ? wp[-i] : 0);
if (glimb != wlimb)
{
printf ("%s: wrong data starting at index %ld from top\n",
name, (long) i);
bad = 1;
break;
}
}
if (bad)
{
printf (" prec %d\n", PREC(got));
printf (" exp got %ld\n", (long) EXP(got));
printf (" exp want %ld\n", (long) EXP(want));
printf (" size got %d\n", SIZ(got));
printf (" size want %d\n", SIZ(want));
printf (" limbs (high to low)\n");
printf (" got ");
for (i = ABSIZ(got)-1; i >= 0; i--)
{
gmp_printf ("%MX", PTR(got)[i]);
if (i != 0)
printf (",");
}
printf ("\n");
printf (" want ");
for (i = ABSIZ(want)-1; i >= 0; i--)
{
gmp_printf ("%MX", PTR(want)[i]);
if (i != 0)
printf (",");
}
printf ("\n");
return 0;
}
return 1;
}
int
refmpf_validate_division (const char *name, mpf_srcptr got,
mpf_srcptr n, mpf_srcptr d)
{
mp_size_t nsize, dsize, sign, prec, qsize, tsize;
mp_srcptr np, dp;
mp_ptr tp, qp, rp;
mpf_t want;
int ret;
nsize = SIZ (n);
dsize = SIZ (d);
ASSERT_ALWAYS (dsize != 0);
sign = nsize ^ dsize;
nsize = ABS (nsize);
dsize = ABS (dsize);
np = PTR (n);
dp = PTR (d);
prec = PREC (got);
EXP (want) = EXP (n) - EXP (d) + 1;
qsize = prec + 2; /* at least prec+1 limbs, after high zero */
tsize = qsize + dsize - 1; /* dividend size to give desired qsize */
/* dividend n, extended or truncated */
tp = refmpn_malloc_limbs (tsize);
refmpn_copy_extend (tp, tsize, np, nsize);
qp = refmpn_malloc_limbs (qsize);
rp = refmpn_malloc_limbs (dsize); /* remainder, unused */
ASSERT_ALWAYS (qsize == tsize - dsize + 1);
refmpn_tdiv_qr (qp, rp, (mp_size_t) 0, tp, tsize, dp, dsize);
PTR (want) = qp;
SIZ (want) = (sign >= 0 ? qsize : -qsize);
refmpf_normalize (want);
ret = refmpf_validate (name, got, want);
free (tp);
free (qp);
free (rp);
return ret;
}
|