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 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 the GNU Lesser General Public License as published by
15 the Free Software Foundation; either version 3 of the License, or (at your
16 option) any later version.
18 The GNU MP Library is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
21 License for more details.
23 You should have received a copy of the GNU Lesser General Public License
24 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
26 /* FIXME: Remove NULL and TMP_*, as soon as all the callers properly
27 allocate and pass the scratch to the function. */
28 #include <stdlib.h> /* for NULL */
35 mpn_invert (mp_ptr ip, mp_srcptr dp, mp_size_t n, mp_ptr scratch)
38 ASSERT (dp[n-1] & GMP_NUMB_HIGHBIT);
39 ASSERT (! MPN_OVERLAP_P (ip, n, dp, n));
40 ASSERT (! MPN_OVERLAP_P (ip, n, scratch, mpn_invertappr_itch(n)));
41 ASSERT (! MPN_OVERLAP_P (dp, n, scratch, mpn_invertappr_itch(n)));
44 invert_limb (*ip, *dp);
50 scratch = TMP_ALLOC_LIMBS (mpn_invert_itch (n));
52 if (BELOW_THRESHOLD (n, INV_APPR_THRESHOLD))
54 /* Maximum scratch needed by this branch: 2*n */
58 xp = scratch; /* 2 * n limbs */
59 for (i = n - 1; i >= 0; i--)
61 mpn_com (xp + n, dp, n);
63 mpn_divrem_2 (ip, 0, xp, 4, dp);
66 invert_pi1 (inv, dp[n-1], dp[n-2]);
67 /* FIXME: should we use dcpi1_div_q, for big sizes? */
68 mpn_sbpi1_div_q (ip, xp, 2 * n, dp, n, inv.inv32);
71 else { /* Use approximated inverse; correct the result if needed. */
72 mp_limb_t e; /* The possible error in the approximate inverse */
74 ASSERT ( mpn_invert_itch (n) >= mpn_invertappr_itch (n) );
75 e = mpn_ni_invertappr (ip, dp, n, scratch);
77 if (e) { /* Assume the error can only be "0" (no error) or "1". */
78 /* Code to detect and correct the "off by one" approximation. */
79 mpn_mul_n (scratch, ip, dp, n);
80 ASSERT_NOCARRY (mpn_add_n (scratch + n, scratch + n, dp, n));
81 if (! mpn_add (scratch, scratch, 2*n, dp, n))
82 MPN_INCR_U (ip, n, 1); /* The value was wrong, correct it. */