1 /* mpfr_set_z_2exp -- set a floating-point number from a multiple-precision
2 integer and an exponent
4 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
5 Contributed by the AriC and Caramel projects, INRIA.
7 This file is part of the GNU MPFR Library.
9 The GNU MPFR Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
14 The GNU MPFR Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
21 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
24 #define MPFR_NEED_LONGLONG_H
25 #include "mpfr-impl.h"
27 /* set f to the integer z multiplied by 2^e */
29 mpfr_set_z_2exp (mpfr_ptr f
, mpz_srcptr z
, mpfr_exp_t e
, mpfr_rnd_t rnd_mode
)
31 mp_size_t fn
, zn
, dif
, en
;
37 if (MPFR_UNLIKELY (sign_z
== 0)) /* ignore the exponent for 0 */
43 MPFR_ASSERTD (sign_z
== MPFR_SIGN_POS
|| sign_z
== MPFR_SIGN_NEG
);
45 zn
= ABS(SIZ(z
)); /* limb size of z */
46 /* compute en = floor(e/GMP_NUMB_BITS) */
47 en
= (e
>= 0) ? e
/ GMP_NUMB_BITS
: (e
+ 1) / GMP_NUMB_BITS
- 1;
48 MPFR_ASSERTD (zn
>= 1);
49 if (MPFR_UNLIKELY (zn
+ en
> MPFR_EMAX_MAX
/ GMP_NUMB_BITS
+ 1))
50 return mpfr_overflow (f
, rnd_mode
, sign_z
);
51 /* because zn + en >= MPFR_EMAX_MAX / GMP_NUMB_BITS + 2
52 implies (zn + en) * GMP_NUMB_BITS >= MPFR_EMAX_MAX + GMP_NUMB_BITS + 1
53 and exp = zn * GMP_NUMB_BITS + e - k
54 >= (zn + en) * GMP_NUMB_BITS - k > MPFR_EMAX_MAX */
57 fn
= MPFR_LIMB_SIZE (f
);
60 count_leading_zeros (k
, zp
[zn
-1]);
62 /* now zn + en <= MPFR_EMAX_MAX / GMP_NUMB_BITS + 1
63 thus (zn + en) * GMP_NUMB_BITS <= MPFR_EMAX_MAX + GMP_NUMB_BITS
64 and exp = zn * GMP_NUMB_BITS + e - k
65 <= (zn + en) * GMP_NUMB_BITS - k + GMP_NUMB_BITS - 1
66 <= MPFR_EMAX_MAX + 2 * GMP_NUMB_BITS - 1 */
67 exp
= (mpfr_prec_t
) zn
* GMP_NUMB_BITS
+ e
- k
;
68 /* The exponent will be exp or exp + 1 (due to rounding) */
69 if (MPFR_UNLIKELY (exp
> __gmpfr_emax
))
70 return mpfr_overflow (f
, rnd_mode
, sign_z
);
71 if (MPFR_UNLIKELY (exp
+ 1 < __gmpfr_emin
))
72 return mpfr_underflow (f
, rnd_mode
== MPFR_RNDN
? MPFR_RNDZ
: rnd_mode
,
75 if (MPFR_LIKELY (dif
>= 0))
77 mp_limb_t rb
, sb
, ulp
;
80 /* number has to be truncated */
81 if (MPFR_LIKELY (k
!= 0))
83 mpn_lshift (fp
, &zp
[dif
], fn
, k
);
84 if (MPFR_LIKELY (dif
> 0))
85 fp
[0] |= zp
[dif
- 1] >> (GMP_NUMB_BITS
- k
);
88 MPN_COPY (fp
, zp
+ dif
, fn
);
90 /* Compute Rounding Bit and Sticky Bit */
91 MPFR_UNSIGNED_MINUS_MODULO (sh
, MPFR_PREC (f
) );
92 if (MPFR_LIKELY (sh
!= 0))
94 mp_limb_t mask
= MPFR_LIMB_ONE
<< (sh
-1);
95 mp_limb_t limb
= fp
[0];
99 fp
[0] = limb
& ~(ulp
-1);
103 mp_limb_t mask
= MPFR_LIMB_ONE
<< (GMP_NUMB_BITS
- 1 - k
);
104 if (MPFR_LIKELY (dif
> 0))
106 rb
= zp
[--dif
] & mask
;
107 sb
= zp
[dif
] & (mask
-1);
114 if (MPFR_UNLIKELY (sb
== 0) && MPFR_LIKELY (dif
> 0))
117 if (MPFR_LIKELY (k
!= 0))
118 sb
&= MPFR_LIMB_MASK (GMP_NUMB_BITS
- k
);
119 if (MPFR_UNLIKELY (sb
== 0) && MPFR_LIKELY (dif
> 0))
122 } while (dif
> 0 && sb
== 0);
126 if (MPFR_LIKELY (rnd_mode
== MPFR_RNDN
))
128 if (rb
== 0 || MPFR_UNLIKELY (sb
== 0 && (fp
[0] & ulp
) == 0))
133 else /* Not Nearest */
135 if (MPFR_LIKELY (MPFR_IS_LIKE_RNDZ (rnd_mode
, sign_z
< 0))
136 || MPFR_UNLIKELY ( (sb
| rb
) == 0 ))
143 inex
= MPFR_LIKELY ((sb
| rb
) != 0) ? -1 : 0;
148 if (MPFR_UNLIKELY (mpn_add_1 (fp
, fp
, fn
, ulp
)))
151 if (MPFR_UNLIKELY (exp
== __gmpfr_emax
))
152 return mpfr_overflow (f
, rnd_mode
, sign_z
);
154 fp
[fn
-1] = MPFR_LIMB_HIGHBIT
;
159 else /* dif < 0: Mantissa F is strictly bigger than z's one */
161 if (MPFR_LIKELY (k
!= 0))
162 mpn_lshift (fp
- dif
, zp
, zn
, k
);
164 MPN_COPY (fp
- dif
, zp
, zn
);
165 /* fill with zeroes */
167 inex
= 0; /* result is exact */
170 if (MPFR_UNLIKELY (exp
< __gmpfr_emin
))
172 if (rnd_mode
== MPFR_RNDN
&& inex
== 0 && mpfr_powerof2_raw (f
))
173 rnd_mode
= MPFR_RNDZ
;
174 return mpfr_underflow (f
, rnd_mode
, sign_z
);
177 MPFR_SET_EXP (f
, exp
);
178 MPFR_SET_SIGN (f
, sign_z
);
179 MPFR_RET (inex
*sign_z
);