Fix bitmask printing
authorRoland Schulz <roland.schulz@intel.com>
Tue, 18 Sep 2018 04:56:35 +0000 (21:56 -0700)
committerMark Abraham <mark.j.abraham@gmail.com>
Tue, 18 Sep 2018 11:16:19 +0000 (13:16 +0200)
Change-Id: I0e9294ad734dfd682cfea3a31011a9b9c7fb9034

src/gromacs/listed-forces/manage-threading.cpp
src/gromacs/utility/bitmask.h
src/gromacs/utility/tests/bitmask.h

index af964153709b962094720e5323b7912872c083f2..c646f21bff17039cc456369aaf00a500443078b4 100644 (file)
@@ -450,14 +450,8 @@ void setup_bonded_threading(bonded_threading_t *bt,
 
             if (gmx_debug_at)
             {
-#if BITMASK_SIZE <= 64 //move into bitmask when it is C++
-                std::string flags = gmx::formatString("%" PRIx64, *mask);
-#else
-                std::string flags = gmx::formatAndJoin(*mask,
-                                                       "", gmx::StringFormatter("%x"));
-#endif
                 fprintf(debug, "block %d flags %s count %d\n",
-                        b, flags.c_str(), c);
+                        b, to_hex_string(*mask).c_str(), c);
             }
         }
     }
index 8182d7269200c2c526c38ea474c8d070a189822c..604081299b77af93fb423074e354613ff6a7bd03 100644 (file)
 
 #include <string.h>
 
+#include <algorithm>
 #include <array>
 
 #include "gromacs/utility/basedefinitions.h"
+#include "gromacs/utility/stringutil.h"
 
 /*! \brief Size of bitmask. Has to be 32 or multiple of 64. */
 #ifndef BITMASK_SIZE
@@ -195,5 +197,28 @@ inline static void bitmask_union(gmx_bitmask_t* a, gmx_bitmask_t b)
     }
 }
 #endif
+//In bitmask.h because only current use is for bitmask.
+
+//! Convert uint32_t to hex string
+inline static std::string to_hex_string(uint32_t m)
+{
+    return gmx::formatString("%08" PRIx32, m);
+}
+//! Convert uint64_t to hex string
+inline static std::string to_hex_string(uint64_t m)
+{
+    return gmx::formatString("%016" PRIx64, m);
+}
+//! Convert container of intergers to hex string
+template <typename C>
+inline static std::string to_hex_string(C m)
+{
+    std::string ret;
+    for (auto it = m.rbegin(); it < m.rend(); ++it)
+    {
+        ret += to_hex_string(*it);
+    }
+    return ret;
+}
 
 #endif
index 9fb0dd0c9e3105874293129f03f8637b4252a167..8a0267f4cf5636c3cb0a30acb003525719386446 100644 (file)
@@ -131,3 +131,10 @@ BITMASK_TEST_P(Union) //NOLINT(misc-definitions-in-headers)
     bitmask_union(&m1, m2);
     EXPECT_TRUE(bitmask_is_equal(m1, m2));
 }
+BITMASK_TEST_P(ToHex) //NOLINT(misc-definitions-in-headers)
+{
+    gmx_bitmask_t m;
+    bitmask_clear(&m);
+    bitmask_set_bit(&m, BITMASK_SIZE-1);
+    EXPECT_EQ(to_hex_string(m), "8" + std::string(BITMASK_SIZE/4-1, '0'));
+}