1 | /* mpf_div_ui -- Divide a float with an unsigned integer. |
---|
2 | |
---|
3 | Copyright 1993, 1994, 1996, 2000, 2001, 2002 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 | void |
---|
27 | mpf_div_ui (mpf_ptr r, mpf_srcptr u, unsigned long int v) |
---|
28 | { |
---|
29 | mp_srcptr up; |
---|
30 | mp_ptr rp, tp, rtp; |
---|
31 | mp_size_t usize; |
---|
32 | mp_size_t rsize, tsize; |
---|
33 | mp_size_t sign_quotient; |
---|
34 | mp_size_t prec; |
---|
35 | mp_limb_t q_limb; |
---|
36 | mp_exp_t rexp; |
---|
37 | TMP_DECL (marker); |
---|
38 | |
---|
39 | #if GMP_NAIL_BITS != 0 |
---|
40 | if (v > GMP_NUMB_MAX) |
---|
41 | { |
---|
42 | mpf_t vf; |
---|
43 | mp_limb_t vl[2]; |
---|
44 | SIZ(vf) = 2; |
---|
45 | EXP(vf) = 2; |
---|
46 | PTR(vf) = vl; |
---|
47 | vl[0] = v & GMP_NUMB_MASK; |
---|
48 | vl[1] = v >> GMP_NUMB_BITS; |
---|
49 | mpf_div (r, u, vf); |
---|
50 | return; |
---|
51 | } |
---|
52 | #endif |
---|
53 | |
---|
54 | usize = u->_mp_size; |
---|
55 | sign_quotient = usize; |
---|
56 | usize = ABS (usize); |
---|
57 | prec = r->_mp_prec; |
---|
58 | |
---|
59 | if (v == 0) |
---|
60 | DIVIDE_BY_ZERO; |
---|
61 | |
---|
62 | if (usize == 0) |
---|
63 | { |
---|
64 | r->_mp_size = 0; |
---|
65 | r->_mp_exp = 0; |
---|
66 | return; |
---|
67 | } |
---|
68 | |
---|
69 | TMP_MARK (marker); |
---|
70 | |
---|
71 | rp = r->_mp_d; |
---|
72 | up = u->_mp_d; |
---|
73 | |
---|
74 | tsize = 1 + prec; |
---|
75 | tp = (mp_ptr) TMP_ALLOC ((tsize + 1) * BYTES_PER_MP_LIMB); |
---|
76 | |
---|
77 | if (usize > tsize) |
---|
78 | { |
---|
79 | up += usize - tsize; |
---|
80 | usize = tsize; |
---|
81 | rtp = tp; |
---|
82 | } |
---|
83 | else |
---|
84 | { |
---|
85 | MPN_ZERO (tp, tsize - usize); |
---|
86 | rtp = tp + (tsize - usize); |
---|
87 | } |
---|
88 | |
---|
89 | /* Move the dividend to the remainder. */ |
---|
90 | MPN_COPY (rtp, up, usize); |
---|
91 | |
---|
92 | mpn_divmod_1 (rp, tp, tsize, (mp_limb_t) v); |
---|
93 | q_limb = rp[tsize - 1]; |
---|
94 | |
---|
95 | rsize = tsize - (q_limb == 0); |
---|
96 | rexp = u->_mp_exp - (q_limb == 0); |
---|
97 | r->_mp_size = sign_quotient >= 0 ? rsize : -rsize; |
---|
98 | r->_mp_exp = rexp; |
---|
99 | TMP_FREE (marker); |
---|
100 | } |
---|