Replace nextafter implementation

This one passes conformance.

llvm-svn: 280961
This commit is contained in:
Matt Arsenault 2016-09-08 16:37:56 +00:00
parent 073472a2bf
commit fbfd828d2a
2 changed files with 29 additions and 28 deletions

View File

@ -2,3 +2,8 @@
#include "../lib/clcmacro.h" #include "../lib/clcmacro.h"
_CLC_DEFINE_BINARY_BUILTIN(float, nextafter, __clc_nextafter, float, float) _CLC_DEFINE_BINARY_BUILTIN(float, nextafter, __clc_nextafter, float, float)
#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
_CLC_DEFINE_BINARY_BUILTIN(double, nextafter, __clc_nextafter, double, double)
#endif

View File

@ -1,43 +1,39 @@
#include <clc/clc.h> #include <clc/clc.h>
#include "../clcmacro.h" #include "../clcmacro.h"
// This file provides OpenCL C implementations of nextafter for targets that // This file provides OpenCL C implementations of nextafter for
// don't support the clang builtin. // targets that don't support the clang builtin.
#define FLT_NAN 0.0f/0.0f #define AS_TYPE(x) as_##x
#define NEXTAFTER(FLOAT_TYPE, UINT_TYPE, NAN, ZERO, NEXTAFTER_ZERO) \ #define NEXTAFTER(FLOAT_TYPE, UINT_TYPE, INT_TYPE) \
_CLC_OVERLOAD _CLC_DEF FLOAT_TYPE __clc_nextafter(FLOAT_TYPE x, FLOAT_TYPE y) { \ _CLC_OVERLOAD _CLC_DEF FLOAT_TYPE __clc_nextafter(FLOAT_TYPE x, FLOAT_TYPE y) { \
union { \ const UINT_TYPE sign_bit \
FLOAT_TYPE f; \ = (UINT_TYPE)1 << (sizeof(INT_TYPE) * 8 - 1); \
UINT_TYPE i; \ const UINT_TYPE sign_bit_mask = sign_bit - 1; \
} next; \ INT_TYPE ix = AS_TYPE(INT_TYPE)(x); \
if (isnan(x) || isnan(y)) { \ INT_TYPE ax = ix & sign_bit_mask; \
return NAN; \ INT_TYPE mx = sign_bit - ix; \
} \ mx = ix < 0 ? mx : ix; \
if (x == y) { \ INT_TYPE iy = AS_TYPE(INT_TYPE)(y); \
return y; \ INT_TYPE ay = iy & sign_bit_mask; \
} \ INT_TYPE my = sign_bit - iy; \
next.f = x; \ my = iy < 0 ? my : iy; \
if (x < y) { \ INT_TYPE t = mx + (mx < my ? 1 : -1); \
next.i++; \ INT_TYPE r = sign_bit - t; \
} else { \ r = t < 0 ? r : t; \
if (next.f == ZERO) { \ r = isnan(x) ? ix : r; \
next.i = NEXTAFTER_ZERO; \ r = isnan(y) ? iy : r; \
} else { \ r = ((ax | ay) == 0 | ix == iy) ? iy : r; \
next.i--; \ return AS_TYPE(FLOAT_TYPE)(r); \
} \
} \
return next.f; \
} }
NEXTAFTER(float, uint, FLT_NAN, 0.0f, 0x80000001) NEXTAFTER(float, uint, int)
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __clc_nextafter, float, float) _CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __clc_nextafter, float, float)
#ifdef cl_khr_fp64 #ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable #pragma OPENCL EXTENSION cl_khr_fp64 : enable
#define DBL_NAN 0.0/0.0
NEXTAFTER(double, ulong, DBL_NAN, 0.0, 0x8000000000000001) NEXTAFTER(double, ulong, long)
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __clc_nextafter, double, double) _CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __clc_nextafter, double, double)
#endif #endif