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 either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
31 #include <stdio.h> /* for NULL */
37 /* As usual the aim is to produce PREC(r) limbs, with the high non-zero.
38 The basic mpn_tdiv_qr produces a quotient of nsize-dsize+1 limbs, with
39 either the high or second highest limb non-zero. We arrange for
40 nsize-dsize+1 to equal prec+1, hence giving either prec or prec+1 result
43 nsize-dsize+1 == prec+1 is achieved by adjusting num(q), either dropping
44 low limbs if it's too big, or padding with low zeros if it's too small.
45 The full given den(q) is always used.
47 We cannot truncate den(q), because even when it's much bigger than prec
48 the last limbs can still influence the final quotient. Often they don't,
49 but we leave optimization of that to a prospective quotient-only mpn
54 If den(q) is a power of 2 then we may end up with low zero limbs on the
55 result. But nothing is done about this, since it should be unlikely on
56 random data, and can be left to an application to call mpf_div_2exp if it
57 might occur with any frequency.
61 The high quotient limb is non-zero when high{np,dsize} >= {dp,dsize}. We
62 could make that comparison and use qsize==prec instead of qsize==prec+1,
63 to save one limb in the division.
67 If/when mpn_tdiv_qr supports its qxn parameter we can use that instead of
68 padding n with zeros in temporary space.
70 If/when a quotient-only division exists it can be used here immediately.
71 remp is only to satisfy mpn_tdiv_qr, the remainder is not used. */
74 mpf_set_q (mpf_t r
, mpq_srcptr q
)
77 mp_size_t prec
, nsize
, dsize
, qsize
, prospective_qsize
, tsize
, zeros
;
78 mp_size_t sign_quotient
, high_zero
;
83 ASSERT (SIZ(&q
->_mp_den
) > 0); /* canonical q */
85 nsize
= SIZ (&q
->_mp_num
);
86 dsize
= SIZ (&q
->_mp_den
);
88 if (UNLIKELY (nsize
== 0))
100 sign_quotient
= nsize
;
102 np
= PTR (&q
->_mp_num
);
103 dp
= PTR (&q
->_mp_den
);
105 prospective_qsize
= nsize
- dsize
+ 1; /* q from using given n,d sizes */
106 exp
= prospective_qsize
; /* ie. number of integer limbs */
107 qsize
= prec
+ 1; /* desired q */
109 zeros
= qsize
- prospective_qsize
; /* n zeros to get desired qsize */
110 tsize
= nsize
+ zeros
; /* possible copy of n */
114 /* separate alloc blocks, for malloc debugging */
115 remp
= TMP_ALLOC_LIMBS (dsize
);
118 tp
= TMP_ALLOC_LIMBS (tsize
);
122 /* one alloc with a conditionalized size, for efficiency */
123 mp_size_t size
= dsize
+ (zeros
> 0 ? tsize
: 0);
124 remp
= TMP_ALLOC_LIMBS (size
);
130 /* pad n with zeros into temporary space */
131 MPN_ZERO (tp
, zeros
);
132 MPN_COPY (tp
+zeros
, np
, nsize
);
138 /* shorten n to get desired qsize */
143 ASSERT (nsize
-dsize
+1 == qsize
);
144 mpn_tdiv_qr (qp
, remp
, (mp_size_t
) 0, np
, nsize
, dp
, dsize
);
146 /* strip possible zero high limb */
147 high_zero
= (qp
[qsize
-1] == 0);
152 SIZ (r
) = sign_quotient
>= 0 ? qsize
: -qsize
;