summaryrefslogtreecommitdiff
path: root/gmp/mpn/generic/sb_divappr_q.c
blob: 42a39be0099b0f138f4010341dfa0656038e3a52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* mpn_sb_divappr_q -- schoolbook division with 2-limb sloppy non-greater
   precomputed inverse, returning approximate quotient.

   Contributed to the GNU project by Torbjörn Granlund.

   THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH A MUTABLE INTERFACE.  IT IS
   ONLY SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS
   ALMOST GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GMP
   RELEASE.

Copyright 2006, 2007 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */

#include "gmp.h"
#include "gmp-impl.h"
#include "longlong.h"

/*
  CAVEATS:
  1. Should it demand normalized operands like now, or normalize on-the-fly?
  2. Overwrites {np,nn}.
  3. Uses mpn_submul_1.  It would be nice to somehow make it use mpn_addmul_1
     instead.  (That would open for mpn_addmul_2 straightforwardly.)
*/

mp_limb_t
mpn_sb_divappr_q (mp_ptr qp,
		  mp_ptr np, mp_size_t nn,
		  mp_srcptr dp, mp_size_t dn,
		  mp_srcptr dip)
{
  mp_limb_t q, q10, q01a, q00a, q01b, q00b;
  mp_limb_t cy;
  mp_size_t i;
  mp_limb_t qh;
  mp_limb_t di1, di0;
  mp_size_t qn;

  ASSERT (dn > 0);
  ASSERT (nn >= dn);
  ASSERT ((dp[dn-1] & GMP_NUMB_HIGHBIT) != 0);
  ASSERT (! MPN_OVERLAP_P (np, nn, dp, dn));
  ASSERT (! MPN_OVERLAP_P (qp, nn-dn, dp, dn));
  ASSERT (! MPN_OVERLAP_P (qp, nn-dn, np, nn) || qp+dn >= np);
  ASSERT_MPN (np, nn);
  ASSERT_MPN (dp, dn);

  np += nn;
  qn = nn - dn;
  if (qn + 1 < dn)
    {
      dp += dn - (qn + 1);
      dn = qn + 1;
    }

  qh = mpn_cmp (np - dn, dp, dn) >= 0;
  if (qh != 0)
    mpn_sub_n (np - dn, np - dn, dp, dn);

  qp += qn;
  di1 = dip[1]; di0 = dip[0];
  for (i = qn; i >= dn; i--)
    {
      np--;
      umul_ppmm (q, q10, np[0], di1);
      umul_ppmm (q01a, q00a, np[-1], di1);
      add_ssaaaa (q, q10, q, q10, np[0], q01a);
      umul_ppmm (q01b, q00b, np[0], di0);
      add_ssaaaa (q, q10, q, q10, 0, q01b);
      add_ssaaaa (q, q10, q, q10, 0, np[-1]);

      cy = mpn_submul_1 (np - dn, dp, dn, q);

      if (UNLIKELY (np[0] > cy || mpn_cmp (np - dn, dp, dn) >= 0))
	{
	  q = q + 1;
	  mpn_sub_n (np - dn, np - dn, dp, dn);
	}

      *--qp = q;
    }

  for (i = dn - 1; i > 0; i--)
    {
      np--;
      umul_ppmm (q, q10, np[0], di1);
      umul_ppmm (q01a, q00a, np[-1], di1);
      add_ssaaaa (q, q10, q, q10, np[0], q01a);
      umul_ppmm (q01b, q00b, np[0], di0);
      add_ssaaaa (q, q10, q, q10, 0, q01b);
      add_ssaaaa (q, q10, q, q10, 0, np[-1]);

      cy = mpn_submul_1 (np - dn, dp, dn, q);

      if (UNLIKELY (np[0] > cy || mpn_cmp (np - dn, dp, dn) >= 0))
	{
	  q = q + 1;
	  if (q == 0)
	    q = GMP_NUMB_MAX;
	  else
	    mpn_sub_n (np - dn, np - dn, dp, dn);
	}

      *--qp = q;

      /* Truncate operands.  */
      dn--;
      dp++;

      /* The partial remainder might be equal to the truncated divisor,
	 thus non-canonical.  When that happens, the rest of the quotient
	 should be all ones.  */
      if (UNLIKELY (mpn_cmp (np - dn, dp, dn) == 0))
	{
	  while (--i)
	    *--qp = GMP_NUMB_MAX;
	  break;
	}
    }

  return qh;
}