#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) // 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 }; template inline T div_down(T const x, T const align) { assert(x >= 0); assert(align > 0); return x / align; } template inline T div_up(T const x, T const align) { assert(x >= 0); assert(align > 0); return (x + align - 1) / align; } 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()) or 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 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