xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/commonfns/test_base.h (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
1 // Copyright (c) 2023 The Khronos Group Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 
16 #ifndef TEST_COMMONFNS_BASE_H
17 #define TEST_COMMONFNS_BASE_H
18 
19 #include <vector>
20 #include <map>
21 #include <memory>
22 #include <cmath>
23 
24 #include <CL/cl_half.h>
25 #include <CL/cl_ext.h>
26 
27 #include "harness/testHarness.h"
28 #include "harness/typeWrappers.h"
29 
30 template <typename T>
31 using VerifyFuncBinary = int (*)(const T *const, const T *const, const T *const,
32                                  const int num, const int vs, const int vp);
33 
34 template <typename T>
35 using VerifyFuncUnary = int (*)(const T *const, const T *const, const int num);
36 
37 using half = cl_half;
38 
39 struct BaseFunctionTest
40 {
BaseFunctionTestBaseFunctionTest41     BaseFunctionTest(cl_device_id device, cl_context context,
42                      cl_command_queue queue, int num_elems, const char *fn,
43                      bool vsp)
44         : device(device), context(context), queue(queue), num_elems(num_elems),
45           fnName(fn), vecParam(vsp)
46     {}
47 
48     // Test body returning an OpenCL error code
49     virtual cl_int Run() = 0;
50 
51     cl_device_id device;
52     cl_context context;
53     cl_command_queue queue;
54 
55     int num_elems;
56     std::string fnName;
57     bool vecParam;
58 
59     static std::map<size_t, std::string> type2name;
60     static cl_half_rounding_mode halfRoundingMode;
61 };
62 
63 struct MinTest : BaseFunctionTest
64 {
MinTestMinTest65     MinTest(cl_device_id device, cl_context context, cl_command_queue queue,
66             int num_elems, const char *fn, bool vsp)
67         : BaseFunctionTest(device, context, queue, num_elems, fn, vsp)
68     {}
69 
70     cl_int Run() override;
71 };
72 
73 struct MaxTest : BaseFunctionTest
74 {
MaxTestMaxTest75     MaxTest(cl_device_id device, cl_context context, cl_command_queue queue,
76             int num_elems, const char *fn, bool vsp)
77         : BaseFunctionTest(device, context, queue, num_elems, fn, vsp)
78     {}
79 
80     cl_int Run() override;
81 };
82 
83 struct ClampTest : BaseFunctionTest
84 {
ClampTestClampTest85     ClampTest(cl_device_id device, cl_context context, cl_command_queue queue,
86               int num_elems, const char *fn, bool vsp)
87         : BaseFunctionTest(device, context, queue, num_elems, fn, vsp)
88     {}
89 
90     cl_int Run() override;
91 };
92 
93 struct DegreesTest : BaseFunctionTest
94 {
DegreesTestDegreesTest95     DegreesTest(cl_device_id device, cl_context context, cl_command_queue queue,
96                 int num_elems, const char *fn, bool vsp)
97         : BaseFunctionTest(device, context, queue, num_elems, fn, vsp)
98     {}
99 
100     cl_int Run() override;
101 };
102 
103 struct RadiansTest : BaseFunctionTest
104 {
RadiansTestRadiansTest105     RadiansTest(cl_device_id device, cl_context context, cl_command_queue queue,
106                 int num_elems, const char *fn, bool vsp)
107         : BaseFunctionTest(device, context, queue, num_elems, fn, vsp)
108     {}
109 
110     cl_int Run() override;
111 };
112 
113 struct SignTest : BaseFunctionTest
114 {
SignTestSignTest115     SignTest(cl_device_id device, cl_context context, cl_command_queue queue,
116              int num_elems, const char *fn, bool vsp)
117         : BaseFunctionTest(device, context, queue, num_elems, fn, vsp)
118     {}
119 
120     cl_int Run() override;
121 };
122 
123 struct SmoothstepTest : BaseFunctionTest
124 {
SmoothstepTestSmoothstepTest125     SmoothstepTest(cl_device_id device, cl_context context,
126                    cl_command_queue queue, int num_elems, const char *fn,
127                    bool vsp)
128         : BaseFunctionTest(device, context, queue, num_elems, fn, vsp)
129     {}
130 
131     cl_int Run() override;
132 };
133 
134 struct StepTest : BaseFunctionTest
135 {
StepTestStepTest136     StepTest(cl_device_id device, cl_context context, cl_command_queue queue,
137              int num_elems, const char *fn, bool vsp)
138         : BaseFunctionTest(device, context, queue, num_elems, fn, vsp)
139     {}
140 
141     cl_int Run() override;
142 };
143 
144 struct MixTest : BaseFunctionTest
145 {
MixTestMixTest146     MixTest(cl_device_id device, cl_context context, cl_command_queue queue,
147             int num_elems, const char *fn, bool vsp)
148         : BaseFunctionTest(device, context, queue, num_elems, fn, vsp)
149     {}
150 
151     cl_int Run() override;
152 };
153 
UlpFn(const T & val,const double & r)154 template <typename T> float UlpFn(const T &val, const double &r)
155 {
156     if (std::is_same<T, half>::value)
157     {
158         return Ulp_Error_Half(val, r);
159     }
160     else if (std::is_same<T, float>::value)
161     {
162         return Ulp_Error(val, r);
163     }
164     else if (std::is_same<T, double>::value)
165     {
166         return Ulp_Error_Double(val, r);
167     }
168     else
169     {
170         log_error("UlpFn: unsupported data type\n");
171     }
172 
173     return -1.f; // wrong val
174 }
175 
conv_to_dbl(const T & val)176 template <typename T> inline double conv_to_dbl(const T &val)
177 {
178     if (std::is_same<T, half>::value)
179         return (double)cl_half_to_float(val);
180     else
181         return (double)val;
182 }
183 
conv_to_flt(const T & val)184 template <typename T> inline double conv_to_flt(const T &val)
185 {
186     if (std::is_same<T, half>::value)
187         return (float)cl_half_to_float(val);
188     else
189         return (float)val;
190 }
191 
conv_to_half(const T & val)192 template <typename T> inline half conv_to_half(const T &val)
193 {
194     if (std::is_floating_point<T>::value)
195         return cl_half_from_float(val, BaseFunctionTest::halfRoundingMode);
196     return 0;
197 }
198 
isfinite_fp(const T & v)199 template <typename T> bool isfinite_fp(const T &v)
200 {
201     if (std::is_same<T, half>::value)
202     {
203         // Extract FP16 exponent and mantissa
204         uint16_t h_exp = (((half)v) >> (CL_HALF_MANT_DIG - 1)) & 0x1F;
205         uint16_t h_mant = ((half)v) & 0x3FF;
206 
207         // !Inf test
208         return !(h_exp == 0x1F && h_mant == 0);
209     }
210     else
211     {
212 #if !defined(_WIN32)
213         return std::isfinite(v);
214 #else
215         return isfinite(v);
216 #endif
217     }
218 }
219 
220 template <class T>
221 int MakeAndRunTest(cl_device_id device, cl_context context,
222                    cl_command_queue queue, int num_elements,
223                    const char *fn = "", bool vsp = false)
224 {
225     auto test_fixture = T(device, context, queue, num_elements, fn, vsp);
226 
227     cl_int error = test_fixture.Run();
228     test_error_ret(error, "Test Failed", TEST_FAIL);
229 
230     return TEST_PASS;
231 }
232 
233 #endif // TEST_COMMONFNS_BASE_H
234