Skip to content

[SYCL] Improve the accuracy of host sycl::cospi #9575

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 5 commits into from
May 30, 2023
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
4 changes: 3 additions & 1 deletion sycl/source/detail/builtins_math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ template <typename T> inline T __atan2pi(T x, T y) {
return std::atan2(x, y) / M_PI;
}

template <typename T> inline T __cospi(T x) { return std::cos(M_PI * x); }
template <typename T> inline T __cospi(T x) {
return std::sin(M_PI * (0.5 - x));
}

template <typename T> T inline __fract(T x, T *iptr) {
T f = std::floor(x);
Expand Down
40 changes: 40 additions & 0 deletions sycl/test-e2e/Basic/built-ins/host_math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// RUN: %{build} -o %t.out
// RUN: %{run} %t.out

#include <iomanip>
#include <iostream>
#include <sycl.hpp>

template <typename T> T get_ulp_std(T x) {
const T inf = std::numeric_limits<T>::infinity();
const T negative = std::fabs(std::nextafter(x, -inf) - x);
const T positive = std::fabs(std::nextafter(x, inf) - x);
return std::fmin(negative, positive);
}

void testCospi() {
const double value = 0.4863334355; // some random value
const double reference =
0.042921588887841428949787569990803604014217853546142578125; // calculated
// with
// oclmath
const unsigned int ulpsExpected =
4; // according to section 4.17.5 of the SYCL 2020 spec, math functions
// correspond to those from OpenCL 1.2 spec, so this accuracy comes
// from the OpenCL 1.2 spec
const double differenceExpected = ulpsExpected * get_ulp_std(reference);
const double hostDifference = std::fabs(sycl::cospi(value) - reference);

std::cout << std::setprecision(17) << "cospi: " << '\n'
<< "ref:\t" << reference << '\n'
<< "host:\t" << sycl::cospi(value) << '\n'
<< "diff host:\t " << hostDifference << '\n'
<< "expected:\t" << differenceExpected << std::endl;

assert(hostDifference <= differenceExpected && "Host result incorrect");
}

int main() {
testCospi();
return 0;
}