1 /* mpfr_get_sj -- convert a MPFR number to a huge machine signed integer
3 Copyright 2004, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
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.
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.
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. */
24 # include "config.h" /* for a build within gmp */
27 #include "mpfr-intmax.h"
28 #include "mpfr-impl.h"
30 #ifdef _MPFR_H_HAVE_INTMAX_T
33 mpfr_get_sj (mpfr_srcptr f, mpfr_rnd_t rnd)
39 if (MPFR_UNLIKELY (!mpfr_fits_intmax_p (f, rnd)))
42 return MPFR_IS_NAN (f) ? 0 :
43 MPFR_IS_NEG (f) ? MPFR_INTMAX_MIN : MPFR_INTMAX_MAX;
49 /* determine the precision of intmax_t */
50 for (r = MPFR_INTMAX_MIN, prec = 0; r != 0; r /= 2, prec++)
52 /* Note: though INTMAX_MAX would have been sufficient for the conversion,
53 we chose INTMAX_MIN so that INTMAX_MIN - 1 is always representable in
54 precision prec; this is useful to detect overflows in MPFR_RNDZ (will
60 mpfr_rint (x, f, rnd);
61 MPFR_ASSERTN (MPFR_IS_FP (x));
66 int sh, n; /* An int should be sufficient in this context. */
69 sh = MPFR_GET_EXP (x);
70 MPFR_ASSERTN ((mpfr_prec_t) sh <= prec);
71 if (MPFR_INTMAX_MIN + MPFR_INTMAX_MAX != 0
72 && MPFR_UNLIKELY ((mpfr_prec_t) sh == prec))
74 /* 2's complement and x <= INTMAX_MIN: in the case mp_limb_t
75 has the same size as intmax_t, we cannot use the code in
76 the for loop since the operations would be performed in
77 unsigned arithmetic. */
78 MPFR_ASSERTN (MPFR_IS_NEG (x) && (mpfr_powerof2_raw (x)));
81 else if (MPFR_IS_POS (x))
83 /* Note: testing the condition sh >= 0 is necessary to avoid
84 an undefined behavior on xp[n] >> S when S >= GMP_NUMB_BITS
85 (even though xp[n] == 0 in such a case). This can happen if
86 sizeof(mp_limb_t) < sizeof(intmax_t) and |x| is small enough
87 because of the trailing bits due to its normalization. */
88 for (n = MPFR_LIMB_SIZE (x) - 1; n >= 0 && sh >= 0; n--)
91 /* Note the concerning the casts below:
92 When sh >= 0, the cast must be performed before the shift
93 for the case sizeof(intmax_t) > sizeof(mp_limb_t).
94 When sh < 0, the cast must be performed after the shift
95 for the case sizeof(intmax_t) == sizeof(mp_limb_t), as
96 mp_limb_t is unsigned, therefore not representable as an
97 intmax_t when the MSB is 1 (this is the case here). */
98 MPFR_ASSERTD (sh < GMP_NUMB_BITS && -sh < GMP_NUMB_BITS);
100 ? (intmax_t) xp[n] << sh
101 : (intmax_t) (xp[n] >> (-sh)));
106 /* See the comments for the case x positive. */
107 for (n = MPFR_LIMB_SIZE (x) - 1; n >= 0 && sh >= 0; n--)
110 MPFR_ASSERTD (sh < GMP_NUMB_BITS && -sh < GMP_NUMB_BITS);
112 ? (intmax_t) xp[n] << sh
113 : (intmax_t) (xp[n] >> (-sh)));