Merge branch 'vendor/TEXINFO'
[dragonfly.git] / contrib / mpfr / fits_uintmax.c
1 /* mpfr_fits_uintmax_p -- test whether an mpfr fits an uintmax_t.
2
3 Copyright 2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao 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 2.1 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.LIB.  If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"            /* for a build within gmp */
25 #endif
26
27 /* The ISO C99 standard specifies that in C++ implementations the
28    INTMAX_MAX, ... macros should only be defined if explicitly requested.  */
29 #if defined __cplusplus
30 # define __STDC_LIMIT_MACROS
31 # define __STDC_CONSTANT_MACROS
32 #endif
33
34 #if HAVE_INTTYPES_H
35 # include <inttypes.h> /* for intmax_t */
36 #else
37 # if HAVE_STDINT_H
38 #  include <stdint.h>
39 # endif
40 #endif
41
42 #include "mpfr-impl.h"
43
44 #ifdef _MPFR_H_HAVE_INTMAX_T
45
46 /* We can't use fits_u.h <= mpfr_cmp_ui */
47 int
48 mpfr_fits_uintmax_p (mpfr_srcptr f, mp_rnd_t rnd)
49 {
50   mp_exp_t exp;
51   mp_prec_t prec;
52   uintmax_t s;
53   mpfr_t x, y;
54   int res;
55
56   if (MPFR_IS_NAN(f) || MPFR_IS_INF(f) || MPFR_SIGN(f) < 0)
57     return 0; /* does not fit */
58
59   if (MPFR_IS_ZERO(f))
60     return 1; /* zero always fits */
61
62   /* now it fits if
63      (a) f <= MAXIMUM
64      (b) round(f, prec(slong), rnd) <= MAXIMUM */
65
66   exp = MPFR_EXP(f);
67   if (exp < 1)
68     return 1; /* |f| < 1: always fits */
69
70   /* first compute prec(MAXIMUM) */
71   for (s = UINTMAX_MAX, prec = 0; s != 0; s /= 2, prec ++);
72
73   /* MAXIMUM needs prec bits, i.e. 2^(prec-1) <= |MAXIMUM| < 2^prec */
74
75    /* if exp < prec - 1, then f < 2^(prec-1) < |MAXIMUM| */
76   if ((mpfr_prec_t) exp < prec - 1)
77     return 1;
78
79   /* if exp > prec + 1, then f >= 2^prec > MAXIMUM */
80   if ((mpfr_prec_t) exp > prec + 1)
81     return 0;
82
83   /* remains cases exp = prec-1 to prec+1 */
84
85   /* hard case: first round to prec bits, then check */
86   mpfr_init2 (x, prec);
87   mpfr_init2 (y, prec);
88   mpfr_set (x, f, rnd);
89   res = mpfr_set_uj (y, UINTMAX_MAX, GMP_RNDN);
90   MPFR_ASSERTD (res == 0);
91   res = mpfr_cmp (x, y) <= 0;
92   mpfr_clear (y);
93   mpfr_clear (x);
94
95   return res;
96 }
97
98 #endif