1*86d7f5d3SJohn Marino /* mpf_set_q (mpf_t rop, mpq_t op) -- Convert the rational op to the float rop.
2*86d7f5d3SJohn Marino
3*86d7f5d3SJohn Marino Copyright 1996, 1999, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
4*86d7f5d3SJohn Marino
5*86d7f5d3SJohn Marino This file is part of the GNU MP Library.
6*86d7f5d3SJohn Marino
7*86d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
8*86d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
9*86d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
10*86d7f5d3SJohn Marino option) any later version.
11*86d7f5d3SJohn Marino
12*86d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
13*86d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14*86d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15*86d7f5d3SJohn Marino License for more details.
16*86d7f5d3SJohn Marino
17*86d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
18*86d7f5d3SJohn Marino along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
19*86d7f5d3SJohn Marino
20*86d7f5d3SJohn Marino #include <stdio.h> /* for NULL */
21*86d7f5d3SJohn Marino #include "gmp.h"
22*86d7f5d3SJohn Marino #include "gmp-impl.h"
23*86d7f5d3SJohn Marino #include "longlong.h"
24*86d7f5d3SJohn Marino
25*86d7f5d3SJohn Marino
26*86d7f5d3SJohn Marino /* As usual the aim is to produce PREC(r) limbs, with the high non-zero.
27*86d7f5d3SJohn Marino The basic mpn_tdiv_qr produces a quotient of nsize-dsize+1 limbs, with
28*86d7f5d3SJohn Marino either the high or second highest limb non-zero. We arrange for
29*86d7f5d3SJohn Marino nsize-dsize+1 to equal prec+1, hence giving either prec or prec+1 result
30*86d7f5d3SJohn Marino limbs at PTR(r).
31*86d7f5d3SJohn Marino
32*86d7f5d3SJohn Marino nsize-dsize+1 == prec+1 is achieved by adjusting num(q), either dropping
33*86d7f5d3SJohn Marino low limbs if it's too big, or padding with low zeros if it's too small.
34*86d7f5d3SJohn Marino The full given den(q) is always used.
35*86d7f5d3SJohn Marino
36*86d7f5d3SJohn Marino We cannot truncate den(q), because even when it's much bigger than prec
37*86d7f5d3SJohn Marino the last limbs can still influence the final quotient. Often they don't,
38*86d7f5d3SJohn Marino but we leave optimization of that to a prospective quotient-only mpn
39*86d7f5d3SJohn Marino division.
40*86d7f5d3SJohn Marino
41*86d7f5d3SJohn Marino Not done:
42*86d7f5d3SJohn Marino
43*86d7f5d3SJohn Marino If den(q) is a power of 2 then we may end up with low zero limbs on the
44*86d7f5d3SJohn Marino result. But nothing is done about this, since it should be unlikely on
45*86d7f5d3SJohn Marino random data, and can be left to an application to call mpf_div_2exp if it
46*86d7f5d3SJohn Marino might occur with any frequency.
47*86d7f5d3SJohn Marino
48*86d7f5d3SJohn Marino Enhancements:
49*86d7f5d3SJohn Marino
50*86d7f5d3SJohn Marino The high quotient limb is non-zero when high{np,dsize} >= {dp,dsize}. We
51*86d7f5d3SJohn Marino could make that comparison and use qsize==prec instead of qsize==prec+1,
52*86d7f5d3SJohn Marino to save one limb in the division.
53*86d7f5d3SJohn Marino
54*86d7f5d3SJohn Marino Future:
55*86d7f5d3SJohn Marino
56*86d7f5d3SJohn Marino If/when mpn_tdiv_qr supports its qxn parameter we can use that instead of
57*86d7f5d3SJohn Marino padding n with zeros in temporary space.
58*86d7f5d3SJohn Marino
59*86d7f5d3SJohn Marino If/when a quotient-only division exists it can be used here immediately.
60*86d7f5d3SJohn Marino remp is only to satisfy mpn_tdiv_qr, the remainder is not used. */
61*86d7f5d3SJohn Marino
62*86d7f5d3SJohn Marino void
mpf_set_q(mpf_t r,mpq_srcptr q)63*86d7f5d3SJohn Marino mpf_set_q (mpf_t r, mpq_srcptr q)
64*86d7f5d3SJohn Marino {
65*86d7f5d3SJohn Marino mp_srcptr np, dp;
66*86d7f5d3SJohn Marino mp_size_t prec, nsize, dsize, qsize, prospective_qsize, tsize, zeros;
67*86d7f5d3SJohn Marino mp_size_t sign_quotient, high_zero;
68*86d7f5d3SJohn Marino mp_ptr qp, tp, remp;
69*86d7f5d3SJohn Marino mp_exp_t exp;
70*86d7f5d3SJohn Marino TMP_DECL;
71*86d7f5d3SJohn Marino
72*86d7f5d3SJohn Marino ASSERT (SIZ(&q->_mp_den) > 0); /* canonical q */
73*86d7f5d3SJohn Marino
74*86d7f5d3SJohn Marino nsize = SIZ (&q->_mp_num);
75*86d7f5d3SJohn Marino dsize = SIZ (&q->_mp_den);
76*86d7f5d3SJohn Marino
77*86d7f5d3SJohn Marino if (UNLIKELY (nsize == 0))
78*86d7f5d3SJohn Marino {
79*86d7f5d3SJohn Marino SIZ (r) = 0;
80*86d7f5d3SJohn Marino EXP (r) = 0;
81*86d7f5d3SJohn Marino return;
82*86d7f5d3SJohn Marino }
83*86d7f5d3SJohn Marino
84*86d7f5d3SJohn Marino TMP_MARK;
85*86d7f5d3SJohn Marino
86*86d7f5d3SJohn Marino prec = PREC (r);
87*86d7f5d3SJohn Marino qp = PTR (r);
88*86d7f5d3SJohn Marino
89*86d7f5d3SJohn Marino sign_quotient = nsize;
90*86d7f5d3SJohn Marino nsize = ABS (nsize);
91*86d7f5d3SJohn Marino np = PTR (&q->_mp_num);
92*86d7f5d3SJohn Marino dp = PTR (&q->_mp_den);
93*86d7f5d3SJohn Marino
94*86d7f5d3SJohn Marino prospective_qsize = nsize - dsize + 1; /* q from using given n,d sizes */
95*86d7f5d3SJohn Marino exp = prospective_qsize; /* ie. number of integer limbs */
96*86d7f5d3SJohn Marino qsize = prec + 1; /* desired q */
97*86d7f5d3SJohn Marino
98*86d7f5d3SJohn Marino zeros = qsize - prospective_qsize; /* n zeros to get desired qsize */
99*86d7f5d3SJohn Marino tsize = nsize + zeros; /* possible copy of n */
100*86d7f5d3SJohn Marino
101*86d7f5d3SJohn Marino if (WANT_TMP_DEBUG)
102*86d7f5d3SJohn Marino {
103*86d7f5d3SJohn Marino /* separate alloc blocks, for malloc debugging */
104*86d7f5d3SJohn Marino remp = TMP_ALLOC_LIMBS (dsize);
105*86d7f5d3SJohn Marino tp = NULL;
106*86d7f5d3SJohn Marino if (zeros > 0)
107*86d7f5d3SJohn Marino tp = TMP_ALLOC_LIMBS (tsize);
108*86d7f5d3SJohn Marino }
109*86d7f5d3SJohn Marino else
110*86d7f5d3SJohn Marino {
111*86d7f5d3SJohn Marino /* one alloc with a conditionalized size, for efficiency */
112*86d7f5d3SJohn Marino mp_size_t size = dsize + (zeros > 0 ? tsize : 0);
113*86d7f5d3SJohn Marino remp = TMP_ALLOC_LIMBS (size);
114*86d7f5d3SJohn Marino tp = remp + dsize;
115*86d7f5d3SJohn Marino }
116*86d7f5d3SJohn Marino
117*86d7f5d3SJohn Marino if (zeros > 0)
118*86d7f5d3SJohn Marino {
119*86d7f5d3SJohn Marino /* pad n with zeros into temporary space */
120*86d7f5d3SJohn Marino MPN_ZERO (tp, zeros);
121*86d7f5d3SJohn Marino MPN_COPY (tp+zeros, np, nsize);
122*86d7f5d3SJohn Marino np = tp;
123*86d7f5d3SJohn Marino nsize = tsize;
124*86d7f5d3SJohn Marino }
125*86d7f5d3SJohn Marino else
126*86d7f5d3SJohn Marino {
127*86d7f5d3SJohn Marino /* shorten n to get desired qsize */
128*86d7f5d3SJohn Marino nsize += zeros;
129*86d7f5d3SJohn Marino np -= zeros;
130*86d7f5d3SJohn Marino }
131*86d7f5d3SJohn Marino
132*86d7f5d3SJohn Marino ASSERT (nsize-dsize+1 == qsize);
133*86d7f5d3SJohn Marino mpn_tdiv_qr (qp, remp, (mp_size_t) 0, np, nsize, dp, dsize);
134*86d7f5d3SJohn Marino
135*86d7f5d3SJohn Marino /* strip possible zero high limb */
136*86d7f5d3SJohn Marino high_zero = (qp[qsize-1] == 0);
137*86d7f5d3SJohn Marino qsize -= high_zero;
138*86d7f5d3SJohn Marino exp -= high_zero;
139*86d7f5d3SJohn Marino
140*86d7f5d3SJohn Marino EXP (r) = exp;
141*86d7f5d3SJohn Marino SIZ (r) = sign_quotient >= 0 ? qsize : -qsize;
142*86d7f5d3SJohn Marino
143*86d7f5d3SJohn Marino TMP_FREE;
144*86d7f5d3SJohn Marino }
145