github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpn/generic/divrem.c (about) 1 /* mpn_divrem -- Divide natural numbers, producing both remainder and 2 quotient. This is now just a middle layer calling mpn_tdiv_qr. 3 4 Copyright 1993-1997, 1999-2002, 2005 Free Software 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 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 15 or 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 21 or both in parallel, as here. 22 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. 27 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/. */ 31 32 #include "gmp.h" 33 #include "gmp-impl.h" 34 #include "longlong.h" 35 36 mp_limb_t 37 mpn_divrem (mp_ptr qp, mp_size_t qxn, 38 mp_ptr np, mp_size_t nn, 39 mp_srcptr dp, mp_size_t dn) 40 { 41 ASSERT (qxn >= 0); 42 ASSERT (nn >= dn); 43 ASSERT (dn >= 1); 44 ASSERT (dp[dn-1] & GMP_NUMB_HIGHBIT); 45 ASSERT (! MPN_OVERLAP_P (np, nn, dp, dn)); 46 ASSERT (! MPN_OVERLAP_P (qp, nn-dn+qxn, np, nn) || qp==np+dn+qxn); 47 ASSERT (! MPN_OVERLAP_P (qp, nn-dn+qxn, dp, dn)); 48 ASSERT_MPN (np, nn); 49 ASSERT_MPN (dp, dn); 50 51 if (dn == 1) 52 { 53 mp_limb_t ret; 54 mp_ptr q2p; 55 mp_size_t qn; 56 TMP_DECL; 57 58 TMP_MARK; 59 q2p = TMP_ALLOC_LIMBS (nn + qxn); 60 61 np[0] = mpn_divrem_1 (q2p, qxn, np, nn, dp[0]); 62 qn = nn + qxn - 1; 63 MPN_COPY (qp, q2p, qn); 64 ret = q2p[qn]; 65 66 TMP_FREE; 67 return ret; 68 } 69 else if (dn == 2) 70 { 71 return mpn_divrem_2 (qp, qxn, np, nn, dp); 72 } 73 else 74 { 75 mp_ptr rp, q2p; 76 mp_limb_t qhl; 77 mp_size_t qn; 78 TMP_DECL; 79 80 TMP_MARK; 81 if (UNLIKELY (qxn != 0)) 82 { 83 mp_ptr n2p; 84 n2p = TMP_ALLOC_LIMBS (nn + qxn); 85 MPN_ZERO (n2p, qxn); 86 MPN_COPY (n2p + qxn, np, nn); 87 q2p = TMP_ALLOC_LIMBS (nn - dn + qxn + 1); 88 rp = TMP_ALLOC_LIMBS (dn); 89 mpn_tdiv_qr (q2p, rp, 0L, n2p, nn + qxn, dp, dn); 90 MPN_COPY (np, rp, dn); 91 qn = nn - dn + qxn; 92 MPN_COPY (qp, q2p, qn); 93 qhl = q2p[qn]; 94 } 95 else 96 { 97 q2p = TMP_ALLOC_LIMBS (nn - dn + 1); 98 rp = TMP_ALLOC_LIMBS (dn); 99 mpn_tdiv_qr (q2p, rp, 0L, np, nn, dp, dn); 100 MPN_COPY (np, rp, dn); /* overwrite np area with remainder */ 101 qn = nn - dn; 102 MPN_COPY (qp, q2p, qn); 103 qhl = q2p[qn]; 104 } 105 TMP_FREE; 106 return qhl; 107 } 108 }