1 /* $NetBSD: bn_mp_sub.c,v 1.1.1.2 2014/04/24 12:45:31 pettai Exp $ */
5 /* LibTomMath, multiple-precision integer library -- Tom St Denis
7 * LibTomMath is a library that provides multiple-precision
8 * integer arithmetic as well as number theoretic functionality.
10 * The library was designed directly after the MPI library by
11 * Michael Fromberger but has been written from scratch with
12 * additional optimizations in place.
14 * The library is free for all purposes without any express
17 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
20 /* high level subtraction (handles signs) */
22 mp_sub (mp_int
* a
, mp_int
* b
, mp_int
* c
)
30 /* subtract a negative from a positive, OR */
31 /* subtract a positive from a negative. */
32 /* In either case, ADD their magnitudes, */
33 /* and use the sign of the first number. */
35 res
= s_mp_add (a
, b
, c
);
37 /* subtract a positive from a positive, OR */
38 /* subtract a negative from a negative. */
39 /* First, take the difference between their */
40 /* magnitudes, then... */
41 if (mp_cmp_mag (a
, b
) != MP_LT
) {
42 /* Copy the sign from the first */
44 /* The first has a larger or equal magnitude */
45 res
= s_mp_sub (a
, b
, c
);
47 /* The result has the *opposite* sign from */
48 /* the first number. */
49 c
->sign
= (sa
== MP_ZPOS
) ? MP_NEG
: MP_ZPOS
;
50 /* The second has a larger magnitude */
51 res
= s_mp_sub (b
, a
, c
);
59 /* Source: /cvs/libtom/libtommath/bn_mp_sub.c,v */
61 /* Date: 2006/12/28 01:25:13 */