Mercurial > trustbridge > nss-cmake-static
annotate nss/lib/freebl/mpi/mplogic.c @ 4:b513267f632f tip
Build DBM module
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Tue, 05 Aug 2014 18:58:03 +0200 |
parents | 1e5118fa0cb1 |
children |
rev | line source |
---|---|
0
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
1 /* |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
2 * mplogic.c |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
3 * |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
4 * Bitwise logical operations on MPI values |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
5 * |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
6 * This Source Code Form is subject to the terms of the Mozilla Public |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
7 * License, v. 2.0. If a copy of the MPL was not distributed with this |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
9 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
10 #include "mpi-priv.h" |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
11 #include "mplogic.h" |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
12 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
13 /* {{{ Lookup table for population count */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
14 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
15 static unsigned char bitc[] = { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
16 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
17 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
18 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
19 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
20 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
21 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
22 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
23 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
24 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
25 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
26 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
27 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
28 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
29 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
30 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
31 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
32 }; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
33 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
34 /* }}} */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
35 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
36 /*------------------------------------------------------------------------*/ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
37 /* |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
38 mpl_not(a, b) - compute b = ~a |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
39 mpl_and(a, b, c) - compute c = a & b |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
40 mpl_or(a, b, c) - compute c = a | b |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
41 mpl_xor(a, b, c) - compute c = a ^ b |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
42 */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
43 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
44 /* {{{ mpl_not(a, b) */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
45 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
46 mp_err mpl_not(mp_int *a, mp_int *b) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
47 { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
48 mp_err res; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
49 unsigned int ix; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
50 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
51 ARGCHK(a != NULL && b != NULL, MP_BADARG); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
52 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
53 if((res = mp_copy(a, b)) != MP_OKAY) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
54 return res; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
55 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
56 /* This relies on the fact that the digit type is unsigned */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
57 for(ix = 0; ix < USED(b); ix++) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
58 DIGIT(b, ix) = ~DIGIT(b, ix); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
59 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
60 s_mp_clamp(b); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
61 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
62 return MP_OKAY; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
63 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
64 } /* end mpl_not() */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
65 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
66 /* }}} */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
67 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
68 /* {{{ mpl_and(a, b, c) */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
69 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
70 mp_err mpl_and(mp_int *a, mp_int *b, mp_int *c) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
71 { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
72 mp_int *which, *other; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
73 mp_err res; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
74 unsigned int ix; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
75 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
76 ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
77 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
78 if(USED(a) <= USED(b)) { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
79 which = a; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
80 other = b; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
81 } else { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
82 which = b; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
83 other = a; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
84 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
85 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
86 if((res = mp_copy(which, c)) != MP_OKAY) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
87 return res; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
88 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
89 for(ix = 0; ix < USED(which); ix++) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
90 DIGIT(c, ix) &= DIGIT(other, ix); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
91 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
92 s_mp_clamp(c); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
93 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
94 return MP_OKAY; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
95 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
96 } /* end mpl_and() */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
97 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
98 /* }}} */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
99 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
100 /* {{{ mpl_or(a, b, c) */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
101 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
102 mp_err mpl_or(mp_int *a, mp_int *b, mp_int *c) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
103 { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
104 mp_int *which, *other; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
105 mp_err res; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
106 unsigned int ix; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
107 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
108 ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
109 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
110 if(USED(a) >= USED(b)) { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
111 which = a; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
112 other = b; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
113 } else { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
114 which = b; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
115 other = a; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
116 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
117 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
118 if((res = mp_copy(which, c)) != MP_OKAY) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
119 return res; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
120 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
121 for(ix = 0; ix < USED(which); ix++) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
122 DIGIT(c, ix) |= DIGIT(other, ix); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
123 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
124 return MP_OKAY; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
125 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
126 } /* end mpl_or() */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
127 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
128 /* }}} */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
129 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
130 /* {{{ mpl_xor(a, b, c) */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
131 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
132 mp_err mpl_xor(mp_int *a, mp_int *b, mp_int *c) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
133 { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
134 mp_int *which, *other; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
135 mp_err res; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
136 unsigned int ix; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
137 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
138 ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
139 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
140 if(USED(a) >= USED(b)) { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
141 which = a; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
142 other = b; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
143 } else { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
144 which = b; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
145 other = a; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
146 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
147 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
148 if((res = mp_copy(which, c)) != MP_OKAY) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
149 return res; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
150 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
151 for(ix = 0; ix < USED(which); ix++) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
152 DIGIT(c, ix) ^= DIGIT(other, ix); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
153 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
154 s_mp_clamp(c); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
155 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
156 return MP_OKAY; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
157 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
158 } /* end mpl_xor() */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
159 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
160 /* }}} */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
161 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
162 /*------------------------------------------------------------------------*/ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
163 /* |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
164 mpl_rsh(a, b, d) - b = a >> d |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
165 mpl_lsh(a, b, d) - b = a << d |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
166 */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
167 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
168 /* {{{ mpl_rsh(a, b, d) */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
169 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
170 mp_err mpl_rsh(const mp_int *a, mp_int *b, mp_digit d) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
171 { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
172 mp_err res; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
173 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
174 ARGCHK(a != NULL && b != NULL, MP_BADARG); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
175 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
176 if((res = mp_copy(a, b)) != MP_OKAY) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
177 return res; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
178 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
179 s_mp_div_2d(b, d); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
180 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
181 return MP_OKAY; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
182 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
183 } /* end mpl_rsh() */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
184 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
185 /* }}} */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
186 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
187 /* {{{ mpl_lsh(a, b, d) */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
188 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
189 mp_err mpl_lsh(const mp_int *a, mp_int *b, mp_digit d) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
190 { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
191 mp_err res; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
192 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
193 ARGCHK(a != NULL && b != NULL, MP_BADARG); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
194 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
195 if((res = mp_copy(a, b)) != MP_OKAY) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
196 return res; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
197 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
198 return s_mp_mul_2d(b, d); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
199 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
200 } /* end mpl_lsh() */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
201 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
202 /* }}} */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
203 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
204 /*------------------------------------------------------------------------*/ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
205 /* |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
206 mpl_num_set(a, num) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
207 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
208 Count the number of set bits in the binary representation of a. |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
209 Returns MP_OKAY and sets 'num' to be the number of such bits, if |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
210 possible. If num is NULL, the result is thrown away, but it is |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
211 not considered an error. |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
212 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
213 mpl_num_clear() does basically the same thing for clear bits. |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
214 */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
215 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
216 /* {{{ mpl_num_set(a, num) */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
217 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
218 mp_err mpl_num_set(mp_int *a, int *num) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
219 { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
220 unsigned int ix; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
221 int db, nset = 0; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
222 mp_digit cur; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
223 unsigned char reg; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
224 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
225 ARGCHK(a != NULL, MP_BADARG); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
226 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
227 for(ix = 0; ix < USED(a); ix++) { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
228 cur = DIGIT(a, ix); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
229 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
230 for(db = 0; db < sizeof(mp_digit); db++) { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
231 reg = (unsigned char)(cur >> (CHAR_BIT * db)); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
232 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
233 nset += bitc[reg]; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
234 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
235 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
236 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
237 if(num) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
238 *num = nset; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
239 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
240 return MP_OKAY; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
241 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
242 } /* end mpl_num_set() */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
243 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
244 /* }}} */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
245 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
246 /* {{{ mpl_num_clear(a, num) */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
247 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
248 mp_err mpl_num_clear(mp_int *a, int *num) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
249 { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
250 unsigned int ix; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
251 int db, nset = 0; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
252 mp_digit cur; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
253 unsigned char reg; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
254 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
255 ARGCHK(a != NULL, MP_BADARG); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
256 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
257 for(ix = 0; ix < USED(a); ix++) { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
258 cur = DIGIT(a, ix); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
259 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
260 for(db = 0; db < sizeof(mp_digit); db++) { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
261 reg = (unsigned char)(cur >> (CHAR_BIT * db)); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
262 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
263 nset += bitc[UCHAR_MAX - reg]; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
264 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
265 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
266 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
267 if(num) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
268 *num = nset; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
269 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
270 return MP_OKAY; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
271 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
272 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
273 } /* end mpl_num_clear() */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
274 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
275 /* }}} */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
276 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
277 /*------------------------------------------------------------------------*/ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
278 /* |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
279 mpl_parity(a) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
280 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
281 Determines the bitwise parity of the value given. Returns MP_EVEN |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
282 if an even number of digits are set, MP_ODD if an odd number are |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
283 set. |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
284 */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
285 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
286 /* {{{ mpl_parity(a) */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
287 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
288 mp_err mpl_parity(mp_int *a) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
289 { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
290 unsigned int ix; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
291 int par = 0; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
292 mp_digit cur; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
293 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
294 ARGCHK(a != NULL, MP_BADARG); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
295 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
296 for(ix = 0; ix < USED(a); ix++) { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
297 int shft = (sizeof(mp_digit) * CHAR_BIT) / 2; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
298 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
299 cur = DIGIT(a, ix); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
300 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
301 /* Compute parity for current digit */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
302 while(shft != 0) { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
303 cur ^= (cur >> shft); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
304 shft >>= 1; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
305 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
306 cur &= 1; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
307 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
308 /* XOR with running parity so far */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
309 par ^= cur; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
310 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
311 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
312 if(par) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
313 return MP_ODD; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
314 else |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
315 return MP_EVEN; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
316 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
317 } /* end mpl_parity() */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
318 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
319 /* }}} */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
320 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
321 /* |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
322 mpl_set_bit |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
323 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
324 Returns MP_OKAY or some error code. |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
325 Grows a if needed to set a bit to 1. |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
326 */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
327 mp_err mpl_set_bit(mp_int *a, mp_size bitNum, mp_size value) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
328 { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
329 mp_size ix; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
330 mp_err rv; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
331 mp_digit mask; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
332 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
333 ARGCHK(a != NULL, MP_BADARG); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
334 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
335 ix = bitNum / MP_DIGIT_BIT; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
336 if (ix + 1 > MP_USED(a)) { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
337 rv = s_mp_pad(a, ix + 1); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
338 if (rv != MP_OKAY) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
339 return rv; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
340 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
341 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
342 bitNum = bitNum % MP_DIGIT_BIT; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
343 mask = (mp_digit)1 << bitNum; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
344 if (value) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
345 MP_DIGIT(a,ix) |= mask; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
346 else |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
347 MP_DIGIT(a,ix) &= ~mask; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
348 s_mp_clamp(a); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
349 return MP_OKAY; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
350 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
351 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
352 /* |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
353 mpl_get_bit |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
354 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
355 returns 0 or 1 or some (negative) error code. |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
356 */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
357 mp_err mpl_get_bit(const mp_int *a, mp_size bitNum) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
358 { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
359 mp_size bit, ix; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
360 mp_err rv; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
361 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
362 ARGCHK(a != NULL, MP_BADARG); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
363 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
364 ix = bitNum / MP_DIGIT_BIT; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
365 ARGCHK(ix <= MP_USED(a) - 1, MP_RANGE); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
366 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
367 bit = bitNum % MP_DIGIT_BIT; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
368 rv = (mp_err)(MP_DIGIT(a, ix) >> bit) & 1; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
369 return rv; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
370 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
371 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
372 /* |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
373 mpl_get_bits |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
374 - Extracts numBits bits from a, where the least significant extracted bit |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
375 is bit lsbNum. Returns a negative value if error occurs. |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
376 - Because sign bit is used to indicate error, maximum number of bits to |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
377 be returned is the lesser of (a) the number of bits in an mp_digit, or |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
378 (b) one less than the number of bits in an mp_err. |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
379 - lsbNum + numbits can be greater than the number of significant bits in |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
380 integer a, as long as bit lsbNum is in the high order digit of a. |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
381 */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
382 mp_err mpl_get_bits(const mp_int *a, mp_size lsbNum, mp_size numBits) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
383 { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
384 mp_size rshift = (lsbNum % MP_DIGIT_BIT); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
385 mp_size lsWndx = (lsbNum / MP_DIGIT_BIT); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
386 mp_digit * digit = MP_DIGITS(a) + lsWndx; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
387 mp_digit mask = ((1 << numBits) - 1); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
388 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
389 ARGCHK(numBits < CHAR_BIT * sizeof mask, MP_BADARG); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
390 ARGCHK(MP_HOWMANY(lsbNum, MP_DIGIT_BIT) <= MP_USED(a), MP_RANGE); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
391 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
392 if ((numBits + lsbNum % MP_DIGIT_BIT <= MP_DIGIT_BIT) || |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
393 (lsWndx + 1 >= MP_USED(a))) { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
394 mask &= (digit[0] >> rshift); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
395 } else { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
396 mask &= ((digit[0] >> rshift) | (digit[1] << (MP_DIGIT_BIT - rshift))); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
397 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
398 return (mp_err)mask; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
399 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
400 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
401 /* |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
402 mpl_significant_bits |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
403 returns number of significnant bits in abs(a). |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
404 returns 1 if value is zero. |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
405 */ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
406 mp_err mpl_significant_bits(const mp_int *a) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
407 { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
408 mp_err bits = 0; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
409 int ix; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
410 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
411 ARGCHK(a != NULL, MP_BADARG); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
412 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
413 ix = MP_USED(a); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
414 for (ix = MP_USED(a); ix > 0; ) { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
415 mp_digit d; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
416 d = MP_DIGIT(a, --ix); |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
417 if (d) { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
418 while (d) { |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
419 ++bits; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
420 d >>= 1; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
421 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
422 break; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
423 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
424 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
425 bits += ix * MP_DIGIT_BIT; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
426 if (!bits) |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
427 bits = 1; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
428 return bits; |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
429 } |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
430 |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
431 /*------------------------------------------------------------------------*/ |
1e5118fa0cb1
This is NSS with a Cmake Buildsyste
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
432 /* HERE THERE BE DRAGONS */ |