xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/mem_host_flags/C_host_memory_block.h (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 #ifndef test_conformance_cHost_MemoryBlock_h
17 #define test_conformance_cHost_MemoryBlock_h
18 
19 #include "harness/compat.h"
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 template <class T> class C_host_memory_block {
26 public:
27     size_t num_elements;
28     int element_size;
29     T *pData;
30 
31     C_host_memory_block();
32     ~C_host_memory_block();
33     void Init(size_t num_elem, T &value);
34     void Init(size_t num_elem);
35     void Set_to(T &val);
36     void Set_to_zero();
37     bool Equal_to(T &val);
38     size_t Count(T &val);
39     bool Equal(C_host_memory_block<T> &another);
40     bool Equal_rect(C_host_memory_block<T> &another, size_t *host_origin,
41                     size_t *region, size_t host_row_pitch,
42                     size_t host_slice_pitch);
43     bool Equal(T *pData, size_t num_elements);
44 
45     bool Equal_rect_from_orig(C_host_memory_block<T> &another, size_t *soffset,
46                               size_t *region, size_t host_row_pitch,
47                               size_t host_slice_pitch);
48 
49     bool Equal_rect_from_orig(T *another_pdata, size_t *soffset, size_t *region,
50                               size_t host_row_pitch, size_t host_slice_pitch);
51 };
52 
C_host_memory_block()53 template <class T> C_host_memory_block<T>::C_host_memory_block()
54 {
55     pData = NULL;
56     element_size = sizeof(T);
57     num_elements = 0;
58 }
59 
~C_host_memory_block()60 template <class T> C_host_memory_block<T>::~C_host_memory_block()
61 {
62     if (pData != NULL) delete[] pData;
63     num_elements = 0;
64 }
65 
Init(size_t num_elem,T & value)66 template <class T> void C_host_memory_block<T>::Init(size_t num_elem, T &value)
67 {
68     if (pData != NULL) delete[] pData;
69     pData = new T[num_elem];
70     for (size_t i = 0; i < num_elem; i++) pData[i] = value;
71 
72     num_elements = num_elem;
73 }
74 
Init(size_t num_elem)75 template <class T> void C_host_memory_block<T>::Init(size_t num_elem)
76 {
77     if (pData != NULL) delete[] pData;
78     pData = new T[num_elem];
79     for (size_t i = 0; i < num_elem; i++) pData[i] = (T)i;
80 
81     num_elements = num_elem;
82 }
Set_to_zero()83 template <class T> void C_host_memory_block<T>::Set_to_zero()
84 {
85     T v = 0;
86     Set_to(v);
87 }
88 
Set_to(T & val)89 template <class T> void C_host_memory_block<T>::Set_to(T &val)
90 {
91     for (size_t i = 0; i < num_elements; i++) pData[i] = val;
92 }
93 
Equal_to(T & val)94 template <class T> bool C_host_memory_block<T>::Equal_to(T &val)
95 {
96     size_t count = 0;
97 
98     for (size_t i = 0; i < num_elements; i++)
99     {
100         if (pData[i] == val) count++;
101     }
102 
103     return (count == num_elements);
104 }
105 
106 template <class T>
Equal(C_host_memory_block<T> & another)107 bool C_host_memory_block<T>::Equal(C_host_memory_block<T> &another)
108 {
109     size_t count = 0;
110 
111     for (size_t i = 0; i < num_elements; i++)
112     {
113         if (pData[i] == another.pData[i]) count++;
114     }
115 
116     return (count == num_elements);
117 }
118 
119 template <class T>
Equal(T * pIn_Data,size_t Innum_elements)120 bool C_host_memory_block<T>::Equal(T *pIn_Data, size_t Innum_elements)
121 {
122     if (this->num_elements != Innum_elements) return false;
123 
124     size_t count = 0;
125 
126     for (size_t i = 0; i < num_elements; i++)
127     {
128         if (pData[i] == pIn_Data[i]) count++;
129     }
130 
131     return (count == num_elements);
132 }
133 
Count(T & val)134 template <class T> size_t C_host_memory_block<T>::Count(T &val)
135 {
136     size_t count = 0;
137     for (size_t i = 0; i < num_elements; i++)
138     {
139         if (pData[i] == val) count++;
140     }
141 
142     return count;
143 }
144 
145 template <class T>
Equal_rect(C_host_memory_block<T> & another,size_t * soffset,size_t * region,size_t host_row_pitch,size_t host_slice_pitch)146 bool C_host_memory_block<T>::Equal_rect(C_host_memory_block<T> &another,
147                                         size_t *soffset, size_t *region,
148                                         size_t host_row_pitch,
149                                         size_t host_slice_pitch)
150 {
151     size_t row_pitch = host_row_pitch ? host_row_pitch : region[0];
152     size_t slice_pitch = host_slice_pitch ? host_row_pitch : region[1];
153 
154     size_t count = 0;
155 
156     size_t total = region[0] * region[1] * region[2];
157 
158     size_t x, y, z;
159     size_t orig = (size_t)(soffset[0] + row_pitch * soffset[1]
160                            + slice_pitch * soffset[2]);
161     for (z = 0; z < region[2]; z++)
162         for (y = 0; y < region[1]; y++)
163             for (x = 0; x < region[0]; x++)
164             {
165                 int p1 = (int)(x + row_pitch * y + slice_pitch * z + orig);
166                 if (pData[p1] == another.pData[p1]) count++;
167             }
168 
169     return (count == total);
170 }
171 
172 template <class T>
Equal_rect_from_orig(C_host_memory_block<T> & another,size_t * soffset,size_t * region,size_t host_row_pitch,size_t host_slice_pitch)173 bool C_host_memory_block<T>::Equal_rect_from_orig(
174     C_host_memory_block<T> &another, size_t *soffset, size_t *region,
175     size_t host_row_pitch, size_t host_slice_pitch)
176 {
177     size_t row_pitch = host_row_pitch ? host_row_pitch : region[0];
178     size_t slice_pitch = host_slice_pitch ? host_row_pitch : region[1];
179 
180     size_t count = 0;
181 
182     size_t total = region[0] * region[1] * region[2];
183 
184     size_t x, y, z;
185     size_t orig =
186         soffset[0] + row_pitch * soffset[1] + slice_pitch * soffset[2];
187     for (z = 0; z < region[2]; z++)
188         for (y = 0; y < region[1]; y++)
189             for (x = 0; x < region[0]; x++)
190             {
191                 size_t p1 = x + (row_pitch * y) + (slice_pitch * z);
192                 size_t p2 = p1 + orig;
193                 if (pData[p2] == another.pData[p1]) count++;
194             }
195 
196     return (count == total);
197 }
198 
199 template <class T>
Equal_rect_from_orig(T * another_pdata,size_t * soffset,size_t * region,size_t host_row_pitch,size_t host_slice_pitch)200 bool C_host_memory_block<T>::Equal_rect_from_orig(T *another_pdata,
201                                                   size_t *soffset,
202                                                   size_t *region,
203                                                   size_t host_row_pitch,
204                                                   size_t host_slice_pitch)
205 {
206     size_t row_pitch = host_row_pitch ? host_row_pitch : region[0];
207     size_t slice_pitch = host_slice_pitch ? host_row_pitch : region[1];
208 
209     size_t count = 0;
210 
211     size_t total = region[0] * region[1] * region[2];
212 
213     size_t x, y, z;
214     size_t orig =
215         soffset[0] + row_pitch * soffset[1] + slice_pitch * soffset[2];
216     for (z = 0; z < region[2]; z++)
217         for (y = 0; y < region[1]; y++)
218             for (x = 0; x < region[0]; x++)
219             {
220                 size_t p1 = x + (row_pitch * y) + (slice_pitch * z);
221                 size_t p2 = p1 + orig;
222                 if (pData[p2] == another_pdata[p1]) count++;
223             }
224 
225     return (count == total);
226 }
227 
228 #endif
229