1 /* mpf_sub -- Subtract two floats.
3 Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Library General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15 License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
27 mpf_sub (mpf_ptr r, mpf_srcptr u, mpf_srcptr v)
37 mp_size_t usize, vsize, rsize;
47 /* Handle special cases that don't work in generic code below. */
59 /* If signs of U and V are different, perform addition. */
60 if ((usize ^ vsize) < 0)
62 __mpf_struct v_negated;
63 v_negated._mp_size = -vsize;
64 v_negated._mp_exp = v->_mp_exp;
65 v_negated._mp_d = v->_mp_d;
66 mpf_add (r, u, &v_negated);
72 /* Signs are now known to be the same. */
75 /* Make U be the operand with the largest exponent. */
76 if (u->_mp_exp < v->_mp_exp)
90 prec = r->_mp_prec + 1;
92 ediff = u->_mp_exp - v->_mp_exp;
94 /* If ediff is 0 or 1, we might have a situation where the operands are
95 extremely close. We need to scan the operands from the most significant
96 end ignore the initial parts that are equal. */
101 /* Skip leading limbs in U and V that are equal. */
102 if (up[usize - 1] == vp[vsize - 1])
104 /* This loop normally exits immediately. Optimize for that. */
125 while (up[usize - 1] == vp[vsize - 1]);
128 if (up[usize - 1] < vp[vsize - 1])
130 /* For simplicity, swap U and V. Note that since the loop above
131 wouldn't have exited unless up[usize - 1] and vp[vsize - 1]
132 were non-equal, this if-statement catches all cases where U
133 is smaller than V. */
134 { mp_srcptr tp = up; up = vp; vp = tp; }
135 { mp_size_t tsize = usize; usize = vsize; vsize = tsize; }
137 /* negating ediff not necessary since it is 0. */
143 if (up[usize - 1] != vp[vsize - 1] + 1)
149 else /* ediff == 1 */
155 if (up[usize - 1] != 1 || vp[vsize - 1] != ~(mp_limb_t) 0
156 || (usize >= 2 && up[usize - 2] != 0))
163 /* Skip sequences of 00000000/ffffffff */
164 while (vsize != 0 && usize != 0 && up[usize - 1] == 0
165 && vp[vsize - 1] == ~(mp_limb_t) 0)
174 while (vsize != 0 && vp[vsize - 1] == ~(mp_limb_t) 0)
181 if (usize > prec - 1)
183 up += usize - (prec - 1);
186 if (vsize > prec - 1)
188 vp += vsize - (prec - 1);
192 tp = (mp_ptr) TMP_ALLOC (prec * BYTES_PER_MP_LIMB);
199 for (i = 0; i < size; i++)
210 for (i = 0; i < size; i++)
212 cy_limb = 1 - mpn_add_1 (tp, tp, vsize, (mp_limb_t) 1);
227 size = usize - vsize;
228 MPN_COPY (tp, up, size);
229 cy_limb = mpn_sub_n (tp + size, up + size, vp, vsize);
232 else /* (usize < vsize) */
237 size = vsize - usize;
238 for (i = 0; i < size; i++)
240 cy_limb = mpn_sub_n (tp + size, up, vp + size, usize);
241 cy_limb+= mpn_sub_1 (tp + size, tp + size, usize, (mp_limb_t) 1);
242 cy_limb-= mpn_add_1 (tp, tp, vsize, (mp_limb_t) 1);
256 /* If U extends beyond PREC, ignore the part that does. */
263 /* If V extends beyond PREC, ignore the part that does.
264 Note that this may make vsize negative. */
265 if (vsize + ediff > prec)
267 vp += vsize + ediff - prec;
268 vsize = prec - ediff;
271 /* Allocate temp space for the result. Allocate
272 just vsize + ediff later??? */
273 tp = (mp_ptr) TMP_ALLOC (prec * BYTES_PER_MP_LIMB);
277 /* V completely cancelled. */
279 MPN_COPY (rp, up, usize);
284 /* Locate the least significant non-zero limb in (the needed
285 parts of) U and V, to simplify the code below. */
290 MPN_COPY (rp, up, usize);
302 MPN_COPY (rp, vp, vsize);
312 /* uuuu | uuuu | uuuu | uuuu | uuuu */
313 /* vvvvvvv | vv | vvvvv | v | vv */
317 /* U and V partially overlaps. */
320 /* Have to compare the leading limbs of u and v
321 to determine whether to compute u - v or v - u. */
327 size = usize - vsize;
328 MPN_COPY (tp, up, size);
329 mpn_sub_n (tp + size, up + size, vp, vsize);
332 else /* (usize < vsize) */
337 size = vsize - usize;
339 for (i = 1; i < size; i++)
341 mpn_sub_n (tp + size, up, vp + size, usize);
342 mpn_sub_1 (tp + size, tp + size, usize, (mp_limb_t) 1);
348 if (vsize + ediff <= usize)
353 size = usize - ediff - vsize;
354 MPN_COPY (tp, up, size);
355 mpn_sub (tp + size, up + size, usize - size, vp, vsize);
363 size = vsize + ediff - usize;
365 for (i = 1; i < size; i++)
367 mpn_sub (tp + size, up, usize, vp + size, usize - ediff);
368 mpn_sub_1 (tp + size, tp + size, usize, (mp_limb_t) 1);
369 rsize = vsize + ediff;
378 size = vsize + ediff - usize;
380 for (i = 1; i < vsize; i++)
382 for (i = vsize; i < size; i++)
383 tp[i] = ~(mp_limb_t) 0;
384 mpn_sub_1 (tp + size, up, usize, (mp_limb_t) 1);
385 rsize = size + usize;
389 /* Full normalize. Optimize later. */
390 while (rsize != 0 && tp[rsize - 1] == 0)
395 MPN_COPY (rp, tp, rsize);
399 r->_mp_size = negate ? -rsize : rsize;