beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / invert.c
blobba6364e18751e1e4e26c2a5910f4dd2a1ac57979
1 /* invert.c -- Compute floor((B^{2n}-1)/U) - B^n.
3 Contributed to the GNU project by Marco Bodrato.
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.
9 Copyright (C) 2007, 2009, 2010, 2012, 2014-2015 Free Software Foundation, Inc.
11 This file is part of the GNU MP Library.
13 The GNU MP Library is free software; you can redistribute it and/or modify
14 it under the terms of either:
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.
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.
26 or both in parallel, as here.
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.
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/. */
37 #include "gmp.h"
38 #include "gmp-impl.h"
39 #include "longlong.h"
41 void
42 mpn_invert (mp_ptr ip, mp_srcptr dp, mp_size_t n, mp_ptr scratch)
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)));
50 if (n == 1)
51 invert_limb (*ip, *dp);
52 else if (BELOW_THRESHOLD (n, INV_APPR_THRESHOLD))
54 /* Maximum scratch needed by this branch: 2*n */
55 mp_size_t i;
56 mp_ptr xp;
58 xp = scratch; /* 2 * n limbs */
59 /* n > 1 here */
60 i = n;
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);
74 else { /* Use approximated inverse; correct the result if needed. */
75 mp_limb_t e; /* The possible error in the approximate inverse */
77 ASSERT ( mpn_invert_itch (n) >= mpn_invertappr_itch (n) );
78 e = mpn_ni_invertappr (ip, dp, n, scratch);
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);