liblvm: Raise WARNS to 1.
[dragonfly.git] / contrib / mpfr / check.c
1 /* mpfr_check -- Check if a floating-point number has not been corrupted.
2
3 Copyright 2003, 2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao projects, INRIA.
5
6 This file is part of the GNU MPFR Library.
7
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.
12
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.
17
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. */
22
23 #include "mpfr-impl.h"
24
25 /*
26  * Check if x is a valid mpfr_t initializes by mpfr_init
27  * Returns 0 if isn't valid
28  */
29 int
30 mpfr_check (mpfr_srcptr x)
31 {
32   mp_size_t s, i;
33   mp_limb_t tmp;
34   volatile mp_limb_t *xm;
35   int rw;
36
37   /* Check Sign */
38   if (MPFR_SIGN(x) != MPFR_SIGN_POS && MPFR_SIGN(x) != MPFR_SIGN_NEG)
39     return 0;
40   /* Check Precision */
41   if ( (MPFR_PREC(x) < MPFR_PREC_MIN) || (MPFR_PREC(x) > MPFR_PREC_MAX))
42     return 0;
43   /* Check Mantissa */
44   xm = MPFR_MANT(x);
45   if (!xm)
46     return 0;
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) > ((mp_prec_t)s*BITS_PER_MP_LIMB))
51     return 0;
52   /* Acces all the mp_limb of the mantissa: may do a seg fault */
53   for(i = 0 ; i < s ; i++)
54     tmp = xm[i];
55   /* Check if it isn't singular*/
56   if (MPFR_IS_PURE_FP(x))
57     {
58       /* Check first mp_limb of mantissa (Must start with a 1 bit) */
59       if ( ((xm[MPFR_LIMB_SIZE(x)-1])>>(BITS_PER_MP_LIMB-1)) == 0)
60         return 0;
61       /* Check last mp_limb of mantissa */
62       rw = (MPFR_PREC(x) % BITS_PER_MP_LIMB);
63       if (rw != 0)
64         {
65           tmp = MPFR_LIMB_MASK (BITS_PER_MP_LIMB - rw);
66           if ((xm[0] & tmp) != 0)
67             return 0;
68         }
69       /* Check exponent range */
70       if ((MPFR_EXP (x) < __gmpfr_emin) || (MPFR_EXP (x) > __gmpfr_emax))
71         return 0;
72     }
73   else
74     {
75       /* Singular value is zero, inf or nan */
76       MPFR_ASSERTD(MPFR_IS_ZERO(x) || MPFR_IS_NAN(x) || MPFR_IS_INF(x));
77     }
78   return 1;
79 }
80