comparison libtommath/bn_mp_or.c @ 1733:d529a52b2f7c coverity coverity

merge coverity from main
author Matt Johnston <matt@ucc.asn.au>
date Fri, 26 Jun 2020 21:07:34 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1643:b59623a64678 1733:d529a52b2f7c
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_OR_C 2 #ifdef BN_MP_OR_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 * 4 /* SPDX-License-Identifier: Unlicense */
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
7 *
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
11 *
12 * The library is free for all purposes without any express
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */
17 5
18 /* OR two ints together */ 6 /* two complement or */
19 int mp_or (mp_int * a, mp_int * b, mp_int * c) 7 mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c)
20 { 8 {
21 int res, ix, px; 9 int used = MP_MAX(a->used, b->used) + 1, i;
22 mp_int t, *x; 10 mp_err err;
11 mp_digit ac = 1, bc = 1, cc = 1;
12 mp_sign csign = ((a->sign == MP_NEG) || (b->sign == MP_NEG)) ? MP_NEG : MP_ZPOS;
23 13
24 if (a->used > b->used) { 14 if (c->alloc < used) {
25 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { 15 if ((err = mp_grow(c, used)) != MP_OKAY) {
26 return res; 16 return err;
27 } 17 }
28 px = b->used; 18 }
29 x = b;
30 } else {
31 if ((res = mp_init_copy (&t, b)) != MP_OKAY) {
32 return res;
33 }
34 px = a->used;
35 x = a;
36 }
37 19
38 for (ix = 0; ix < px; ix++) { 20 for (i = 0; i < used; i++) {
39 t.dp[ix] |= x->dp[ix]; 21 mp_digit x, y;
40 } 22
41 mp_clamp (&t); 23 /* convert to two complement if negative */
42 mp_exch (c, &t); 24 if (a->sign == MP_NEG) {
43 mp_clear (&t); 25 ac += (i >= a->used) ? MP_MASK : (~a->dp[i] & MP_MASK);
44 return MP_OKAY; 26 x = ac & MP_MASK;
27 ac >>= MP_DIGIT_BIT;
28 } else {
29 x = (i >= a->used) ? 0uL : a->dp[i];
30 }
31
32 /* convert to two complement if negative */
33 if (b->sign == MP_NEG) {
34 bc += (i >= b->used) ? MP_MASK : (~b->dp[i] & MP_MASK);
35 y = bc & MP_MASK;
36 bc >>= MP_DIGIT_BIT;
37 } else {
38 y = (i >= b->used) ? 0uL : b->dp[i];
39 }
40
41 c->dp[i] = x | y;
42
43 /* convert to to sign-magnitude if negative */
44 if (csign == MP_NEG) {
45 cc += ~c->dp[i] & MP_MASK;
46 c->dp[i] = cc & MP_MASK;
47 cc >>= MP_DIGIT_BIT;
48 }
49 }
50
51 c->used = used;
52 c->sign = csign;
53 mp_clamp(c);
54 return MP_OKAY;
45 } 55 }
46 #endif 56 #endif
47
48 /* ref: $Format:%D$ */
49 /* git commit: $Format:%H$ */
50 /* commit time: $Format:%ai$ */