#ifndef DEFS_HH #define DEFS_HH #include #include #include #include #include #include #include #include #include #include #include #include #include "typeprops.hh" using namespace std; // TODO: autoconf these #ifdef CARPET_USE_BOOST_FOREACH # include // We call the macro "forall", not "foreach", since the name "foreach" // is taken by Boost and cannot be used here. An alternative name // would be "foreach_" with a trailing underscore. # define forall BOOST_FOREACH // # define forall_r BOOST_REVERSE_FOREACH #else # define forall(var, expr) for (var: expr) #endif #ifdef CARPET_USE_BOOST_SHARED_PTR # include # include # define shared_ptr boost::shared_ptr # define make_shared boost::make_shared #endif // Stringify #define STRINGIFY1(x) #x #define STRINGIFY(x) STRINGIFY1(x) // 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"; // 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; namespace bboxset1 { template class bboxset; } #ifdef CARPET_ENABLE_BBOXSET2 namespace bboxset2 { template class bboxset; } #endif template class fulltree; typedef vect bvect; typedef vect ivect; typedef vect jvect; typedef vect rvect; typedef bbox ibbox; typedef bbox jbbox; typedef bbox rbbox; namespace bboxset1 { typedef bboxset ibset; } #ifdef CARPET_ENABLE_BBOXSET2 namespace bboxset2 { typedef bboxset ibset; } #endif // (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 }; // Divide, rounding to minus infinity template inline T idiv(T const x, T const y) { // round down manually if the result is negative return (x^y) >= T(0) ? x/y : (x-y+1)/y; } // Modulo, rounding to minus infinity template inline T imod(T const x, T const y) { // return x - idiv(x,y)*y; return (x^y) >= T(0) ? x%y : (x-y+1)%y + y-1; } template inline T div_down(T const x, T const y) { assert(x >= 0); assert(y > 0); return x / y; } template inline T div_up(T const x, T const y) { assert(x >= 0); assert(y > 0); return (x + y - 1) / y; } template inline T align_down(T const x, T const align) { assert(x >= 0); assert(align > 0); return div_down(x, align) * align; } template inline T align_up(T const x, T const align) { assert(x >= 0); assert(align > 0); return div_up(x, align) * align; } // 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 // Provide implementations for some functions for complex numbers #define IMPLEMENT_FUNCTIONS(T) \ \ inline int good_isfinite(T const& x) \ { \ return isfinite(x.real()) and isfinite(x.imag()); \ } \ \ inline int good_isinf(T const& x) \ { \ return isinf(x.real()) or isinf(x.imag()); \ } \ \ inline int good_isnan(T const& x) \ { \ return isnan(x.real()) or isnan(x.imag()); \ } \ \ inline int good_isnormal(T const& x) \ { \ return isnormal(x.real()) and isnormal(x.imag()); \ } namespace std { namespace Cactus { #ifdef HAVE_CCTK_COMPLEX8 IMPLEMENT_FUNCTIONS(CCTK_COMPLEX8) #endif #ifdef HAVE_CCTK_COMPLEX16 IMPLEMENT_FUNCTIONS(CCTK_COMPLEX16) #endif #ifdef HAVE_CCTK_COMPLEX32 IMPLEMENT_FUNCTIONS(CCTK_COMPLEX32) #endif } } #undef IMPLEMENT_FUNCTIONS // Container memory usage inline size_t memoryof (char const & e) { return sizeof e; } inline size_t memoryof (short const & e) { return sizeof e; } inline size_t memoryof (int const & e) { return sizeof e; } inline size_t memoryof (long const & e) { return sizeof e; } inline size_t memoryof (long long const & e) { return sizeof e; } inline size_t memoryof (unsigned char const & e) { return sizeof e; } inline size_t memoryof (unsigned short const & e) { return sizeof e; } inline size_t memoryof (unsigned int const & e) { return sizeof e; } inline size_t memoryof (unsigned long const & e) { return sizeof e; } inline size_t memoryof (unsigned long long const & e) { return sizeof e; } inline size_t memoryof (float const & e) { return sizeof e; } inline size_t memoryof (double const & e) { return sizeof e; } inline size_t memoryof (long double const & e) { return sizeof e; } inline size_t memoryof (void const * const & e) { return sizeof e; } template 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 (map 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); #ifdef CARPET_ENABLE_BBOXSET2 template ostream& output (ostream& os, const shared_ptr& s); #endif 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 pair& s) { return output(os,s); } template inline ostream& operator<< (ostream& os, const set& s) { return output(os,s); } #ifdef CARPET_ENABLE_BBOXSET2 template inline ostream& operator<< (ostream& os, const shared_ptr& s) { return output(os,s); } #endif 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