1
/* mpf_sub -- Subtract two floats.
3
Copyright 1993, 1994, 1995, 1996, 1999, 2000, 2001 Free Software Foundation,
6
This file is part of the GNU MP Library.
8
The GNU MP Library is free software; you can redistribute it and/or modify
9
it under the terms of the GNU Lesser General Public License as published by
10
the Free Software Foundation; either version 2.1 of the License, or (at your
11
option) any later version.
13
The GNU MP Library is distributed in the hope that it will be useful, but
14
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16
License for more details.
18
You should have received a copy of the GNU Lesser General Public License
19
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21
MA 02111-1307, USA. */
27
mpf_sub (mpf_ptr r, mpf_srcptr u, mpf_srcptr v)
31
mp_size_t usize, vsize, rsize;
41
/* Handle special cases that don't work in generic code below. */
54
/* If signs of U and V are different, perform addition. */
55
if ((usize ^ vsize) < 0)
57
__mpf_struct v_negated;
58
v_negated._mp_size = -vsize;
59
v_negated._mp_exp = v->_mp_exp;
60
v_negated._mp_d = v->_mp_d;
61
mpf_add (r, u, &v_negated);
67
/* Signs are now known to be the same. */
70
/* Make U be the operand with the largest exponent. */
71
if (u->_mp_exp < v->_mp_exp)
85
prec = r->_mp_prec + 1;
87
ediff = u->_mp_exp - v->_mp_exp;
89
/* If ediff is 0 or 1, we might have a situation where the operands are
90
extremely close. We need to scan the operands from the most significant
91
end ignore the initial parts that are equal. */
96
/* Skip leading limbs in U and V that are equal. */
97
if (up[usize - 1] == vp[vsize - 1])
99
/* This loop normally exits immediately. Optimize for that. */
130
while (up[usize - 1] == vp[vsize - 1]);
133
if (up[usize - 1] < vp[vsize - 1])
135
/* For simplicity, swap U and V. Note that since the loop above
136
wouldn't have exited unless up[usize - 1] and vp[vsize - 1]
137
were non-equal, this if-statement catches all cases where U
138
is smaller than V. */
139
MPN_SRCPTR_SWAP (up,usize, vp,vsize);
141
/* negating ediff not necessary since it is 0. */
147
if (up[usize - 1] != vp[vsize - 1] + 1)
153
else /* ediff == 1 */
159
if (up[usize - 1] != 1 || vp[vsize - 1] != ~(mp_limb_t) 0
160
|| (usize >= 2 && up[usize - 2] != 0))
167
/* Skip sequences of 00000000/ffffffff */
168
while (vsize != 0 && usize != 0 && up[usize - 1] == 0
169
&& vp[vsize - 1] == ~(mp_limb_t) 0)
178
while (vsize != 0 && vp[vsize - 1] == ~(mp_limb_t) 0)
185
if (usize > prec - 1)
187
up += usize - (prec - 1);
190
if (vsize > prec - 1)
192
vp += vsize - (prec - 1);
196
tp = (mp_ptr) TMP_ALLOC (prec * BYTES_PER_MP_LIMB);
203
for (i = 0; i < size; i++)
214
for (i = 0; i < size; i++)
216
cy_limb = 1 - mpn_add_1 (tp, tp, vsize, (mp_limb_t) 1);
231
size = usize - vsize;
232
MPN_COPY (tp, up, size);
233
cy_limb = mpn_sub_n (tp + size, up + size, vp, vsize);
236
else /* (usize < vsize) */
241
size = vsize - usize;
242
for (i = 0; i < size; i++)
244
cy_limb = mpn_sub_n (tp + size, up, vp + size, usize);
245
cy_limb+= mpn_sub_1 (tp + size, tp + size, usize, (mp_limb_t) 1);
246
cy_limb-= mpn_add_1 (tp, tp, vsize, (mp_limb_t) 1);
260
/* If U extends beyond PREC, ignore the part that does. */
267
/* If V extends beyond PREC, ignore the part that does.
268
Note that this may make vsize negative. */
269
if (vsize + ediff > prec)
271
vp += vsize + ediff - prec;
272
vsize = prec - ediff;
275
/* Allocate temp space for the result. Allocate
276
just vsize + ediff later??? */
277
tp = (mp_ptr) TMP_ALLOC (prec * BYTES_PER_MP_LIMB);
281
/* V completely cancelled. */
283
MPN_COPY (rp, up, usize);
288
/* Locate the least significant non-zero limb in (the needed
289
parts of) U and V, to simplify the code below. */
294
MPN_COPY (rp, up, usize);
306
MPN_COPY (rp, vp, vsize);
316
/* uuuu | uuuu | uuuu | uuuu | uuuu */
317
/* vvvvvvv | vv | vvvvv | v | vv */
321
/* U and V partially overlaps. */
324
/* Have to compare the leading limbs of u and v
325
to determine whether to compute u - v or v - u. */
331
size = usize - vsize;
332
MPN_COPY (tp, up, size);
333
mpn_sub_n (tp + size, up + size, vp, vsize);
336
else /* (usize < vsize) */
341
size = vsize - usize;
343
for (i = 1; i < size; i++)
345
mpn_sub_n (tp + size, up, vp + size, usize);
346
mpn_sub_1 (tp + size, tp + size, usize, (mp_limb_t) 1);
352
if (vsize + ediff <= usize)
357
size = usize - ediff - vsize;
358
MPN_COPY (tp, up, size);
359
mpn_sub (tp + size, up + size, usize - size, vp, vsize);
367
size = vsize + ediff - usize;
369
for (i = 1; i < size; i++)
371
mpn_sub (tp + size, up, usize, vp + size, usize - ediff);
372
mpn_sub_1 (tp + size, tp + size, usize, (mp_limb_t) 1);
373
rsize = vsize + ediff;
382
size = vsize + ediff - usize;
384
for (i = 1; i < vsize; i++)
386
for (i = vsize; i < size; i++)
387
tp[i] = ~(mp_limb_t) 0;
388
mpn_sub_1 (tp + size, up, usize, (mp_limb_t) 1);
389
rsize = size + usize;
393
/* Full normalize. Optimize later. */
394
while (rsize != 0 && tp[rsize - 1] == 0)
399
MPN_COPY (rp, tp, rsize);
403
r->_mp_size = negate ? -rsize : rsize;