#ifndef DEFS_HH #define DEFS_HH #include #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include #include #include #include #include #include #include #include "typeprops.hh" using namespace std; // Stringify #define STRINGIFY1(x) #x #define STRINGIFY(x) STRINGIFY1(x) // Define the restrict qualifier #ifdef CCTK_CXX_RESTRICT # define restrict CCTK_CXX_RESTRICT #endif // Structure member offsets #undef offsetof #define offsetof(TYPE,MEMBER) ((size_t)&((TYPE*)0)->MEMBER) #undef __offsetof__ #define __offsetof__ offsetof // Number of dimensions #ifndef CARPET_DIM # define CARPET_DIM 3 #endif const int dim = CARPET_DIM; // Begin a new line without flushing the output buffer char const * const eol = "\n"; // A compile time pseudo assert statement #define static_assert(_x, _msg) do { typedef int ai[(_x) ? 1 : -1]; } while(0) // Check a return value #define check(_expr) do { bool const _val = (_expr); assert(_val); } while(0) // Use this macro AT instead of vector's operator[] or at(). // Depending on the macro CARPET_OPTIMISE, this macro AT either checks // for valid indices or not. #if ! defined(CARPET_OPTIMISE) # define AT(index) at(index) #else # define AT(index) operator[](index) #endif // Some shortcuts for type names template class vect; template class bbox; template class bboxset; template class fulltree; typedef vect bvect; typedef vect ivect; typedef vect jvect; typedef vect rvect; typedef bbox ibbox; typedef bbox jbbox; typedef bbox rbbox; typedef bboxset ibset; // (Try to replace these by b2vect and i2vect) typedef vect,dim> bbvect; typedef vect,dim> iivect; typedef vect,dim> jjvect; typedef vect,2> b2vect; typedef vect,2> i2vect; typedef vect,2> j2vect; typedef vect,2> r2vect; struct pseudoregion_t; struct region_t; typedef fulltree ipfulltree; // A general type enum centering { error_centered, vertex_centered, cell_centered }; // Useful helper template inline T square (const T x) { return x*x; } // Another useful helper template T ipow (T x, int y) CCTK_ATTRIBUTE_CONST; // Access to CarpetLib parameters CCTK_INT get_poison_value() CCTK_ATTRIBUTE_PURE; CCTK_INT get_deadbeef() CCTK_ATTRIBUTE_PURE; // Input streams struct input_error { }; void skipws (istream& is); void expect (istream& is, char c); void consume (istream& is, char c); void consume (istream& is, char const * c); // Names for types #ifdef HAVE_CCTK_INT1 inline const char * typestring (const CCTK_INT1&) { return "CCTK_INT1"; } #endif #ifdef HAVE_CCTK_INT2 inline const char * typestring (const CCTK_INT2&) { return "CCTK_INT2"; } #endif #ifdef HAVE_CCTK_INT4 inline const char * typestring (const CCTK_INT4&) { return "CCTK_INT4"; } #endif #ifdef HAVE_CCTK_INT8 inline const char * typestring (const CCTK_INT8&) { return "CCTK_INT8"; } #endif #ifdef HAVE_CCTK_REAL4 inline const char * typestring (const CCTK_REAL4&) { return "CCTK_REAL4"; } #endif #ifdef HAVE_CCTK_REAL8 inline const char * typestring (const CCTK_REAL8&) { return "CCTK_REAL8"; } #endif #ifdef HAVE_CCTK_REAL16 inline const char * typestring (const CCTK_REAL16&) { return "CCTK_REAL16"; } #endif #ifdef HAVE_CCTK_REAL4 inline const char * typestring (const CCTK_COMPLEX8&) { return "CCTK_COMPLEX8"; } #endif #ifdef HAVE_CCTK_REAL8 inline const char * typestring (const CCTK_COMPLEX16&) { return "CCTK_COMPLEX16"; } #endif #ifdef HAVE_CCTK_REAL16 inline const char * typestring (const CCTK_COMPLEX32&) { return "CCTK_COMPLEX32"; } #endif // Capture the system's fpclassify, isfinite, isinf, isnan, and // isnormal functions // #ifdef HAVE_CCTK_REAL4 // inline int myfpclassify (CCTK_REAL4 const x) // { return fpclassify (x); } // #endif // #ifdef HAVE_CCTK_REAL8 // inline int myfpclassify (CCTK_REAL8 const x) // { return fpclassify (x); } // #endif // #ifdef HAVE_CCTK_REAL16 // inline int myfpclassify (CCTK_REAL16 const x) // { return fpclassify (x); } // #endif // // #undef fpclassify #ifdef HAVE_CCTK_REAL4 inline int myisfinite (CCTK_REAL4 const x) { return isfinite (x); } #endif #ifdef HAVE_CCTK_REAL8 inline int myisfinite (CCTK_REAL8 const x) { return isfinite (x); } #endif #ifdef HAVE_CCTK_REAL16 inline int myisfinite (CCTK_REAL16 const x) { return isfinite (x); } #endif #undef isfinite #ifdef HAVE_CCTK_REAL4 inline int myisinf (CCTK_REAL4 const x) { return isinf (x); } #endif #ifdef HAVE_CCTK_REAL8 inline int myisinf (CCTK_REAL8 const x) { return isinf (x); } #endif #ifdef HAVE_CCTK_REAL16 inline int myisinf (CCTK_REAL16 const x) { return isinf (x); } #endif #undef isinf #ifdef HAVE_CCTK_REAL4 inline int myisnan (CCTK_REAL4 const x) { return isnan (x); } #endif #ifdef HAVE_CCTK_REAL8 inline int myisnan (CCTK_REAL8 const x) { return isnan (x); } #endif #ifdef HAVE_CCTK_REAL16 inline int myisnan (CCTK_REAL16 const x) { return isnan (x); } #endif #undef isnan #ifdef HAVE_CCTK_REAL4 inline int myisnormal (CCTK_REAL4 const x) { return isnormal (x); } #endif #ifdef HAVE_CCTK_REAL8 inline int myisnormal (CCTK_REAL8 const x) { return isnormal (x); } #endif #ifdef HAVE_CCTK_REAL16 inline int myisnormal (CCTK_REAL16 const x) { return isnormal (x); } #endif #undef isnormal namespace CarpetLib { namespace good { // Explicitly overload some functions for all types in the same // namespace CarpetLib::good, to circumvent confusion among some // compilers // // abs // template inline typename typeprops::real abs (T const x) { return std::abs (x); } // // This does not work on Linux with Intel compilers, which do not // // always have long long llabs (long long) // template<> inline signed char abs (signed char const x) { return ::abs (x); } // template<> inline unsigned char abs (unsigned char const x) CCTK_ATTRIBUTE_CONST { return ::abs (x); } // template<> inline short abs (short const x) { return ::abs (x); } // template<> inline int abs (int const x) { return ::abs (x); } // template<> inline long abs (long const x) { return ::labs (x); } // #ifdef SIZEOF_LONG_LONG // inline long long abs (long long const x) { return ::llabs (x); } // #endif // // This template does not work on AIX, which does not have long // // long abs (long long) // #ifdef HAVE_CCTK_INT1 // template<> inline CCTK_INT1 abs (CCTK_INT1 const x) { return x < 0 ? - x : x; } // #endif // #ifdef HAVE_CCTK_INT2 // template<> inline CCTK_INT2 abs (CCTK_INT2 const x) { return x < 0 ? - x : x; } // #endif // #ifdef HAVE_CCTK_INT4 // template<> inline CCTK_INT4 abs (CCTK_INT4 const x) { return x < 0 ? - x : x; } // #endif // #ifdef HAVE_CCTK_INT8 // template<> inline CCTK_INT8 abs (CCTK_INT8 const x) { return x < 0 ? - x : x; } // #endif #ifdef HAVE_CCTK_COMPLEX8 template<> inline CCTK_REAL4 abs (CCTK_COMPLEX8 const x) { return CCTK_Cmplx8Abs (x); } #endif #ifdef HAVE_CCTK_COMPLEX16 template<> inline CCTK_REAL8 abs (CCTK_COMPLEX16 const x) { return CCTK_Cmplx16Abs (x); } #endif #ifdef HAVE_CCTK_COMPLEX32 template<> inline CCTK_REAL16 abs (CCTK_COMPLEX32 const x) { return CCTK_Cmplx32Abs (x); } #endif // // // // fpclassify // // // // // Default implementation, only good for integers // template // inline int fpclassify (T const x) // { return x ? FP_NORMAL : FP_ZERO; } // // #ifdef HAVE_CCTK_REAL4 // template<> inline int fpclassify (CCTK_REAL4 const x) // { return myfpclassify (x); } // #endif // #ifdef HAVE_CCTK_REAL8 // template<> inline int fpclassify (CCTK_REAL8 const x) // { return myfpclassify (x); } // #endif // #ifdef HAVE_CCTK_REAL16 // template<> inline int fpclassify (CCTK_REAL16 const x) // { return myfpclassify (x); } // #endif // // #ifdef HAVE_CCTK_COMPLEX8 // template<> inline int fpclassify (CCTK_COMPLEX8 const x) // { assert(0); } // #endif // #ifdef HAVE_CCTK_COMPLEX16 // template<> inline int fpclassify (CCTK_COMPLEX16 const x) // { assert(0); } // #endif // #ifdef HAVE_CCTK_COMPLEX32 // template<> inline int fpclassify (CCTK_COMPLEX32 const x) // { assert(0); } // #endif // // isfinite // // Default implementation, only good for integers template inline int isfinite (T const x) { return 1; } #ifdef HAVE_CCTK_REAL4 template<> inline int isfinite (CCTK_REAL4 const x) { return myisfinite (x); } #endif #ifdef HAVE_CCTK_REAL8 template<> inline int isfinite (CCTK_REAL8 const x) { return myisfinite (x); } #endif #ifdef HAVE_CCTK_REAL16 template<> inline int isfinite (CCTK_REAL16 const x) { return myisfinite (x); } #endif #ifdef HAVE_CCTK_COMPLEX8 template<> inline int isfinite (CCTK_COMPLEX8 const x) { return myisfinite (CCTK_Cmplx8Real (x)) and myisfinite (CCTK_Cmplx8Imag (x)); } #endif #ifdef HAVE_CCTK_COMPLEX16 template<> inline int isfinite (CCTK_COMPLEX16 const x) { return myisfinite (CCTK_Cmplx16Real (x)) and myisfinite (CCTK_Cmplx16Imag (x)); } #endif #ifdef HAVE_CCTK_COMPLEX32 template<> inline int isfinite (CCTK_COMPLEX32 const x) { return myisfinite (CCTK_Cmplx32Real (x)) and myisfinite (CCTK_Cmplx32Imag (x)); } #endif // // isinf // // Default implementation, only good for integers template inline int isinf (T const x) { return 0; } #ifdef HAVE_CCTK_REAL4 template<> inline int isinf (CCTK_REAL4 const x) { return myisinf (x); } #endif #ifdef HAVE_CCTK_REAL8 template<> inline int isinf (CCTK_REAL8 const x) { return myisinf (x); } #endif #ifdef HAVE_CCTK_REAL16 template<> inline int isinf (CCTK_REAL16 const x) { return myisinf (x); } #endif #ifdef HAVE_CCTK_COMPLEX8 template<> inline int isinf (CCTK_COMPLEX8 const x) { return myisinf (CCTK_Cmplx8Real (x)) or myisinf (CCTK_Cmplx8Imag (x)); } #endif #ifdef HAVE_CCTK_COMPLEX16 template<> inline int isinf (CCTK_COMPLEX16 const x) { return myisinf (CCTK_Cmplx16Real (x)) or myisinf (CCTK_Cmplx16Imag (x)); } #endif #ifdef HAVE_CCTK_COMPLEX32 template<> inline int isinf (CCTK_COMPLEX32 const x) { return myisinf (CCTK_Cmplx32Real (x)) or myisinf (CCTK_Cmplx32Imag (x)); } #endif // // isnan // // Default implementation, only good for integers template inline int isnan (T const x) { return 0; } #ifdef HAVE_CCTK_REAL4 template<> inline int isnan (CCTK_REAL4 const x) { return myisnan (x); } #endif #ifdef HAVE_CCTK_REAL8 template<> inline int isnan (CCTK_REAL8 const x) { return myisnan (x); } #endif #ifdef HAVE_CCTK_REAL16 template<> inline int isnan (CCTK_REAL16 const x) { return myisnan (x); } #endif #ifdef HAVE_CCTK_COMPLEX8 template<> inline int isnan (CCTK_COMPLEX8 const x) { return myisnan (CCTK_Cmplx8Real (x)) or myisnan (CCTK_Cmplx8Imag (x)); } #endif #ifdef HAVE_CCTK_COMPLEX16 template<> inline int isnan (CCTK_COMPLEX16 const x) { return myisnan (CCTK_Cmplx16Real (x)) or myisnan (CCTK_Cmplx16Imag (x)); } #endif #ifdef HAVE_CCTK_COMPLEX32 template<> inline int isnan (CCTK_COMPLEX32 const x) { return myisnan (CCTK_Cmplx32Real (x)) or myisnan (CCTK_Cmplx32Imag (x)); } #endif // // isnormal // // Default implementation, only good for integers template inline int isnormal (T const x) { return 1; } #ifdef HAVE_CCTK_REAL4 template<> inline int isnormal (CCTK_REAL4 const x) { return myisnormal (x); } #endif #ifdef HAVE_CCTK_REAL8 template<> inline int isnormal (CCTK_REAL8 const x) { return myisnormal (x); } #endif #ifdef HAVE_CCTK_REAL16 template<> inline int isnormal (CCTK_REAL16 const x) { return myisnormal (x); } #endif #ifdef HAVE_CCTK_COMPLEX8 template<> inline int isnormal (CCTK_COMPLEX8 const x) { return myisnormal (CCTK_Cmplx8Real (x)) and myisnormal (CCTK_Cmplx8Imag (x)); } #endif #ifdef HAVE_CCTK_COMPLEX16 template<> inline int isnormal (CCTK_COMPLEX16 const x) { return myisnormal (CCTK_Cmplx16Real (x)) and myisnormal (CCTK_Cmplx16Imag (x)); } #endif #ifdef HAVE_CCTK_COMPLEX32 template<> inline int isnormal (CCTK_COMPLEX32 const x) { return myisnormal (CCTK_Cmplx32Real (x)) and myisnormal (CCTK_Cmplx32Imag (x)); } #endif } // namespace good } // namespace CarpetLib // Use #defines to access the CarpetLib::good functions from the // global namespace. Because macros are not scoped, this makes the // corresponding functions in CarpetLib::good inaccessible, so we // rename them by adding an underscore. namespace CarpetLib { namespace good { #define ALIAS_GOOD_FUNCTION(typ,func) \ template \ inline typ func##_ (T const x) \ { return func (x); } // ALIAS_GOOD_FUNCTION(int,fpclassify) ALIAS_GOOD_FUNCTION(int,isfinite) ALIAS_GOOD_FUNCTION(int,isinf) ALIAS_GOOD_FUNCTION(int,isnan) ALIAS_GOOD_FUNCTION(int,isnormal) #undef ALIAS_GOOD_FUNCTION } // namespace good } // namespace CarpetLib #define isfinite (::CarpetLib::good::isfinite_) #define isinf (::CarpetLib::good::isinf_) #define isnan (::CarpetLib::good::isnan_) #define isnormal (::CarpetLib::good::isnormal_) // Container equality template bool equals (vector const& v, vector const& w) { if (v.size() != w.size()) return false; for (size_t i=0; i inline size_t memoryof (T const * const & e) { return sizeof e; } template inline size_t memoryof (typename list::iterator const & i) { return sizeof i; } template inline size_t memoryof (typename list::const_iterator const & i) { return sizeof i; } template size_t memoryof (list const & c) CCTK_ATTRIBUTE_PURE; template size_t memoryof (set const & c) CCTK_ATTRIBUTE_PURE; template size_t memoryof (stack const & c) CCTK_ATTRIBUTE_PURE; template size_t memoryof (vector const & c) CCTK_ATTRIBUTE_PURE; // Container input template istream& input (istream& is, list& l); template istream& input (istream& is, set& s); template istream& input (istream& is, vector& v); template inline istream& operator>> (istream& is, list& l) { return input(is,l); } template inline istream& operator>> (istream& is, set& s) { return input(is,s); } template inline istream& operator>> (istream& is, vector& v) { return input(is,v); } // Container output template ostream& output (ostream& os, const list& l); template ostream& output (ostream& os, const map& m); template ostream& output (ostream& os, const pair& p); template ostream& output (ostream& os, const set& s); template ostream& output (ostream& os, const stack& s); template ostream& output (ostream& os, const vector& v); template inline ostream& operator<< (ostream& os, const list& l) { return output(os,l); } template inline ostream& operator<< (ostream& os, const map& m) { return output(os,m); } template inline ostream& operator<< (ostream& os, const set& s) { return output(os,s); } template inline ostream& operator<< (ostream& os, const stack& s) { return output(os,s); } template inline ostream& operator<< (ostream& os, const vector& v) { return output(os,v); } #endif // DEFS_HH