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