xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/math_brute_force/common.h (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
1 //
2 // Copyright (c) 2021 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 COMMON_H
17 #define COMMON_H
18 
19 #include "harness/typeWrappers.h"
20 #include "utility.h"
21 
22 #include <array>
23 #include <string>
24 #include <vector>
25 
26 // Array of thread-specific kernels for each vector size.
27 using KernelMatrix =
28     std::array<std::vector<clKernelWrapper>, VECTOR_SIZE_COUNT>;
29 
30 // Array of programs for each vector size.
31 using Programs = std::array<clProgramWrapper, VECTOR_SIZE_COUNT>;
32 
33 // Array of buffers for each vector size.
34 using Buffers = std::array<clMemWrapper, VECTOR_SIZE_COUNT>;
35 
36 // Types supported for kernel code generation.
37 enum class ParameterType
38 {
39     Float,
40     Double,
41     Int,
42     UInt,
43     Long,
44     ULong,
45 };
46 
47 // Return kernel name suffixed with vector size.
48 std::string GetKernelName(int vector_size_index);
49 
50 // Generate kernel code for the given builtin function/operator.
51 std::string GetUnaryKernel(const std::string &kernel_name, const char *builtin,
52                            ParameterType retType, ParameterType type1,
53                            int vector_size_index);
54 std::string GetUnaryKernel(const std::string &kernel_name, const char *builtin,
55                            ParameterType retType1, ParameterType retType2,
56                            ParameterType type1, int vector_size_index);
57 std::string GetBinaryKernel(const std::string &kernel_name, const char *builtin,
58                             ParameterType retType, ParameterType type1,
59                             ParameterType type2, int vector_size_index);
60 std::string GetBinaryKernel(const std::string &kernel_name, const char *builtin,
61                             ParameterType retType1, ParameterType retType2,
62                             ParameterType type1, ParameterType type2,
63                             int vector_size_index);
64 std::string GetTernaryKernel(const std::string &kernel_name,
65                              const char *builtin, ParameterType retType,
66                              ParameterType type1, ParameterType type2,
67                              ParameterType type3, int vector_size_index);
68 
69 // Information to generate OpenCL kernels.
70 struct BuildKernelInfo
71 {
72     // Number of kernels to build, one for each thread to avoid data races.
73     cl_uint threadCount;
74 
75     KernelMatrix &kernels;
76 
77     Programs &programs;
78 
79     // Function, macro or symbol tested by the kernel.
80     const char *nameInCode;
81 
82     // Whether to build with -cl-fast-relaxed-math.
83     bool relaxedMode;
84 };
85 
86 using SourceGenerator = std::string (*)(const std::string &kernel_name,
87                                         const char *builtin,
88                                         cl_uint vector_size_index);
89 
90 /// Build kernels for all threads in "info" for the given job_id.
91 cl_int BuildKernels(BuildKernelInfo &info, cl_uint job_id,
92                     SourceGenerator generator);
93 
94 #endif /* COMMON_H */
95