1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #ifndef TEST_CONFORMANCE_CLCPP_MATH_FUNCS_POWER_FUNCS_HPP
17 #define TEST_CONFORMANCE_CLCPP_MATH_FUNCS_POWER_FUNCS_HPP
18
19 #include <limits>
20 #include <type_traits>
21 #include <cmath>
22
23 #include "common.hpp"
24
25 #define DEFINE_BINARY_POWER_FUNC_INT(NAME, HOST_FUNC, USE_ULP, ULP, ULP_EMBEDDED, MIN1, MAX1, MIN2, MAX2) \
26 struct power_func_ ## NAME : public binary_func<cl_float, cl_int, cl_float> \
27 { \
28 power_func_ ## NAME(bool is_embedded) : m_is_embedded(is_embedded) \
29 { \
30 \
31 } \
32 \
33 std::string str() \
34 { \
35 return #NAME; \
36 } \
37 \
38 std::string headers() \
39 { \
40 return "#include <opencl_math>\n"; \
41 } \
42 /* Reference value type is cl_double */ \
43 cl_double operator()(const cl_float& x, const cl_int& y) \
44 { \
45 return (HOST_FUNC)(static_cast<cl_double>(x), y); \
46 } \
47 \
48 cl_float min1() \
49 { \
50 return MIN1; \
51 } \
52 \
53 cl_float max1() \
54 { \
55 return MAX1; \
56 } \
57 \
58 cl_int min2() \
59 { \
60 return MIN2; \
61 } \
62 \
63 cl_int max2() \
64 { \
65 return MAX2; \
66 } \
67 \
68 std::vector<cl_float> in1_special_cases() \
69 { \
70 return { \
71 cl_float(-1.0f), \
72 cl_float(0.0f), \
73 cl_float(-0.0f), \
74 }; \
75 } \
76 \
77 std::vector<cl_int> in2_special_cases() \
78 { \
79 return { \
80 2, 3, -1, 1, -2, 2 \
81 }; \
82 } \
83 \
84 bool use_ulp() \
85 { \
86 return USE_ULP; \
87 } \
88 \
89 float ulp() \
90 { \
91 if(m_is_embedded) \
92 { \
93 return ULP_EMBEDDED; \
94 } \
95 return ULP; \
96 } \
97 private: \
98 bool m_is_embedded; \
99 };
100
101 // group_name, func_name, reference_func, use_ulp, ulp, ulp_for_embedded, max_delta, min1, max1
102 MATH_FUNCS_DEFINE_UNARY_FUNC(power, cbrt, std::cbrt, true, 2.0f, 4.0f, 0.001f, -1000.0f, -9.0f)
103 MATH_FUNCS_DEFINE_UNARY_FUNC(power, rsqrt, reference::rsqrt, true, 2.0f, 4.0f, 0.001f, 1.0f, 100.0f)
104 MATH_FUNCS_DEFINE_UNARY_FUNC(power, sqrt, std::sqrt, true, 3.0f, 4.0f, 0.001f, 1.0f, 100.0f)
105
106 // group_name, func_name, reference_func, use_ulp, ulp, ulp_for_embedded, max_delta, min1, max1, min2, max2
107 MATH_FUNCS_DEFINE_BINARY_FUNC(power, pow, std::pow, true, 16.0f, 16.0f, 0.001f, 1.0f, 100.0f, 1.0f, 10.0f)
108 MATH_FUNCS_DEFINE_BINARY_FUNC(power, powr, reference::powr, true, 16.0f, 16.0f, 0.001f, 1.0f, 100.0f, 1.0f, 10.0f)
109
110 // func_name, reference_func, use_ulp, ulp, ulp_for_embedded, min1, max1, min2, max2
111 DEFINE_BINARY_POWER_FUNC_INT(pown, std::pow, true, 16.0f, 16.0f, 1.0f, 100.0f, 1, 10)
112 DEFINE_BINARY_POWER_FUNC_INT(rootn, reference::rootn, true, 16.0f, 16.0f, -100.0f, 100.0f, -10, 10)
113
114 // power functions
AUTO_TEST_CASE(test_power_funcs)115 AUTO_TEST_CASE(test_power_funcs)
116 (cl_device_id device, cl_context context, cl_command_queue queue, int n_elems)
117 {
118 int error = CL_SUCCESS;
119 int last_error = CL_SUCCESS;
120
121 // Check for EMBEDDED_PROFILE
122 bool is_embedded_profile = false;
123 char profile[128];
124 last_error = clGetDeviceInfo(device, CL_DEVICE_PROFILE, sizeof(profile), (void *)&profile, NULL);
125 RETURN_ON_CL_ERROR(last_error, "clGetDeviceInfo")
126 if (std::strcmp(profile, "EMBEDDED_PROFILE") == 0)
127 is_embedded_profile = true;
128
129 // gentype cbrt(gentype x);
130 // gentype rsqrt(gentype x);
131 // gentype sqrt(gentype x);
132 TEST_UNARY_FUNC_MACRO((power_func_cbrt(is_embedded_profile)))
133 TEST_UNARY_FUNC_MACRO((power_func_sqrt(is_embedded_profile)))
134 TEST_UNARY_FUNC_MACRO((power_func_rsqrt(is_embedded_profile)))
135
136 // gentype pow(gentype x, gentype y);
137 // gentype powr(gentype x, gentype y);
138 TEST_BINARY_FUNC_MACRO((power_func_pow(is_embedded_profile)))
139 TEST_BINARY_FUNC_MACRO((power_func_powr(is_embedded_profile)))
140
141 // gentype pown(gentype x, intn y);
142 // gentype rootn(gentype x, intn y);
143 TEST_BINARY_FUNC_MACRO((power_func_pown(is_embedded_profile)))
144 TEST_BINARY_FUNC_MACRO((power_func_rootn(is_embedded_profile)))
145
146 if(error != CL_SUCCESS)
147 {
148 return -1;
149 }
150 return error;
151 }
152
153 #endif // TEST_CONFORMANCE_CLCPP_MATH_FUNCS_POWER_FUNCS_HPP
154