Simplify test-cases, by using mp_set_l. Also fix use of types in 2 other files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
diff --git a/demo/test.c b/demo/test.c
index 41e0394..5e8b8a1 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -40,11 +40,6 @@ static int64_t rand_int64(void)
return x;
}
-static unsigned long ulabs(long x)
-{
- return x > 0 ? (unsigned long)x : -(unsigned long)x;
-}
-
static uint32_t uabs32(int32_t x)
{
return x > 0 ? (uint32_t)x : -(uint32_t)x;
@@ -351,20 +346,10 @@ static int test_mp_kronecker(void)
}
for (cnt = 0; cnt < (int)(sizeof(kronecker)/sizeof(kronecker[0])); ++cnt) {
k = kronecker[cnt].n;
- if (k < 0) {
- mp_set_ul(&a, (unsigned long)(-k));
- mp_neg(&a, &a);
- } else {
- mp_set_ul(&a, (unsigned long) k);
- }
+ mp_set_l(&a, k);
/* only test positive values of a */
for (m = -10; m <= 10; m++) {
- if (m < 0) {
- mp_set_ul(&b,(unsigned long)(-m));
- mp_neg(&b, &b);
- } else {
- mp_set_ul(&b, (unsigned long) m);
- }
+ mp_set_l(&b, m);
if ((err = mp_kronecker(&a, &b, &i)) != MP_OKAY) {
printf("Failed executing mp_kronecker(%ld | %ld) %s.\n", kronecker[cnt].n, m, mp_error_to_string(err));
goto LBL_ERR;
@@ -394,15 +379,11 @@ static int test_mp_complement(void)
for (i = 0; i < 1000; ++i) {
long l = rand_long();
- mp_set_ul(&a, ulabs(l));
- if (l < 0)
- mp_neg(&a, &a);
+ mp_set_l(&a, l);
mp_complement(&a, &b);
l = ~l;
- mp_set_ul(&c, ulabs(l));
- if (l < 0)
- mp_neg(&c, &c);
+ mp_set_l(&c, l);
if (mp_cmp(&b, &c) != MP_EQ) {
printf("\nmp_complement() bad result!");
@@ -431,15 +412,11 @@ static int test_mp_signed_rsh(void)
int em;
l = rand_long();
- mp_set_ul(&a, ulabs(l));
- if (l < 0)
- mp_neg(&a, &a);
+ mp_set_l(&a, l);
em = abs(rand_int()) % 32;
- mp_set_ul(&d, ulabs(l >> em));
- if ((l >> em) < 0)
- mp_neg(&d, &d);
+ mp_set_l(&d, l >> em);
mp_signed_rsh(&a, em, &b);
if (mp_cmp(&b, &d) != MP_EQ) {
@@ -469,18 +446,12 @@ static int test_mp_xor(void)
long l, em;
l = rand_long();
- mp_set_ul(&a, ulabs(l));
- if (l < 0)
- mp_neg(&a, &a);
+ mp_set_l(&a,l);
em = rand_long();
- mp_set_ul(&b, ulabs(em));
- if (em < 0)
- mp_neg(&b, &b);
+ mp_set_l(&b, em);
- mp_set_ul(&d, ulabs(l ^ em));
- if ((l ^ em) < 0)
- mp_neg(&d, &d);
+ mp_set_l(&d, l ^ em);
mp_xor(&a, &b, &c);
if (mp_cmp(&c, &d) != MP_EQ) {
@@ -510,18 +481,12 @@ static int test_mp_or(void)
long l, em;
l = rand_long();
- mp_set_ul(&a, ulabs(l));
- if (l < 0)
- mp_neg(&a, &a);
+ mp_set_l(&a, l);
em = rand_long();
- mp_set_ul(&b, ulabs(em));
- if (em < 0)
- mp_neg(&b, &b);
+ mp_set_l(&b, em);
- mp_set_ul(&d, ulabs(l | em));
- if ((l | em) < 0)
- mp_neg(&d, &d);
+ mp_set_l(&d, l | em);
mp_or(&a, &b, &c);
if (mp_cmp(&c, &d) != MP_EQ) {
@@ -550,18 +515,12 @@ static int test_mp_and(void)
long l, em;
l = rand_long();
- mp_set_ul(&a, ulabs(l));
- if (l < 0)
- mp_neg(&a, &a);
+ mp_set_l(&a, l);
em = rand_long();
- mp_set_ul(&b, ulabs(em));
- if (em < 0)
- mp_neg(&b, &b);
+ mp_set_l(&b, em);
- mp_set_ul(&d, ulabs(l & em));
- if ((l & em) < 0)
- mp_neg(&d, &d);
+ mp_set_l(&d, l & em);
mp_and(&a, &b, &c);
if (mp_cmp(&c, &d) != MP_EQ) {
diff --git a/etc/mersenne.c b/etc/mersenne.c
index dfefe23..f6d7cb6 100644
--- a/etc/mersenne.c
+++ b/etc/mersenne.c
@@ -123,7 +123,7 @@ int main(void)
tt = clock() - tt;
/* display if prime */
- printf("2^%-5ld - 1 is prime, test took %ld ticks\n", k, tt);
+ printf("2^%-5ld - 1 is prime, test took %ld ticks\n", k, (long)tt);
}
/* goto next odd exponent */
diff --git a/etc/pprime.c b/etc/pprime.c
index 68f882a..4c60331 100644
--- a/etc/pprime.c
+++ b/etc/pprime.c
@@ -398,7 +398,7 @@ int main(void)
pprime(k, li, &p, &q);
t1 = clock() - t1;
- printf("\n\nTook %ld ticks, %d bits\n", t1, mp_count_bits(&p));
+ printf("\n\nTook %d ticks, %d bits\n", t1, mp_count_bits(&p));
mp_toradix(&p, buf, 10);
printf("P == %s\n", buf);