xref: /dragonfly/contrib/gmp/mpf/div.c (revision 86d7f5d3)
1 /* mpf_div -- Divide two floats.
2 
3 Copyright 1993, 1994, 1996, 2000, 2001, 2002, 2004, 2005, 2010 Free Software
4 Foundation, Inc.
5 
6 This file is part of the GNU MP Library.
7 
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.
12 
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.
17 
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/.  */
20 
21 #include "gmp.h"
22 #include "gmp-impl.h"
23 
24 
25 /* Not done:
26 
27    No attempt is made to identify an overlap u==v.  The result will be
28    correct (1.0), but a full actual division is done whereas of course
29    x/x==1 needs no work.  Such a call is not a sensible thing to make, and
30    it's left to an application to notice and optimize if it might arise
31    somehow through pointer aliasing or whatever.
32 
33    Enhancements:
34 
35    The high quotient limb is non-zero when high{up,vsize} >= {vp,vsize}.  We
36    could make that comparison and use qsize==prec instead of qsize==prec+1,
37    to save one limb in the division.
38 
39    If r==u but the size is enough bigger than prec that there won't be an
40    overlap between quotient and dividend in mpn_tdiv_qr, then we can avoid
41    copying up,usize.  This would only arise from a prec reduced with
42    mpf_set_prec_raw and will be pretty unusual, but might be worthwhile if
43    it could be worked into the copy_u decision cleanly.  */
44 
45 void
mpf_div(mpf_ptr r,mpf_srcptr u,mpf_srcptr v)46 mpf_div (mpf_ptr r, mpf_srcptr u, mpf_srcptr v)
47 {
48   mp_srcptr up, vp;
49   mp_ptr rp, tp, new_vp;
50   mp_size_t usize, vsize, rsize, prospective_rsize, tsize, zeros;
51   mp_size_t sign_quotient, prec, high_zero, chop;
52   mp_exp_t rexp;
53   int copy_u;
54   TMP_DECL;
55 
56   usize = SIZ(u);
57   vsize = SIZ(v);
58   sign_quotient = usize ^ vsize;
59   usize = ABS (usize);
60   vsize = ABS (vsize);
61   prec = PREC(r);
62 
63   if (vsize == 0)
64     DIVIDE_BY_ZERO;
65 
66   if (usize == 0)
67     {
68       SIZ(r) = 0;
69       EXP(r) = 0;
70       return;
71     }
72 
73   TMP_MARK;
74   rexp = EXP(u) - EXP(v) + 1;
75 
76   rp = PTR(r);
77   up = PTR(u);
78   vp = PTR(v);
79 
80   prospective_rsize = usize - vsize + 1; /* quot from using given u,v sizes */
81   rsize = prec + 1;			 /* desired quot */
82 
83   zeros = rsize - prospective_rsize;	 /* padding u to give rsize */
84   copy_u = (zeros > 0 || rp == up);	 /* copy u if overlap or padding */
85 
86   chop = MAX (-zeros, 0);		 /* negative zeros means shorten u */
87   up += chop;
88   usize -= chop;
89   zeros += chop;			 /* now zeros >= 0 */
90 
91   tsize = usize + zeros;		 /* size for possible copy of u */
92 
93   /* copy and possibly extend u if necessary */
94   if (copy_u)
95     {
96       tp = TMP_ALLOC_LIMBS (tsize + 1);	/* +1 for mpn_div_q's scratch needs */
97       MPN_ZERO (tp, zeros);
98       MPN_COPY (tp+zeros, up, usize);
99       up = tp;
100       usize = tsize;
101     }
102   else
103     {
104       tp = TMP_ALLOC_LIMBS (usize + 1);
105     }
106 
107   /* ensure divisor doesn't overlap quotient */
108   if (rp == vp)
109     {
110       new_vp = TMP_ALLOC_LIMBS (vsize);
111       MPN_COPY (new_vp, vp, vsize);
112       vp = new_vp;
113     }
114 
115   ASSERT (usize-vsize+1 == rsize);
116   mpn_div_q (rp, up, usize, vp, vsize, tp);
117 
118   /* strip possible zero high limb */
119   high_zero = (rp[rsize-1] == 0);
120   rsize -= high_zero;
121   rexp -= high_zero;
122 
123   SIZ(r) = sign_quotient >= 0 ? rsize : -rsize;
124   EXP(r) = rexp;
125   TMP_FREE;
126 }
127