3 * $Id: mp-arith.c,v 1.5 1999/12/22 15:54:41 mdw Exp $
5 * Basic arithmetic on multiprecision integers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: mp-arith.c,v $
33 * Revision 1.5 1999/12/22 15:54:41 mdw
34 * Adjust Karatsuba parameters. Calculate destination size better.
36 * Revision 1.4 1999/12/13 15:35:16 mdw
37 * Slightly different rules on memory allocation.
39 * Revision 1.3 1999/12/11 10:57:43 mdw
40 * Karatsuba squaring algorithm.
42 * Revision 1.2 1999/12/10 23:18:39 mdw
43 * Change interface for suggested destinations.
45 * Revision 1.1 1999/11/17 18:02:16 mdw
46 * New multiprecision integer arithmetic suite.
50 /*----- Header files ------------------------------------------------------*/
54 /*----- Macros ------------------------------------------------------------*/
56 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
58 /*----- Main code ---------------------------------------------------------*/
62 * Arguments: @mp *a@ = source
64 * Returns: Result, @a@ converted to two's complement notation.
67 mp
*mp_2c(mp
*d
, mp
*a
)
72 MP_MODIFY(d
, MP_LEN(a
));
73 mpx_2c(d
->v
, d
->vl
, a
->v
, a
->vl
);
74 d
->f
= a
->f
& MP_BURN
;
81 * Arguments: @mp *d@ = destination
84 * Returns: Result, @a@ converted to the native signed-magnitude
88 mp
*mp_sm(mp
*d
, mp
*a
)
90 if (!MP_LEN(a
) || a
->vl
[-1] < MPW_MAX
/ 2)
93 MP_MODIFY(d
, MP_LEN(a
));
94 mpx_2c(d
->v
, d
->vl
, a
->v
, a
->vl
);
95 d
->f
= (a
->f
& (MP_BURN
| MP_NEG
)) ^ MP_NEG
;
100 /* --- @mp_lsl@ --- *
102 * Arguments: @mp *d@ = destination
104 * @size_t n@ = number of bits to move
106 * Returns: Result, @a@ shifted left by @n@.
109 mp
*mp_lsl(mp
*d
, mp
*a
, size_t n
)
111 MP_MODIFY(d
, MP_LEN(a
) + (n
+ MPW_BITS
- 1) / MPW_BITS
);
112 mpx_lsl(d
->v
, d
->vl
, a
->v
, a
->vl
, n
);
113 d
->f
= a
->f
& (MP_NEG
| MP_BURN
);
118 /* --- @mp_lsr@ --- *
120 * Arguments: @mp *d@ = destination
122 * @size_t n@ = number of bits to move
124 * Returns: Result, @a@ shifted left by @n@.
127 mp
*mp_lsr(mp
*d
, mp
*a
, size_t n
)
129 MP_MODIFY(d
, MP_LEN(a
));
130 mpx_lsr(d
->v
, d
->vl
, a
->v
, a
->vl
, n
);
131 d
->f
= a
->f
& (MP_NEG
| MP_BURN
);
136 /* --- @mp_cmp@ --- *
138 * Arguments: @const mp *a, *b@ = two numbers
140 * Returns: Less than, equal to or greater than zero, according to
141 * whether @a@ is less than, equal to or greater than @b@.
144 int mp_cmp(const mp
*a
, const mp
*b
)
146 if (!((a
->f
^ b
->f
) & MP_NEG
))
147 return (mpx_ucmp(a
->v
, a
->vl
, b
->v
, b
->vl
));
148 else if (a
->f
& MP_NEG
)
154 /* --- @mp_add@ --- *
156 * Arguments: @mp *d@ = destination
157 * @mp *a, *b@ = sources
159 * Returns: Result, @a@ added to @b@.
162 mp
*mp_add(mp
*d
, mp
*a
, mp
*b
)
164 MP_MODIFY(d
, MAX(MP_LEN(a
), MP_LEN(b
)) + 1);
165 if (!((a
->f
^ b
->f
) & MP_NEG
))
166 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
168 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
169 mp
*t
= a
; a
= b
; b
= t
;
171 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
173 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | (a
->f
& MP_NEG
);
178 /* --- @mp_sub@ --- *
180 * Arguments: @mp *d@ = destination
181 * @mp *a, *b@ = sources
183 * Returns: Result, @b@ subtracted from @a@.
186 mp
*mp_sub(mp
*d
, mp
*a
, mp
*b
)
189 MP_MODIFY(d
, MAX(MP_LEN(a
), MP_LEN(b
)) + 1);
190 if ((a
->f
^ b
->f
) & MP_NEG
)
191 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
193 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
194 mp
*t
= a
; a
= b
; b
= t
;
197 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
199 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ sgn
) & MP_NEG
);
204 /* --- @mp_mul@ --- *
206 * Arguments: @mp *d@ = destination
207 * @mp *a, *b@ = sources
209 * Returns: Result, @a@ multiplied by @b@.
212 mp
*mp_mul(mp
*d
, mp
*a
, mp
*b
)
217 if (MP_LEN(a
) <= KARATSUBA_CUTOFF
|| MP_LEN(b
) <= KARATSUBA_CUTOFF
) {
218 MP_MODIFY(d
, MP_LEN(a
) + MP_LEN(b
));
219 mpx_umul(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
221 size_t m
= 2 * MAX(MP_LEN(a
), MP_LEN(b
)) + 2;
226 mpx_kmul(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
, s
, s
+ m
);
230 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ b
->f
) & MP_NEG
);
237 /* --- @mp_sqr@ --- *
239 * Arguments: @mp *d@ = destination
242 * Returns: Result, @a@ squared.
245 mp
*mp_sqr(mp
*d
, mp
*a
)
247 size_t m
= MP_LEN(a
);
250 MP_MODIFY(d
, 2 * m
+ 2);
251 if (m
> KARATSUBA_CUTOFF
) {
253 m
= 2 * (m
+ 1) + KARATSUBA_SLOP
;
255 mpx_ksqr(d
->v
, d
->vl
, a
->v
, a
->vl
, s
, s
+ m
);
258 mpx_usqr(d
->v
, d
->vl
, a
->v
, a
->vl
);
259 d
->f
= a
->f
& MP_BURN
;
265 /* --- @mp_div@ --- *
267 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
268 * @mp *a, *b@ = sources
270 * Use: Calculates the quotient and remainder when @a@ is divided by
271 * @b@. The destinations @*qq@ and @*rr@ must be distinct.
272 * Either of @qq@ or @rr@ may be null to indicate that the
273 * result is irrelevant. (Discarding both results is silly.)
274 * There is a performance advantage if @a == *rr@.
276 * The behaviour when @a@ and @b@ have the same sign is
277 * straightforward. When the signs differ, this implementation
278 * chooses @r@ to have the same sign as @b@, rather than the
279 * more normal choice that the remainder has the same sign as
280 * the dividend. This makes modular arithmetic a little more
284 void mp_div(mp
**qq
, mp
**rr
, mp
*a
, mp
*b
)
286 mp
*r
= rr ?
*rr
: MP_NEW
;
287 mp
*q
= qq ?
*qq
: MP_NEW
;
290 /* --- Set up some temporary workspace --- */
293 size_t rq
= MP_LEN(b
) + 1;
298 /* --- Set the remainder up right --- *
300 * Just in case the divisor is larger, be able to cope with this. It's not
301 * important in @mpx_udiv@, but it is here because of the sign correction.
305 size_t rq
= MP_LEN(a
) + 2;
313 MP_ENSURE(r
, MP_LEN(r
) + 2);
316 MP_MODIFY(r
, MP_LEN(a
) + 2);
317 memcpy(r
->v
, a
->v
, MPWS(MP_LEN(a
)));
318 memset(r
->v
+ MP_LEN(a
), 0, MPWS(2));
322 /* --- Fix up the quotient too --- */
324 MP_MODIFY(q
, MP_LEN(a
));
326 /* --- Perform the calculation --- */
328 mpx_udiv(q
->v
, q
->vl
, r
->v
, r
->vl
, b
->v
, b
->vl
, sv
, svl
);
330 /* --- Sort out the sign of the results --- *
332 * If the signs of the arguments differ, and the remainder is nonzero, I
333 * must add one to the absolute value of the quotient and subtract the
334 * remainder from @b@.
337 q
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ b
->f
) & MP_NEG
);
340 for (v
= r
->v
; v
< r
->vl
; v
++) {
342 MPX_UADDN(q
->v
, q
->vl
, 1);
343 mpx_usub(r
->v
, r
->vl
, b
->v
, b
->vl
, r
->v
, r
->vl
);
349 r
->f
= ((a
->f
| b
->f
) & MP_BURN
) | (b
->f
& MP_NEG
);
351 /* --- Store the return values --- */
372 /*----- Test rig ----------------------------------------------------------*/
376 static int verify(const char *op
, mp
*expect
, mp
*result
, mp
*a
, mp
*b
)
378 if (MP_CMP(expect
, !=, result
)) {
379 fprintf(stderr
, "\n*** %s failed", op
);
380 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 10);
381 fputs("\n*** b = ", stderr
); mp_writefile(b
, stderr
, 10);
382 fputs("\n*** result = ", stderr
); mp_writefile(result
, stderr
, 10);
383 fputs("\n*** expect = ", stderr
); mp_writefile(expect
, stderr
, 10);
390 #define RIG(name, op) \
391 static int t##name(dstr *v) \
393 mp *a = *(mp **)v[0].buf; \
394 mpw n = *(int *)v[1].buf; \
396 mp *r = *(mp **)v[2].buf; \
397 mp *c = op(MP_NEW, a, n); \
399 mp_build(&b, &n, &n + 1); \
400 ok = verify(#name, r, c, a, &b); \
401 mp_drop(a); mp_drop(c); mp_drop(r); \
402 assert(mparena_count(MPARENA_GLOBAL) == 0); \
411 #define RIG(name, op) \
412 static int t##name(dstr *v) \
414 mp *a = *(mp **)v[0].buf; \
415 mp *b = *(mp **)v[1].buf; \
416 mp *r = *(mp **)v[2].buf; \
417 mp *c = op(MP_NEW, a, b); \
418 int ok = verify(#name, r, c, a, b); \
419 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
420 assert(mparena_count(MPARENA_GLOBAL) == 0); \
430 static int tdiv(dstr
*v
)
432 mp
*a
= *(mp
**)v
[0].buf
;
433 mp
*b
= *(mp
**)v
[1].buf
;
434 mp
*q
= *(mp
**)v
[2].buf
;
435 mp
*r
= *(mp
**)v
[3].buf
;
436 mp
*c
= MP_NEW
, *d
= MP_NEW
;
438 mp_div(&c
, &d
, a
, b
);
439 ok
&= verify("div(quotient)", q
, c
, a
, b
);
440 ok
&= verify("div(remainder)", r
, d
, a
, b
);
441 mp_drop(a
); mp_drop(b
); mp_drop(c
); mp_drop(d
); mp_drop(r
); mp_drop(q
);
442 assert(mparena_count(MPARENA_GLOBAL
) == 0);
446 static test_chunk tests
[] = {
447 { "lsl", tlsl
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
448 { "lsr", tlsr
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
449 { "add", tadd
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
450 { "sub", tsub
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
451 { "mul", tmul
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
452 { "div", tdiv
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
456 int main(int argc
, char *argv
[])
459 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
465 /*----- That's all, folks -------------------------------------------------*/