xref: /netbsd-src/external/lgpl3/mpfr/dist/src/fits_intmax.c (revision 1580a27b92f58fcdcb23fdfbc04a7c2b54a0b7c8)
1 /* mpfr_fits_intmax_p -- test whether an mpfr fits an intmax_t.
2 
3 Copyright 2004, 2006-2016 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 http://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 #ifdef HAVE_CONFIG_H
24 # include "config.h"            /* for a build within gmp */
25 #endif
26 
27 #include "mpfr-intmax.h"
28 #include "mpfr-impl.h"
29 
30 #ifdef _MPFR_H_HAVE_INTMAX_T
31 
32 /* We can't use fits_s.h <= mpfr_cmp_ui */
33 int
34 mpfr_fits_intmax_p (mpfr_srcptr f, mpfr_rnd_t rnd)
35 {
36   unsigned int saved_flags;
37   mpfr_exp_t e;
38   int prec;
39   mpfr_t x, y;
40   int neg;
41   int res;
42 
43   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (f)))
44     /* Zero always fit */
45     return MPFR_IS_ZERO (f) ? 1 : 0;
46 
47   /* now it fits if either
48      (a) MINIMUM <= f <= MAXIMUM
49      (b) or MINIMUM <= round(f, prec(slong), rnd) <= MAXIMUM */
50 
51   e = MPFR_EXP (f);
52   if (e < 1)
53     return 1; /* |f| < 1: always fits */
54 
55   neg = MPFR_IS_NEG (f);
56 
57   /* let EXTREMUM be MAXIMUM if f > 0, and MINIMUM if f < 0 */
58 
59   /* first compute prec(EXTREMUM), this could be done at configure time,
60      but the result can depend on neg (the loop is moved inside the "if"
61      to give the compiler a better chance to compute prec statically) */
62   if (neg)
63     {
64       uintmax_t s;
65       /* In C89, the division on negative integers isn't well-defined. */
66       s = SAFE_ABS (uintmax_t, MPFR_INTMAX_MIN);
67       for (prec = 0; s != 0; s /= 2, prec ++);
68     }
69   else
70     {
71       intmax_t s;
72       s = MPFR_INTMAX_MAX;
73       for (prec = 0; s != 0; s /= 2, prec ++);
74     }
75 
76   /* EXTREMUM needs prec bits, i.e. 2^(prec-1) <= |EXTREMUM| < 2^prec */
77 
78    /* if e <= prec - 1, then f < 2^(prec-1) <= |EXTREMUM| */
79   if (e <= prec - 1)
80     return 1;
81 
82   /* if e >= prec + 1, then f >= 2^prec > |EXTREMUM| */
83   if (e >= prec + 1)
84     return 0;
85 
86   MPFR_ASSERTD (e == prec);
87 
88   /* hard case: first round to prec bits, then check */
89   saved_flags = __gmpfr_flags;
90   mpfr_init2 (x, prec);
91   mpfr_set (x, f, rnd);
92 
93   if (neg)
94     {
95       mpfr_init2 (y, prec);
96       mpfr_set_sj (y, MPFR_INTMAX_MIN, MPFR_RNDN);
97       res = mpfr_cmp (x, y) >= 0;
98       mpfr_clear (y);
99     }
100   else
101     {
102       /* Warning! Due to the rounding, x can be an infinity. Here we use
103          the fact that singular numbers have a special exponent field,
104          thus well-defined and different from e, in which case this means
105          that the number does not fit. That's why we use MPFR_EXP, not
106          MPFR_GET_EXP. */
107       res = MPFR_EXP (x) == e;
108     }
109 
110   mpfr_clear (x);
111   __gmpfr_flags = saved_flags;
112   return res;
113 }
114 
115 #endif
116