~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/mpz/get_d.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* double mpz_get_d (mpz_t src) -- Return the double approximation to SRC.
 
2
 
 
3
Copyright 1996, 1997, 2000, 2001 Free Software Foundation, Inc.
 
4
 
 
5
This file is part of the GNU MP Library.
 
6
 
 
7
The GNU MP 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 GNU MP 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 GNU MP Library; see the file COPYING.LIB.  If not, write to
 
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
20
MA 02111-1307, USA. */
 
21
 
 
22
#include "gmp.h"
 
23
#include "gmp-impl.h"
 
24
#include "longlong.h"
 
25
 
 
26
 
 
27
/* FIXME: Would prefer to inline this on all compilers, not just those with
 
28
   "inline".  */
 
29
static inline int
 
30
mpn_zero_p (mp_srcptr p, mp_size_t n)
 
31
{
 
32
  mp_size_t i;
 
33
 
 
34
  for (i = 0; i < n; i++)
 
35
    if (p[i] != 0)
 
36
      return 0;
 
37
 
 
38
  return 1;
 
39
}
 
40
 
 
41
 
 
42
double
 
43
mpz_get_d (mpz_srcptr src)
 
44
{
 
45
  double res;
 
46
  mp_size_t size;
 
47
  int negative;
 
48
  mp_ptr qp;
 
49
  mp_limb_t hz, lz;
 
50
  int cnt;
 
51
 
 
52
  size = SIZ(src);
 
53
  if (size == 0)
 
54
    return 0.0;
 
55
 
 
56
  negative = size < 0;
 
57
  size = ABS (size);
 
58
  qp = PTR(src);
 
59
 
 
60
  if (size == 1)
 
61
    {
 
62
      res = qp[size - 1];
 
63
    }
 
64
  else if (size == 2)
 
65
    {
 
66
      res = MP_BASE_AS_DOUBLE * qp[size - 1] + qp[size - 2];
 
67
    }
 
68
  else
 
69
    {
 
70
      count_leading_zeros (cnt, qp[size - 1]);
 
71
 
 
72
#if BITS_PER_MP_LIMB == 32
 
73
      if (cnt == 0)
 
74
        {
 
75
          hz = qp[size - 1];
 
76
          lz = qp[size - 2];
 
77
        }
 
78
      else
 
79
        {
 
80
          hz = (qp[size - 1] << cnt) | (qp[size - 2] >> BITS_PER_MP_LIMB - cnt);
 
81
          lz = (qp[size - 2] << cnt) | (qp[size - 3] >> BITS_PER_MP_LIMB - cnt);
 
82
        }
 
83
#if _GMP_IEEE_FLOATS
 
84
      /* Take bits from less significant limbs, but only if they may affect
 
85
         the result.  */
 
86
      if ((lz & 0x7ff) == 0x400)
 
87
        {
 
88
          if (cnt != 0)
 
89
            lz += ((qp[size - 3] << cnt) != 0 || ! mpn_zero_p (qp, size - 3));
 
90
          else
 
91
            lz += (! mpn_zero_p (qp, size - 2));
 
92
        }
 
93
#endif
 
94
      res = MP_BASE_AS_DOUBLE * hz + lz;
 
95
      res = __gmp_scale2 (res, (size - 2) * BITS_PER_MP_LIMB - cnt);
 
96
#endif
 
97
#if BITS_PER_MP_LIMB == 64
 
98
      if (cnt == 0)
 
99
        hz = qp[size - 1];
 
100
      else
 
101
        hz = (qp[size - 1] << cnt) | (qp[size - 2] >> BITS_PER_MP_LIMB - cnt);
 
102
#if _GMP_IEEE_FLOATS
 
103
      if ((hz & 0x7ff) == 0x400)
 
104
        {
 
105
          if (cnt != 0)
 
106
            hz += ((qp[size - 2] << cnt) != 0 || ! mpn_zero_p (qp, size - 2));
 
107
          else
 
108
            hz += (! mpn_zero_p (qp, size - 1));
 
109
        }
 
110
#endif
 
111
      res = hz;
 
112
      res = __gmp_scale2 (res, (size - 1) * BITS_PER_MP_LIMB - cnt);
 
113
#endif
 
114
    }
 
115
 
 
116
  return negative ? -res : res;
 
117
}