xref: /dflybsd-src/contrib/mpfr/src/set_uj.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_set_uj -- set a MPFR number from a huge machine unsigned integer
24a238c70SJohn Marino 
3*ab6d115fSJohn Marino Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
54a238c70SJohn Marino 
64a238c70SJohn Marino This file is part of the GNU MPFR Library.
74a238c70SJohn Marino 
84a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
94a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
104a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
114a238c70SJohn Marino option) any later version.
124a238c70SJohn Marino 
134a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
144a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
154a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
164a238c70SJohn Marino License for more details.
174a238c70SJohn Marino 
184a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
194a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
204a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
214a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
224a238c70SJohn Marino 
234a238c70SJohn Marino #ifdef HAVE_CONFIG_H
244a238c70SJohn Marino # include "config.h"       /* for a build within gmp */
254a238c70SJohn Marino #endif
264a238c70SJohn Marino 
274a238c70SJohn Marino #define MPFR_NEED_LONGLONG_H
284a238c70SJohn Marino #include "mpfr-intmax.h"
294a238c70SJohn Marino #include "mpfr-impl.h"
304a238c70SJohn Marino 
314a238c70SJohn Marino #ifdef _MPFR_H_HAVE_INTMAX_T
324a238c70SJohn Marino 
334a238c70SJohn Marino int
mpfr_set_uj(mpfr_t x,uintmax_t j,mpfr_rnd_t rnd)344a238c70SJohn Marino mpfr_set_uj (mpfr_t x, uintmax_t j, mpfr_rnd_t rnd)
354a238c70SJohn Marino {
364a238c70SJohn Marino   return mpfr_set_uj_2exp (x, j, 0, rnd);
374a238c70SJohn Marino }
384a238c70SJohn Marino 
394a238c70SJohn Marino int
mpfr_set_uj_2exp(mpfr_t x,uintmax_t j,intmax_t e,mpfr_rnd_t rnd)404a238c70SJohn Marino mpfr_set_uj_2exp (mpfr_t x, uintmax_t j, intmax_t e, mpfr_rnd_t rnd)
414a238c70SJohn Marino {
424a238c70SJohn Marino   unsigned int cnt, i;
434a238c70SJohn Marino   mp_size_t k, len;
444a238c70SJohn Marino   mp_limb_t limb;
454a238c70SJohn Marino   mp_limb_t yp[sizeof(uintmax_t) / sizeof(mp_limb_t)];
464a238c70SJohn Marino   mpfr_t y;
474a238c70SJohn Marino   unsigned long uintmax_bit_size = sizeof(uintmax_t) * CHAR_BIT;
484a238c70SJohn Marino   unsigned long bpml = GMP_NUMB_BITS % uintmax_bit_size;
494a238c70SJohn Marino 
504a238c70SJohn Marino   /* Special case */
514a238c70SJohn Marino   if (j == 0)
524a238c70SJohn Marino     {
534a238c70SJohn Marino       MPFR_SET_POS(x);
544a238c70SJohn Marino       MPFR_SET_ZERO(x);
554a238c70SJohn Marino       MPFR_RET(0);
564a238c70SJohn Marino     }
574a238c70SJohn Marino 
584a238c70SJohn Marino   MPFR_ASSERTN (sizeof(uintmax_t) % sizeof(mp_limb_t) == 0);
594a238c70SJohn Marino 
604a238c70SJohn Marino   /* Create an auxillary var */
614a238c70SJohn Marino   MPFR_TMP_INIT1 (yp, y, uintmax_bit_size);
624a238c70SJohn Marino   k = numberof (yp);
634a238c70SJohn Marino   if (k == 1)
644a238c70SJohn Marino     limb = yp[0] = j;
654a238c70SJohn Marino   else
664a238c70SJohn Marino     {
674a238c70SJohn Marino       /* Note: either GMP_NUMB_BITS = uintmax_bit_size, then k = 1 the
684a238c70SJohn Marino          shift j >>= bpml is never done, or GMP_NUMB_BITS < uintmax_bit_size
694a238c70SJohn Marino          and bpml = GMP_NUMB_BITS. */
704a238c70SJohn Marino       for (i = 0; i < k; i++, j >>= bpml)
714a238c70SJohn Marino         yp[i] = j; /* Only the low bits are copied */
724a238c70SJohn Marino 
734a238c70SJohn Marino       /* Find the first limb not equal to zero. */
744a238c70SJohn Marino       do
754a238c70SJohn Marino         {
764a238c70SJohn Marino           MPFR_ASSERTD (k > 0);
774a238c70SJohn Marino           limb = yp[--k];
784a238c70SJohn Marino         }
794a238c70SJohn Marino       while (limb == 0);
804a238c70SJohn Marino       k++;
814a238c70SJohn Marino     }
824a238c70SJohn Marino   count_leading_zeros(cnt, limb);
834a238c70SJohn Marino   len = numberof (yp) - k;
844a238c70SJohn Marino 
854a238c70SJohn Marino   /* Normalize it: len = number of last 0 limb, k number of non-zero limbs */
864a238c70SJohn Marino   if (MPFR_LIKELY(cnt))
874a238c70SJohn Marino     mpn_lshift (yp+len, yp, k, cnt);  /* Normalize the High Limb*/
884a238c70SJohn Marino   else if (len != 0)
894a238c70SJohn Marino     MPN_COPY_DECR (yp+len, yp, k);    /* Must use DECR */
904a238c70SJohn Marino   if (len != 0)
914a238c70SJohn Marino     /* Note: when numberof(yp)==1, len is constant and null, so the compiler
924a238c70SJohn Marino        can optimize out this code. */
934a238c70SJohn Marino     {
944a238c70SJohn Marino       if (len == 1)
954a238c70SJohn Marino         yp[0] = (mp_limb_t) 0;
964a238c70SJohn Marino       else
974a238c70SJohn Marino         MPN_ZERO (yp, len);   /* Zeroing the last limbs */
984a238c70SJohn Marino     }
994a238c70SJohn Marino   e += k * GMP_NUMB_BITS - cnt;    /* Update Expo */
1004a238c70SJohn Marino   MPFR_ASSERTD (MPFR_LIMB_MSB(yp[numberof (yp) - 1]) != 0);
1014a238c70SJohn Marino 
1024a238c70SJohn Marino   /* Check expo underflow / overflow (can't use mpfr_check_range) */
1034a238c70SJohn Marino   if (MPFR_UNLIKELY(e < __gmpfr_emin))
1044a238c70SJohn Marino     {
1054a238c70SJohn Marino       /* The following test is necessary because in the rounding to the
1064a238c70SJohn Marino        * nearest mode, mpfr_underflow always rounds away from 0. In
1074a238c70SJohn Marino        * this rounding mode, we need to round to 0 if:
1084a238c70SJohn Marino        *   _ |x| < 2^(emin-2), or
1094a238c70SJohn Marino        *   _ |x| = 2^(emin-2) and the absolute value of the exact
1104a238c70SJohn Marino        *     result is <= 2^(emin-2). */
1114a238c70SJohn Marino       if (rnd == MPFR_RNDN && (e+1 < __gmpfr_emin || mpfr_powerof2_raw(y)))
1124a238c70SJohn Marino         rnd = MPFR_RNDZ;
1134a238c70SJohn Marino       return mpfr_underflow (x, rnd, MPFR_SIGN_POS);
1144a238c70SJohn Marino     }
1154a238c70SJohn Marino   if (MPFR_UNLIKELY(e > __gmpfr_emax))
1164a238c70SJohn Marino     return mpfr_overflow (x, rnd, MPFR_SIGN_POS);
1174a238c70SJohn Marino   MPFR_SET_EXP (y, e);
1184a238c70SJohn Marino 
1194a238c70SJohn Marino   /* Final: set x to y (rounding if necessary) */
1204a238c70SJohn Marino   return mpfr_set (x, y, rnd);
1214a238c70SJohn Marino }
1224a238c70SJohn Marino 
1234a238c70SJohn Marino #endif
124