have a single union ubuf definition in lmptype.h and remove others

This commit is contained in:
Axel Kohlmeyer
2020-06-11 18:37:31 -04:00
parent 006f7956c1
commit 6274234376
6 changed files with 40 additions and 63 deletions

View File

@ -209,25 +209,6 @@ class AtomVec : protected Pointers {
bool *threads; 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 // local methods
void grow_nmax(); void grow_nmax();

View File

@ -161,17 +161,6 @@ class Compute : protected Pointers {
return j >> SBBITS & 3; 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 // private methods
void adjust_dof_fix(); void adjust_dof_fix();

View File

@ -242,17 +242,6 @@ class Fix : protected Pointers {
void v_tally(int, int *, double, double *); void v_tally(int, int *, double, double *);
void v_tally(int, double *); void v_tally(int, double *);
void v_tally(int, 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 { namespace FixConst {

View File

@ -171,6 +171,46 @@ typedef int bigint;
#endif #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 // preprocessor macros for compiler specific settings

View File

@ -258,17 +258,6 @@ class Pair : protected Pointers {
double, double, double, double, double, double); double, double, double, double, double, double);
void virial_fdotr_compute(); 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 { inline int sbmask(int j) const {
return j >> SBBITS & 3; return j >> SBBITS & 3;
} }

View File

@ -53,17 +53,6 @@ class ResetIDs : protected Pointers {
static int sort_bins(int, char *, int &, int *&, char *&, void *); static int sort_bins(int, char *, int &, int *&, char *&, void *);
void sort(); 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) {}
};
}; };
} }