fix undefined behavior in labs
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
diff --git a/demo/test.c b/demo/test.c
index d036e1d..c4314d0 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -21,6 +21,11 @@ static int rand_int(void)
return x;
}
+static unsigned long ulabs(long x)
+{
+ return x > 0 ? (unsigned long)x : -(unsigned long)x;
+}
+
static int test_trivial_stuff(void)
{
mp_int a, b, c, d;
@@ -282,13 +287,13 @@ static int test_mp_complement(void)
for (i = 0; i < 1000; ++i) {
long l = rand_long();
- mp_set_long(&a, (unsigned long)labs(l));
+ mp_set_long(&a, ulabs(l));
if (l < 0)
mp_neg(&a, &a);
mp_complement(&a, &b);
l = ~l;
- mp_set_long(&c, (unsigned long)labs(l));
+ mp_set_long(&c, ulabs(l));
if (l < 0)
mp_neg(&c, &c);
@@ -319,13 +324,13 @@ static int test_mp_tc_div_2d(void)
int em;
l = rand_long();
- mp_set_long(&a, (unsigned long)labs(l));
+ mp_set_long(&a, ulabs(l));
if (l < 0)
mp_neg(&a, &a);
em = abs(rand_int()) % 32;
- mp_set_long(&d, (unsigned long)labs(l >> em));
+ mp_set_long(&d, ulabs(l >> em));
if ((l >> em) < 0)
mp_neg(&d, &d);
@@ -357,16 +362,16 @@ static int test_mp_tc_xor(void)
long l, em;
l = rand_long();
- mp_set_int(&a, (unsigned long)labs(l));
+ mp_set_int(&a, ulabs(l));
if (l < 0)
mp_neg(&a, &a);
em = rand_long();
- mp_set_int(&b, (unsigned long)labs(em));
+ mp_set_int(&b, ulabs(em));
if (em < 0)
mp_neg(&b, &b);
- mp_set_int(&d, (unsigned long)labs(l ^ em));
+ mp_set_int(&d, ulabs(l ^ em));
if ((l ^ em) < 0)
mp_neg(&d, &d);
@@ -398,16 +403,16 @@ static int test_mp_tc_or(void)
long l, em;
l = rand_long();
- mp_set_long(&a, (unsigned long)labs(l));
+ mp_set_long(&a, ulabs(l));
if (l < 0)
mp_neg(&a, &a);
em = rand_long();
- mp_set_long(&b, (unsigned long)labs(em));
+ mp_set_long(&b, ulabs(em));
if (em < 0)
mp_neg(&b, &b);
- mp_set_long(&d, (unsigned long)labs(l | em));
+ mp_set_long(&d, ulabs(l | em));
if ((l | em) < 0)
mp_neg(&d, &d);
@@ -438,16 +443,16 @@ static int test_mp_tc_and(void)
long l, em;
l = rand_long();
- mp_set_long(&a, (unsigned long)labs(l));
+ mp_set_long(&a, ulabs(l));
if (l < 0)
mp_neg(&a, &a);
em = rand_long();
- mp_set_long(&b, (unsigned long)labs(em));
+ mp_set_long(&b, ulabs(em));
if (em < 0)
mp_neg(&b, &b);
- mp_set_long(&d, (unsigned long)labs(l & em));
+ mp_set_long(&d, ulabs(l & em));
if ((l & em) < 0)
mp_neg(&d, &d);