github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpn/generic/sbpi1_bdiv_q.c (about) 1 /* mpn_sbpi1_bdiv_q -- schoolbook Hensel division with precomputed inverse, 2 returning quotient only. 3 4 Contributed to the GNU project by Niels Möller. 5 6 THE FUNCTIONS IN THIS FILE ARE INTERNAL FUNCTIONS WITH MUTABLE INTERFACES. 7 IT IS ONLY SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS 8 ALMOST GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE. 9 10 Copyright 2005, 2006, 2009, 2011, 2012 Free Software Foundation, Inc. 11 12 This file is part of the GNU MP Library. 13 14 The GNU MP Library is free software; you can redistribute it and/or modify 15 it under the terms of either: 16 17 * the GNU Lesser General Public License as published by the Free 18 Software Foundation; either version 3 of the License, or (at your 19 option) any later version. 20 21 or 22 23 * the GNU General Public License as published by the Free Software 24 Foundation; either version 2 of the License, or (at your option) any 25 later version. 26 27 or both in parallel, as here. 28 29 The GNU MP Library is distributed in the hope that it will be useful, but 30 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 31 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 32 for more details. 33 34 You should have received copies of the GNU General Public License and the 35 GNU Lesser General Public License along with the GNU MP Library. If not, 36 see https://www.gnu.org/licenses/. */ 37 38 #include "gmp.h" 39 #include "gmp-impl.h" 40 41 42 /* Computes Q = N / D mod B^nn, destroys N. 43 44 D must be odd. dinv is (-D)^-1 mod B. 45 46 47 The straightforward way to compute Q is to cancel one limb at a time, using 48 49 qp[i] = D^{-1} * np[i] (mod B) 50 N -= B^i * qp[i] * D 51 52 But we prefer addition to subtraction, since mpn_addmul_1 is often faster 53 than mpn_submul_1. Q = - N / D can be computed by iterating 54 55 qp[i] = (-D)^{-1} * np[i] (mod B) 56 N += B^i * qp[i] * D 57 58 And then we flip the sign, -Q = (not Q) + 1. */ 59 60 void 61 mpn_sbpi1_bdiv_q (mp_ptr qp, 62 mp_ptr np, mp_size_t nn, 63 mp_srcptr dp, mp_size_t dn, 64 mp_limb_t dinv) 65 { 66 mp_size_t i; 67 mp_limb_t cy, q; 68 69 ASSERT (dn > 0); 70 ASSERT (nn >= dn); 71 ASSERT ((dp[0] & 1) != 0); 72 /* FIXME: Add ASSERTs for allowable overlapping; i.e., that qp = np is OK, 73 but some over N/Q overlaps will not work. */ 74 75 for (i = nn - dn; i > 0; i--) 76 { 77 q = dinv * np[0]; 78 cy = mpn_addmul_1 (np, dp, dn, q); 79 mpn_add_1 (np + dn, np + dn, i, cy); 80 ASSERT (np[0] == 0); 81 qp[0] = ~q; 82 qp++; 83 np++; 84 } 85 86 for (i = dn; i > 1; i--) 87 { 88 q = dinv * np[0]; 89 mpn_addmul_1 (np, dp, i, q); 90 ASSERT (np[0] == 0); 91 qp[0] = ~q; 92 qp++; 93 np++; 94 } 95 96 /* Final limb */ 97 q = dinv * np[0]; 98 qp[0] = ~q; 99 mpn_add_1 (qp - nn + 1, qp - nn + 1, nn, 1); 100 }