github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpf/ui_div.c (about) 1 /* mpf_ui_div -- Divide an unsigned integer with a float. 2 3 Copyright 1993-1996, 2000-2002, 2004, 2005, 2012 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 either: 9 10 * the GNU Lesser General Public License as published by the Free 11 Software Foundation; either version 3 of the License, or (at your 12 option) any later version. 13 14 or 15 16 * the GNU General Public License as published by the Free Software 17 Foundation; either version 2 of the License, or (at your option) any 18 later version. 19 20 or both in parallel, as here. 21 22 The GNU MP Library is distributed in the hope that it will be useful, but 23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 25 for more details. 26 27 You should have received copies of the GNU General Public License and the 28 GNU Lesser General Public License along with the GNU MP Library. If not, 29 see https://www.gnu.org/licenses/. */ 30 31 #include <stdio.h> /* for NULL */ 32 #include "gmp.h" 33 #include "gmp-impl.h" 34 #include "longlong.h" 35 36 37 void 38 mpf_ui_div (mpf_ptr r, unsigned long int u, mpf_srcptr v) 39 { 40 mp_srcptr vp; 41 mp_ptr rp, tp, remp, new_vp; 42 mp_size_t vsize; 43 mp_size_t rsize, prospective_rsize, zeros, tsize, high_zero; 44 mp_size_t sign_quotient; 45 mp_size_t prec; 46 mp_exp_t rexp; 47 TMP_DECL; 48 49 vsize = v->_mp_size; 50 sign_quotient = vsize; 51 52 if (UNLIKELY (vsize == 0)) 53 DIVIDE_BY_ZERO; 54 55 if (UNLIKELY (u == 0)) 56 { 57 r->_mp_size = 0; 58 r->_mp_exp = 0; 59 return; 60 } 61 62 vsize = ABS (vsize); 63 prec = r->_mp_prec; 64 65 TMP_MARK; 66 rexp = 1 - v->_mp_exp + 1; 67 68 rp = r->_mp_d; 69 vp = v->_mp_d; 70 71 prospective_rsize = 1 - vsize + 1; /* quot from using given u,v sizes */ 72 rsize = prec + 1; /* desired quot size */ 73 74 zeros = rsize - prospective_rsize; /* padding u to give rsize */ 75 tsize = 1 + zeros; /* u with zeros */ 76 77 if (WANT_TMP_DEBUG) 78 { 79 /* separate alloc blocks, for malloc debugging */ 80 remp = TMP_ALLOC_LIMBS (vsize); 81 tp = TMP_ALLOC_LIMBS (tsize); 82 new_vp = NULL; 83 if (rp == vp) 84 new_vp = TMP_ALLOC_LIMBS (vsize); 85 } 86 else 87 { 88 /* one alloc with calculated size, for efficiency */ 89 mp_size_t size = vsize + tsize + (rp == vp ? vsize : 0); 90 remp = TMP_ALLOC_LIMBS (size); 91 tp = remp + vsize; 92 new_vp = tp + tsize; 93 } 94 95 /* ensure divisor doesn't overlap quotient */ 96 if (rp == vp) 97 { 98 MPN_COPY (new_vp, vp, vsize); 99 vp = new_vp; 100 } 101 102 MPN_ZERO (tp, tsize-1); 103 104 tp[tsize - 1] = u & GMP_NUMB_MASK; 105 #if BITS_PER_ULONG > GMP_NUMB_BITS 106 if (u > GMP_NUMB_MAX) 107 { 108 /* tsize-vsize+1 == rsize, so tsize >= rsize. rsize == prec+1 >= 2, 109 so tsize >= 2, hence there's room for 2-limb u with nails */ 110 ASSERT (tsize >= 2); 111 tp[tsize - 1] = u >> GMP_NUMB_BITS; 112 tp[tsize - 2] = u & GMP_NUMB_MASK; 113 rexp++; 114 } 115 #endif 116 117 ASSERT (tsize-vsize+1 == rsize); 118 mpn_tdiv_qr (rp, remp, (mp_size_t) 0, tp, tsize, vp, vsize); 119 120 /* strip possible zero high limb */ 121 high_zero = (rp[rsize-1] == 0); 122 rsize -= high_zero; 123 rexp -= high_zero; 124 125 r->_mp_size = sign_quotient >= 0 ? rsize : -rsize; 126 r->_mp_exp = rexp; 127 TMP_FREE; 128 }