beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpf / div_ui.c
blob9be7e680bef68441d8b26cc39ff6309d9d5006ee
1 /* mpf_div_ui -- Divide a float with an unsigned integer.
3 Copyright 1993, 1994, 1996, 2000-2002, 2004, 2005, 2012 Free Software
4 Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of either:
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
17 * the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any
19 later version.
21 or both in parallel, as here.
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 for more details.
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library. If not,
30 see https://www.gnu.org/licenses/. */
32 #include "gmp.h"
33 #include "gmp-impl.h"
34 #include "longlong.h"
36 void
37 mpf_div_ui (mpf_ptr r, mpf_srcptr u, unsigned long int v)
39 mp_srcptr up;
40 mp_ptr rp, tp, rtp;
41 mp_size_t usize;
42 mp_size_t rsize, tsize;
43 mp_size_t sign_quotient;
44 mp_size_t prec;
45 mp_limb_t q_limb;
46 mp_exp_t rexp;
47 TMP_DECL;
49 #if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */
50 if (v > GMP_NUMB_MAX)
52 mpf_t vf;
53 mp_limb_t vl[2];
54 SIZ(vf) = 2;
55 EXP(vf) = 2;
56 PTR(vf) = vl;
57 vl[0] = v & GMP_NUMB_MASK;
58 vl[1] = v >> GMP_NUMB_BITS;
59 mpf_div (r, u, vf);
60 return;
62 #endif
64 if (UNLIKELY (v == 0))
65 DIVIDE_BY_ZERO;
67 usize = u->_mp_size;
69 if (usize == 0)
71 r->_mp_size = 0;
72 r->_mp_exp = 0;
73 return;
76 sign_quotient = usize;
77 usize = ABS (usize);
78 prec = r->_mp_prec;
80 TMP_MARK;
82 rp = r->_mp_d;
83 up = u->_mp_d;
85 tsize = 1 + prec;
86 tp = TMP_ALLOC_LIMBS (tsize + 1);
88 if (usize > tsize)
90 up += usize - tsize;
91 usize = tsize;
92 rtp = tp;
94 else
96 MPN_ZERO (tp, tsize - usize);
97 rtp = tp + (tsize - usize);
100 /* Move the dividend to the remainder. */
101 MPN_COPY (rtp, up, usize);
103 mpn_divmod_1 (rp, tp, tsize, (mp_limb_t) v);
104 q_limb = rp[tsize - 1];
106 rsize = tsize - (q_limb == 0);
107 rexp = u->_mp_exp - (q_limb == 0);
108 r->_mp_size = sign_quotient >= 0 ? rsize : -rsize;
109 r->_mp_exp = rexp;
110 TMP_FREE;