netif - alc driver port - Initial work by Samuel J. Greear
[dragonfly.git] / contrib / gmp / mpf / set_q.c
bloba54aa3b1653466d4619e0b650344fc148e8e8fee
1 /* mpf_set_q (mpf_t rop, mpq_t op) -- Convert the rational op to the float rop.
3 Copyright 1996, 1999, 2001, 2002, 2004, 2005 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 Lesser General Public License as published by
9 the Free Software Foundation; either version 3 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 Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
20 #include <stdio.h> /* for NULL */
21 #include "gmp.h"
22 #include "gmp-impl.h"
23 #include "longlong.h"
26 /* As usual the aim is to produce PREC(r) limbs, with the high non-zero.
27 The basic mpn_tdiv_qr produces a quotient of nsize-dsize+1 limbs, with
28 either the high or second highest limb non-zero. We arrange for
29 nsize-dsize+1 to equal prec+1, hence giving either prec or prec+1 result
30 limbs at PTR(r).
32 nsize-dsize+1 == prec+1 is achieved by adjusting num(q), either dropping
33 low limbs if it's too big, or padding with low zeros if it's too small.
34 The full given den(q) is always used.
36 We cannot truncate den(q), because even when it's much bigger than prec
37 the last limbs can still influence the final quotient. Often they don't,
38 but we leave optimization of that to a prospective quotient-only mpn
39 division.
41 Not done:
43 If den(q) is a power of 2 then we may end up with low zero limbs on the
44 result. But nothing is done about this, since it should be unlikely on
45 random data, and can be left to an application to call mpf_div_2exp if it
46 might occur with any frequency.
48 Enhancements:
50 The high quotient limb is non-zero when high{np,dsize} >= {dp,dsize}. We
51 could make that comparison and use qsize==prec instead of qsize==prec+1,
52 to save one limb in the division.
54 Future:
56 If/when mpn_tdiv_qr supports its qxn parameter we can use that instead of
57 padding n with zeros in temporary space.
59 If/when a quotient-only division exists it can be used here immediately.
60 remp is only to satisfy mpn_tdiv_qr, the remainder is not used. */
62 void
63 mpf_set_q (mpf_t r, mpq_srcptr q)
65 mp_srcptr np, dp;
66 mp_size_t prec, nsize, dsize, qsize, prospective_qsize, tsize, zeros;
67 mp_size_t sign_quotient, high_zero;
68 mp_ptr qp, tp, remp;
69 mp_exp_t exp;
70 TMP_DECL;
72 ASSERT (SIZ(&q->_mp_den) > 0); /* canonical q */
74 nsize = SIZ (&q->_mp_num);
75 dsize = SIZ (&q->_mp_den);
77 if (UNLIKELY (nsize == 0))
79 SIZ (r) = 0;
80 EXP (r) = 0;
81 return;
84 TMP_MARK;
86 prec = PREC (r);
87 qp = PTR (r);
89 sign_quotient = nsize;
90 nsize = ABS (nsize);
91 np = PTR (&q->_mp_num);
92 dp = PTR (&q->_mp_den);
94 prospective_qsize = nsize - dsize + 1; /* q from using given n,d sizes */
95 exp = prospective_qsize; /* ie. number of integer limbs */
96 qsize = prec + 1; /* desired q */
98 zeros = qsize - prospective_qsize; /* n zeros to get desired qsize */
99 tsize = nsize + zeros; /* possible copy of n */
101 if (WANT_TMP_DEBUG)
103 /* separate alloc blocks, for malloc debugging */
104 remp = TMP_ALLOC_LIMBS (dsize);
105 tp = NULL;
106 if (zeros > 0)
107 tp = TMP_ALLOC_LIMBS (tsize);
109 else
111 /* one alloc with a conditionalized size, for efficiency */
112 mp_size_t size = dsize + (zeros > 0 ? tsize : 0);
113 remp = TMP_ALLOC_LIMBS (size);
114 tp = remp + dsize;
117 if (zeros > 0)
119 /* pad n with zeros into temporary space */
120 MPN_ZERO (tp, zeros);
121 MPN_COPY (tp+zeros, np, nsize);
122 np = tp;
123 nsize = tsize;
125 else
127 /* shorten n to get desired qsize */
128 nsize += zeros;
129 np -= zeros;
132 ASSERT (nsize-dsize+1 == qsize);
133 mpn_tdiv_qr (qp, remp, (mp_size_t) 0, np, nsize, dp, dsize);
135 /* strip possible zero high limb */
136 high_zero = (qp[qsize-1] == 0);
137 qsize -= high_zero;
138 exp -= high_zero;
140 EXP (r) = exp;
141 SIZ (r) = sign_quotient >= 0 ? qsize : -qsize;
143 TMP_FREE;