Actually hook powernow.4 into the build.
[dragonfly.git] / contrib / gmp / mpf / div.c
blobef3675eb346b7baeab6ccd311298c2f48cfd6f9b
1 /* mpf_div -- Divide two floats.
3 Copyright 1993, 1994, 1996, 2000, 2001, 2002, 2004, 2005 Free Software
4 Foundation, Inc.
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 */
22 #include "gmp.h"
23 #include "gmp-impl.h"
24 #include "longlong.h"
27 /* Not done:
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.
35 Enhancements:
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.
47 Future:
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. */
55 void
56 mpf_div (mpf_ptr r, mpf_srcptr u, mpf_srcptr v)
58 mp_srcptr up, vp;
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;
62 mp_exp_t rexp;
63 int copy_u;
64 TMP_DECL;
66 usize = u->_mp_size;
67 vsize = v->_mp_size;
68 sign_quotient = usize ^ vsize;
69 usize = ABS (usize);
70 vsize = ABS (vsize);
71 prec = r->_mp_prec;
73 if (vsize == 0)
74 DIVIDE_BY_ZERO;
76 if (usize == 0)
78 r->_mp_size = 0;
79 r->_mp_exp = 0;
80 return;
83 TMP_MARK;
84 rexp = u->_mp_exp - v->_mp_exp + 1;
86 rp = r->_mp_d;
87 up = u->_mp_d;
88 vp = v->_mp_d;
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 */
97 up += chop;
98 usize -= chop;
99 zeros += chop; /* now zeros >= 0 */
101 tsize = usize + zeros; /* size for possible copy of u */
103 if (WANT_TMP_DEBUG)
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);
110 else
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 */
120 if (copy_u)
122 MPN_ZERO (tp, zeros);
123 MPN_COPY (tp+zeros, up, usize);
124 up = tp;
125 usize = tsize;
128 /* ensure divisor doesn't overlap quotient */
129 if (rp == vp)
131 MPN_COPY (new_vp, vp, vsize);
132 vp = new_vp;
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);
140 rsize -= high_zero;
141 rexp -= high_zero;
143 r->_mp_size = sign_quotient >= 0 ? rsize : -rsize;
144 r->_mp_exp = rexp;
145 TMP_FREE;