forked from KhronosGroup/SYCL-CTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accuracy.h
34 lines (30 loc) · 1.12 KB
/
accuracy.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*******************************************************************************
//
// SYCL 1.2.1 Conformance Test Suite
//
// Support for accuracy check
//
*******************************************************************************/
#ifndef __SYCLCTS_UTIL_ACCURACY_H
#define __SYCLCTS_UTIL_ACCURACY_H
#include <cmath>
#include <limits>
/**
* @brief Provides ulp(x) by definition given in OpenCL 1.2 rev. 19, 7.4
* See Jean-Michel Muller "On the definition of ulp (x)", definition 7
* Using sycl functions.
*/
template <typename T> T get_ulp_sycl(T x) {
const T inf = std::numeric_limits<T>::infinity();
const T negative = cl::sycl::fabs(cl::sycl::nextafter(x, -1.0f * inf) - x);
const T positive = cl::sycl::fabs(cl::sycl::nextafter(x, inf) - x);
return cl::sycl::fmin(negative, positive);
}
template <>
inline cl::sycl::half get_ulp_sycl<cl::sycl::half>(cl::sycl::half x) {
const auto ulp = get_ulp_sycl<float>(x);
const float multiplier = 8192.0f;
// Multiplier is set according to the difference in precision
return static_cast<cl::sycl::half>(ulp * multiplier);
}
#endif // __SYCLCTS_UTIL_ACCURACY_H