[Libclc-dev] [PATCH 2/5] Implement isnan() builtin

Tom Stellard tom at stellard.net
Thu Sep 12 15:07:11 PDT 2013


From: Tom Stellard <thomas.stellard at amd.com>

---
 generic/include/clc/clc.h              |  1 +
 generic/include/clc/clcmacro.h         |  6 ++++++
 generic/include/clc/relational/isnan.h | 21 +++++++++++++++++++++
 generic/lib/SOURCES                    |  1 +
 generic/lib/relational/isnan.cl        | 17 +++++++++++++++++
 5 files changed, 46 insertions(+)
 create mode 100644 generic/include/clc/relational/isnan.h
 create mode 100644 generic/lib/relational/isnan.cl

diff --git a/generic/include/clc/clc.h b/generic/include/clc/clc.h
index 105180e..75ca7c7 100644
--- a/generic/include/clc/clc.h
+++ b/generic/include/clc/clc.h
@@ -93,6 +93,7 @@
 /* 6.11.6 Relational Functions */
 #include <clc/relational/any.h>
 #include <clc/relational/bitselect.h>
+#include <clc/relational/isnan.h>
 #include <clc/relational/select.h>
 
 /* 6.11.8 Synchronization Functions */
diff --git a/generic/include/clc/clcmacro.h b/generic/include/clc/clcmacro.h
index d10a613..ece0d3b 100644
--- a/generic/include/clc/clcmacro.h
+++ b/generic/include/clc/clcmacro.h
@@ -40,3 +40,9 @@
   DECLSPEC RET_TYPE##16 FUNCTION(ARG1_TYPE##16 x, ARG2_TYPE##16 y) { \
     return (RET_TYPE##16)(FUNCTION(x.lo, y.lo), FUNCTION(x.hi, y.hi)); \
   }
+
+#define _CLC_DEFINE_UNARY_BUILTIN(RET_TYPE, FUNCTION, BUILTIN, ARG1_TYPE) \
+_CLC_DEF _CLC_OVERLOAD RET_TYPE FUNCTION(ARG1_TYPE x) { \
+  return BUILTIN(x); \
+} \
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, RET_TYPE, FUNCTION, ARG1_TYPE)
diff --git a/generic/include/clc/relational/isnan.h b/generic/include/clc/relational/isnan.h
new file mode 100644
index 0000000..93eb9df
--- /dev/null
+++ b/generic/include/clc/relational/isnan.h
@@ -0,0 +1,21 @@
+
+#define _CLC_ISNAN_DECL(RET_TYPE, ARG_TYPE) \
+  _CLC_OVERLOAD _CLC_DECL RET_TYPE isnan(ARG_TYPE);
+
+#define _CLC_VECTOR_ISNAN_DECL(RET_TYPE, ARG_TYPE) \
+  _CLC_ISNAN_DECL(RET_TYPE##2, ARG_TYPE##2) \
+  _CLC_ISNAN_DECL(RET_TYPE##3, ARG_TYPE##3) \
+  _CLC_ISNAN_DECL(RET_TYPE##4, ARG_TYPE##4) \
+  _CLC_ISNAN_DECL(RET_TYPE##8, ARG_TYPE##8) \
+  _CLC_ISNAN_DECL(RET_TYPE##16, ARG_TYPE##16)
+
+_CLC_ISNAN_DECL(int, float)
+_CLC_VECTOR_ISNAN_DECL(int, float)
+
+#ifdef cl_khr_fp64
+_CLC_ISNAN_DECL(int, double)
+_CLC_VECTOR_ISNAN_DECL(long, double)
+#endif
+
+#undef _CLC_ISNAN_DECL
+#undef _CLC_VECTOR_ISNAN_DECL
diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES
index 16db9bc..383a66e 100644
--- a/generic/lib/SOURCES
+++ b/generic/lib/SOURCES
@@ -27,6 +27,7 @@ math/fmin.cl
 math/hypot.cl
 math/mad.cl
 relational/any.cl
+relational/isnan.cl
 shared/clamp.cl
 shared/max.cl
 shared/min.cl
diff --git a/generic/lib/relational/isnan.cl b/generic/lib/relational/isnan.cl
new file mode 100644
index 0000000..1a64f67
--- /dev/null
+++ b/generic/lib/relational/isnan.cl
@@ -0,0 +1,17 @@
+#include <clc/clc.h>
+
+_CLC_DEFINE_UNARY_BUILTIN(int, isnan, __builtin_isnan, float)
+
+#ifdef cl_khr_fp64
+
+#pragma OPENCL EXTENSION cl_khr_fp64 : enable
+
+// The scalar version of isnan(double) returns an int, but the vector versions
+// return long.
+_CLC_DEF _CLC_OVERLOAD int isnan(double x) {
+  return __builtin_isnan(x);
+}
+
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, long, isnan, double)
+
+#endif
-- 
1.7.11.4





More information about the Libclc-dev mailing list