1
/* mpfr_check -- Check if a floating-point number has not been corrupted.
3
Copyright 2003, 2004 Free Software Foundation, Inc.
5
This file is part of the MPFR Library.
7
The MPFR Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
12
The MPFR Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with the MPFR Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
20
MA 02110-1301, USA. */
22
#include "mpfr-impl.h"
25
* Check if x is a valid mpfr_t initializes by mpfr_init
26
* Returns 0 if isn't valid
29
mpfr_check (mpfr_srcptr x)
33
volatile mp_limb_t *xm;
37
if (MPFR_SIGN(x) != MPFR_SIGN_POS && MPFR_SIGN(x) != MPFR_SIGN_NEG)
40
if ( (MPFR_PREC(x) < MPFR_PREC_MIN) || (MPFR_PREC(x) > MPFR_PREC_MAX))
46
/* Check size of mantissa */
47
s = MPFR_GET_ALLOC_SIZE(x);
48
if (s<=0 || s > MP_SIZE_T_MAX ||
49
MPFR_PREC(x) > ((mp_prec_t)s*BITS_PER_MP_LIMB))
51
/* Acces all the mp_limb of the mantissa: may do a seg fault */
52
for(i = 0 ; i < s ; i++)
54
/* Check if it isn't singular*/
55
if (MPFR_IS_PURE_FP(x))
57
/* Check first mp_limb of mantissa (Must start with a 1 bit) */
58
if ( ((xm[MPFR_LIMB_SIZE(x)-1])>>(BITS_PER_MP_LIMB-1)) == 0)
60
/* Check last mp_limb of mantissa */
61
rw = (MPFR_PREC(x) % BITS_PER_MP_LIMB);
64
tmp = MPFR_LIMB_MASK (BITS_PER_MP_LIMB - rw);
65
if ((xm[0] & tmp) != 0)
68
/* Check exponent range */
69
if ((MPFR_EXP (x) < __gmpfr_emin) || (MPFR_EXP (x) > __gmpfr_emax))
74
/* Singular value is zero, inf or nan */
75
MPFR_ASSERTD(MPFR_IS_ZERO(x) || MPFR_IS_NAN(x) || MPFR_IS_INF(x));