Annotation of OpenXM_contrib/gmp/mpf/ui_div.c, Revision 1.1.1.2
1.1 maekawa 1: /* mpf_ui_div -- Divide an unsigned integer with a float.
2:
1.1.1.2 ! maekawa 3: Copyright (C) 1993, 1994, 1995, 1996, 2000 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: void
27: #if __STDC__
28: mpf_ui_div (mpf_ptr r, unsigned long int u, mpf_srcptr v)
29: #else
30: mpf_ui_div (r, u, v)
31: mpf_ptr r;
32: unsigned long int u;
33: mpf_srcptr v;
34: #endif
35: {
36: mp_srcptr vp;
37: mp_ptr rp, tp;
38: mp_size_t vsize;
39: mp_size_t rsize, tsize;
40: mp_size_t sign_quotient;
41: mp_size_t prec;
42: unsigned normalization_steps;
43: mp_limb_t q_limb;
44: mp_exp_t rexp;
45: TMP_DECL (marker);
46:
47: vsize = v->_mp_size;
48: sign_quotient = vsize;
49: vsize = ABS (vsize);
50: prec = r->_mp_prec;
51:
52: if (vsize == 0)
1.1.1.2 ! maekawa 53: DIVIDE_BY_ZERO;
! 54:
1.1 maekawa 55: if (u == 0)
56: {
57: r->_mp_size = 0;
58: r->_mp_exp = 0;
59: return;
60: }
61:
62: TMP_MARK (marker);
63: rexp = 1 - v->_mp_exp;
64:
65: rp = r->_mp_d;
66: vp = v->_mp_d;
67:
68: if (vsize > prec)
69: {
70: vp += vsize - prec;
71: vsize = prec;
72: }
73:
74: tsize = vsize + prec;
75: tp = (mp_ptr) TMP_ALLOC ((tsize + 1) * BYTES_PER_MP_LIMB);
76: MPN_ZERO (tp, tsize);
77:
78: count_leading_zeros (normalization_steps, vp[vsize - 1]);
79:
80: /* Normalize the divisor and the dividend. */
81: if (normalization_steps != 0)
82: {
83: mp_ptr tmp;
84: mp_limb_t dividend_high, dividend_low;
85:
86: /* Shift up the divisor setting the most significant bit of
87: the most significant limb. Use temporary storage not to clobber
88: the original contents of the divisor. */
89: tmp = (mp_ptr) TMP_ALLOC (vsize * BYTES_PER_MP_LIMB);
90: mpn_lshift (tmp, vp, vsize, normalization_steps);
91: vp = tmp;
92:
93: /* Shift up the dividend, possibly introducing a new most
94: significant word. */
95: dividend_high = (mp_limb_t) u >> (BITS_PER_MP_LIMB - normalization_steps);
96: dividend_low = (mp_limb_t) u << normalization_steps;
97:
98: tp[tsize - 1] = dividend_low;
99: if (dividend_high != 0)
100: {
101: tp[tsize] = dividend_high;
102: tsize++;
103: rexp++;
104: }
105: }
106: else
107: {
108: /* The divisor is already normalized, as required.
109: Copy it to temporary space if it overlaps with the quotient. */
110: if (vp - rp <= tsize - vsize)
111: {
112: mp_ptr tmp = (mp_ptr) TMP_ALLOC (vsize * BYTES_PER_MP_LIMB);
113: MPN_COPY (tmp, vp, vsize);
114: vp = (mp_srcptr) tmp;
115: }
116:
117: tp[tsize - 1] = u;
118: }
119:
120: q_limb = mpn_divmod (rp, tp, tsize, vp, vsize);
121: rsize = tsize - vsize;
122: if (q_limb)
123: {
124: rp[rsize] = q_limb;
125: rsize++;
126: rexp++;
127: }
128:
129: r->_mp_size = sign_quotient >= 0 ? rsize : -rsize;
130: r->_mp_exp = rexp;
131: TMP_FREE (marker);
132: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>