diff options
author | Erik Schnetter <schnetter@cct.lsu.edu> | 2009-09-03 16:19:15 -0500 |
---|---|---|
committer | Barry Wardell <barry.wardell@gmail.com> | 2011-12-14 16:42:31 +0000 |
commit | 11c4d98017cbb86d08e15fd1b549180184b58a26 (patch) | |
tree | 2546a154c6f7bc0bec87de7316125ae7d1453569 /Carpet/CarpetLib/src/defs.hh | |
parent | f520477b1c14e02f1495cfa8d3e09f4e21ab34d0 (diff) |
Import Carpet
Ignore-this: 309b4dd613f4af2b84aa5d6743fdb6b3
Diffstat (limited to 'Carpet/CarpetLib/src/defs.hh')
-rw-r--r-- | Carpet/CarpetLib/src/defs.hh | 302 |
1 files changed, 201 insertions, 101 deletions
diff --git a/Carpet/CarpetLib/src/defs.hh b/Carpet/CarpetLib/src/defs.hh index a44d20ef0..3a1188d21 100644 --- a/Carpet/CarpetLib/src/defs.hh +++ b/Carpet/CarpetLib/src/defs.hh @@ -11,25 +11,24 @@ #include <cstdlib> #include <iostream> #include <list> +#include <map> #include <set> #include <stack> #include <vector> #include "cctk.h" +#include "typeprops.hh" -using namespace std; - - -// A compile time pseudo assert statement -#define static_assert(_x, _msg) do { typedef int ai[(_x) ? 1 : -1]; } while(0) +using namespace std; -// Check a return value -#define check(_expr) do { bool const _val = (_expr); assert(_val); } while(0) +// Stringify +#define STRINGIFY1(x) #x +#define STRINGIFY(x) STRINGIFY1(x) @@ -40,14 +39,11 @@ using namespace std; -// Use this macro AT instead of vector's operator[] or at(). -// Depending on the macro NDEBUG, this macro AT either checks for -// valid indices or not. -#ifndef CARPET_OPTIMISE -# define AT(index) at(index) -#else -# define AT(index) operator[](index) +// Number of dimensions +#ifndef CARPET_DIM +# define CARPET_DIM 3 #endif +const int dim = CARPET_DIM; @@ -55,9 +51,25 @@ using namespace std; 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) -// Number of dimensions -const int dim = 3; + + +// 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 @@ -100,11 +112,19 @@ enum centering { error_centered, vertex_centered, cell_centered }; // Useful helper template<class T> +inline T square (const T& x) CCTK_ATTRIBUTE_CONST; +template<class T> inline T square (const T& x) { return x*x; } // Another useful helper template<class T> -T ipow (T x, int y); +T ipow (T x, int y) CCTK_ATTRIBUTE_CONST; + + + +// Access to CarpetLib parameters +CCTK_INT get_poison_value() CCTK_ATTRIBUTE_CONST; +CCTK_INT get_deadbeef() CCTK_ATTRIBUTE_CONST; @@ -120,52 +140,62 @@ void consume (istream& is, char const * c); // Names for types #ifdef HAVE_CCTK_INT1 -inline const char * typestring (const CCTK_INT1& dummy) +inline const char * typestring (const CCTK_INT1&) CCTK_ATTRIBUTE_CONST; +inline const char * typestring (const CCTK_INT1&) { return "CCTK_INT1"; } #endif #ifdef HAVE_CCTK_INT2 -inline const char * typestring (const CCTK_INT2& dummy) +inline const char * typestring (const CCTK_INT2&) CCTK_ATTRIBUTE_CONST; +inline const char * typestring (const CCTK_INT2&) { return "CCTK_INT2"; } #endif #ifdef HAVE_CCTK_INT4 -inline const char * typestring (const CCTK_INT4& dummy) +inline const char * typestring (const CCTK_INT4&) CCTK_ATTRIBUTE_CONST; +inline const char * typestring (const CCTK_INT4&) { return "CCTK_INT4"; } #endif #ifdef HAVE_CCTK_INT8 -inline const char * typestring (const CCTK_INT8& dummy) +inline const char * typestring (const CCTK_INT8&) CCTK_ATTRIBUTE_CONST; +inline const char * typestring (const CCTK_INT8&) { return "CCTK_INT8"; } #endif #ifdef HAVE_CCTK_REAL4 -inline const char * typestring (const CCTK_REAL4& dummy) +inline const char * typestring (const CCTK_REAL4&) CCTK_ATTRIBUTE_CONST; +inline const char * typestring (const CCTK_REAL4&) { return "CCTK_REAL4"; } #endif #ifdef HAVE_CCTK_REAL8 -inline const char * typestring (const CCTK_REAL8& dummy) +inline const char * typestring (const CCTK_REAL8&) CCTK_ATTRIBUTE_CONST; +inline const char * typestring (const CCTK_REAL8&) { return "CCTK_REAL8"; } #endif #ifdef HAVE_CCTK_REAL16 -inline const char * typestring (const CCTK_REAL16& dummy) +inline const char * typestring (const CCTK_REAL16&) CCTK_ATTRIBUTE_CONST; +inline const char * typestring (const CCTK_REAL16&) { return "CCTK_REAL16"; } #endif #ifdef HAVE_CCTK_REAL4 -inline const char * typestring (const CCTK_COMPLEX8& dummy) +inline const char * typestring (const CCTK_COMPLEX8&) CCTK_ATTRIBUTE_CONST; +inline const char * typestring (const CCTK_COMPLEX8&) { return "CCTK_COMPLEX8"; } #endif #ifdef HAVE_CCTK_REAL8 -inline const char * typestring (const CCTK_COMPLEX16& dummy) +inline const char * typestring (const CCTK_COMPLEX16&) CCTK_ATTRIBUTE_CONST; +inline const char * typestring (const CCTK_COMPLEX16&) { return "CCTK_COMPLEX16"; } #endif #ifdef HAVE_CCTK_REAL16 -inline const char * typestring (const CCTK_COMPLEX32& dummy) +inline const char * typestring (const CCTK_COMPLEX32&) CCTK_ATTRIBUTE_CONST; +inline const char * typestring (const CCTK_COMPLEX32&) { return "CCTK_COMPLEX32"; } #endif @@ -174,78 +204,105 @@ inline const char * typestring (const CCTK_COMPLEX32& dummy) namespace CarpetLib { namespace good { - // Explicitly overload abs for all types in the same namespace, to - // circumvent confusion among some compilers + // Explicitly overload some functions for all types in the same + // namespace CarpetLib::good, to circumvent confusion among some + // compilers - // CCTK_BYTE is unsigned - inline CCTK_BYTE abs (CCTK_BYTE const & x) { return x; } + // + // abs + // -#if 0 - // This does not work on AIX, which does not have long long abs - // (long long) -# ifdef HAVE_CCTK_INT1 - inline CCTK_INT1 abs (CCTK_INT1 const & x) { return std::abs (x); } -# endif -# ifdef HAVE_CCTK_INT2 - inline CCTK_INT2 abs (CCTK_INT2 const & x) { return std::abs (x); } -# endif -# ifdef HAVE_CCTK_INT4 - inline CCTK_INT4 abs (CCTK_INT4 const & x) { return std::abs (x); } -# endif -# ifdef HAVE_CCTK_INT8 - inline CCTK_INT8 abs (CCTK_INT8 const & x) { return std::abs (x); } -# endif -#endif + template <typename T> + inline typename typeprops<T>::real abs (T const & x) CCTK_ATTRIBUTE_CONST; + template <typename T> + inline typename typeprops<T>::real abs (T const & x) + { return std::abs (x); } -#if 0 - // This does not work on Linux with Intel compilers, which do not - // always have long long llabs (long long) - inline signed char abs (signed char const & x) { return ::abs (x); } - inline unsigned char abs (unsigned char const & x) { return ::abs (x); } - inline short abs (short const & x) { return ::abs (x); } - inline int abs (int const & x) { return ::abs (x); } - 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 -#endif +// // 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> (signed char const & x) CCTK_ATTRIBUTE_CONST { return ::abs (x); } +// template<> inline unsigned char abs<unsigned char> (unsigned char const & x) CCTK_ATTRIBUTE_CONST { return ::abs (x); } +// template<> inline short abs<short> (short const & x) { return ::abs (x); } +// template<> inline int abs<int> (int const & x) CCTK_ATTRIBUTE_CONST { return ::abs (x); } +// template<> inline long abs<long> (long const & x) CCTK_ATTRIBUTE_CONST { return ::labs (x); } +// #ifdef SIZEOF_LONG_LONG +// inline long long abs<long long> (long long const & x) CCTK_ATTRIBUTE_CONST { return ::llabs (x); } +// #endif -#if 1 -# ifdef HAVE_CCTK_INT1 - inline CCTK_INT1 abs (CCTK_INT1 const & x) { return x < 0 ? - x : x; } -# endif -# ifdef HAVE_CCTK_INT2 - inline CCTK_INT2 abs (CCTK_INT2 const & x) { return x < 0 ? - x : x; } -# endif -# ifdef HAVE_CCTK_INT4 - inline CCTK_INT4 abs (CCTK_INT4 const & x) { return x < 0 ? - x : x; } -# endif -# ifdef HAVE_CCTK_INT8 - inline CCTK_INT8 abs (CCTK_INT8 const & x) { return x < 0 ? - x : 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> (CCTK_INT1 const & x) CCTK_ATTRIBUTE_CONST { return x < 0 ? - x : x; } +// #endif +// #ifdef HAVE_CCTK_INT2 +// template<> inline CCTK_INT2 abs<CCTK_INT2> (CCTK_INT2 const & x) CCTK_ATTRIBUTE_CONST { return x < 0 ? - x : x; } +// #endif +// #ifdef HAVE_CCTK_INT4 +// template<> inline CCTK_INT4 abs<CCTK_INT4> (CCTK_INT4 const & x) CCTK_ATTRIBUTE_CONST { return x < 0 ? - x : x; } +// #endif +// #ifdef HAVE_CCTK_INT8 +// template<> inline CCTK_INT8 abs<CCTK_INT8> (CCTK_INT8 const & x) CCTK_ATTRIBUTE_CONST { return x < 0 ? - x : x; } +// #endif + +#ifdef HAVE_CCTK_COMPLEX8 + template<> inline CCTK_REAL4 abs<CCTK_COMPLEX8> (CCTK_COMPLEX8 const & x) CCTK_ATTRIBUTE_CONST; + template<> inline CCTK_REAL4 abs<CCTK_COMPLEX8> (CCTK_COMPLEX8 const & x) + { return CCTK_Cmplx8Abs (x); } +#endif +#ifdef HAVE_CCTK_COMPLEX16 + template<> inline CCTK_REAL8 abs<CCTK_COMPLEX16> (CCTK_COMPLEX16 const & x) CCTK_ATTRIBUTE_CONST; + template<> inline CCTK_REAL8 abs<CCTK_COMPLEX16> (CCTK_COMPLEX16 const & x) + { return CCTK_Cmplx16Abs (x); } +#endif +#ifdef HAVE_CCTK_COMPLEX32 + template<> inline CCTK_REAL16 abs<CCTK_COMPLEX32> (CCTK_COMPLEX32 const & x) CCTK_ATTRIBUTE_CONST; + template<> inline CCTK_REAL16 abs<CCTK_COMPLEX32> (CCTK_COMPLEX32 const & x) + { return CCTK_Cmplx32Abs (x); } #endif + // + // isnan + // + +#undef isnan + + // Default implementation, only good for integers + template <typename T> + inline int isnan (T const & x) CCTK_ATTRIBUTE_CONST; + template <typename T> + inline int isnan (T const & x) + { return 0; } + #ifdef HAVE_CCTK_REAL4 - inline CCTK_REAL4 abs (CCTK_REAL4 const & x) { return std::abs (x); } + template<> inline int isnan (CCTK_REAL4 const & x) CCTK_ATTRIBUTE_CONST; + template<> inline int isnan (CCTK_REAL4 const & x) + { return ::isnan (x); } #endif #ifdef HAVE_CCTK_REAL8 - inline CCTK_REAL8 abs (CCTK_REAL8 const & x) { return std::abs (x); } + template<> inline int isnan (CCTK_REAL8 const & x) CCTK_ATTRIBUTE_CONST; + template<> inline int isnan (CCTK_REAL8 const & x) + { return ::isnan (x); } #endif #ifdef HAVE_CCTK_REAL16 - inline CCTK_REAL16 abs (CCTK_REAL16 const & x) { return std::abs (x); } + template<> inline int isnan (CCTK_REAL16 const & x) CCTK_ATTRIBUTE_CONST; + template<> inline int isnan (CCTK_REAL16 const & x) + { return ::isnan (x); } #endif #ifdef HAVE_CCTK_COMPLEX8 - inline CCTK_REAL4 abs (CCTK_COMPLEX8 const & x) - { return CCTK_Cmplx8Abs (x); } + template<> inline int isnan (CCTK_COMPLEX8 const & x) CCTK_ATTRIBUTE_CONST; + template<> inline int isnan (CCTK_COMPLEX8 const & x) + { return ::isnan (CCTK_Cmplx8Real (x)) or ::isnan (CCTK_Cmplx8Imag (x)); } #endif #ifdef HAVE_CCTK_COMPLEX16 - inline CCTK_REAL8 abs (CCTK_COMPLEX16 const & x) - { return CCTK_Cmplx16Abs (x); } + template<> inline int isnan (CCTK_COMPLEX16 const & x) CCTK_ATTRIBUTE_CONST; + template<> inline int isnan (CCTK_COMPLEX16 const & x) + { return ::isnan (CCTK_Cmplx16Real (x)) or ::isnan (CCTK_Cmplx16Imag (x)); } #endif #ifdef HAVE_CCTK_COMPLEX32 - inline CCTK_REAL16 abs (CCTK_COMPLEX32 const & x) - { return CCTK_Cmplx32Abs (x); } + template<> inline int isnan (CCTK_COMPLEX32 const & x) CCTK_ATTRIBUTE_CONST; + template<> inline int isnan (CCTK_COMPLEX32 const & x) + { return ::isnan (CCTK_Cmplx32Real (x)) or std::isnan (CCTK_Cmplx32Imag (x)); } #endif } // namespace good @@ -254,33 +311,69 @@ namespace CarpetLib { // Container memory usage -inline size_t memoryof (char e) { return sizeof e; } -inline size_t memoryof (short e) { return sizeof e; } -inline size_t memoryof (int e) { return sizeof e; } -inline size_t memoryof (long e) { return sizeof e; } -inline size_t memoryof (long long e) { return sizeof e; } -inline size_t memoryof (unsigned char e) { return sizeof e; } -inline size_t memoryof (unsigned short e) { return sizeof e; } -inline size_t memoryof (unsigned int e) { return sizeof e; } -inline size_t memoryof (unsigned long e) { return sizeof e; } -inline size_t memoryof (unsigned long long e) { return sizeof e; } -inline size_t memoryof (float e) { return sizeof e; } -inline size_t memoryof (double e) { return sizeof e; } -inline size_t memoryof (long double e) { return sizeof e; } -inline size_t memoryof (void * e) { return sizeof e; } -template<class T> inline size_t memoryof (T * e) { return sizeof e; } -template<class T> inline size_t memoryof (T const * e) { return sizeof e; } -template<class T> size_t memoryof (list<T> const & c); -template<class T> size_t memoryof (set<T> const & c); -template<class T> size_t memoryof (stack<T> const & c); -template<class T> size_t memoryof (vector<T> const & c); +inline size_t memoryof (char const e) CCTK_ATTRIBUTE_CONST; +inline size_t memoryof (short const e) CCTK_ATTRIBUTE_CONST; +inline size_t memoryof (int const e) CCTK_ATTRIBUTE_CONST; +inline size_t memoryof (long const e) CCTK_ATTRIBUTE_CONST; +inline size_t memoryof (long long const e) CCTK_ATTRIBUTE_CONST; +inline size_t memoryof (unsigned char const e) CCTK_ATTRIBUTE_CONST; +inline size_t memoryof (unsigned short const e) CCTK_ATTRIBUTE_CONST; +inline size_t memoryof (unsigned int const e) CCTK_ATTRIBUTE_CONST; +inline size_t memoryof (unsigned long const e) CCTK_ATTRIBUTE_CONST; +inline size_t memoryof (unsigned long long const e) CCTK_ATTRIBUTE_CONST; +inline size_t memoryof (float const e) CCTK_ATTRIBUTE_CONST; +inline size_t memoryof (double const e) CCTK_ATTRIBUTE_CONST; +inline size_t memoryof (long double const e) CCTK_ATTRIBUTE_CONST; +inline size_t memoryof (void * const e) CCTK_ATTRIBUTE_CONST; +inline size_t memoryof (void const * const e) CCTK_ATTRIBUTE_CONST; +template<class T> inline size_t memoryof (T * const e) CCTK_ATTRIBUTE_CONST; +template<class T> inline size_t memoryof (T const * const e) CCTK_ATTRIBUTE_CONST; +template<class T> inline size_t memoryof (typename list<T>::iterator const & i) CCTK_ATTRIBUTE_CONST; +template<class T> inline size_t memoryof (typename list<T>::const_iterator const & i) CCTK_ATTRIBUTE_CONST; + +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 e) { return sizeof e; } +inline size_t memoryof (void const * const e) { return sizeof e; } +template<class T> inline size_t memoryof (T * const e) { return sizeof e; } +template<class T> inline size_t memoryof (T const * const e) { return sizeof e; } +template<class T> inline size_t memoryof (typename list<T>::iterator const & i) { return sizeof i; } +template<class T> inline size_t memoryof (typename list<T>::const_iterator const & i) { return sizeof i; } + +template<class T> size_t memoryof (list<T> const & c) CCTK_ATTRIBUTE_PURE; +template<class T> size_t memoryof (set<T> const & c) CCTK_ATTRIBUTE_PURE; +template<class T> size_t memoryof (stack<T> const & c) CCTK_ATTRIBUTE_PURE; +template<class T> size_t memoryof (vector<T> const & c) CCTK_ATTRIBUTE_PURE; // Container input +template<class T> istream& input (istream& is, list<T>& l); +template<class T> istream& input (istream& is, set<T>& s); template<class T> istream& input (istream& is, vector<T>& v); template<class T> +inline istream& operator>> (istream& is, list<T>& l) { + return input(is,l); +} + +template<class T> +inline istream& operator>> (istream& is, set<T>& s) { + return input(is,s); +} + +template<class T> inline istream& operator>> (istream& is, vector<T>& v) { return input(is,v); } @@ -289,6 +382,8 @@ inline istream& operator>> (istream& is, vector<T>& v) { // Container output template<class T> ostream& output (ostream& os, const list<T>& l); +template<class S, class T> ostream& output (ostream& os, const map<S,T>& m); +template<class S, class T> ostream& output (ostream& os, const pair<S,T>& p); template<class T> ostream& output (ostream& os, const set<T>& s); template<class T> ostream& output (ostream& os, const stack<T>& s); template<class T> ostream& output (ostream& os, const vector<T>& v); @@ -298,6 +393,11 @@ inline ostream& operator<< (ostream& os, const list<T>& l) { return output(os,l); } +template<class S, class T> +inline ostream& operator<< (ostream& os, const map<S,T>& m) { + return output(os,m); +} + template<class T> inline ostream& operator<< (ostream& os, const set<T>& s) { return output(os,s); |