[Libclc-dev] [PATCH 3/5] Implement nextafter() builtin

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


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

There are two implementations of nextafter():
1. Using clang's __builtin_nextafter.  Clang replaces this builtin with
a call to nextafter which is part of libm.  Therefore, this
implementation will only work for targets with an implementation of
libm (e.g. most CPU targets).

2. The other implementation is written in OpenCL C.  This function is
known internally as __clc_nextafter and can be used by targets that
don't have access to libm.
---
 configure.py                             |  1 +
 generic/include/clc/clc.h                |  6 +++++
 generic/include/clc/clcmacro.h           |  6 +++++
 generic/include/clc/math/clc_nextafter.h | 11 +++++++++
 generic/include/clc/math/nextafter.h     |  5 ++++
 generic/lib/SOURCES                      |  2 ++
 generic/lib/math/clc_nextafter.cl        | 42 ++++++++++++++++++++++++++++++++
 generic/lib/math/nextafter.cl            | 11 +++++++++
 r600/lib/SOURCES                         |  1 +
 r600/lib/math/nextafter.cl               |  3 +++
 10 files changed, 88 insertions(+)
 create mode 100644 generic/include/clc/math/clc_nextafter.h
 create mode 100644 generic/include/clc/math/nextafter.h
 create mode 100644 generic/lib/math/clc_nextafter.cl
 create mode 100644 generic/lib/math/nextafter.cl
 create mode 100644 r600/lib/math/nextafter.cl

diff --git a/configure.py b/configure.py
index 1a98647..dbb578b 100755
--- a/configure.py
+++ b/configure.py
@@ -145,6 +145,7 @@ for target in targets:
     clang_bc_flags = "-target %s -I`dirname $in` %s " \
                      "-Dcl_clang_storage_class_specifiers " \
                      "-Dcl_khr_fp64 " \
+                     "-D__CLC_INTERNAL " \
                      "-emit-llvm" % (target, clang_cl_includes)
     if device['gpu'] != '':
       clang_bc_flags += ' -mcpu=' + device['gpu']
diff --git a/generic/include/clc/clc.h b/generic/include/clc/clc.h
index 75ca7c7..580750d 100644
--- a/generic/include/clc/clc.h
+++ b/generic/include/clc/clc.h
@@ -45,6 +45,7 @@
 #include <clc/math/log.h>
 #include <clc/math/log2.h>
 #include <clc/math/mad.h>
+#include <clc/math/nextafter.h>
 #include <clc/math/pow.h>
 #include <clc/math/rint.h>
 #include <clc/math/sin.h>
@@ -104,4 +105,9 @@
 #include <clc/atomic/atomic_add.h>
 #include <clc/atomic/atomic_inc.h>
 
+/* libclc internal defintions */
+#ifdef __CLC_INTERNAL
+#include <math/clc_nextafter.h>
+#endif
+
 #pragma OPENCL EXTENSION all : disable
diff --git a/generic/include/clc/clcmacro.h b/generic/include/clc/clcmacro.h
index ece0d3b..730073a 100644
--- a/generic/include/clc/clcmacro.h
+++ b/generic/include/clc/clcmacro.h
@@ -41,6 +41,12 @@
     return (RET_TYPE##16)(FUNCTION(x.lo, y.lo), FUNCTION(x.hi, y.hi)); \
   }
 
