[libclc] r192383 - Implement nextafter() builtin

Tom Stellard thomas.stellard at amd.com
Thu Oct 10 12:08:51 PDT 2013


Author: tstellar
Date: Thu Oct 10 14:08:51 2013
New Revision: 192383

URL: http://llvm.org/viewvc/llvm-project?rev=192383&view=rev
Log:
Implement nextafter() builtin

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.

Added:
    libclc/trunk/generic/include/clc/math/clc_nextafter.h
    libclc/trunk/generic/include/clc/math/nextafter.h
    libclc/trunk/generic/include/math/
    libclc/trunk/generic/include/math/clc_nextafter.h
    libclc/trunk/generic/lib/math/clc_nextafter.cl
    libclc/trunk/generic/lib/math/nextafter.cl
    libclc/trunk/r600/lib/math/
    libclc/trunk/r600/lib/math/nextafter.cl
Modified:
    libclc/trunk/configure.py
    libclc/trunk/generic/include/clc/clc.h
    libclc/trunk/generic/include/clc/clcmacro.h
    libclc/trunk/generic/lib/SOURCES
    libclc/trunk/r600/lib/SOURCES

Modified: libclc/trunk/configure.py
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/configure.py?rev=192383&r1=192382&r2=192383&view=diff
==============================================================================
--- libclc/trunk/configure.py (original)
+++ libclc/trunk/configure.py Thu Oct 10 14:08:51 2013
@@ -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']

Modified: libclc/trunk/generic/include/clc/clc.h
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/clc.h?rev=192383&r1=192382&r2=192383&view=diff
==============================================================================
--- libclc/trunk/generic/include/clc/clc.h (original)
+++ libclc/trunk/generic/include/clc/clc.h Thu Oct 10 14:08:51 2013
@@ -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>
@@ -107,4 +108,9 @@
 #include <clc/atomic/atomic_inc.h>
 #include <clc/atomic/atomic_sub.h>
 
+/* libclc internal defintions */
+#ifdef __CLC_INTERNAL
+#include <math/clc_nextafter.h>
+#endif
+
 #pragma OPENCL EXTENSION all : disable

Modified: libclc/trunk/generic/include/clc/clcmacro.h
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/clcmacro.h?rev=192383&r1=192382&r2=192383&view=diff
==============================================================================
--- libclc/trunk/generic/include/clc/clcmacro.h (original)
+++ libclc/trunk/generic/include/clc/clcmacro.h Thu Oct 10 14:08:51 2013
@@ -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); \

Added: libclc/trunk/generic/include/clc/math/clc_nextafter.h
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/math/clc_nextafter.h?rev=192383&view=auto
==============================================================================
--- libclc/trunk/generic/include/clc/math/clc_nextafter.h (added)
+++ libclc/trunk/generic/include/clc/math/clc_nextafter.h Thu Oct 10 14:08:51 2013
@@ -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

Added: libclc/trunk/generic/include/clc/math/nextafter.h
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/math/nextafter.h?rev=192383&view=auto
==============================================================================
--- libclc/trunk/generic/include/clc/math/nextafter.h (added)
+++ libclc/trunk/generic/include/clc/math/nextafter.h Thu Oct 10 14:08:51 2013
@@ -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

Added: libclc/trunk/generic/include/math/clc_nextafter.h
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/math/clc_nextafter.h?rev=192383&view=auto
==============================================================================
--- libclc/trunk/generic/include/math/clc_nextafter.h (added)
+++ libclc/trunk/generic/include/math/clc_nextafter.h Thu Oct 10 14:08:51 2013
@@ -0,0 +1,7 @@
+#define __CLC_BODY <clc/math/binary_decl.inc>
+#define __CLC_FUNCTION __clc_nextafter
+
+#include <clc/math/gentype.inc>
+
+#undef __CLC_BODY
+#undef __CLC_FUNCTION

Modified: libclc/trunk/generic/lib/SOURCES
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/SOURCES?rev=192383&r1=192382&r2=192383&view=diff
==============================================================================
--- libclc/trunk/generic/lib/SOURCES (original)
+++ libclc/trunk/generic/lib/SOURCES Thu Oct 10 14:08:51 2013
@@ -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

Added: libclc/trunk/generic/lib/math/clc_nextafter.cl
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/math/clc_nextafter.cl?rev=192383&view=auto
==============================================================================
--- libclc/trunk/generic/lib/math/clc_nextafter.cl (added)
+++ libclc/trunk/generic/lib/math/clc_nextafter.cl Thu Oct 10 14:08:51 2013
@@ -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

Added: libclc/trunk/generic/lib/math/nextafter.cl
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/math/nextafter.cl?rev=192383&view=auto
==============================================================================
--- libclc/trunk/generic/lib/math/nextafter.cl (added)
+++ libclc/trunk/generic/lib/math/nextafter.cl Thu Oct 10 14:08:51 2013
@@ -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

Modified: libclc/trunk/r600/lib/SOURCES
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/r600/lib/SOURCES?rev=192383&r1=192382&r2=192383&view=diff
==============================================================================
--- libclc/trunk/r600/lib/SOURCES (original)
+++ libclc/trunk/r600/lib/SOURCES Thu Oct 10 14:08:51 2013
@@ -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

Added: libclc/trunk/r600/lib/math/nextafter.cl
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/r600/lib/math/nextafter.cl?rev=192383&view=auto
==============================================================================
--- libclc/trunk/r600/lib/math/nextafter.cl (added)
+++ libclc/trunk/r600/lib/math/nextafter.cl Thu Oct 10 14:08:51 2013
@@ -0,0 +1,3 @@
+#include <clc/clc.h>
+
+_CLC_DEFINE_BINARY_BUILTIN(float, nextafter, __clc_nextafter, float, float)





More information about the cfe-commits mailing list