Commit 86d7d718e3f6efdc921e4779f20395695d190222

Steffen Jaeckel 2019-11-14T14:22:06

Merge pull request #457 from libtom/wrong-use-of-ull-for-develop Wrong use of ull for develop

diff --git a/demo/test.c b/demo/test.c
index f153509..281255b 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -223,9 +223,9 @@ static int test_mp_get_set_i64(void)
 
    DOR(mp_init(&a));
 
-   check_get_set_i64(&a, 0LL);
-   check_get_set_i64(&a, -1LL);
-   check_get_set_i64(&a, 1LL);
+   check_get_set_i64(&a, 0);
+   check_get_set_i64(&a, -1);
+   check_get_set_i64(&a, 1);
    check_get_set_i64(&a, INT64_MIN);
    check_get_set_i64(&a, INT64_MAX);
 
@@ -618,14 +618,14 @@ LBL_ERR:
 
 static int test_mp_get_u32(void)
 {
-   unsigned long t;
+   uint32_t t;
    int i;
 
    mp_int a, b;
    DOR(mp_init_multi(&a, &b, NULL));
 
    for (i = 0; i < 1000; ++i) {
-      t = (unsigned long)rand_long() & 0xFFFFFFFFuL;
+      t = (uint32_t)rand_long();
       mp_set_ul(&a, t);
       if (t != mp_get_u32(&a)) {
          printf("\nmp_get_u32() bad result!");
@@ -637,8 +637,8 @@ static int test_mp_get_u32(void)
       printf("\nmp_get_u32() bad result!");
       goto LBL_ERR;
    }
-   mp_set_ul(&a, 0xFFFFFFFFuL);
-   if (mp_get_u32(&a) != 0xFFFFFFFFuL) {
+   mp_set_ul(&a, UINT32_MAX);
+   if (mp_get_u32(&a) != UINT32_MAX) {
       printf("\nmp_get_u32() bad result!");
       goto LBL_ERR;
    }
@@ -683,26 +683,26 @@ LBL_ERR:
 
 static int test_mp_get_u64(void)
 {
-   unsigned long long q, r;
+   uint64_t q, r;
    int i;
 
    mp_int a, b;
    DOR(mp_init_multi(&a, &b, NULL));
 
-   for (i = 0; i < (int)(MP_SIZEOF_BITS(unsigned long long) - 1); ++i) {
-      r = (1ULL << (i+1)) - 1;
+   for (i = 0; i < (int)(MP_SIZEOF_BITS(uint64_t) - 1); ++i) {
+      r = ((uint64_t)1 << (i+1)) - 1;
       if (!r)
-         r = ~0ULL;
-      printf(" r = 0x%llx i = %d\r", r, i);
+         r = UINT64_MAX;
+      printf(" r = 0x%" PRIx64 " i = %d\r", r, i);
       do {
          mp_set_u64(&a, r);
          q = mp_get_u64(&a);
          if (q != r) {
-            printf("\nmp_get_u64() bad result! 0x%llx != 0x%llx", q, r);
+            printf("\nmp_get_u64() bad result! 0x%" PRIx64 " != 0x%" PRIx64, q, r);
             goto LBL_ERR;
          }
          r <<= 1;
-      } while (r != 0uLL);
+      } while (r != 0u);
    }
 
    mp_clear_multi(&a, &b, NULL);
@@ -1411,7 +1411,7 @@ static mp_err s_rs(const mp_int *a, int radix, int *size)
       *size = mp_count_bits(a) + 1;
       return MP_OKAY;
    }
-   DOR(mp_init_copy(&t, a));
+   DO_WHAT(mp_init_copy(&t, a), return MP_ERR);
    t.sign = MP_ZPOS;
    while (!mp_iszero(&t)) {
       if ((res = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
@@ -2177,7 +2177,7 @@ static int test_mp_read_write_ubin(void)
 
    size = mp_ubin_size(&a);
    printf("mp_to_ubin_size  %zu - ", size);
-   buf = malloc(sizeof(*buf) * size);
+   buf = (uint8_t *)malloc(sizeof(*buf) * size);
    if (buf == NULL) {
       fprintf(stderr, "test_read_write_binaries (u) failed to allocate %zu bytes\n",
               sizeof(*buf) * size);
@@ -2215,7 +2215,7 @@ static int test_mp_read_write_sbin(void)
 
    size = mp_sbin_size(&a);
    printf("mp_to_sbin_size  %zu - ", size);
-   buf = malloc(sizeof(*buf) * size);
+   buf = (uint8_t *)malloc(sizeof(*buf) * size);
    if (buf == NULL) {
       fprintf(stderr, "test_read_write_binaries (s) failed to allocate %zu bytes\n",
               sizeof(*buf) * size);
@@ -2255,7 +2255,7 @@ static int test_mp_pack_unpack(void)
 
    count = mp_pack_count(&a, 0uL, 1uL);
 
-   buf = malloc(count);
+   buf = (uint8_t *)malloc(count);
    if (buf == NULL) {
       fprintf(stderr, "test_pack_unpack failed to allocate\n");
       goto LBL_ERR;
@@ -2346,7 +2346,7 @@ static int unit_tests(int argc, char **argv)
    ok = fail = nop = 0;
 
    t = (uint64_t)time(NULL);
-   printf("SEED: 0x%"PRIx64"\n\n", t);
+   printf("SEED: 0x%" PRIx64 "\n\n", t);
    s_mp_rand_jenkins_init(t);
    mp_rand_source(s_mp_rand_jenkins);
 
diff --git a/demo/timing.c b/demo/timing.c
index 4385a08..a66ce85 100644
--- a/demo/timing.c
+++ b/demo/timing.c
@@ -47,7 +47,7 @@ static unsigned long lfsr = 0xAAAAAAAAuL;
 static unsigned int lbit(void)
 {
    if ((lfsr & 0x80000000uL) != 0uL) {
-      lfsr = ((lfsr << 1) ^ 0x8000001BuL) & 0xFFFFFFFFuL;
+      lfsr = ((lfsr << 1) ^ 0x8000001BuL) & UINT32_MAX;
       return 1u;
    } else {
       lfsr <<= 1;
diff --git a/etc/tune.c b/etc/tune.c
index 0b73734..be06632 100644
--- a/etc/tune.c
+++ b/etc/tune.c
@@ -93,7 +93,7 @@ static uint64_t s_time_mul(int size)
          }
          if (mp_cmp(&c, &d) != MP_EQ) {
             /* Time of 0 cannot happen (famous last words?) */
-            t1 = 0uLL;
+            t1 = 0u;
             goto LBL_ERR;
          }
       }
@@ -134,7 +134,7 @@ static uint64_t s_time_sqr(int size)
             goto LBL_ERR;
          }
          if (mp_cmp(&c, &b) != MP_EQ) {
-            t1 = 0uLL;
+            t1 = 0u;
             goto LBL_ERR;
          }
       }
@@ -166,20 +166,20 @@ static void s_run(const char *name, uint64_t (*op)(int size), int *cutoff)
    for (x = 8; x < args.upper_limit_print; x += args.increment_print) {
       *cutoff = INT_MAX;
       t1 = op(x);
-      if ((t1 == 0uLL) || (t1 == UINT64_MAX)) {
+      if ((t1 == 0u) || (t1 == UINT64_MAX)) {
          fprintf(stderr,"%s failed at x = INT_MAX (%s)\n", name,
-                 (t1 == 0uLL)?"wrong result":"internal error");
+                 (t1 == 0u)?"wrong result":"internal error");
          exit(EXIT_FAILURE);
       }
       *cutoff = x;
       t2 = op(x);
-      if ((t2 == 0uLL) || (t2 == UINT64_MAX)) {
+      if ((t2 == 0u) || (t2 == UINT64_MAX)) {
          fprintf(stderr,"%s failed (%s)\n", name,
-                 (t2 == 0uLL)?"wrong result":"internal error");
+                 (t2 == 0u)?"wrong result":"internal error");
          exit(EXIT_FAILURE);
       }
       if (args.verbose == 1) {
-         printf("%d: %9"PRIu64" %9"PRIu64", %9"PRIi64"\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
+         printf("%d: %9" PRIu64 " %9" PRIu64 ", %9" PRIi64 "\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
       }
       if (t2 < t1) {
          if (count == s_stabilization_extra) {
@@ -504,20 +504,20 @@ int main(int argc, char **argv)
          t1 = s_time_mul(x);
          set_cutoffs(&orig);
          t2 = s_time_mul(x);
-         fprintf(multiplying, "%d: %9"PRIu64" %9"PRIu64", %9"PRIi64"\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
+         fprintf(multiplying, "%d: %9" PRIu64 " %9" PRIu64 ", %9" PRIi64 "\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
          fflush(multiplying);
          if (args.verbose == 1) {
-            printf("MUL %d: %9"PRIu64" %9"PRIu64", %9"PRIi64"\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
+            printf("MUL %d: %9" PRIu64 " %9" PRIu64 ", %9" PRIi64 "\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
             fflush(stdout);
          }
          set_cutoffs(&max_cutoffs);
          t1 = s_time_sqr(x);
          set_cutoffs(&orig);
          t2 = s_time_sqr(x);
-         fprintf(squaring,"%d: %9"PRIu64" %9"PRIu64", %9"PRIi64"\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
+         fprintf(squaring,"%d: %9" PRIu64 " %9" PRIu64 ", %9" PRIi64 "\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
          fflush(squaring);
          if (args.verbose == 1) {
-            printf("SQR %d: %9"PRIu64" %9"PRIu64", %9"PRIi64"\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
+            printf("SQR %d: %9" PRIu64 " %9" PRIu64 ", %9" PRIi64 "\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
             fflush(stdout);
          }
       }
diff --git a/makefile b/makefile
index 1ce63ee..0ce0a48 100644
--- a/makefile
+++ b/makefile
@@ -162,12 +162,12 @@ c89:
 	-e 's/bool/mp_bool/g' \
 	-e 's/true/MP_YES/g' \
 	-e 's/false/MP_NO/g' \
-	-e 's/UINT32_MAX/0xFFFFFFFFU/g' \
-	-e 's/UINT64_MAX/0xFFFFFFFFFFFFFFFFULL/g' \
-	-e 's/INT32_MAX/2147483647/g' \
+	-e 's/UINT32_MAX/0xFFFFFFFFu/g' \
+	-e 's/UINT64_MAX/(mp_u64)-1/g' \
+	-e 's/INT32_MAX/0x7FFFFFFF/g' \
         -e 's/INT32_MIN/(-2147483647-1)/g' \
-	-e 's/INT64_MAX/((mp_i64)9223372036854775807LL)/g' \
-	-e 's/INT64_MIN/((mp_i64)-9223372036854775807LL-1)/g' \
+	-e 's/INT64_MAX/(mp_i64)(((mp_u64)1<<63)-1)/g' \
+	-e 's/INT64_MIN/(mp_i64)((mp_u64)1<<63)/g' \
 	-e 's/SIZE_MAX/((size_t)-1)/g' \
 	-e 's/\(PRI[iux]64\)/MP_\1/g' \
 	-e 's/uint\([0-9][0-9]*\)_t/mp_u\1/g' \
@@ -184,12 +184,12 @@ c99:
 	-e 's/MP_YES/true/g' \
 	-e 's/MP_NO/false/g' \
 	-e 's/false_/MP_NO_/g' \
-	-e 's/0xFFFFFFFFU/UINT32_MAX/g' \
-	-e 's/0xFFFFFFFFFFFFFFFFULL/UINT64_MAX/g' \
+	-e 's/0xFFFFFFFFu/UINT32_MAX/g' \
+	-e 's/(mp_u64)-1/UINT64_MAX/g' \
         -e 's/(-2147483647-1)/INT32_MIN/g' \
-	-e 's/2147483647/INT32_MAX/g' \
-	-e 's/((mp_i64)-9223372036854775807LL-1)/INT64_MIN/g' \
-	-e 's/((mp_i64)9223372036854775807LL)/INT64_MAX/g' \
+	-e 's/0x7FFFFFFF/INT32_MAX/g' \
+	-e 's/(mp_i64)((mp_u64)1<<63)/INT64_MIN/g' \
+	-e 's/(mp_i64)(((mp_u64)1<<63)-1)/INT64_MAX/g' \
 	-e 's/((size_t)-1)/SIZE_MAX/g' \
 	-e 's/MP_\(PRI[iux]64\)/\1/g' \
 	-e 's/mp_u\([0-9][0-9]*\)/uint\1_t/g' \
diff --git a/mp_set_double.c b/mp_set_double.c
index 467d7fd..78550c8 100644
--- a/mp_set_double.c
+++ b/mp_set_double.c
@@ -16,7 +16,7 @@ mp_err mp_set_double(mp_int *a, double b)
    cast.dbl = b;
 
    exp = (int)((unsigned)(cast.bits >> 52) & 0x7FFu);
-   frac = (cast.bits & ((1uLL << 52) - 1uLL)) | (1uLL << 52);
+   frac = (cast.bits & (((uint64_t)1 << 52) - (uint64_t)1)) | ((uint64_t)1 << 52);
 
    if (exp == 0x7FF) { /* +-inf, NaN */
       return MP_VAL;
@@ -30,7 +30,7 @@ mp_err mp_set_double(mp_int *a, double b)
       return err;
    }
 
-   if (((cast.bits >> 63) != 0uLL) && !mp_iszero(a)) {
+   if (((cast.bits >> 63) != 0u) && !mp_iszero(a)) {
       a->sign = MP_NEG;
    }
 
diff --git a/s_mp_rand_jenkins.c b/s_mp_rand_jenkins.c
index 3a905ba..6aba0fb 100644
--- a/s_mp_rand_jenkins.c
+++ b/s_mp_rand_jenkins.c
@@ -28,7 +28,7 @@ static uint64_t s_rand_jenkins_val(void)
 void s_mp_rand_jenkins_init(uint64_t seed)
 {
    int i;
-   jenkins_x.a = 0xF1EA5EEDuLL;
+   jenkins_x.a = 0xF1EA5EEDuL;
    jenkins_x.b = jenkins_x.c = jenkins_x.d = seed;
    for (i = 0; i < 20; ++i) {
       (void)s_rand_jenkins_val();
@@ -42,7 +42,7 @@ mp_err s_mp_rand_jenkins(void *p, size_t n)
       int i;
       uint64_t x = s_rand_jenkins_val();
       for (i = 0; (i < 8) && (n > 0u); ++i, --n) {
-         *q++ = (char)(x & 0xFFuLL);
+         *q++ = (char)(x & 0xFFu);
          x >>= 8;
       }
    }