1/* mpfr_get_sj -- convert a MPFR number to a huge machine signed integer
2
3Copyright 2004, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4Contributed by the Arenaire and Cacao projects, INRIA.
5
6This file is part of the GNU MPFR Library.
7
8The GNU MPFR Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13The GNU MPFR Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2151 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/* 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
46intmax_t
47mpfr_get_sj (mpfr_srcptr f, mpfr_rnd_t rnd)
48{
49  intmax_t r;
50  mpfr_prec_t prec;
51  mpfr_t x;
52
53  if (MPFR_UNLIKELY (!mpfr_fits_intmax_p (f, rnd)))
54    {
55      MPFR_SET_ERANGE ();
56      return MPFR_IS_NAN (f) ? 0 :
57        MPFR_IS_NEG (f) ? MPFR_INTMAX_MIN : MPFR_INTMAX_MAX;
58    }
59
60  if (MPFR_IS_ZERO (f))
61     return (intmax_t) 0;
62
63  /* determine the precision of intmax_t */
64  for (r = MPFR_INTMAX_MIN, prec = 0; r != 0; r /= 2, prec++)
65    { }
66  /* Note: though INTMAX_MAX would have been sufficient for the conversion,
67     we chose INTMAX_MIN so that INTMAX_MIN - 1 is always representable in
68     precision prec; this is useful to detect overflows in MPFR_RNDZ (will
69     be needed later). */
70
71  /* Now, r = 0. */
72
73  mpfr_init2 (x, prec);
74  mpfr_rint (x, f, rnd);
75  MPFR_ASSERTN (MPFR_IS_FP (x));
76
77  if (MPFR_NOTZERO (x))
78    {
79      mp_limb_t *xp;
80      int sh, n;        /* An int should be sufficient in this context. */
81
82      xp = MPFR_MANT (x);
83      sh = MPFR_GET_EXP (x);
84      MPFR_ASSERTN ((mpfr_prec_t) sh <= prec);
85      if (MPFR_INTMAX_MIN + MPFR_INTMAX_MAX != 0
86          && MPFR_UNLIKELY ((mpfr_prec_t) sh == prec))
87        {
88          /* 2's complement and x <= INTMAX_MIN: in the case mp_limb_t
89             has the same size as intmax_t, we cannot use the code in
90             the for loop since the operations would be performed in
91             unsigned arithmetic. */
92          MPFR_ASSERTN (MPFR_IS_NEG (x) && (mpfr_powerof2_raw (x)));
93          r = MPFR_INTMAX_MIN;
94        }
95      else if (MPFR_IS_POS (x))
96        {
97          /* Note: testing the condition sh >= 0 is necessary to avoid
98             an undefined behavior on xp[n] >> S when S >= GMP_NUMB_BITS
99             (even though xp[n] == 0 in such a case). This can happen if
100             sizeof(mp_limb_t) < sizeof(intmax_t) and |x| is small enough
101             because of the trailing bits due to its normalization. */
102          for (n = MPFR_LIMB_SIZE (x) - 1; n >= 0 && sh >= 0; n--)
103            {
104              sh -= GMP_NUMB_BITS;
105              /* Note the concerning the casts below:
106                 When sh >= 0, the cast must be performed before the shift
107                 for the case sizeof(intmax_t) > sizeof(mp_limb_t).
108                 When sh < 0, the cast must be performed after the shift
109                 for the case sizeof(intmax_t) == sizeof(mp_limb_t), as
110                 mp_limb_t is unsigned, therefore not representable as an
111                 intmax_t when the MSB is 1 (this is the case here). */
112              MPFR_ASSERTD (sh < GMP_NUMB_BITS && -sh < GMP_NUMB_BITS);
113              r += (sh >= 0
114                    ? (intmax_t) xp[n] << sh
115                    : (intmax_t) (xp[n] >> (-sh)));
116            }
117        }
118      else
119        {
120          /* See the comments for the case x positive. */
121          for (n = MPFR_LIMB_SIZE (x) - 1; n >= 0 && sh >= 0; n--)
122            {
123              sh -= GMP_NUMB_BITS;
124              MPFR_ASSERTD (sh < GMP_NUMB_BITS && -sh < GMP_NUMB_BITS);
125              r -= (sh >= 0
126                    ? (intmax_t) xp[n] << sh
127                    : (intmax_t) (xp[n] >> (-sh)));
128            }
129        }
130    }
131
132  mpfr_clear (x);
133
134  return r;
135}
136
137#endif
138