Merge branch 'vendor/TEXINFO'
[dragonfly.git] / contrib / mpfr / fits_intmax.c
1 /* mpfr_fits_intmax_p -- test whether an mpfr fits an intmax_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_s.h <= mpfr_cmp_ui */
47 int
48 mpfr_fits_intmax_p (mpfr_srcptr f, mp_rnd_t rnd)
49 {
50   mp_exp_t exp;
51   mp_prec_t prec;
52   intmax_t s;
53   mpfr_t x, y;
54   int neg;
55   int res;
56
57   if (MPFR_IS_NAN (f) || MPFR_IS_INF (f))
58     return 0; /* does not fit */
59
60   if (MPFR_IS_ZERO (f))
61     return 1; /* zero always fits */
62
63   /* now it fits if either
64      (a) MINIMUM <= f <= MAXIMUM
65      (b) or MINIMUM <= round(f, prec(slong), rnd) <= MAXIMUM */
66
67   exp = MPFR_EXP (f);
68   if (exp < 1)
69     return 1; /* |f| < 1: always fits */
70
71   neg = (MPFR_SIGN (f) > 0) ? 0 : 1;
72
73   /* let EXTREMUM be MAXIMUM if f > 0, and MINIMUM if f < 0 */
74
75   /* first compute prec(EXTREMUM), this could be done at configure time */
76   s = (neg) ? INTMAX_MIN : INTMAX_MAX;
77   for (prec = 0; s != 0; s /= 2, prec ++);
78
79   /* EXTREMUM needs prec bits, i.e. 2^(prec-1) <= |EXTREMUM| < 2^prec */
80
81    /* if exp < prec - 1, then f < 2^(prec-1) < |EXTREMUM| */
82   if ((mpfr_prec_t) exp < prec - 1)
83     return 1;
84
85   /* if exp > prec + 1, then f >= 2^prec > EXTREMUM */
86   if ((mpfr_prec_t) exp > prec + 1)
87     return 0;
88
89   /* remains cases exp = prec-1 to prec+1 */
90
91   /* hard case: first round to prec bits, then check */
92   mpfr_init2 (x, prec);
93   mpfr_init2 (y, prec);
94
95   mpfr_set (x, f, rnd);
96   mpfr_set_sj (y, neg ? INTMAX_MIN : INTMAX_MAX, GMP_RNDN);
97
98   res = (neg
99          ? (mpfr_cmp (x, y) >= 0)
100          : (mpfr_cmp (x, y) <= 0));
101
102   mpfr_clear (y);
103   mpfr_clear (x);
104
105   return res;
106 }
107
108 #endif