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_TRI_FUNCS_HPP
17 #define TEST_CONFORMANCE_CLCPP_MATH_FUNCS_TRI_FUNCS_HPP
18
19 #include <type_traits>
20 #include <cmath>
21
22 #include "common.hpp"
23
24 // group_name, func_name, reference_func, use_ulp, ulp, ulp_for_embedded, max_delta, min1, max1
25 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, acos, std::acos, true, 4.0f, 4.0f, 0.001f, -1.0f, 1.0f)
26 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, acosh, std::acosh, true, 4.0f, 4.0f, 0.001f, -1.0f, 1.0f)
27 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, acospi, reference::acospi, true, 5.0f, 5.0f, 0.001f, -1.0f, 1.0f)
28 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, asin, std::asin, true, 4.0f, 4.0f, 0.001f, -1.0f, 1.0f)
29 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, asinh, std::asinh, true, 4.0f, 4.0f, 0.001f, -1.0f, 1.0f)
30 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, asinpi, reference::asinpi, true, 5.0f, 5.0f, 0.001f, -1.0f, 1.0f)
31 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, atan, std::atan, true, 5.0f, 5.0f, 0.001f, -1.0f, 1.0f)
32 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, atanh, std::atanh, true, 5.0f, 5.0f, 0.001f, -1.0f, 1.0f)
33 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, atanpi, reference::atanpi, true, 5.0f, 5.0f, 0.001f, -1.0f, 1.0f)
34
35 // For (sin/cos/tan)pi functions min input value is -0.24 and max input value is 0.24,
36 // so (CL_M_PI * x) is never greater than CL_M_PI_F.
37 // group_name, func_name, reference_func, use_ulp, ulp, ulp_for_embedded, max_delta, min1, max1
38 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, cos, std::cos, true, 4.0f, 4.0f, 0.001f, -CL_M_PI_F, CL_M_PI_F)
39 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, cosh, std::cosh, true, 4.0f, 4.0f, 0.001f, -CL_M_PI_F, CL_M_PI_F)
40 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, cospi, reference::cospi, true, 4.0f, 4.0f, 0.001f, -0.24, -0.24f)
41 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, sin, std::sin, true, 4.0f, 4.0f, 0.001f, -CL_M_PI_F, CL_M_PI_F)
42 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, sinh, std::sinh, true, 4.0f, 4.0f, 0.001f, -CL_M_PI_F, CL_M_PI_F)
43 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, sinpi, reference::sinpi, true, 4.0f, 4.0f, 0.001f, -0.24, -0.24f)
44 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, tan, std::tan, true, 5.0f, 5.0f, 0.001f, -CL_M_PI_F, CL_M_PI_F)
45 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, tanh, std::tanh, true, 5.0f, 5.0f, 0.001f, -CL_M_PI_F, CL_M_PI_F)
46 MATH_FUNCS_DEFINE_UNARY_FUNC(trigonometric, tanpi, reference::tanpi, true, 6.0f, 6.0f, 0.001f, -0.24, -0.24f)
47
48 // group_name, func_name, reference_func, use_ulp, ulp, ulp_for_embedded, max_delta, min1, max1, min2, max2
49 MATH_FUNCS_DEFINE_BINARY_FUNC(trigonometric, atan2, reference::atan2, true, 6.0f, 6.0f, 0.001f, -1.0f, 1.0f, -1.0f, 1.0f)
50 MATH_FUNCS_DEFINE_BINARY_FUNC(trigonometric, atan2pi, reference::atan2pi, true, 6.0f, 6.0f, 0.001f, -1.0f, 1.0f, -1.0f, 1.0f)
51
52 // gentype sincos(gentype x, gentype * cosval);
53 //
54 // Fact that second argument is a pointer is inconvenient.
55 //
56 // We don't want to modify all helper functions defined in funcs_test_utils.hpp
57 // that run test kernels generated based on this class and check if results are
58 // correct, so instead of having two output cl_float buffers, one for sines and
59 // one for cosines values, we use one cl_float2 output buffer (first component is
60 // sine, second is cosine).
61 //
62 // Below we also define specialization of generate_kernel_unary function template
63 // for trigonometric_func_sincos.
64 struct trigonometric_func_sincos : public unary_func<cl_float, cl_float2>
65 {
trigonometric_func_sincostrigonometric_func_sincos66 trigonometric_func_sincos(bool is_embedded) : m_is_embedded(is_embedded)
67 {
68
69 }
70
strtrigonometric_func_sincos71 std::string str()
72 {
73 return "sincos";
74 }
75
headerstrigonometric_func_sincos76 std::string headers()
77 {
78 return "#include <opencl_math>\n";
79 }
80
81 /* Reference value type is cl_double */
operator ()trigonometric_func_sincos82 cl_double2 operator()(const cl_float& x)
83 {
84 return (reference::sincos)(static_cast<cl_double>(x));
85 }
86
min1trigonometric_func_sincos87 cl_float min1()
88 {
89 return -CL_M_PI_F;
90 }
91
max1trigonometric_func_sincos92 cl_float max1()
93 {
94 return CL_M_PI_F;
95 }
96
use_ulptrigonometric_func_sincos97 bool use_ulp()
98 {
99 return true;
100 }
101
ulptrigonometric_func_sincos102 float ulp()
103 {
104 if(m_is_embedded)
105 {
106 return 4.0f;
107 }
108 return 4.0f;
109 }
110 private:
111 bool m_is_embedded;
112 };
113
114 // -----------------------------------------------------------------------------------
115 // ------------- ONLY FOR OPENCL 22 CONFORMANCE TEST 22 DEVELOPMENT ------------------
116 // -----------------------------------------------------------------------------------
117 #if defined(DEVELOPMENT) && defined(USE_OPENCLC_KERNELS)
118 template <>
generate_kernel_unary(trigonometric_func_sincos func)119 std::string generate_kernel_unary<trigonometric_func_sincos, cl_float, cl_float2>(trigonometric_func_sincos func)
120 {
121 return
122 "__kernel void test_sincos(global float *input, global float2 *output)\n"
123 "{\n"
124 " size_t gid = get_global_id(0);\n"
125 " float2 sine_cosine_of_x;\n"
126 " float cosine_of_x = 0;\n"
127 " sine_cosine_of_x.x = sincos(input[gid], &(cosine_of_x));\n"
128 " sine_cosine_of_x.y = cosine_of_x;\n"
129 " output[gid] = sine_cosine_of_x;\n"
130 "}\n";
131 }
132 #else
133 template <>
generate_kernel_unary(trigonometric_func_sincos func)134 std::string generate_kernel_unary<trigonometric_func_sincos, cl_float, cl_float2>(trigonometric_func_sincos func)
135 {
136 return
137 "" + func.defs() +
138 "" + func.headers() +
139 "#include <opencl_memory>\n"
140 "#include <opencl_work_item>\n"
141 "using namespace cl;\n"
142 "__kernel void test_sincos(global_ptr<float[]> input, global_ptr<float2[]> output)\n"
143 "{\n"
144 " size_t gid = get_global_id(0);\n"
145 " float2 sine_cosine_of_x;\n"
146 " float cosine_of_x = 0;\n"
147 " sine_cosine_of_x.x = sincos(input[gid], &(cosine_of_x));\n"
148 " sine_cosine_of_x.y = cosine_of_x;\n"
149 " output[gid] = sine_cosine_of_x;\n"
150 "}\n";
151 }
152 #endif
153
154 // trigonometric functions
AUTO_TEST_CASE(test_trigonometric_funcs)155 AUTO_TEST_CASE(test_trigonometric_funcs)
156 (cl_device_id device, cl_context context, cl_command_queue queue, int n_elems)
157 {
158 int error = CL_SUCCESS;
159 int last_error = CL_SUCCESS;
160
161 // Check for EMBEDDED_PROFILE
162 bool is_embedded_profile = false;
163 char profile[128];
164 last_error = clGetDeviceInfo(device, CL_DEVICE_PROFILE, sizeof(profile), (void *)&profile, NULL);
165 RETURN_ON_CL_ERROR(last_error, "clGetDeviceInfo")
166 if (std::strcmp(profile, "EMBEDDED_PROFILE") == 0)
167 is_embedded_profile = true;
168
169 // gentype acos(gentype x);
170 // gentype acosh(gentype x);
171 // gentype acospi(gentype x);
172 // gentype asin(gentype x);
173 // gentype asinh(gentype x);
174 // gentype asinpi(gentype x);
175 // gentype atan(gentype x);
176 // gentype atanh(gentype x);
177 // gentype atanpi(gentype x);
178 TEST_UNARY_FUNC_MACRO((trigonometric_func_acos(is_embedded_profile)))
179 TEST_UNARY_FUNC_MACRO((trigonometric_func_acosh(is_embedded_profile)))
180 TEST_UNARY_FUNC_MACRO((trigonometric_func_acospi(is_embedded_profile)))
181 TEST_UNARY_FUNC_MACRO((trigonometric_func_asin(is_embedded_profile)))
182 TEST_UNARY_FUNC_MACRO((trigonometric_func_asinh(is_embedded_profile)))
183 TEST_UNARY_FUNC_MACRO((trigonometric_func_asinpi(is_embedded_profile)))
184 TEST_UNARY_FUNC_MACRO((trigonometric_func_atan(is_embedded_profile)))
185 TEST_UNARY_FUNC_MACRO((trigonometric_func_atanh(is_embedded_profile)))
186 TEST_UNARY_FUNC_MACRO((trigonometric_func_atanpi(is_embedded_profile)))
187
188 // gentype cos(gentype x);
189 // gentype cosh(gentype x);
190 // gentype cospi(gentype x);
191 // gentype sin(gentype x);
192 // gentype sinh(gentype x);
193 // gentype sinpi(gentype x);
194 // gentype tan(gentype x);
195 // gentype tanh(gentype x);
196 // gentype tanpi(gentype x);
197 TEST_UNARY_FUNC_MACRO((trigonometric_func_cos(is_embedded_profile)))
198 TEST_UNARY_FUNC_MACRO((trigonometric_func_cosh(is_embedded_profile)))
199 TEST_UNARY_FUNC_MACRO((trigonometric_func_cospi(is_embedded_profile)))
200 TEST_UNARY_FUNC_MACRO((trigonometric_func_sin(is_embedded_profile)))
201 TEST_UNARY_FUNC_MACRO((trigonometric_func_sinh(is_embedded_profile)))
202 TEST_UNARY_FUNC_MACRO((trigonometric_func_sinpi(is_embedded_profile)))
203 TEST_UNARY_FUNC_MACRO((trigonometric_func_tan(is_embedded_profile)))
204 TEST_UNARY_FUNC_MACRO((trigonometric_func_tanh(is_embedded_profile)))
205 TEST_UNARY_FUNC_MACRO((trigonometric_func_tanpi(is_embedded_profile)))
206
207 // gentype atan2(gentype y, gentype x);
208 // gentype atan2pi(gentype y, gentype x);
209 TEST_BINARY_FUNC_MACRO((trigonometric_func_atan2(is_embedded_profile)))
210 TEST_BINARY_FUNC_MACRO((trigonometric_func_atan2pi(is_embedded_profile)))
211
212 // gentype sincos(gentype x, gentype * cosval);
213 TEST_UNARY_FUNC_MACRO((trigonometric_func_sincos(is_embedded_profile)))
214
215 if(error != CL_SUCCESS)
216 {
217 return -1;
218 }
219 return error;
220 }
221
222 #endif // TEST_CONFORMANCE_CLCPP_MATH_FUNCS_TRI_FUNCS_HPP
223