gnucash master: C++-numeric: Fix up some 64-bit and GCC problems.

John Ralls jralls at code.gnucash.org
Sun Dec 7 14:43:17 EST 2014


Updated	 via  https://github.com/Gnucash/gnucash/commit/495c939c (commit)
	from  https://github.com/Gnucash/gnucash/commit/ae7e9c24 (commit)



commit 495c939c3eed8288b664e804411f8d88b5dcc747
Author: John Ralls <jralls at ceridwen.us>
Date:   Sun Dec 7 11:42:39 2014 -0800

    C++-numeric: Fix up some 64-bit and GCC problems.
    
    Still won't compile on gcc-4.7 (f18) because of poor C++11 compliance wrt UINT64_MAX, UINT64_C, and PRIu_64 macros.

diff --git a/src/engine/test-core/test-engine-stuff.c b/src/engine/test-core/test-engine-stuff.c
index 9a5ad6a..08f9963 100644
--- a/src/engine/test-core/test-engine-stuff.c
+++ b/src/engine/test-core/test-engine-stuff.c
@@ -22,6 +22,8 @@
 #include <fcntl.h>
 #include <glib.h>
 #include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/stat.h>
@@ -414,7 +416,7 @@ gnc_numeric
 get_random_gnc_numeric(int64_t deno)
 {
     gint64 numer;
-
+    int64_t limit;
     if (deno == GNC_DENOM_AUTO)
     {
         if (RAND_MAX / 8 > rand())
@@ -444,7 +446,7 @@ get_random_gnc_numeric(int64_t deno)
      * the numerator is clamped to the larger of num_limit / deno or num_limit /
      * max_denom_mult.
      */
-    const int64_t limit = num_limit / (max_denom_mult / deno == 0 ? max_denom_mult : max_denom_mult / deno);
+    limit = num_limit / (max_denom_mult / deno == 0 ? max_denom_mult : max_denom_mult / deno);
     numer = get_random_gint64 ();
     if (numer > limit)
     {
@@ -455,7 +457,7 @@ get_random_gnc_numeric(int64_t deno)
              numer = limit;
     }
     if (0 == numer) numer = 1;
-    g_log("test.engine.suff", G_LOG_LEVEL_INFO, "New GncNumeric %lld / %lld !\n", numer, deno);
+    g_log("test.engine.suff", G_LOG_LEVEL_INFO, "New GncNumeric %" PRIu64 " / %" PRIu64 " !\n", numer, deno);
     return gnc_numeric_create(numer, deno);
 }
 
@@ -942,7 +944,8 @@ add_random_splits(QofBook *book, Transaction *trn, GList *account_list)
     if (do_bork())
     {
         val = get_random_gnc_numeric(GNC_DENOM_AUTO);
-        g_log ("test.engine.suff", G_LOG_LEVEL_DEBUG, "Borking second %lld / %lld, scu %d\n", val.num, val.denom, s2_scu);
+        g_log ("test.engine.suff", G_LOG_LEVEL_DEBUG, "Borking second %" PRIu64
+	       " / %" PRIu64 ", scu %d\n", val.num, val.denom, s2_scu);
     }
     val = gnc_numeric_neg(val);
     xaccSplitSetValue(s2, val);
@@ -1306,14 +1309,18 @@ get_random_split(QofBook *book, Account *acct, Transaction *trn)
             if (val.denom > scu && val.num > num_limit / (max_denom_mult / scu))
             {
                 int64_t new_num = val.num / (val.denom / scu);
-                g_log("test.engine.suff", G_LOG_LEVEL_DEBUG, "Adjusting val.denom from %lld to %lld\n", val.num, new_num);
+                g_log("test.engine.suff", G_LOG_LEVEL_DEBUG,
+		      "Adjusting val.denom from %" PRIu64 " to %" PRIu64 "\n",
+		      val.num, new_num);
                 val.num = new_num;
             }
             val.denom = scu;
         }
     }
     while (gnc_numeric_check(val) != GNC_ERROR_OK);
