Bump actions/upload-artifacts version
[libtommath.git] / mp_sub.c
blob1c95ad544b6ea8468889dad431d2c8e68f7c3b7e
1 #include "tommath_private.h"
2 #ifdef MP_SUB_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* high level subtraction (handles signs) */
7 mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
9 if (a->sign != b->sign) {
10 /* subtract a negative from a positive, OR */
11 /* subtract a positive from a negative. */
12 /* In either case, ADD their magnitudes, */
13 /* and use the sign of the first number. */
14 c->sign = a->sign;
15 return s_mp_add(a, b, c);
18 /* subtract a positive from a positive, OR */
19 /* subtract a negative from a negative. */
20 /* First, take the difference between their */
21 /* magnitudes, then... */
22 if (mp_cmp_mag(a, b) == MP_LT) {
23 /* The second has a larger magnitude */
24 /* The result has the *opposite* sign from */
25 /* the first number. */
26 c->sign = (!mp_isneg(a) ? MP_NEG : MP_ZPOS);
27 MP_EXCH(const mp_int *, a, b);
28 } else {
29 /* The first has a larger or equal magnitude */
30 /* Copy the sign from the first */
31 c->sign = a->sign;
33 return s_mp_sub(a, b, c);
36 #endif