1/* mpf_div_ui -- Divide a float with an unsigned integer. 2 3Copyright 1993, 1994, 1996, 2000-2002, 2004, 2005, 2012 Free Software 4Foundation, Inc. 5 6This file is part of the GNU MP Library. 7 8The GNU MP Library is free software; you can redistribute it and/or modify 9it under the terms of either: 10 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. 14 15or 16 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. 20 21or both in parallel, as here. 22 23The GNU MP Library is distributed in the hope that it will be useful, but 24WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 25or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 26for more details. 27 28You should have received copies of the GNU General Public License and the 29GNU Lesser General Public License along with the GNU MP Library. If not, 30see https://www.gnu.org/licenses/. */ 31 32#include "gmp-impl.h" 33#include "longlong.h" 34 35void 36mpf_div_ui (mpf_ptr r, mpf_srcptr u, unsigned long int v) 37{ 38 mp_srcptr up; 39 mp_ptr rp, tp, rtp; 40 mp_size_t usize; 41 mp_size_t rsize, tsize; 42 mp_size_t sign_quotient; 43 mp_size_t prec; 44 mp_limb_t q_limb; 45 mp_exp_t rexp; 46 TMP_DECL; 47 48#if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */ 49 if (v > GMP_NUMB_MAX) 50 { 51 mpf_t vf; 52 mp_limb_t vl[2]; 53 SIZ(vf) = 2; 54 EXP(vf) = 2; 55 PTR(vf) = vl; 56 vl[0] = v & GMP_NUMB_MASK; 57 vl[1] = v >> GMP_NUMB_BITS; 58 mpf_div (r, u, vf); 59 return; 60 } 61#endif 62 63 if (UNLIKELY (v == 0)) 64 DIVIDE_BY_ZERO; 65 66 usize = u->_mp_size; 67 68 if (usize == 0) 69 { 70 r->_mp_size = 0; 71 r->_mp_exp = 0; 72 return; 73 } 74 75 sign_quotient = usize; 76 usize = ABS (usize); 77 prec = r->_mp_prec; 78 79 TMP_MARK; 80 81 rp = r->_mp_d; 82 up = u->_mp_d; 83 84 tsize = 1 + prec; 85 tp = TMP_ALLOC_LIMBS (tsize + 1); 86 87 if (usize > tsize) 88 { 89 up += usize - tsize; 90 usize = tsize; 91 rtp = tp; 92 } 93 else 94 { 95 MPN_ZERO (tp, tsize - usize); 96 rtp = tp + (tsize - usize); 97 } 98 99 /* Move the dividend to the remainder. */ 100 MPN_COPY (rtp, up, usize); 101 102 mpn_divmod_1 (rp, tp, tsize, (mp_limb_t) v); 103 q_limb = rp[tsize - 1]; 104 105 rsize = tsize - (q_limb == 0); 106 rexp = u->_mp_exp - (q_limb == 0); 107 r->_mp_size = sign_quotient >= 0 ? rsize : -rsize; 108 r->_mp_exp = rexp; 109 TMP_FREE; 110} 111