Annotation of OpenXM/src/kan96xx/gmp-2.0.2-ssh-2/mpn/generic/udiv_w_sdiv.c, Revision 1.1.1.1
1.1 takayama 1: /* mpn_udiv_w_sdiv -- implement udiv_qrnnd on machines with only signed
2: division.
3:
4: Contributed by Peter L. Montgomery.
5:
6: Copyright (C) 1992, 1994, 1996 Free Software Foundation, Inc.
7:
8: This file is part of the GNU MP Library.
9:
10: The GNU MP Library is free software; you can redistribute it and/or modify
11: it under the terms of the GNU Library General Public License as published by
12: the Free Software Foundation; either version 2 of the License, or (at your
13: option) any later version.
14:
15: The GNU MP Library is distributed in the hope that it will be useful, but
16: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
18: License for more details.
19:
20: You should have received a copy of the GNU Library General Public License
21: along with the GNU MP Library; see the file COPYING.LIB. If not, write to
22: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23: MA 02111-1307, USA. */
24:
25: #include "gmp.h"
26: #include "gmp-impl.h"
27: #include "longlong.h"
28:
29: mp_limb_t
30: mpn_udiv_w_sdiv (rp, a1, a0, d)
31: mp_limb_t *rp, a1, a0, d;
32: {
33: mp_limb_t q, r;
34: mp_limb_t c0, c1, b1;
35:
36: if ((mp_limb_signed_t) d >= 0)
37: {
38: if (a1 < d - a1 - (a0 >> (BITS_PER_MP_LIMB - 1)))
39: {
40: /* dividend, divisor, and quotient are nonnegative */
41: sdiv_qrnnd (q, r, a1, a0, d);
42: }
43: else
44: {
45: /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
46: sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (BITS_PER_MP_LIMB - 1));
47: /* Divide (c1*2^32 + c0) by d */
48: sdiv_qrnnd (q, r, c1, c0, d);
49: /* Add 2^31 to quotient */
50: q += (mp_limb_t) 1 << (BITS_PER_MP_LIMB - 1);
51: }
52: }
53: else
54: {
55: b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
56: c1 = a1 >> 1; /* A/2 */
57: c0 = (a1 << (BITS_PER_MP_LIMB - 1)) + (a0 >> 1);
58:
59: if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
60: {
61: sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
62:
63: r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
64: if ((d & 1) != 0)
65: {
66: if (r >= q)
67: r = r - q;
68: else if (q - r <= d)
69: {
70: r = r - q + d;
71: q--;
72: }
73: else
74: {
75: r = r - q + 2*d;
76: q -= 2;
77: }
78: }
79: }
80: else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
81: {
82: c1 = (b1 - 1) - c1;
83: c0 = ~c0; /* logical NOT */
84:
85: sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
86:
87: q = ~q; /* (A/2)/b1 */
88: r = (b1 - 1) - r;
89:
90: r = 2*r + (a0 & 1); /* A/(2*b1) */
91:
92: if ((d & 1) != 0)
93: {
94: if (r >= q)
95: r = r - q;
96: else if (q - r <= d)
97: {
98: r = r - q + d;
99: q--;
100: }
101: else
102: {
103: r = r - q + 2*d;
104: q -= 2;
105: }
106: }
107: }
108: else /* Implies c1 = b1 */
109: { /* Hence a1 = d - 1 = 2*b1 - 1 */
110: if (a0 >= -d)
111: {
112: q = -1;
113: r = a0 + d;
114: }
115: else
116: {
117: q = -2;
118: r = a0 + 2*d;
119: }
120: }
121: }
122:
123: *rp = r;
124: return q;
125: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>