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
|
/* Reference floating point routines.
Copyright (C) 1996 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
The GNU MP Library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
#include "gmp.h"
#include "gmp-impl.h"
#if __STDC__
void ref_mpf_add (mpf_t, const mpf_t, const mpf_t);
void ref_mpf_sub (mpf_t, const mpf_t, const mpf_t);
#else
void ref_mpf_add ();
void ref_mpf_sub ();
#endif
void
#if __STDC__
ref_mpf_add (mpf_t w, const mpf_t u, const mpf_t v)
#else
ref_mpf_add (w, u, v)
mpf_t w;
const mpf_t u;
const mpf_t v;
#endif
{
mp_size_t hi, lo, size;
mp_ptr ut, vt, wt;
int neg;
mp_exp_t exp;
mp_limb_t cy;
TMP_DECL (mark);
TMP_MARK (mark);
if (SIZ (u) == 0)
{
size = ABSIZ (v);
wt = (mp_ptr) TMP_ALLOC (size * BYTES_PER_MP_LIMB);
MPN_COPY (wt, PTR (v), size);
exp = EXP (v);
neg = SIZ (v) < 0;
goto done;
}
if (SIZ (v) == 0)
{
size = ABSIZ (u);
wt = (mp_ptr) TMP_ALLOC (size * BYTES_PER_MP_LIMB);
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);
ref_mpf_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 = (mp_ptr) TMP_ALLOC ((size + 1) * BYTES_PER_MP_LIMB);
vt = (mp_ptr) TMP_ALLOC ((size + 1) * BYTES_PER_MP_LIMB);
wt = (mp_ptr) TMP_ALLOC ((size + 1) * BYTES_PER_MP_LIMB);
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 (mark);
}
void
#if __STDC__
ref_mpf_sub (mpf_t w, const mpf_t u, const mpf_t v)
#else
ref_mpf_sub (w, u, v)
mpf_t w;
const mpf_t u;
const mpf_t v;
#endif
{
mp_size_t hi, lo, size;
mp_ptr ut, vt, wt;
int neg;
mp_exp_t exp;
TMP_DECL (mark);
TMP_MARK (mark);
if (SIZ (u) == 0)
{
size = ABSIZ (v);
wt = (mp_ptr) TMP_ALLOC (size * BYTES_PER_MP_LIMB);
MPN_COPY (wt, PTR (v), size);
exp = EXP (v);
neg = SIZ (v) > 0;
goto done;
}
if (SIZ (v) == 0)
{
size = ABSIZ (u);
wt = (mp_ptr) TMP_ALLOC (size * BYTES_PER_MP_LIMB);
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);
ref_mpf_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 = (mp_ptr) TMP_ALLOC ((size + 1) * BYTES_PER_MP_LIMB);
vt = (mp_ptr) TMP_ALLOC ((size + 1) * BYTES_PER_MP_LIMB);
wt = (mp_ptr) TMP_ALLOC ((size + 1) * BYTES_PER_MP_LIMB);
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 (mark);
}
|