~ubuntu-branches/debian/sid/genius/sid

« back to all changes in this revision

Viewing changes to mpfr/check.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-08-21 12:57:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060821125745-sl9ks8v7fq324bdf
Tags: upstream-0.7.6.1
ImportĀ upstreamĀ versionĀ 0.7.6.1

Show diffs side-by-side

added added

removed removed

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