14a238c70SJohn Marino /* mpfr_init2 -- initialize a floating-point number with given precision
24a238c70SJohn Marino
3*ab6d115fSJohn Marino Copyright 2001, 2002, 2003, 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 #include "mpfr-impl.h"
244a238c70SJohn Marino
254a238c70SJohn Marino void
mpfr_init2(mpfr_ptr x,mpfr_prec_t p)264a238c70SJohn Marino mpfr_init2 (mpfr_ptr x, mpfr_prec_t p)
274a238c70SJohn Marino {
284a238c70SJohn Marino mp_size_t xsize;
294a238c70SJohn Marino mpfr_limb_ptr tmp;
304a238c70SJohn Marino
314a238c70SJohn Marino /* Check if we can represent the number of limbs
324a238c70SJohn Marino * associated to the maximum of mpfr_prec_t*/
334a238c70SJohn Marino MPFR_ASSERTN( MP_SIZE_T_MAX >= (MPFR_PREC_MAX/BYTES_PER_MP_LIMB) );
344a238c70SJohn Marino
354a238c70SJohn Marino /* Check for correct GMP_NUMB_BITS and BYTES_PER_MP_LIMB */
364a238c70SJohn Marino MPFR_ASSERTN( GMP_NUMB_BITS == BYTES_PER_MP_LIMB * CHAR_BIT
374a238c70SJohn Marino && sizeof(mp_limb_t) == BYTES_PER_MP_LIMB );
384a238c70SJohn Marino
394a238c70SJohn Marino MPFR_ASSERTN (mp_bits_per_limb == GMP_NUMB_BITS);
404a238c70SJohn Marino
414a238c70SJohn Marino /* Check for correct EXP NAN, ZERO & INF in both mpfr.h and mpfr-impl.h */
424a238c70SJohn Marino MPFR_ASSERTN( __MPFR_EXP_NAN == MPFR_EXP_NAN );
434a238c70SJohn Marino MPFR_ASSERTN( __MPFR_EXP_ZERO == MPFR_EXP_ZERO );
444a238c70SJohn Marino MPFR_ASSERTN( __MPFR_EXP_INF == MPFR_EXP_INF );
454a238c70SJohn Marino
464a238c70SJohn Marino MPFR_ASSERTN( MPFR_EMAX_MAX <= (MPFR_EXP_MAX >> 1) );
474a238c70SJohn Marino MPFR_ASSERTN( MPFR_EMIN_MIN >= -(MPFR_EXP_MAX >> 1) );
484a238c70SJohn Marino
494a238c70SJohn Marino /* p=1 is not allowed since the rounding to nearest even rule requires at
504a238c70SJohn Marino least two bits of mantissa: the neighbours of 3/2 are 1*2^0 and 1*2^1,
514a238c70SJohn Marino which both have an odd mantissa */
524a238c70SJohn Marino MPFR_ASSERTN(p >= MPFR_PREC_MIN && p <= MPFR_PREC_MAX);
534a238c70SJohn Marino
54*ab6d115fSJohn Marino xsize = MPFR_PREC2LIMBS (p);
554a238c70SJohn Marino tmp = (mpfr_limb_ptr) (*__gmp_allocate_func)(MPFR_MALLOC_SIZE(xsize));
564a238c70SJohn Marino
574a238c70SJohn Marino MPFR_PREC(x) = p; /* Set prec */
584a238c70SJohn Marino MPFR_EXP (x) = MPFR_EXP_INVALID; /* make sure that the exp field has a
594a238c70SJohn Marino valid value in the C point of view */
604a238c70SJohn Marino MPFR_SET_POS(x); /* Set a sign */
614a238c70SJohn Marino MPFR_SET_MANT_PTR(x, tmp); /* Set Mantissa ptr */
624a238c70SJohn Marino MPFR_SET_ALLOC_SIZE(x, xsize); /* Fix alloc size of Mantissa */
634a238c70SJohn Marino MPFR_SET_NAN(x); /* initializes to NaN */
644a238c70SJohn Marino }
654a238c70SJohn Marino
664a238c70SJohn Marino #ifdef MPFR_USE_OWN_MPFR_TMP_ALLOC
674a238c70SJohn Marino static unsigned char mpfr_stack_tab[8000000];
684a238c70SJohn Marino unsigned char *mpfr_stack = mpfr_stack_tab;
694a238c70SJohn Marino #endif
70