xref: /netbsd-src/external/lgpl3/mpfr/dist/src/set_q.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1 /* mpfr_set_q -- set a floating-point number from a multiple-precision rational
2 
3 Copyright 2000-2002, 2004-2023 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5 
6 This file is part of the GNU MPFR Library.
7 
8 The GNU MPFR 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 MPFR 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 MPFR Library; see the file COPYING.LESSER.  If not, see
20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22 
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
25 
26 #ifndef MPFR_USE_MINI_GMP
27 /*
28  * Set f to z, choosing the smallest precision for f
29  * so that z = f*(2^BPML)*zs*2^(RetVal)
30  */
31 static int
set_z(mpfr_ptr f,mpz_srcptr z,mp_size_t * zs)32 set_z (mpfr_ptr f, mpz_srcptr z, mp_size_t *zs)
33 {
34   mp_limb_t *p;
35   mp_size_t s;
36   int c;
37   mpfr_prec_t pf;
38 
39   MPFR_ASSERTD (mpz_sgn (z) != 0);
40 
41   /* Remove useless ending 0 */
42   for (p = PTR (z), s = *zs = ABSIZ (z) ; *p == 0; p++, s--)
43     MPFR_ASSERTD (s >= 0);
44 
45   /* Get working precision */
46   count_leading_zeros (c, p[s-1]);
47   pf = s * GMP_NUMB_BITS - c;
48   MPFR_ASSERTD (pf >= 1);
49   mpfr_init2 (f, pf >= MPFR_PREC_MIN ? pf : MPFR_PREC_MIN);
50 
51   /* Copy Mantissa */
52   if (MPFR_LIKELY (c))
53     mpn_lshift (MPFR_MANT (f), p, s, c);
54   else
55     MPN_COPY (MPFR_MANT (f), p, s);
56 
57   MPFR_SET_SIGN (f, mpz_sgn (z));
58   MPFR_SET_EXP (f, 0);
59 
60   return -c;
61 }
62 
63 /* set f to the rational q */
64 int
mpfr_set_q(mpfr_ptr f,mpq_srcptr q,mpfr_rnd_t rnd)65 mpfr_set_q (mpfr_ptr f, mpq_srcptr q, mpfr_rnd_t rnd)
66 {
67   mpz_srcptr num, den;
68   mpfr_t n, d;
69   int inexact;
70   int cn, cd;
71   long shift;
72   mp_size_t sn, sd;
73   MPFR_SAVE_EXPO_DECL (expo);
74 
75   num = mpq_numref (q);
76   den = mpq_denref (q);
77   /* NAN and INF for mpq are not really documented, but could be found */
78   if (MPFR_UNLIKELY (mpz_sgn (num) == 0))
79     {
80       if (MPFR_UNLIKELY (mpz_sgn (den) == 0))
81         {
82           MPFR_SET_NAN (f);
83           MPFR_RET_NAN;
84         }
85       else
86         {
87           MPFR_SET_ZERO (f);
88           MPFR_SET_POS (f);
89           MPFR_RET (0);
90         }
91     }
92   if (MPFR_UNLIKELY (mpz_sgn (den) == 0))
93     {
94       MPFR_SET_INF (f);
95       MPFR_SET_SIGN (f, mpz_sgn (num));
96       MPFR_RET (0);
97     }
98 
99   MPFR_SAVE_EXPO_MARK (expo);
100 
101   cn = set_z (n, num, &sn);
102   cd = set_z (d, den, &sd);
103 
104   /* sn is the number of limbs of the numerator, sd that of the denominator */
105 
106   sn -= sd;
107 #if GMP_NUMB_BITS <= 32 /* overflow/underflow cannot happen on 64-bit
108                            processors, where MPFR_EMAX_MAX is 2^62 - 1, due to
109                            memory limits */
110   /* If sn >= 0, the quotient has at most sn limbs, thus is larger or equal to
111      2^((sn-1)*GMP_NUMB_BITS), thus its exponent >= (sn-1)*GMP_NUMB_BITS)+1.
112      (sn-1)*GMP_NUMB_BITS)+1 > emax yields (sn-1)*GMP_NUMB_BITS) >= emax,
113      i.e., sn-1 >= floor(emax/GMP_NUMB_BITS). */
114   if (MPFR_UNLIKELY (sn > MPFR_EMAX_MAX / GMP_NUMB_BITS))
115     {
116       MPFR_SAVE_EXPO_FREE (expo);
117       inexact = mpfr_overflow (f, rnd, MPFR_SIGN (f));
118       goto end;
119     }
120   /* If sn < 0, the inverse quotient is >= 2^((-sn-1)*GMP_NUMB_BITS),
121      thus the quotient is <= 2^((sn+1)*GMP_NUMB_BITS), and thus its
122      exponent is <= (sn+1)*GMP_NUMB_BITS+1.
123      (sn+1)*GMP_NUMB_BITS+1 < emin yields (sn+1)*GMP_NUMB_BITS+2 <= emin,
124      i.e., sn+1 <= floor((emin-2)/GMP_NUMB_BITS). */
125   if (MPFR_UNLIKELY (sn <= (MPFR_EMIN_MIN - 2) / GMP_NUMB_BITS - 1))
126     {
127       MPFR_SAVE_EXPO_FREE (expo);
128       if (rnd == MPFR_RNDN)
129         rnd = MPFR_RNDZ;
130       inexact = mpfr_underflow (f, rnd, MPFR_SIGN (f));
131       goto end;
132     }
133 #endif
134 
135   inexact = mpfr_div (f, n, d, rnd);
136   shift = GMP_NUMB_BITS*sn+cn-cd;
137   MPFR_ASSERTD (shift == GMP_NUMB_BITS*sn+cn-cd);
138   cd = mpfr_mul_2si (f, f, shift, rnd);
139   MPFR_SAVE_EXPO_FREE (expo);
140   /* we can have cd <> 0 only in case of underflow or overflow, but since we
141      are still in extended exponent range, this cannot happen on 64-bit (see
142      above) */
143 #if GMP_NUMB_BITS <= 32
144   if (MPFR_UNLIKELY (cd != 0))
145     inexact = cd;
146   else
147     inexact = mpfr_check_range (f, inexact, rnd);
148  end:
149 #else
150   MPFR_ASSERTD(cd == 0);
151   inexact = mpfr_check_range (f, inexact, rnd);
152 #endif
153   mpfr_clear (d);
154   mpfr_clear (n);
155   MPFR_RET (inexact);
156 }
157 #endif
158