+#define _CLC_DEFINE_BINARY_BUILTIN(RET_TYPE, FUNCTION, BUILTIN, ARG1_TYPE, ARG2_TYPE) \
+_CLC_DEF _CLC_OVERLOAD RET_TYPE FUNCTION(ARG1_TYPE x, ARG2_TYPE y) { \
+  return BUILTIN(x, y); \
+} \
+_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, RET_TYPE, FUNCTION, ARG1_TYPE, ARG2_TYPE)
+
 #define _CLC_DEFINE_UNARY_BUILTIN(RET_TYPE, FUNCTION, BUILTIN, ARG1_TYPE) \
 _CLC_DEF _CLC_OVERLOAD RET_TYPE FUNCTION(ARG1_TYPE x) { \
   return BUILTIN(x); \
diff --git a/generic/include/clc/math/clc_nextafter.h b/generic/include/clc/math/clc_nextafter.h
new file mode 100644
index 0000000..81c8f36
--- /dev/null
+++ b/generic/include/clc/math/clc_nextafter.h
@@ -0,0 +1,11 @@
+#define __CLC_BODY <clc/math/binary_decl.inc>
+
+#define __CLC_FUNCTION nextafter
+#include <clc/math/gentype.inc>
+#undef __CLC_FUNCTION
+
+#define __CLC_FUNCTION __clc_nextafter
+#include <clc/math/gentype.inc>
+#undef __CLC_FUNCTION
+
+#undef __CLC_BODY
diff --git a/generic/include/clc/math/nextafter.h b/generic/include/clc/math/nextafter.h
new file mode 100644
index 0000000..06e1b2a
--- /dev/null
+++ b/generic/include/clc/math/nextafter.h
@@ -0,0 +1,5 @@
+#define __CLC_BODY <clc/math/binary_decl.inc>
+#define __CLC_FUNCTION nextafter
+#include <clc/math/gentype.inc>
+#undef __CLC_FUNCTION
+#undef __CLC_BODY
diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES
index 383a66e..e1a2972 100644
--- a/generic/lib/SOURCES
+++ b/generic/lib/SOURCES
@@ -26,6 +26,8 @@ math/fmax.cl
 math/fmin.cl
 math/hypot.cl
 math/mad.cl
+math/clc_nextafter.cl
+math/nextafter.cl
 relational/any.cl
 relational/isnan.cl
 shared/clamp.cl
diff --git a/generic/lib/math/clc_nextafter.cl b/generic/lib/math/clc_nextafter.cl
new file mode 100644
index 0000000..92b990d
--- /dev/null
+++ b/generic/lib/math/clc_nextafter.cl
@@ -0,0 +1,42 @@
+#include <clc/clc.h>
+
+// This file provides OpenCL C implementations of nextafter for targets that
+// don't support the clang builtin.
+
+#define FLT_NAN 0.0f/0.0f
+
+#define NEXTAFTER(FLOAT_TYPE, UINT_TYPE, NAN, ZERO, NEXTAFTER_ZERO) \
+_CLC_OVERLOAD _CLC_DEF FLOAT_TYPE __clc_nextafter(FLOAT_TYPE x, FLOAT_TYPE y) { \
+  union {                     \
+    FLOAT_TYPE f;             \
+    UINT_TYPE i;              \
+  } next;                     \
+  if (isnan(x) || isnan(y)) { \
+    return NAN;               \
+  }                           \
+  if (x == y) {               \
+    return y;                 \
+  }                           \
+  next.f = x;                 \
+  if (x < y) {                \
+    next.i++;                 \
+  } else {                    \
+    if (next.f == ZERO) {     \
+    next.i = NEXTAFTER_ZERO;  \
+    } else {                  \
+      next.i--;               \
+    }                         \
+  }                           \
+  return next.f;              \
+}
+
+NEXTAFTER(float, uint, FLT_NAN, 0.0f, 0x80000001)
+_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __clc_nextafter, float, float)
+
+#ifdef cl_khr_fp64
+#pragma OPENCL EXTENSION cl_khr_fp64 : enable
+#define DBL_NAN 0.0/0.0
+
+NEXTAFTER(double, ulong, DBL_NAN, 0.0, 0x8000000000000001)
+_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __clc_nextafter, double, double)
+#endif
diff --git a/generic/lib/math/nextafter.cl b/generic/lib/math/nextafter.cl
new file mode 100644
index 0000000..1a7f169
--- /dev/null
+++ b/generic/lib/math/nextafter.cl
@@ -0,0 +1,11 @@
+#include <clc/clc.h>
+
+_CLC_DEFINE_BINARY_BUILTIN(float, nextafter, __builtin_nextafterf, float, float)
+
+#ifdef cl_khr_fp64
+
+#pragma OPENCL EXTENSION cl_khr_fp64 : enable
+
+_CLC_DEFINE_BINARY_BUILTIN(double, nextafter, __builtin_nextafter, double, double)
+
+#endif
diff --git a/r600/lib/SOURCES b/r600/lib/SOURCES
index de30f6e..aac6d8f 100644
--- a/r600/lib/SOURCES
+++ b/r600/lib/SOURCES
@@ -1,4 +1,5 @@
 atomic/atomic.cl
+math/nextafter.cl
 workitem/get_num_groups.ll
 workitem/get_group_id.ll
 workitem/get_local_size.ll
diff --git a/r600/lib/math/nextafter.cl b/r600/lib/math/nextafter.cl
new file mode 100644
index 0000000..54bed5f
--- /dev/null
+++ b/r600/lib/math/nextafter.cl
@@ -0,0 +1,3 @@
+#include <clc/clc.h>
+
+_CLC_DEFINE_BINARY_BUILTIN(float, nextafter, __clc_nextafter, float, float)
-- 
1.7.11.4





More information about the Libclc-dev mailing list