Skip to content

Commit 37aa84b

Browse files
authored
[SYCL] Improve the accuracy of host sycl::cospi (#9575)
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
1 parent b682bc9 commit 37aa84b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

sycl/source/detail/builtins_math.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ template <typename T> inline T __atan2pi(T x, T y) {
3636
return std::atan2(x, y) / M_PI;
3737
}
3838

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

4143
template <typename T> T inline __fract(T x, T *iptr) {
4244
T f = std::floor(x);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %{build} -o %t.out
2+
// RUN: %{run} %t.out
3+
4+
#include <iomanip>
5+
#include <iostream>
6+
#include <sycl.hpp>
7+
8+
template <typename T> T get_ulp_std(T x) {
9+
const T inf = std::numeric_limits<T>::infinity();
10+
const T negative = std::fabs(std::nextafter(x, -inf) - x);
11+
const T positive = std::fabs(std::nextafter(x, inf) - x);
12+
return std::fmin(negative, positive);
13+
}
14+
15+
void testCospi() {
16+
const double value = 0.4863334355; // some random value
17+
const double reference =
18+
0.042921588887841428949787569990803604014217853546142578125; // calculated
19+
// with
20+
// oclmath
21+
const unsigned int ulpsExpected =
22+
4; // according to section 4.17.5 of the SYCL 2020 spec, math functions
23+
// correspond to those from OpenCL 1.2 spec, so this accuracy comes
24+
// from the OpenCL 1.2 spec
25+
const double differenceExpected = ulpsExpected * get_ulp_std(reference);
26+
const double hostDifference = std::fabs(sycl::cospi(value) - reference);
27+
28+
std::cout << std::setprecision(17) << "cospi: " << '\n'
29+
<< "ref:\t" << reference << '\n'
30+
<< "host:\t" << sycl::cospi(value) << '\n'
31+
<< "diff host:\t " << hostDifference << '\n'
32+
<< "expected:\t" << differenceExpected << std::endl;
33+
34+
assert(hostDifference <= differenceExpected && "Host result incorrect");
35+
}
36+
37+
int main() {
38+
testCospi();
39+
return 0;
40+
}

0 commit comments

Comments
 (0)