diff --git a/src/atom_vec.h b/src/atom_vec.h index eaff9b888a..8ccf922c4b 100644 --- a/src/atom_vec.h +++ b/src/atom_vec.h @@ -209,25 +209,6 @@ class AtomVec : protected Pointers { bool *threads; - // union data struct for packing 32-bit and 64-bit ints into double bufs - // this avoids aliasing issues by having 2 pointers (double,int) - // to same buf memory - // constructor for 32-bit int prevents compiler - // from possibly calling the double constructor when passed an int - // copy to a double *buf: - // buf[m++] = ubuf(foo).d, where foo is a 32-bit or 64-bit int - // copy from a double *buf: - // foo = (int) ubuf(buf[m++]).i;, where (int) or (tagint) match foo - // the cast prevents compiler warnings about possible truncation - - union ubuf { - double d; - int64_t i; - ubuf(double arg) : d(arg) {} - ubuf(int64_t arg) : i(arg) {} - ubuf(int arg) : i(arg) {} - }; - // local methods void grow_nmax(); diff --git a/src/compute.h b/src/compute.h index 66974bf106..b6d053dd0e 100644 --- a/src/compute.h +++ b/src/compute.h @@ -161,17 +161,6 @@ class Compute : protected Pointers { return j >> SBBITS & 3; } - // union data struct for packing 32-bit and 64-bit ints into double bufs - // see atom_vec.h for documentation - - union ubuf { - double d; - int64_t i; - ubuf(double arg) : d(arg) {} - ubuf(int64_t arg) : i(arg) {} - ubuf(int arg) : i(arg) {} - }; - // private methods void adjust_dof_fix(); diff --git a/src/fix.h b/src/fix.h index 134b1550e9..ab8dddf1d9 100644 --- a/src/fix.h +++ b/src/fix.h @@ -242,17 +242,6 @@ class Fix : protected Pointers { void v_tally(int, int *, double, double *); void v_tally(int, double *); void v_tally(int, int, double); - - // union data struct for packing 32-bit and 64-bit ints into double bufs - // see atom_vec.h for documentation - - union ubuf { - double d; - int64_t i; - ubuf(double arg) : d(arg) {} - ubuf(int64_t arg) : i(arg) {} - ubuf(int arg) : i(arg) {} - }; }; namespace FixConst { diff --git a/src/lmptype.h b/src/lmptype.h index 1513fe5782..68af28af61 100644 --- a/src/lmptype.h +++ b/src/lmptype.h @@ -171,6 +171,46 @@ typedef int bigint; #endif + /// Data structe for packing 32-bit and 64-bit integers + /// into double (communication) buffers + /// + /// Using this union avoids aliasing issues by having member types + /// (double, int) referencing the same buffer memory location. + /// + /// The explicit constructor for 32-bit integers prevents compilers + /// from (incorrectly) calling the double constructor when storing + /// an int into a double buffer. + /* +\verbatim embed:rst +**Usage:** + +To copy an integer into a double buffer: + +.. code-block: c++ + + double buf[2]; + int foo = 1; + tagint bar = 2<<40; + buf[1] = ubuf(foo).d; + buf[2] = ubuf(bar).d; + +To copy from a double buffer back to an int: + +.. code-block: c++ + + foo = (int) ubuf(buf[1]).i; + bar = (tagint) ubuf(buf[2]).i; + +The typecast prevents compiler warnings about possible truncations. +\endverbatim + */ + union ubuf { + double d; + int64_t i; + ubuf(const double &arg) : d(arg) {} + ubuf(const int64_t &arg) : i(arg) {} + ubuf(const int &arg) : i(arg) {} + }; } // preprocessor macros for compiler specific settings diff --git a/src/pair.h b/src/pair.h index d578908b20..d0aee8cd7f 100644 --- a/src/pair.h +++ b/src/pair.h @@ -258,17 +258,6 @@ class Pair : protected Pointers { double, double, double, double, double, double); void virial_fdotr_compute(); - // union data struct for packing 32-bit and 64-bit ints into double bufs - // see atom_vec.h for documentation - - union ubuf { - double d; - int64_t i; - ubuf(double arg) : d(arg) {} - ubuf(int64_t arg) : i(arg) {} - ubuf(int arg) : i(arg) {} - }; - inline int sbmask(int j) const { return j >> SBBITS & 3; } diff --git a/src/reset_ids.h b/src/reset_ids.h index 69582bd5c3..d0e0cdfce8 100644 --- a/src/reset_ids.h +++ b/src/reset_ids.h @@ -53,17 +53,6 @@ class ResetIDs : protected Pointers { static int sort_bins(int, char *, int &, int *&, char *&, void *); void sort(); - - // union data struct for packing 32-bit and 64-bit ints into double bufs - // see atom_vec.h for documentation - - union ubuf { - double d; - int64_t i; - ubuf(double arg) : d(arg) {} - ubuf(int64_t arg) : i(arg) {} - ubuf(int arg) : i(arg) {} - }; }; }