1 /* mpfr_get_sj -- convert a MPFR number to a huge machine signed integer 2 3 Copyright 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc. 4 Contributed by the AriC and Caramel 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 intmax_t 33 mpfr_get_sj (mpfr_srcptr f, mpfr_rnd_t rnd) 34 { 35 intmax_t r; 36 mpfr_prec_t prec; 37 mpfr_t x; 38 39 if (MPFR_UNLIKELY (!mpfr_fits_intmax_p (f, rnd))) 40 { 41 MPFR_SET_ERANGE (); 42 return MPFR_IS_NAN (f) ? 0 : 43 MPFR_IS_NEG (f) ? MPFR_INTMAX_MIN : MPFR_INTMAX_MAX; 44 } 45 46 if (MPFR_IS_ZERO (f)) 47 return (intmax_t) 0; 48 49 /* determine the precision of intmax_t */ 50 for (r = MPFR_INTMAX_MIN, prec = 0; r != 0; r /= 2, prec++) 51 { } 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 55 be needed later). */ 56 57 /* Now, r = 0. */ 58 59 mpfr_init2 (x, prec); 60 mpfr_rint (x, f, rnd); 61 MPFR_ASSERTN (MPFR_IS_FP (x)); 62 63 if (MPFR_NOTZERO (x)) 64 { 65 mp_limb_t *xp; 66 int sh, n; /* An int should be sufficient in this context. */ 67 68 xp = MPFR_MANT (x); 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)) 73 { 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))); 79 r = MPFR_INTMAX_MIN; 80 } 81 else if (MPFR_IS_POS (x)) 82 { 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--) 89 { 90 sh -= GMP_NUMB_BITS; 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); 99 r += (sh >= 0 100 ? (intmax_t) xp[n] << sh 101 : (intmax_t) (xp[n] >> (-sh))); 102 } 103 } 104 else 105 { 106 /* See the comments for the case x positive. */ 107 for (n = MPFR_LIMB_SIZE (x) - 1; n >= 0 && sh >= 0; n--) 108 { 109 sh -= GMP_NUMB_BITS; 110 MPFR_ASSERTD (sh < GMP_NUMB_BITS && -sh < GMP_NUMB_BITS); 111 r -= (sh >= 0 112 ? (intmax_t) xp[n] << sh 113 : (intmax_t) (xp[n] >> (-sh))); 114 } 115 } 116 } 117 118 mpfr_clear (x); 119 120 return r; 121 } 122 123 #endif 124