Annotation of OpenXM_contrib/gmp/mpq/get_d.c, Revision 1.1.1.3
1.1 maekawa 1: /* double mpq_get_d (mpq_t src) -- Return the double approximation to SRC.
2:
1.1.1.3 ! ohara 3: Copyright 1995, 1996, 2001, 2002 Free Software Foundation, Inc.
1.1 maekawa 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
1.1.1.2 maekawa 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
1.1 maekawa 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
1.1.1.2 maekawa 14: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1.1 maekawa 15: License for more details.
16:
1.1.1.2 maekawa 17: You should have received a copy of the GNU Lesser General Public License
1.1 maekawa 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: /* Algorithm:
27: 1. Develop >= n bits of src.num / src.den, where n is the number of bits
28: in a double. This (partial) division will use all bits from the
29: denominator.
30: 2. Use the remainder to determine how to round the result.
31: 3. Assign the integral result to a temporary double.
32: 4. Scale the temporary double, and return the result.
33:
34: An alternative algorithm, that would be faster:
35: 0. Let n be somewhat larger than the number of significant bits in a double.
36: 1. Extract the most significant n bits of the denominator, and an equal
37: number of bits from the numerator.
38: 2. Interpret the extracted numbers as integers, call them a and b
39: respectively, and develop n bits of the fractions ((a + 1) / b) and
40: (a / (b + 1)) using mpn_divrem.
41: 3. If the computed values are identical UP TO THE POSITION WE CARE ABOUT,
42: we are done. If they are different, repeat the algorithm from step 1,
43: but first let n = n * 2.
44: 4. If we end up using all bits from the numerator and denominator, fall
45: back to the first algorithm above.
46: 5. Just to make life harder, The computation of a + 1 and b + 1 above
47: might give carry-out... Needs special handling. It might work to
48: subtract 1 in both cases instead.
49: */
50:
51: double
52: mpq_get_d (const MP_RAT *src)
53: {
54: mp_ptr np, dp;
55: mp_ptr rp;
56: mp_size_t nsize = src->_mp_num._mp_size;
57: mp_size_t dsize = src->_mp_den._mp_size;
58: mp_size_t qsize, rsize;
59: mp_size_t sign_quotient = nsize ^ dsize;
60: mp_limb_t qlimb;
61: #define N_QLIMBS (1 + (sizeof (double) + BYTES_PER_MP_LIMB-1) / BYTES_PER_MP_LIMB)
1.1.1.2 maekawa 62: mp_limb_t qarr[N_QLIMBS + 1];
63: mp_ptr qp = qarr;
1.1 maekawa 64: TMP_DECL (marker);
65:
66: if (nsize == 0)
67: return 0.0;
68:
69: TMP_MARK (marker);
70: nsize = ABS (nsize);
71: dsize = ABS (dsize);
72: np = src->_mp_num._mp_d;
73: dp = src->_mp_den._mp_d;
74:
75: rsize = dsize + N_QLIMBS;
76: rp = (mp_ptr) TMP_ALLOC ((rsize + 1) * BYTES_PER_MP_LIMB);
77:
78: /* Normalize the denominator, i.e. make its most significant bit set by
79: shifting it NORMALIZATION_STEPS bits to the left. Also shift the
80: numerator the same number of steps (to keep the quotient the same!). */
1.1.1.3 ! ohara 81: if ((dp[dsize - 1] & GMP_NUMB_HIGHBIT) == 0)
1.1 maekawa 82: {
83: mp_ptr tp;
84: mp_limb_t nlimb;
1.1.1.3 ! ohara 85: unsigned normalization_steps;
! 86:
! 87: count_leading_zeros (normalization_steps, dp[dsize - 1]);
! 88: normalization_steps -= GMP_NAIL_BITS;
1.1 maekawa 89:
90: /* Shift up the denominator setting the most significant bit of
91: the most significant limb. Use temporary storage not to clobber
92: the original contents of the denominator. */
93: tp = (mp_ptr) TMP_ALLOC (dsize * BYTES_PER_MP_LIMB);
94: mpn_lshift (tp, dp, dsize, normalization_steps);
95: dp = tp;
96:
97: if (rsize > nsize)
98: {
99: MPN_ZERO (rp, rsize - nsize);
100: nlimb = mpn_lshift (rp + (rsize - nsize),
101: np, nsize, normalization_steps);
102: }
103: else
104: {
105: nlimb = mpn_lshift (rp, np + (nsize - rsize),
106: rsize, normalization_steps);
107: }
108: if (nlimb != 0)
109: {
110: rp[rsize] = nlimb;
111: rsize++;
112: }
113: }
114: else
115: {
116: if (rsize > nsize)
117: {
118: MPN_ZERO (rp, rsize - nsize);
119: MPN_COPY (rp + (rsize - nsize), np, nsize);
120: }
121: else
122: {
123: MPN_COPY (rp, np + (nsize - rsize), rsize);
124: }
125: }
126:
127: qlimb = mpn_divmod (qp, rp, rsize, dp, dsize);
128: qsize = rsize - dsize;
129: if (qlimb)
130: {
131: qp[qsize] = qlimb;
132: qsize++;
133: }
134:
135: {
136: double res;
137: mp_size_t i;
1.1.1.3 ! ohara 138: mp_size_t scale = nsize - dsize - N_QLIMBS;
1.1.1.2 maekawa 139:
140: #if defined (__vax__)
141: /* Ignore excess quotient limbs. This is necessary on a vax
142: with its small double exponent, since we'd otherwise get
143: exponent overflow while forming RES. */
144: if (qsize > N_QLIMBS)
145: {
146: qp += qsize - N_QLIMBS;
147: scale += qsize - N_QLIMBS;
148: qsize = N_QLIMBS;
149: }
150: #endif
1.1 maekawa 151:
152: res = qp[qsize - 1];
153: for (i = qsize - 2; i >= 0; i--)
154: res = res * MP_BASE_AS_DOUBLE + qp[i];
155:
1.1.1.3 ! ohara 156: res = __gmp_scale2 (res, GMP_NUMB_BITS * scale);
1.1 maekawa 157:
158: TMP_FREE (marker);
159: return sign_quotient >= 0 ? res : -res;
160: }
161: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>