github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpn/generic/invert.c (about)

     1  /* invert.c -- Compute floor((B^{2n}-1)/U) - B^n.
     2  
     3     Contributed to the GNU project by Marco Bodrato.
     4  
     5     THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES.  IT IS ONLY
     6     SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
     7     GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
     8  
     9  Copyright (C) 2007, 2009, 2010, 2012, 2014-2015 Free Software Foundation, Inc.
    10  
    11  This file is part of the GNU MP Library.
    12  
    13  The GNU MP Library is free software; you can redistribute it and/or modify
    14  it under the terms of either:
    15  
    16    * the GNU Lesser General Public License as published by the Free
    17      Software Foundation; either version 3 of the License, or (at your
    18      option) any later version.
    19  
    20  or
    21  
    22    * the GNU General Public License as published by the Free Software
    23      Foundation; either version 2 of the License, or (at your option) any
    24      later version.
    25  
    26  or both in parallel, as here.
    27  
    28  The GNU MP Library is distributed in the hope that it will be useful, but
    29  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    30  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    31  for more details.
    32  
    33  You should have received copies of the GNU General Public License and the
    34  GNU Lesser General Public License along with the GNU MP Library.  If not,
    35  see https://www.gnu.org/licenses/.  */
    36  
    37  #include "gmp.h"
    38  #include "gmp-impl.h"
    39  #include "longlong.h"
    40  
    41  void
    42  mpn_invert (mp_ptr ip, mp_srcptr dp, mp_size_t n, mp_ptr scratch)
    43  {
    44    ASSERT (n > 0);
    45    ASSERT (dp[n-1] & GMP_NUMB_HIGHBIT);
    46    ASSERT (! MPN_OVERLAP_P (ip, n, dp, n));
    47    ASSERT (! MPN_OVERLAP_P (ip, n, scratch, mpn_invertappr_itch(n)));
    48    ASSERT (! MPN_OVERLAP_P (dp, n, scratch, mpn_invertappr_itch(n)));
    49  
    50    if (n == 1)
    51      invert_limb (*ip, *dp);
    52    else if (BELOW_THRESHOLD (n, INV_APPR_THRESHOLD))
    53      {
    54  	/* Maximum scratch needed by this branch: 2*n */
    55  	mp_size_t i;
    56  	mp_ptr xp;
    57  
    58  	xp = scratch;				/* 2 * n limbs */
    59  	/* n > 1 here */
    60  	i = n;
    61  	do
    62  	  xp[--i] = GMP_NUMB_MAX;
    63  	while (i);
    64  	mpn_com (xp + n, dp, n);
    65  	if (n == 2) {
    66  	  mpn_divrem_2 (ip, 0, xp, 4, dp);
    67  	} else {
    68  	  gmp_pi1_t inv;
    69  	  invert_pi1 (inv, dp[n-1], dp[n-2]);
    70  	  /* FIXME: should we use dcpi1_div_q, for big sizes? */
    71  	  mpn_sbpi1_div_q (ip, xp, 2 * n, dp, n, inv.inv32);
    72  	}
    73      }
    74    else { /* Use approximated inverse; correct the result if needed. */
    75        mp_limb_t e; /* The possible error in the approximate inverse */
    76  
    77        ASSERT ( mpn_invert_itch (n) >= mpn_invertappr_itch (n) );
    78        e = mpn_ni_invertappr (ip, dp, n, scratch);
    79  
    80        if (UNLIKELY (e)) { /* Assume the error can only be "0" (no error) or "1". */
    81  	/* Code to detect and correct the "off by one" approximation. */
    82  	mpn_mul_n (scratch, ip, dp, n);
    83  	e = mpn_add_n (scratch, scratch, dp, n); /* FIXME: we only need e.*/
    84  	if (LIKELY(e)) /* The high part can not give a carry by itself. */
    85  	  e = mpn_add_nc (scratch + n, scratch + n, dp, n, e); /* FIXME:e */
    86  	/* If the value was wrong (no carry), correct it (increment). */
    87  	e ^= CNST_LIMB (1);
    88  	MPN_INCR_U (ip, n, e);
    89        }
    90    }
    91  }