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