1 /* mpfr_check -- Check if a floating-point number has not been corrupted.
3 Copyright 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Caramel 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 3 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.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. */
23 #include "mpfr-impl.h"
26 * Check if x is a valid mpfr_t initializes by mpfr_init
27 * Returns 0 if isn't valid
30 mpfr_check (mpfr_srcptr x)
34 volatile mp_limb_t *xm;
38 if (MPFR_SIGN(x) != MPFR_SIGN_POS && MPFR_SIGN(x) != MPFR_SIGN_NEG)
41 if ( (MPFR_PREC(x) < MPFR_PREC_MIN) || (MPFR_PREC(x) > MPFR_PREC_MAX))
47 /* Check size of mantissa */
48 s = MPFR_GET_ALLOC_SIZE(x);
49 if (s<=0 || s > MP_SIZE_T_MAX ||
50 MPFR_PREC(x) > ((mpfr_prec_t)s*GMP_NUMB_BITS))
52 /* Acces all the mp_limb of the mantissa: may do a seg fault */
53 for(i = 0 ; i < s ; i++)
55 /* Check if it isn't singular*/
56 if (! MPFR_IS_SINGULAR (x))
58 /* Check first mp_limb of mantissa (Must start with a 1 bit) */
59 if ( ((xm[MPFR_LIMB_SIZE(x)-1])>>(GMP_NUMB_BITS-1)) == 0)
61 /* Check last mp_limb of mantissa */
62 rw = (MPFR_PREC(x) % GMP_NUMB_BITS);
65 tmp = MPFR_LIMB_MASK (GMP_NUMB_BITS - rw);
66 if ((xm[0] & tmp) != 0)
69 /* Check exponent range */
70 if ((MPFR_EXP (x) < __gmpfr_emin) || (MPFR_EXP (x) > __gmpfr_emax))
75 /* Singular value is zero, inf or nan */
76 MPFR_ASSERTD(MPFR_IS_ZERO(x) || MPFR_IS_NAN(x) || MPFR_IS_INF(x));