summaryrefslogtreecommitdiff
path: root/mpn/generic/bdiv_q_1.c
blob: 6beb9a02c45e1830d1fe99a650a8368873196b01 (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
/* mpn_bdiv_q_1, mpn_pi1_bdiv_q_1 -- schoolbook Hensel division by 1-limb
   divisor, returning quotient only.

   THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
   CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
   FUTURE GNU MP RELEASES.

Copyright 2000-2003, 2005, 2009, 2017 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 either:

  * 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.

or

  * the GNU General Public License as published by the Free Software
    Foundation; either version 2 of the License, or (at your option) any
    later version.

or both in parallel, as here.

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 General Public License
for more details.

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

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

mp_limb_t
mpn_pi1_bdiv_q_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t d,
		  mp_limb_t di, int shift)
{
  mp_size_t  i;
  mp_limb_t  c, h, l, u, u_next, dummy;

  ASSERT (n >= 1);
  ASSERT (d != 0);
  ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
  ASSERT_MPN (up, n);
  ASSERT_LIMB (d);

  d <<= GMP_NAIL_BITS;

  if (shift != 0)
    {
      c = 0;

      u = up[0];
      rp--;
      for (i = 1; i < n; i++)
	{
	  u_next = up[i];
	  u = ((u >> shift) | (u_next << (GMP_NUMB_BITS-shift))) & GMP_NUMB_MASK;

	  SUBC_LIMB (c, l, u, c);

	  l = (l * di) & GMP_NUMB_MASK;
	  rp[i] = l;

	  umul_ppmm (h, dummy, l, d);
	  c += h;
	  u = u_next;
	}

      u = u >> shift;
      SUBC_LIMB (c, l, u, c);

      l = (l * di) & GMP_NUMB_MASK;
      rp[n] = l;
    }
  else
    {
      u = up[0];
      l = (u * di) & GMP_NUMB_MASK;
      rp[0] = l;
      c = 0;

      for (i = 1; i < n; i++)
	{
	  umul_ppmm (h, dummy, l, d);
	  c += h;

	  u = up[i];
	  SUBC_LIMB (c, l, u, c);

	  l = (l * di) & GMP_NUMB_MASK;
	  rp[i] = l;
	}
    }

  return c;
}

mp_limb_t
mpn_bdiv_q_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t d)
{
  mp_limb_t di;
  int shift;

  ASSERT (n >= 1);
  ASSERT (d != 0);
  ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
  ASSERT_MPN (up, n);
  ASSERT_LIMB (d);

  count_trailing_zeros (shift, d);
  d >>= shift;

  binvert_limb (di, d);
  return mpn_pi1_bdiv_q_1 (rp, up, n, d, di, shift);
}