-    g_log ("test.engine.suff", G_LOG_LEVEL_DEBUG, "Random split value: %lld / %lld, scu %d\n", val.num, val.denom, scu);
+    g_log ("test.engine.suff", G_LOG_LEVEL_DEBUG,
+	   "Random split value: %" PRIu64 " / %" PRIu64 ", scu %d\n",
+	   val.num, val.denom, scu);
     xaccSplitSetValue(ret, val);
 
     /* If the currencies are the same, the split amount should equal
@@ -1335,7 +1342,9 @@ get_random_split(QofBook *book, Account *acct, Transaction *trn)
         }
         while (gnc_numeric_check(amt) != GNC_ERROR_OK);
     }
-    g_log ("test.engine.suff", G_LOG_LEVEL_DEBUG, "Random split amount: %lld / %lld, rate %lld / %lld\n", amt.num, amt.denom, rate.num, rate.denom);
+    g_log ("test.engine.suff", G_LOG_LEVEL_DEBUG, "Random split amount: %"
+	   PRIu64 " / %" PRIu64 ", rate %" PRIu64 " / %" PRIu64 "\n",
+	   amt.num, amt.denom, rate.num, rate.denom);
 
 
      xaccSplitSetAmount(ret, amt);
diff --git a/src/libqof/qof/gnc-int128.cpp b/src/libqof/qof/gnc-int128.cpp
index d87b5b3..3b9997c 100644
--- a/src/libqof/qof/gnc-int128.cpp
+++ b/src/libqof/qof/gnc-int128.cpp
@@ -154,7 +154,7 @@ GncInt128::gcd(GncInt128 b) const noexcept
     GncInt128 t {a & one ? -b : a}; //B2
     while (a != b)
     {
-        while (t && (t & one ^ one)) t >>= 1;  //B3 & B4
+        while (t && ((t & one) ^ one)) t >>= 1;  //B3 & B4
         if (t.isNeg())  //B5
             b = -t;
         else
@@ -439,7 +439,7 @@ GncInt128::operator*= (const GncInt128& b) noexcept
     uint64_t bv[sublegs] {(b.m_lo & sublegmask), (b.m_lo >> sublegbits),
             (b.m_hi & sublegmask), (b.m_hi >> sublegbits)};
     uint64_t rv[sublegs] {};
-    int64_t carry {}, scratch {};
+    uint64_t carry {}, scratch {};
 
     rv[0] = av[0] * bv[0];
 
@@ -495,7 +495,7 @@ div_multi_leg (uint64_t* u, size_t m, uint64_t* v, size_t n, GncInt128& q, GncIn
     uint64_t d {(UINT64_C(1) << sublegbits)/(v[n - 1] + UINT64_C(1))};
     uint64_t carry {UINT64_C(0)};
     bool negative {q.isNeg()};
-    for (auto i = 0; i < m; ++i)
+    for (size_t i = 0; i < m; ++i)
     {
         u[i] = u[i] * d + carry;
         if (u[i] > sublegmask)
@@ -512,7 +512,7 @@ div_multi_leg (uint64_t* u, size_t m, uint64_t* v, size_t n, GncInt128& q, GncIn
         u[m++] = carry;
         carry = UINT64_C(0);
     }
-    for (auto i = 0; i < n; ++i)
+    for (size_t i = 0; i < n; ++i)
     {
         v[i] = v[i] * d + carry;
         if (v[i] > sublegmask)
@@ -527,7 +527,7 @@ div_multi_leg (uint64_t* u, size_t m, uint64_t* v, size_t n, GncInt128& q, GncIn
     assert (carry == UINT64_C(0));
     for (int j = m - n; j >= 0; j--) //D3
     {
-        int64_t qhat, rhat;
+        uint64_t qhat, rhat;
         qhat = ((u[j + n] << sublegbits) + u[j + n - 1]) / v[n - 1];
         rhat = ((u[j + n] << sublegbits) + u[j + n - 1]) % v[n - 1];
 
@@ -540,7 +540,7 @@ div_multi_leg (uint64_t* u, size_t m, uint64_t* v, size_t n, GncInt128& q, GncIn
         }
         carry = UINT64_C(0);
         uint64_t borrow {};
-        for (auto k = 0; k < n; ++k) //D4
+        for (size_t k = 0; k < n; ++k) //D4
         {
             auto subend = qhat * v[k] + carry;
             carry = subend >> sublegbits;
@@ -566,7 +566,7 @@ div_multi_leg (uint64_t* u, size_t m, uint64_t* v, size_t n, GncInt128& q, GncIn
         { //D6
             --qv[j];
             carry = UINT64_C(0);
-            for (auto k = 0; k < n; ++k)
+            for (size_t k = 0; k < n; ++k)
             {
                 u[j + k] += v[k] + carry;
                 if (u[j + k] > sublegmask)



Summary of changes:
 src/engine/test-core/test-engine-stuff.c | 23 ++++++++++++++++-------
 src/libqof/qof/gnc-int128.cpp            | 14 +++++++-------
 2 files changed, 23 insertions(+), 14 deletions(-)



More information about the gnucash-changes mailing list