xref: /dflybsd-src/contrib/gmp/mpf/div_ui.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* mpf_div_ui -- Divide a float with an unsigned integer.
286d7f5d3SJohn Marino 
386d7f5d3SJohn Marino Copyright 1993, 1994, 1996, 2000, 2001, 2002, 2004, 2005 Free Software
486d7f5d3SJohn Marino Foundation, Inc.
586d7f5d3SJohn Marino 
686d7f5d3SJohn Marino This file is part of the GNU MP Library.
786d7f5d3SJohn Marino 
886d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
986d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1086d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1186d7f5d3SJohn Marino option) any later version.
1286d7f5d3SJohn Marino 
1386d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1486d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1586d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1686d7f5d3SJohn Marino License for more details.
1786d7f5d3SJohn Marino 
1886d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
1986d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
2086d7f5d3SJohn Marino 
2186d7f5d3SJohn Marino #include "gmp.h"
2286d7f5d3SJohn Marino #include "gmp-impl.h"
2386d7f5d3SJohn Marino #include "longlong.h"
2486d7f5d3SJohn Marino 
2586d7f5d3SJohn Marino void
mpf_div_ui(mpf_ptr r,mpf_srcptr u,unsigned long int v)2686d7f5d3SJohn Marino mpf_div_ui (mpf_ptr r, mpf_srcptr u, unsigned long int v)
2786d7f5d3SJohn Marino {
2886d7f5d3SJohn Marino   mp_srcptr up;
2986d7f5d3SJohn Marino   mp_ptr rp, tp, rtp;
3086d7f5d3SJohn Marino   mp_size_t usize;
3186d7f5d3SJohn Marino   mp_size_t rsize, tsize;
3286d7f5d3SJohn Marino   mp_size_t sign_quotient;
3386d7f5d3SJohn Marino   mp_size_t prec;
3486d7f5d3SJohn Marino   mp_limb_t q_limb;
3586d7f5d3SJohn Marino   mp_exp_t rexp;
3686d7f5d3SJohn Marino   TMP_DECL;
3786d7f5d3SJohn Marino 
3886d7f5d3SJohn Marino #if BITS_PER_ULONG > GMP_NUMB_BITS  /* avoid warnings about shift amount */
3986d7f5d3SJohn Marino   if (v > GMP_NUMB_MAX)
4086d7f5d3SJohn Marino     {
4186d7f5d3SJohn Marino       mpf_t vf;
4286d7f5d3SJohn Marino       mp_limb_t vl[2];
4386d7f5d3SJohn Marino       SIZ(vf) = 2;
4486d7f5d3SJohn Marino       EXP(vf) = 2;
4586d7f5d3SJohn Marino       PTR(vf) = vl;
4686d7f5d3SJohn Marino       vl[0] = v & GMP_NUMB_MASK;
4786d7f5d3SJohn Marino       vl[1] = v >> GMP_NUMB_BITS;
4886d7f5d3SJohn Marino       mpf_div (r, u, vf);
4986d7f5d3SJohn Marino       return;
5086d7f5d3SJohn Marino     }
5186d7f5d3SJohn Marino #endif
5286d7f5d3SJohn Marino 
5386d7f5d3SJohn Marino   usize = u->_mp_size;
5486d7f5d3SJohn Marino   sign_quotient = usize;
5586d7f5d3SJohn Marino   usize = ABS (usize);
5686d7f5d3SJohn Marino   prec = r->_mp_prec;
5786d7f5d3SJohn Marino 
5886d7f5d3SJohn Marino   if (v == 0)
5986d7f5d3SJohn Marino     DIVIDE_BY_ZERO;
6086d7f5d3SJohn Marino 
6186d7f5d3SJohn Marino   if (usize == 0)
6286d7f5d3SJohn Marino     {
6386d7f5d3SJohn Marino       r->_mp_size = 0;
6486d7f5d3SJohn Marino       r->_mp_exp = 0;
6586d7f5d3SJohn Marino       return;
6686d7f5d3SJohn Marino     }
6786d7f5d3SJohn Marino 
6886d7f5d3SJohn Marino   TMP_MARK;
6986d7f5d3SJohn Marino 
7086d7f5d3SJohn Marino   rp = r->_mp_d;
7186d7f5d3SJohn Marino   up = u->_mp_d;
7286d7f5d3SJohn Marino 
7386d7f5d3SJohn Marino   tsize = 1 + prec;
7486d7f5d3SJohn Marino   tp = TMP_ALLOC_LIMBS (tsize + 1);
7586d7f5d3SJohn Marino 
7686d7f5d3SJohn Marino   if (usize > tsize)
7786d7f5d3SJohn Marino     {
7886d7f5d3SJohn Marino       up += usize - tsize;
7986d7f5d3SJohn Marino       usize = tsize;
8086d7f5d3SJohn Marino       rtp = tp;
8186d7f5d3SJohn Marino     }
8286d7f5d3SJohn Marino   else
8386d7f5d3SJohn Marino     {
8486d7f5d3SJohn Marino       MPN_ZERO (tp, tsize - usize);
8586d7f5d3SJohn Marino       rtp = tp + (tsize - usize);
8686d7f5d3SJohn Marino     }
8786d7f5d3SJohn Marino 
8886d7f5d3SJohn Marino   /* Move the dividend to the remainder.  */
8986d7f5d3SJohn Marino   MPN_COPY (rtp, up, usize);
9086d7f5d3SJohn Marino 
9186d7f5d3SJohn Marino   mpn_divmod_1 (rp, tp, tsize, (mp_limb_t) v);
9286d7f5d3SJohn Marino   q_limb = rp[tsize - 1];
9386d7f5d3SJohn Marino 
9486d7f5d3SJohn Marino   rsize = tsize - (q_limb == 0);
9586d7f5d3SJohn Marino   rexp = u->_mp_exp - (q_limb == 0);
9686d7f5d3SJohn Marino   r->_mp_size = sign_quotient >= 0 ? rsize : -rsize;
9786d7f5d3SJohn Marino   r->_mp_exp = rexp;
9886d7f5d3SJohn Marino   TMP_FREE;
9986d7f5d3SJohn Marino }
100