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.

llvm-svn: 192383
This commit is contained in:
Tom Stellard 2013-10-10 19:08:51 +00:00
parent e36e9dec65
commit 6c7b86c106
11 changed files with 95 additions and 0 deletions

View File

@ -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']

View File

@ -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

View File

@ -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); \

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,3 @@
#include <clc/clc.h>
_CLC_DEFINE_BINARY_BUILTIN(float, nextafter, __clc_nextafter, float, float)