xref: /netbsd-src/external/lgpl3/gmp/dist/mpn/generic/udiv_w_sdiv.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* mpn_udiv_w_sdiv -- implement udiv_qrnnd on machines with only signed
2    division.
3 
4    Contributed by Peter L. Montgomery.
5 
6    THIS IS AN INTERNAL FUNCTION WITH A MUTABLE INTERFACE.  IT IS ONLY SAFE
7    TO REACH THIS FUNCTION THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS
8    ALMOST GUARANTEED THAT THIS FUNCTION WILL CHANGE OR DISAPPEAR IN A FUTURE
9    GNU MP RELEASE.
10 
11 
12 Copyright 1992, 1994, 1996, 2000, 2011, 2012 Free Software Foundation, Inc.
13 
14 This file is part of the GNU MP Library.
15 
16 The GNU MP Library is free software; you can redistribute it and/or modify
17 it under the terms of either:
18 
19   * the GNU Lesser General Public License as published by the Free
20     Software Foundation; either version 3 of the License, or (at your
21     option) any later version.
22 
23 or
24 
25   * the GNU General Public License as published by the Free Software
26     Foundation; either version 2 of the License, or (at your option) any
27     later version.
28 
29 or both in parallel, as here.
30 
31 The GNU MP Library is distributed in the hope that it will be useful, but
32 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
33 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
34 for more details.
35 
36 You should have received copies of the GNU General Public License and the
37 GNU Lesser General Public License along with the GNU MP Library.  If not,
38 see https://www.gnu.org/licenses/.  */
39 
40 #include "gmp-impl.h"
41 #include "longlong.h"
42 
43 mp_limb_t
mpn_udiv_w_sdiv(mp_limb_t * rp,mp_limb_t a1,mp_limb_t a0,mp_limb_t d)44 mpn_udiv_w_sdiv (mp_limb_t *rp, mp_limb_t a1, mp_limb_t a0, mp_limb_t d)
45 {
46   mp_limb_t q, r;
47   mp_limb_t c0, c1, b1;
48 
49   ASSERT (d != 0);
50   ASSERT (a1 < d);
51 
52   if ((mp_limb_signed_t) d >= 0)
53     {
54       if (a1 < d - a1 - (a0 >> (GMP_LIMB_BITS - 1)))
55 	{
56 	  /* dividend, divisor, and quotient are nonnegative */
57 	  sdiv_qrnnd (q, r, a1, a0, d);
58 	}
59       else
60 	{
61 	  /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
62 	  sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (GMP_LIMB_BITS - 1));
63 	  /* Divide (c1*2^32 + c0) by d */
64 	  sdiv_qrnnd (q, r, c1, c0, d);
65 	  /* Add 2^31 to quotient */
66 	  q += (mp_limb_t) 1 << (GMP_LIMB_BITS - 1);
67 	}
68     }
69   else
70     {
71       b1 = d >> 1;			/* d/2, between 2^30 and 2^31 - 1 */
72       c1 = a1 >> 1;			/* A/2 */
73       c0 = (a1 << (GMP_LIMB_BITS - 1)) + (a0 >> 1);
74 
75       if (a1 < b1)			/* A < 2^32*b1, so A/2 < 2^31*b1 */
76 	{
77 	  sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
78 
79 	  r = 2*r + (a0 & 1);		/* Remainder from A/(2*b1) */
80 	  if ((d & 1) != 0)
81 	    {
82 	      if (r >= q)
83 		r = r - q;
84 	      else if (q - r <= d)
85 		{
86 		  r = r - q + d;
87 		  q--;
88 		}
89 	      else
90 		{
91 		  r = r - q + 2*d;
92 		  q -= 2;
93 		}
94 	    }
95 	}
96       else if (c1 < b1)			/* So 2^31 <= (A/2)/b1 < 2^32 */
97 	{
98 	  c1 = (b1 - 1) - c1;
99 	  c0 = ~c0;			/* logical NOT */
100 
101 	  sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
102 
103 	  q = ~q;			/* (A/2)/b1 */
104 	  r = (b1 - 1) - r;
105 
106 	  r = 2*r + (a0 & 1);		/* A/(2*b1) */
107 
108 	  if ((d & 1) != 0)
109 	    {
110 	      if (r >= q)
111 		r = r - q;
112 	      else if (q - r <= d)
113 		{
114 		  r = r - q + d;
115 		  q--;
116 		}
117 	      else
118 		{
119 		  r = r - q + 2*d;
120 		  q -= 2;
121 		}
122 	    }
123 	}
124       else				/* Implies c1 = b1 */
125 	{				/* Hence a1 = d - 1 = 2*b1 - 1 */
126 	  if (a0 >= -d)
127 	    {
128 	      q = -CNST_LIMB(1);
129 	      r = a0 + d;
130 	    }
131 	  else
132 	    {
133 	      q = -CNST_LIMB(2);
134 	      r = a0 + 2*d;
135 	    }
136 	}
137     }
138 
139   *rp = r;
140   return q;
141 }
142