Skip to content

[SYCL] Add missing fabs #12218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libdevice/cmath_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ long int labs(long int x) { return __devicelib_labs(x); }
DEVICE_EXTERN_C_INLINE
long long int llabs(long long int x) { return __devicelib_llabs(x); }

DEVICE_EXTERN_C_INLINE
float fabsf(float x) { return __devicelib_fabsf(x); }

DEVICE_EXTERN_C_INLINE
div_t div(int x, int y) { return __devicelib_div(x, y); }

Expand Down
4 changes: 4 additions & 0 deletions libdevice/cmath_wrapper_fp64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
// reference. If users provide their own math or complex functions(with
// the prototype), functions in device libraries will be ignored and
// overrided by users' version.

DEVICE_EXTERN_C_INLINE
double fabs(double x) { return __devicelib_fabs(x); }

DEVICE_EXTERN_C_INLINE
double log(double x) { return __devicelib_log(x); }

Expand Down
6 changes: 6 additions & 0 deletions libdevice/device_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ long int __devicelib_labs(long int x);
DEVICE_EXTERN_C
long long int __devicelib_llabs(long long int x);

DEVICE_EXTERN_C
float __devicelib_fabsf(float x);

DEVICE_EXTERN_C
double __devicelib_fabs(double x);

DEVICE_EXTERN_C
div_t __devicelib_div(int x, int y);

Expand Down
4 changes: 4 additions & 0 deletions libdevice/fallback-cmath-fp64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
// To support fallback device libraries on-demand loading, please update the
// DeviceLibFuncMap in llvm/tools/sycl-post-link/sycl-post-link.cpp if you add
// or remove any item in this file.

DEVICE_EXTERN_C_INLINE
double __devicelib_fabs(double x) { return x < 0 ? -x : x; }

DEVICE_EXTERN_C_INLINE
double __devicelib_log(double x) { return __spirv_ocl_log(x); }

Expand Down
3 changes: 3 additions & 0 deletions libdevice/fallback-cmath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ long int __devicelib_labs(long int x) { return x < 0 ? -x : x; }
DEVICE_EXTERN_C_INLINE
long long int __devicelib_llabs(long long int x) { return x < 0 ? -x : x; }

DEVICE_EXTERN_C_INLINE
float __devicelib_fabsf(float x) { return x < 0 ? -x : x; }

DEVICE_EXTERN_C_INLINE
div_t __devicelib_div(int x, int y) { return {x / y, x % y}; }

Expand Down
9 changes: 5 additions & 4 deletions sycl/test-e2e/DeviceLib/cmath_fp64_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ namespace s = sycl;
constexpr s::access::mode sycl_read = s::access::mode::read;
constexpr s::access::mode sycl_write = s::access::mode::write;

#define TEST_NUM 63
#define TEST_NUM 64

double ref[TEST_NUM] = {
1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0.5, 0, 2, 0, 0, 1, 0, 2, 0, 0,
0, 0, 0, 1, 0, 1, 2, 0, 1, 2, 5, 0, 0, 0, 0, 0.5, 0.5, NAN, NAN, 2, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0.5, 0, 2, 0, 0, 1, 0, 2, 0, 0,
0, 0, 0, 1, 0, 1, 2, 0, 1, 2, 5, 0, 0, 0, 0, 0.5, 0.5, NAN, NAN, 2, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

double refIptr = 1;

Expand Down Expand Up @@ -59,6 +59,7 @@ template <class T> void device_cmath_test(s::queue &deviceQueue) {
T minus_infinity = -INFINITY;
double subnormal;
*((uint64_t *)&subnormal) = 0xFFFFFFFFFFFFFULL;
res_access[i++] = std::fabs(-1.0);
res_access[i++] = std::cos(0.0);
res_access[i++] = std::sin(0.0);
res_access[i++] = std::round(1.0);
Expand Down
11 changes: 6 additions & 5 deletions sycl/test-e2e/DeviceLib/cmath_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ template <class T> void device_cmath_test_1(s::queue &deviceQueue) {
assert(quo == 0);
}

// MSVC implements std::ldexp<float> and std::frexp<float> by invoking the
// 'double' version of corresponding C math functions(ldexp and frexp). Those
// 2 functions can only work on Windows with fp64 extension support from
// underlying device.
// MSVC implements std::ldexp<float>, std::fabs<float> and std::frexp<float> by
// invoking the 'double' version of corresponding C math functions(ldexp, fabs
// and frexp). Those functions can only work on Windows with fp64 extension
// support from underlying device.
#ifndef _WIN32
template <class T> void device_cmath_test_2(s::queue &deviceQueue) {
s::range<1> numOfItems{2};
T result[2] = {-1};
T ref[2] = {0, 2};
T ref[3] = {0, 2, 1};
// Variable exponent is an integer value to store the exponent in frexp
// function
int exponent = -1;
Expand All @@ -166,6 +166,7 @@ template <class T> void device_cmath_test_2(s::queue &deviceQueue) {
int i = 0;
res_access[i++] = std::frexp(0.0f, &exp_access[0]);
res_access[i++] = std::ldexp(1.0f, 1);
res_access[i++] = std::fabs(-1.0f);
});
});
}
Expand Down