summaryrefslogtreecommitdiff
path: root/src/include
diff options
context:
space:
mode:
authoreschnett <eschnett@17b73243-c579-4c4c-a9d2-2d5706c11dac>2012-10-22 18:19:03 +0000
committereschnett <eschnett@17b73243-c579-4c4c-a9d2-2d5706c11dac>2012-10-22 18:19:03 +0000
commit97ca065e9e98b45adb820e5a80bafd17120355e4 (patch)
tree24f1c6edfb814174f7307fdac81079f3647351aa /src/include
parent8dce20df720b35dc1fe400c9e9fb45843a31ad36 (diff)
Provide macros to always make isnan available under its standard name
Provide macros to always make isnan available under its standard name, even if it actually needs to be called in a different manner. Provide C wrapper functions for isnan, which can function as fallback for C++. git-svn-id: http://svn.cactuscode.org/flesh/trunk@4882 17b73243-c579-4c4c-a9d2-2d5706c11dac
Diffstat (limited to 'src/include')
-rw-r--r--src/include/cctk_Math.h192
-rw-r--r--src/include/cctk_core.h1
2 files changed, 193 insertions, 0 deletions
diff --git a/src/include/cctk_Math.h b/src/include/cctk_Math.h
new file mode 100644
index 00000000..ac1029b4
--- /dev/null
+++ b/src/include/cctk_Math.h
@@ -0,0 +1,192 @@
+ /*@@
+ @header cctk_Math.h
+ @date 2012-10-17
+ @author Erik Schnetter
+ @desc
+ Miscellaneous math routines, providing fallback C
+ implementations for broken C++ compilers, and providing
+ dummy implementations for broken C compilers.
+ @enddesc
+ @@*/
+
+#ifndef _CCTK_MATH_H_
+#define _CCTK_MATH_H_
+
+#ifdef __cplusplus
+# include <math.h>
+# include <cmath>
+# include <cctk_Config.h>
+# include <cctk_Types.h>
+#endif
+
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+double CCTK_copysign(double x, double y);
+int CCTK_fpclassify(double x);
+int CCTK_isfinite(double x);
+int CCTK_isinf(double x);
+int CCTK_isnan(double x);
+int CCTK_isnormal(double x);
+int CCTK_signbit(double x);
+
+#ifdef __cplusplus
+}
+#endif
+
+
+
+#ifdef __cplusplus
+
+/* If necessary, provide fallback C implementations for C++, using the
+ routines declared above. */
+
+#ifndef HAVE_CCTK_CXX_COPYSIGN
+# define HAVE_CCTK_CXX_COPYSIGN 1
+# define CCTK_CXX_COPYSIGN CCTK_copysign
+# define HAVE_CCTK_COPYSIGN HAVE_CCTK_CXX_COPYSIGN
+# define CCTK_COPYSIGN CCTK_CXX_COPYSIGN
+#endif
+
+#ifndef HAVE_CCTK_CXX_FPCLASSIFY
+# define HAVE_CCTK_CXX_FPCLASSIFY 1
+# define CCTK_CXX_FPCLASSIFY CCTK_fpclassify
+# define HAVE_CCTK_FPCLASSIFY HAVE_CCTK_CXX_FPCLASSIFY
+# define CCTK_FPCLASSIFY CCTK_CXX_FPCLASSIFY
+#endif
+
+#ifndef HAVE_CCTK_CXX_ISFINITE
+# define HAVE_CCTK_CXX_ISFINITE 1
+# define CCTK_CXX_ISFINITE CCTK_isfinite
+# define HAVE_CCTK_ISFINITE HAVE_CCTK_CXX_ISFINITE
+# define CCTK_ISFINITE CCTK_CXX_ISFINITE
+#endif
+
+#ifndef HAVE_CCTK_CXX_ISINF
+# define HAVE_CCTK_CXX_ISINF 1
+# define CCTK_CXX_ISINF CCTK_isinf
+# define HAVE_CCTK_ISINF HAVE_CCTK_CXX_ISINF
+# define CCTK_ISINF CCTK_CXX_ISINF
+#endif
+
+#ifndef HAVE_CCTK_CXX_ISNAN
+# define HAVE_CCTK_CXX_ISNAN 1
+# define CCTK_CXX_ISNAN CCTK_isnan
+# define HAVE_CCTK_ISNAN HAVE_CCTK_CXX_ISNAN
+# define CCTK_ISNAN CCTK_CXX_ISNAN
+#endif
+
+#ifndef HAVE_CCTK_CXX_ISNORMAL
+# define HAVE_CCTK_CXX_ISNORMAL 1
+# define CCTK_CXX_ISNORMAL CCTK_isnormal
+# define HAVE_CCTK_ISNORMAL HAVE_CCTK_CXX_ISNORMAL
+# define CCTK_ISNORMAL CCTK_CXX_ISNORMAL
+#endif
+
+#ifndef HAVE_CCTK_CXX_SIGNBIT
+# define HAVE_CCTK_CXX_SIGNBIT 1
+# define CCTK_CXX_SIGNBIT CCTK_signbit
+# define HAVE_CCTK_SIGNBIT HAVE_CCTK_CXX_SIGNBIT
+# define CCTK_SIGNBIT CCTK_CXX_SIGNBIT
+#endif
+
+
+
+/* Provide macros, so that these routines can be accessed in a simple
+ manner. */
+
+/* First, we define known-to-be-good implementations in a separate
+ namespace "std::Cactus". These routines can be called from anywhere
+ and will work fine, independent of whether they are implemented as
+ macros, or in which (other) namespace they actually live (could be
+ std or the global namespace).
+
+ Then we define our own macros, making sure to undefine any possibly
+ previously existing macros. To avoid infinite recursion when
+ expanding the macros, we use a prefix "good_" for the functions in
+ the Cactus namespace. */
+
+#define IMPLEMENT_FUNCTIONS(T) \
+ \
+ inline T good_copysign(T x, T y) \
+ { \
+ return CCTK_COPYSIGN(x, y); \
+ } \
+ \
+ inline int good_fpclassify(T x) \
+ { \
+ return CCTK_FPCLASSIFY(x); \
+ } \
+ \
+ inline int good_isfinite(T x) \
+ { \
+ return CCTK_ISFINITE(x); \
+ } \
+ \
+ inline int good_isinf(T x) \
+ { \
+ return CCTK_ISINF(x); \
+ } \
+ \
+ inline int good_isnan(T x) \
+ { \
+ return CCTK_ISNAN(x); \
+ } \
+ \
+ inline int good_isnormal(T x) \
+ { \
+ return CCTK_ISNORMAL(x); \
+ } \
+ \
+ inline int good_signbit(T x) \
+ { \
+ return CCTK_SIGNBIT(x); \
+ }
+
+/* Define the functions in std::Cactus, so that the macro isnan (see
+ below) can be called as std::isnan. */
+namespace std {
+ namespace Cactus {
+
+#ifdef HAVE_CCTK_REAL4
+ IMPLEMENT_FUNCTIONS(CCTK_REAL4)
+#endif
+#ifdef HAVE_CCTK_REAL8
+ IMPLEMENT_FUNCTIONS(CCTK_REAL8)
+#endif
+#ifdef HAVE_CCTK_REAL16
+ IMPLEMENT_FUNCTIONS(CCTK_REAL16)
+#endif
+
+ }
+}
+
+#undef IMPLEMENT_FUNCTIONS
+
+/* Define the macros after the functions above, so that the functions
+ above can refer to the standard (non-Cactus) definitions. */
+
+#undef copysign
+#undef fpclassify
+#undef isfinite
+#undef isinf
+#undef isnan
+#undef isnormal
+#undef signbit
+
+#define copysign Cactus::good_copysign
+#define fpclassify Cactus::good_fpclassify
+#define isfinite Cactus::good_isfinite
+#define isinf Cactus::good_isinf
+#define isnan Cactus::good_isnan
+#define isnormal Cactus::good_isnormal
+#define signbit Cactus::good_signbit
+
+#endif
+
+
+
+#endif
diff --git a/src/include/cctk_core.h b/src/include/cctk_core.h
index 16709e30..cebc6c96 100644
--- a/src/include/cctk_core.h
+++ b/src/include/cctk_core.h
@@ -149,6 +149,7 @@ cctk_ash1,cctk_ash2,cctk_ash3
#include "cctk_Loop.h"
#include "cctk_Main.h"
#include "cctk_Malloc.h"
+#include "cctk_Math.h"
#include "cctk_Misc.h"
#include "cctk_Parameter.h"
#include "cctk_Reduction.h"