beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / udiv_w_sdiv.c
blob7136429f0f8b67699240c5235e7bd51898502ddc
1 /* mpn_udiv_w_sdiv -- implement udiv_qrnnd on machines with only signed
2 division.
4 Contributed by Peter L. Montgomery.
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.
12 Copyright 1992, 1994, 1996, 2000, 2011, 2012 Free Software Foundation, Inc.
14 This file is part of the GNU MP Library.
16 The GNU MP Library is free software; you can redistribute it and/or modify
17 it under the terms of either:
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.
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.
29 or both in parallel, as here.
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.
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/. */
40 #include "gmp.h"
41 #include "gmp-impl.h"
42 #include "longlong.h"
44 mp_limb_t
45 mpn_udiv_w_sdiv (mp_limb_t *rp, mp_limb_t a1, mp_limb_t a0, mp_limb_t d)
47 mp_limb_t q, r;
48 mp_limb_t c0, c1, b1;
50 ASSERT (d != 0);
51 ASSERT (a1 < d);
53 if ((mp_limb_signed_t) d >= 0)
55 if (a1 < d - a1 - (a0 >> (GMP_LIMB_BITS - 1)))
57 /* dividend, divisor, and quotient are nonnegative */
58 sdiv_qrnnd (q, r, a1, a0, d);
60 else
62 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
63 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (GMP_LIMB_BITS - 1));
64 /* Divide (c1*2^32 + c0) by d */
65 sdiv_qrnnd (q, r, c1, c0, d);
66 /* Add 2^31 to quotient */
67 q += (mp_limb_t) 1 << (GMP_LIMB_BITS - 1);
70 else
72 b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
73 c1 = a1 >> 1; /* A/2 */
74 c0 = (a1 << (GMP_LIMB_BITS - 1)) + (a0 >> 1);
76 if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
78 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
80 r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
81 if ((d & 1) != 0)
83 if (r >= q)
84 r = r - q;
85 else if (q - r <= d)
87 r = r - q + d;
88 q--;
90 else
92 r = r - q + 2*d;
93 q -= 2;
97 else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
99 c1 = (b1 - 1) - c1;
100 c0 = ~c0; /* logical NOT */
102 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
104 q = ~q; /* (A/2)/b1 */
105 r = (b1 - 1) - r;
107 r = 2*r + (a0 & 1); /* A/(2*b1) */
109 if ((d & 1) != 0)
111 if (r >= q)
112 r = r - q;
113 else if (q - r <= d)
115 r = r - q + d;
116 q--;
118 else
120 r = r - q + 2*d;
121 q -= 2;
125 else /* Implies c1 = b1 */
126 { /* Hence a1 = d - 1 = 2*b1 - 1 */
127 if (a0 >= -d)
129 q = -CNST_LIMB(1);
130 r = a0 + d;
132 else
134 q = -CNST_LIMB(2);
135 r = a0 + 2*d;
140 *rp = r;
141 return q;