xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/basic/test_float2int.cpp (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
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 #include "harness/compat.h"
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 
24 
25 #include "procs.h"
26 
27 const char *float2int_kernel_code =
28 "__kernel void test_float2int(__global float *src, __global int *dst)\n"
29 "{\n"
30 "    int  tid = get_global_id(0);\n"
31 "\n"
32 "    dst[tid] = (int)src[tid];\n"
33 "\n"
34 "}\n";
35 
36 
37 int
verify_float2int(cl_float * inptr,cl_int * outptr,int n)38 verify_float2int(cl_float *inptr, cl_int *outptr, int n)
39 {
40   int     i;
41 
42   for (i=0; i<n; i++)
43   {
44     if (outptr[i] != (int)inptr[i])
45     {
46       log_error("FLOAT2INT test failed\n");
47       return -1;
48     }
49   }
50 
51   log_info("FLOAT2INT test passed\n");
52   return 0;
53 }
54 
55 
56 int
test_float2int(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)57 test_float2int(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
58 {
59     cl_mem            streams[2];
60     cl_float        *input_ptr;
61     cl_int          *output_ptr;
62     cl_program        program;
63     cl_kernel        kernel;
64     size_t    threads[1];
65     int                err;
66     int                i;
67     MTdata          d;
68 
69     input_ptr = (cl_float*)malloc(sizeof(cl_float) * num_elements);
70     output_ptr = (cl_int*)malloc(sizeof(cl_int) * num_elements);
71     streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE,
72                                 sizeof(cl_float) * num_elements, NULL, NULL);
73     if (!streams[0])
74     {
75         log_error("clCreateBuffer failed\n");
76         return -1;
77     }
78     streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE,
79                                 sizeof(cl_int) * num_elements, NULL, NULL);
80     if (!streams[1])
81     {
82         log_error("clCreateBuffer failed\n");
83         return -1;
84     }
85 
86     d = init_genrand( gRandomSeed );
87     for (i=0; i<num_elements; i++)
88         input_ptr[i] = get_random_float(-MAKE_HEX_FLOAT( 0x1.0p31f, 0x1, 31), MAKE_HEX_FLOAT( 0x1.0p31f, 0x1, 31), d);
89     free_mtdata(d); d = NULL;
90 
91     err = clEnqueueWriteBuffer(queue, streams[0], CL_TRUE, 0, sizeof(cl_float)*num_elements, (void *)input_ptr, 0, NULL, NULL);
92     if (err != CL_SUCCESS)
93     {
94         log_error("clWriteArray failed\n");
95         return -1;
96     }
97 
98     err = create_single_kernel_helper(context, &program, &kernel, 1, &float2int_kernel_code, "test_float2int");
99     if (err != CL_SUCCESS)
100     {
101         log_error("create_single_kernel_helper failed\n");
102         return -1;
103     }
104 
105   err = clSetKernelArg(kernel, 0, sizeof streams[0], &streams[0]);
106   err = clSetKernelArg(kernel, 1, sizeof streams[1], &streams[1]);
107     if (err != CL_SUCCESS)
108     {
109         log_error("clSetKernelArgs failed\n");
110         return -1;
111     }
112 
113     threads[0] = (size_t)num_elements;
114     err = clEnqueueNDRangeKernel( queue, kernel, 1, NULL, threads, NULL, 0, NULL, NULL );
115     if (err != CL_SUCCESS)
116     {
117         log_error("clEnqueueNDRangeKernel failed\n");
118         return -1;
119     }
120 
121     err = clEnqueueReadBuffer( queue, streams[1], true, 0, sizeof(cl_int)*num_elements, (void *)output_ptr, 0, NULL, NULL );
122     if (err != CL_SUCCESS)
123     {
124         log_error("clEnqueueReadBuffer failed\n");
125         return -1;
126     }
127 
128     err = verify_float2int(input_ptr, output_ptr, num_elements);
129 
130     // cleanup
131     clReleaseMemObject(streams[0]);
132     clReleaseMemObject(streams[1]);
133     clReleaseKernel(kernel);
134     clReleaseProgram(program);
135     free(input_ptr);
136     free(output_ptr);
137 
138     return err;
139 }
140 
141 
142 
143 
144 
145