xref: /netbsd-src/external/lgpl3/gmp/dist/mpn/generic/invert.c (revision fdd524d4ccd2bb0c6f67401e938dabf773eb0372)
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 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 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.
17 
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.
22 
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/.  */
25 
26 #include "gmp.h"
27 #include "gmp-impl.h"
28 #include "longlong.h"
29 
30 void
31 mpn_invert (mp_ptr ip, mp_srcptr dp, mp_size_t n, mp_ptr scratch)
32 {
33   ASSERT (n > 0);
34   ASSERT (dp[n-1] & GMP_NUMB_HIGHBIT);
35   ASSERT (! MPN_OVERLAP_P (ip, n, dp, n));
36   ASSERT (! MPN_OVERLAP_P (ip, n, scratch, mpn_invertappr_itch(n)));
37   ASSERT (! MPN_OVERLAP_P (dp, n, scratch, mpn_invertappr_itch(n)));
38 
39   if (n == 1)
40     invert_limb (*ip, *dp);
41   else {
42     TMP_DECL;
43 
44     TMP_MARK;
45     if (BELOW_THRESHOLD (n, INV_APPR_THRESHOLD))
46       {
47 	/* Maximum scratch needed by this branch: 2*n */
48 	mp_size_t i;
49 	mp_ptr xp;
50 
51 	xp = scratch;				/* 2 * n limbs */
52 	for (i = n - 1; i >= 0; i--)
53 	  xp[i] = GMP_NUMB_MAX;
54 	mpn_com (xp + n, dp, n);
55 	if (n == 2) {
56 	  mpn_divrem_2 (ip, 0, xp, 4, dp);
57 	} else {
58 	  gmp_pi1_t inv;
59 	  invert_pi1 (inv, dp[n-1], dp[n-2]);
60 	  /* FIXME: should we use dcpi1_div_q, for big sizes? */
61 	  mpn_sbpi1_div_q (ip, xp, 2 * n, dp, n, inv.inv32);
62 	}
63       }
64     else { /* Use approximated inverse; correct the result if needed. */
65       mp_limb_t e; /* The possible error in the approximate inverse */
66 
67       ASSERT ( mpn_invert_itch (n) >= mpn_invertappr_itch (n) );
68       e = mpn_ni_invertappr (ip, dp, n, scratch);
69 
70       if (UNLIKELY (e)) { /* Assume the error can only be "0" (no error) or "1". */
71 	/* Code to detect and correct the "off by one" approximation. */
72 	mpn_mul_n (scratch, ip, dp, n);
73 	ASSERT_NOCARRY (mpn_add_n (scratch + n, scratch + n, dp, n));
74 	if (! mpn_add (scratch, scratch, 2*n, dp, n))
75 	  MPN_INCR_U (ip, n, 1); /* The value was wrong, correct it.  */
76       }
77     }
78     TMP_FREE;
79   }
80 }
81