1 /* mpfr_set_uj -- set a MPFR number from a huge machine unsigned integer
3 Copyright 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao 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 2.1 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.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
24 # include "config.h" /* for a build within gmp */
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
35 # include <inttypes.h> /* for intmax_t */
42 #define MPFR_NEED_LONGLONG_H
43 #include "mpfr-impl.h"
45 #ifdef _MPFR_H_HAVE_INTMAX_T
48 mpfr_set_uj (mpfr_t x
, uintmax_t j
, mp_rnd_t rnd
)
50 return mpfr_set_uj_2exp (x
, j
, 0, rnd
);
54 mpfr_set_uj_2exp (mpfr_t x
, uintmax_t j
, intmax_t e
, mp_rnd_t rnd
)
59 mp_limb_t yp
[sizeof(uintmax_t) / sizeof(mp_limb_t
)];
61 unsigned long uintmax_bit_size
= sizeof(uintmax_t) * CHAR_BIT
;
62 unsigned long bpml
= BITS_PER_MP_LIMB
% uintmax_bit_size
;
72 MPFR_ASSERTN (sizeof(uintmax_t) % sizeof(mp_limb_t
) == 0);
74 /* Create an auxillary var */
75 MPFR_TMP_INIT1 (yp
, y
, uintmax_bit_size
);
81 /* Note: either BITS_PER_MP_LIMB = uintmax_bit_size, then k = 1 the
82 shift j >>= bpml is never done, or BITS_PER_MP_LIMB < uintmax_bit_size
83 and bpml = BITS_PER_MP_LIMB. */
84 for (i
= 0; i
< k
; i
++, j
>>= bpml
)
85 yp
[i
] = j
; /* Only the low bits are copied */
87 /* Find the first limb not equal to zero. */
96 count_leading_zeros(cnt
, limb
);
97 len
= numberof (yp
) - k
;
99 /* Normalize it: len = number of last 0 limb, k number of non-zero limbs */
100 if (MPFR_LIKELY(cnt
))
101 mpn_lshift (yp
+len
, yp
, k
, cnt
); /* Normalize the High Limb*/
103 MPN_COPY_DECR (yp
+len
, yp
, k
); /* Must use DECR */
105 /* Note: when numberof(yp)==1, len is constant and null, so the compiler
106 can optimize out this code. */
109 yp
[0] = (mp_limb_t
) 0;
111 MPN_ZERO (yp
, len
); /* Zeroing the last limbs */
113 e
+= k
* BITS_PER_MP_LIMB
- cnt
; /* Update Expo */
114 MPFR_ASSERTD (MPFR_LIMB_MSB(yp
[numberof (yp
) - 1]) != 0);
116 /* Check expo underflow / overflow (can't use mpfr_check_range) */
117 if (MPFR_UNLIKELY(e
< __gmpfr_emin
))
119 /* The following test is necessary because in the rounding to the
120 * nearest mode, mpfr_underflow always rounds away from 0. In
121 * this rounding mode, we need to round to 0 if:
122 * _ |x| < 2^(emin-2), or
123 * _ |x| = 2^(emin-2) and the absolute value of the exact
124 * result is <= 2^(emin-2). */
125 if (rnd
== GMP_RNDN
&& (e
+1 < __gmpfr_emin
|| mpfr_powerof2_raw(y
)))
127 return mpfr_underflow (x
, rnd
, MPFR_SIGN_POS
);
129 if (MPFR_UNLIKELY(e
> __gmpfr_emax
))
130 return mpfr_overflow (x
, rnd
, MPFR_SIGN_POS
);
133 /* Final: set x to y (rounding if necessary) */
134 return mpfr_set (x
, y
, rnd
);