1 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2 /* All Rights Reserved */
6 * Copyright (c) 1980 Regents of the University of California.
7 * All rights reserved. The Berkeley software License Agreement
8 * specifies the terms and conditions for redistribution.
10 /* Portions Copyright(c) 1988, Sun Microsystems Inc. */
11 /* All Rights Reserved */
14 * Copyright (c) 1997, by Sun Microsystems, Inc.
15 * All rights reserved.
18 #ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.1 */
25 #include <sys/types.h>
28 static void m_div(MINT
*, MINT
*, MINT
*, MINT
*);
31 mp_mdiv(MINT
*a
, MINT
*b
, MINT
*q
, MINT
*r
)
60 m_dsb(int qx
, int n
, short *a
, short *b
)
68 (void) printf("m_dsb %d %d %d %d\n", qx
, n
, *a
, *b
);
74 for (j
= n
; j
> 0; j
--) {
76 (void) printf("1 borrow=%x %d %d %d\n", borrow
, (*aptr
* qx
),
79 borrow
-= (*aptr
++) * qx
- *bptr
;
81 (void) printf("2 borrow=%x %d %d %d\n", borrow
, (*aptr
* qx
),
84 *bptr
++ = (short)(borrow
& 077777);
86 (void) printf("3 borrow=%x %d %d %d\n", borrow
, (*aptr
* qx
),
89 if (borrow
>= 0) borrow
>>= fifteen
; /* 3b2 */
90 else borrow
= 0xfffe0000 | (borrow
>> fifteen
);
92 (void) printf("4 borrow=%x %d %d %d\n", borrow
, (*aptr
* qx
),
97 *bptr
= (short)(borrow
& 077777);
98 if (borrow
>= 0) s3b2shit
= borrow
>> fifteen
; /* 3b2 */
99 else s3b2shit
= 0xfffe0000 | (borrow
>> fifteen
);
102 (void) printf("mdsb 0\n");
109 for (j
= n
; j
> 0; j
--) {
110 borrow
+= *aptr
++ + *bptr
;
111 *bptr
++ = (short)(borrow
& 077777);
112 if (borrow
>= 0) borrow
>>= fifteen
; /* 3b2 */
113 else borrow
= 0xfffe0000 | (borrow
>>fifteen
);
116 (void) printf("mdsb 1\n");
122 m_trq(short v1
, short v2
, short u1
, short u2
, short u3
)
128 c1
= u1
* 0100000 + u2
;
132 d
= (short)(c1
/ v1
);
136 x1
= x1
* 0100000 + u3
- v2
* d
;
140 (void) printf("mtrq %d %d %d %d %d %d\n", v1
, v2
, u1
, u2
, u3
, (d
+1));
146 m_div(MINT
*a
, MINT
*b
, MINT
*q
, MINT
*r
)
158 u
.len
= v
.len
= x
.len
= w
.len
= 0;
160 _mp_fatal("mdiv divide by zero");
164 r
->val
= _mp_xalloc(1, "m_div1");
165 mp_sdiv(a
, b
->val
[0], q
, r
->val
);
166 if (r
->val
[0] == 0) {
174 if (a
-> len
< b
-> len
) {
177 r
->val
= _mp_xalloc(r
->len
, "m_div2");
178 for (qq
= 0; qq
< r
->len
; qq
++) {
179 r
->val
[qq
] = a
->val
[qq
];
186 d
= 0100000 / (b
->val
[n
- 1] + 1);
187 mp_mult(a
, &x
, &u
); /* subtle: relies on mult allocing extra space */
190 (void) printf(" u=%s\n", mtox(&u
));
191 (void) printf(" v=%s\n", mtox(&v
));
195 qval
= _mp_xalloc(a
-> len
- n
+ 1, "m_div3");
197 for (j
= a
->len
- n
; j
>= 0; j
--) {
198 qq
= m_trq(v1
, v2
, uval
[j
+ n
], uval
[j
+ n
- 1],
200 if (m_dsb(qq
, n
, v
.val
, uval
+ j
))
208 (void) printf(" x=%s\n", mtox(&x
));
209 (void) printf(" d(in)=%d\n", (d
));
211 mp_sdiv(&x
, d
, &w
, &d
);
213 (void) printf(" w=%s\n", mtox(&w
));
214 (void) printf(" d(out)=%d\n", (d
));
220 if (qq
> 0 && qval
[qq
- 1] == 0)