xref: /dragonfly/contrib/gmp/mpn/generic/invert.c (revision 479ab7f0)
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 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 /* 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 */
29 
30 #include "gmp.h"
31 #include "gmp-impl.h"
32 #include "longlong.h"
33 
34 void
35 mpn_invert (mp_ptr ip, mp_srcptr dp, mp_size_t n, mp_ptr scratch)
36 {
37   ASSERT (n > 0);
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)));
42 
43   if (n == 1)
44     invert_limb (*ip, *dp);
45   else {
46     TMP_DECL;
47 
48     TMP_MARK;
49     if (scratch == NULL)
50       scratch = TMP_ALLOC_LIMBS (mpn_invert_itch (n));
51 
52     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 	for (i = n - 1; i >= 0; i--)
60 	  xp[i] = GMP_NUMB_MAX;
61 	mpn_com (xp + n, dp, n);
62 	if (n == 2) {
63 	  mpn_divrem_2 (ip, 0, xp, 4, dp);
64 	} else {
65 	  gmp_pi1_t inv;
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);
69 	}
70       }
71     else { /* Use approximated inverse; correct the result if needed. */
72       mp_limb_t e; /* The possible error in the approximate inverse */
73 
74       ASSERT ( mpn_invert_itch (n) >= mpn_invertappr_itch (n) );
75       e = mpn_ni_invertappr (ip, dp, n, scratch);
76 
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.  */
83       }
84     }
85     TMP_FREE;
86   }
87 }
88