1 /* mpf_div -- Divide two floats.
3 Copyright 1993, 1994, 1996, 2000, 2001, 2002, 2004, 2005 Free Software
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 3 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. If not, see http://www.gnu.org/licenses/. */
21 #include <stdio.h> /* for NULL */
29 No attempt is made to identify an overlap u==v. The result will be
30 correct (1.0), but a full actual division is done whereas of course
31 x/x==1 needs no work. Such a call is not a sensible thing to make, and
32 it's left to an application to notice and optimize if it might arise
33 somehow through pointer aliasing or whatever.
37 The high quotient limb is non-zero when high{up,vsize} >= {vp,vsize}. We
38 could make that comparison and use qsize==prec instead of qsize==prec+1,
39 to save one limb in the division.
41 If r==u but the size is enough bigger than prec that there won't be an
42 overlap between quotient and dividend in mpn_tdiv_qr, then we can avoid
43 copying up,usize. This would only arise from a prec reduced with
44 mpf_set_prec_raw and will be pretty unusual, but might be worthwhile if
45 it could be worked into the copy_u decision cleanly.
49 If/when mpn_tdiv_qr supports its qxn parameter we can use that instead of
50 padding u with zeros in temporary space.
52 If/when a quotient-only division exists it can be used here immediately.
53 remp is only to satisfy mpn_tdiv_qr, the remainder is not used. */
56 mpf_div (mpf_ptr r, mpf_srcptr u, mpf_srcptr v)
59 mp_ptr rp, remp, tp, new_vp;
60 mp_size_t usize, vsize, rsize, prospective_rsize, tsize, zeros, copy_v_size;
61 mp_size_t sign_quotient, prec, high_zero, chop;
68 sign_quotient = usize ^ vsize;
84 rexp = u->_mp_exp - v->_mp_exp + 1;
90 prospective_rsize = usize - vsize + 1; /* quot from using given u,v sizes */
91 rsize = prec + 1; /* desired quot */
93 zeros = rsize - prospective_rsize; /* padding u to give rsize */
94 copy_u = (zeros > 0 || rp == up); /* copy u if overlap or padding */
96 chop = MAX (-zeros, 0); /* negative zeros means shorten u */
99 zeros += chop; /* now zeros >= 0 */
101 tsize = usize + zeros; /* size for possible copy of u */
105 /* separate blocks, for malloc debugging */
106 remp = TMP_ALLOC_LIMBS (vsize);
107 tp = (copy_u ? TMP_ALLOC_LIMBS (tsize) : NULL);
108 new_vp = (rp == vp ? TMP_ALLOC_LIMBS (vsize) : NULL);
112 /* one block with conditionalized size, for efficiency */
113 copy_v_size = (rp == vp ? vsize : 0);
114 remp = TMP_ALLOC_LIMBS (vsize + copy_v_size + (copy_u ? tsize : 0));
115 new_vp = remp + vsize;
116 tp = new_vp + copy_v_size;
119 /* copy and possibly extend u if necessary */
122 MPN_ZERO (tp, zeros);
123 MPN_COPY (tp+zeros, up, usize);
128 /* ensure divisor doesn't overlap quotient */
131 MPN_COPY (new_vp, vp, vsize);
135 ASSERT (usize-vsize+1 == rsize);
136 mpn_tdiv_qr (rp, remp, (mp_size_t) 0, up, usize, vp, vsize);
138 /* strip possible zero high limb */
139 high_zero = (rp[rsize-1] == 0);
143 r->_mp_size = sign_quotient >= 0 ? rsize : -rsize;