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
17 #include "common.h"
18 #include "function_list.h"
19 #include "test_functions.h"
20 #include "utility.h"
21
22 #include <cinttypes>
23 #include <climits>
24 #include <cstring>
25
26 namespace {
27
BuildKernelFn(cl_uint job_id,cl_uint thread_id UNUSED,void * p)28 cl_int BuildKernelFn(cl_uint job_id, cl_uint thread_id UNUSED, void *p)
29 {
30 BuildKernelInfo &info = *(BuildKernelInfo *)p;
31 auto generator = [](const std::string &kernel_name, const char *builtin,
32 cl_uint vector_size_index) {
33 return GetBinaryKernel(kernel_name, builtin, ParameterType::Float,
34 ParameterType::Int, ParameterType::Float,
35 ParameterType::Float, vector_size_index);
36 };
37 return BuildKernels(info, job_id, generator);
38 }
39
40 struct ComputeReferenceInfoF
41 {
42 const float *x;
43 const float *y;
44 float *r;
45 int *i;
46 double (*f_ffpI)(double, double, int *);
47 cl_uint lim;
48 cl_uint count;
49 };
50
ReferenceF(cl_uint jid,cl_uint tid,void * userInfo)51 cl_int ReferenceF(cl_uint jid, cl_uint tid, void *userInfo)
52 {
53 ComputeReferenceInfoF *cri = (ComputeReferenceInfoF *)userInfo;
54 cl_uint lim = cri->lim;
55 cl_uint count = cri->count;
56 cl_uint off = jid * count;
57 const float *x = cri->x + off;
58 const float *y = cri->y + off;
59 float *r = cri->r + off;
60 int *i = cri->i + off;
61 double (*f)(double, double, int *) = cri->f_ffpI;
62
63 if (off + count > lim) count = lim - off;
64
65 for (cl_uint j = 0; j < count; ++j)
66 r[j] = (float)f((double)x[j], (double)y[j], i + j);
67
68 return CL_SUCCESS;
69 }
70
71 } // anonymous namespace
72
TestFunc_FloatI_Float_Float(const Func * f,MTdata d,bool relaxedMode)73 int TestFunc_FloatI_Float_Float(const Func *f, MTdata d, bool relaxedMode)
74 {
75 int error;
76
77 logFunctionInfo(f->name, sizeof(cl_float), relaxedMode);
78
79 Programs programs;
80 const unsigned thread_id = 0; // Test is currently not multithreaded.
81 KernelMatrix kernels;
82 float maxError = 0.0f;
83 int ftz = f->ftz || gForceFTZ || 0 == (CL_FP_DENORM & gFloatCapabilities);
84 int64_t maxError2 = 0;
85 float maxErrorVal = 0.0f;
86 float maxErrorVal2 = 0.0f;
87 uint64_t step = getTestStep(sizeof(float), BUFFER_SIZE);
88
89 cl_uint threadCount = GetThreadCount();
90
91 float float_ulps;
92 if (gIsEmbedded)
93 float_ulps = f->float_embedded_ulps;
94 else
95 float_ulps = f->float_ulps;
96
97 int testingRemquo = !strcmp(f->name, "remquo");
98
99 // Init the kernels
100 BuildKernelInfo build_info{ 1, kernels, programs, f->nameInCode,
101 relaxedMode };
102 if ((error = ThreadPool_Do(BuildKernelFn,
103 gMaxVectorSizeIndex - gMinVectorSizeIndex,
104 &build_info)))
105 return error;
106
107 for (uint64_t i = 0; i < (1ULL << 32); i += step)
108 {
109 // Init input array
110 cl_uint *p = (cl_uint *)gIn;
111 cl_uint *p2 = (cl_uint *)gIn2;
112 for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
113 {
114 p[j] = genrand_int32(d);
115 p2[j] = genrand_int32(d);
116 }
117
118 if ((error = clEnqueueWriteBuffer(gQueue, gInBuffer, CL_FALSE, 0,
119 BUFFER_SIZE, gIn, 0, NULL, NULL)))
120 {
121 vlog_error("\n*** Error %d in clEnqueueWriteBuffer ***\n", error);
122 return error;
123 }
124
125 if ((error = clEnqueueWriteBuffer(gQueue, gInBuffer2, CL_FALSE, 0,
126 BUFFER_SIZE, gIn2, 0, NULL, NULL)))
127 {
128 vlog_error("\n*** Error %d in clEnqueueWriteBuffer2 ***\n", error);
129 return error;
130 }
131
132 // Write garbage into output arrays
133 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
134 {
135 uint32_t pattern = 0xffffdead;
136 if (gHostFill)
137 {
138 memset_pattern4(gOut[j], &pattern, BUFFER_SIZE);
139 if ((error = clEnqueueWriteBuffer(gQueue, gOutBuffer[j],
140 CL_FALSE, 0, BUFFER_SIZE,
141 gOut[j], 0, NULL, NULL)))
142 {
143 vlog_error(
144 "\n*** Error %d in clEnqueueWriteBuffer2(%d) ***\n",
145 error, j);
146 return error;
147 }
148
149 memset_pattern4(gOut2[j], &pattern, BUFFER_SIZE);
150 if ((error = clEnqueueWriteBuffer(gQueue, gOutBuffer2[j],
151 CL_FALSE, 0, BUFFER_SIZE,
152 gOut2[j], 0, NULL, NULL)))
153 {
154 vlog_error(
155 "\n*** Error %d in clEnqueueWriteBuffer2b(%d) ***\n",
156 error, j);
157 return error;
158 }
159 }
160 else
161 {
162 if ((error = clEnqueueFillBuffer(gQueue, gOutBuffer[j],
163 &pattern, sizeof(pattern), 0,
164 BUFFER_SIZE, 0, NULL, NULL)))
165 {
166 vlog_error("Error: clEnqueueFillBuffer 1 failed! err: %d\n",
167 error);
168 return error;
169 }
170
171 if ((error = clEnqueueFillBuffer(gQueue, gOutBuffer2[j],
172 &pattern, sizeof(pattern), 0,
173 BUFFER_SIZE, 0, NULL, NULL)))
174 {
175 vlog_error("Error: clEnqueueFillBuffer 2 failed! err: %d\n",
176 error);
177 return error;
178 }
179 }
180 }
181
182 // Run the kernels
183 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
184 {
185 size_t vectorSize = sizeof(cl_float) * sizeValues[j];
186 size_t localCount = (BUFFER_SIZE + vectorSize - 1)
187 / vectorSize; // BUFFER_SIZE / vectorSize rounded up
188 if ((error = clSetKernelArg(kernels[j][thread_id], 0,
189 sizeof(gOutBuffer[j]), &gOutBuffer[j])))
190 {
191 LogBuildError(programs[j]);
192 return error;
193 }
194 if ((error =
195 clSetKernelArg(kernels[j][thread_id], 1,
196 sizeof(gOutBuffer2[j]), &gOutBuffer2[j])))
197 {
198 LogBuildError(programs[j]);
199 return error;
200 }
201 if ((error = clSetKernelArg(kernels[j][thread_id], 2,
202 sizeof(gInBuffer), &gInBuffer)))
203 {
204 LogBuildError(programs[j]);
205 return error;
206 }
207 if ((error = clSetKernelArg(kernels[j][thread_id], 3,
208 sizeof(gInBuffer2), &gInBuffer2)))
209 {
210 LogBuildError(programs[j]);
211 return error;
212 }
213
214 if ((error = clEnqueueNDRangeKernel(gQueue, kernels[j][thread_id],
215 1, NULL, &localCount, NULL, 0,
216 NULL, NULL)))
217 {
218 vlog_error("FAILED -- could not execute kernel\n");
219 return error;
220 }
221 }
222
223 // Get that moving
224 if ((error = clFlush(gQueue))) vlog("clFlush failed\n");
225
226 // Calculate the correctly rounded reference result
227 float *s = (float *)gIn;
228 float *s2 = (float *)gIn2;
229
230 if (threadCount > 1)
231 {
232 ComputeReferenceInfoF cri;
233 cri.x = s;
234 cri.y = s2;
235 cri.r = (float *)gOut_Ref;
236 cri.i = (int *)gOut_Ref2;
237 cri.f_ffpI = f->func.f_ffpI;
238 cri.lim = BUFFER_SIZE / sizeof(float);
239 cri.count = (cri.lim + threadCount - 1) / threadCount;
240 ThreadPool_Do(ReferenceF, threadCount, &cri);
241 }
242 else
243 {
244 float *r = (float *)gOut_Ref;
245 int *r2 = (int *)gOut_Ref2;
246 for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
247 r[j] = (float)f->func.f_ffpI(s[j], s2[j], r2 + j);
248 }
249
250 // Read the data back
251 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
252 {
253 if ((error =
254 clEnqueueReadBuffer(gQueue, gOutBuffer[j], CL_TRUE, 0,
255 BUFFER_SIZE, gOut[j], 0, NULL, NULL)))
256 {
257 vlog_error("ReadArray failed %d\n", error);
258 return error;
259 }
260 if ((error =
261 clEnqueueReadBuffer(gQueue, gOutBuffer2[j], CL_TRUE, 0,
262 BUFFER_SIZE, gOut2[j], 0, NULL, NULL)))
263 {
264 vlog_error("ReadArray2 failed %d\n", error);
265 return error;
266 }
267 }
268
269 if (gSkipCorrectnessTesting) break;
270
271 // Verify data
272 uint32_t *t = (uint32_t *)gOut_Ref;
273 int32_t *t2 = (int32_t *)gOut_Ref2;
274 for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
275 {
276 for (auto k = gMinVectorSizeIndex; k < gMaxVectorSizeIndex; k++)
277 {
278 uint32_t *q = (uint32_t *)(gOut[k]);
279 int32_t *q2 = (int32_t *)gOut2[k];
280
281 // Check for exact match to correctly rounded result
282 if (t[j] == q[j] && t2[j] == q2[j]) continue;
283
284 // Check for paired NaNs
285 if ((t[j] & 0x7fffffff) > 0x7f800000
286 && (q[j] & 0x7fffffff) > 0x7f800000 && t2[j] == q2[j])
287 continue;
288
289 float test = ((float *)q)[j];
290 int correct2 = INT_MIN;
291 double correct = f->func.f_ffpI(s[j], s2[j], &correct2);
292 float err = Ulp_Error(test, correct);
293 int64_t iErr;
294
295 // in case of remquo, we only care about the sign and last
296 // seven bits of integer as per the spec.
297 if (testingRemquo)
298 iErr = (long long)(q2[j] & 0x0000007f)
299 - (long long)(correct2 & 0x0000007f);
300 else
301 iErr = (long long)q2[j] - (long long)correct2;
302
303 // For remquo, if y = 0, x is infinite, or either is NaN
304 // then the standard either neglects to say what is returned
305 // in iptr or leaves it undefined or implementation defined.
306 int iptrUndefined = fabs(((float *)gIn)[j]) == INFINITY
307 || ((float *)gIn2)[j] == 0.0f || isnan(((float *)gIn2)[j])
308 || isnan(((float *)gIn)[j]);
309 if (iptrUndefined) iErr = 0;
310
311 int fail = !(fabsf(err) <= float_ulps && iErr == 0);
312 if ((ftz || relaxedMode) && fail)
313 {
314 // retry per section 6.5.3.2
315 if (IsFloatResultSubnormal(correct, float_ulps))
316 {
317 fail = fail && !(test == 0.0f && iErr == 0);
318 if (!fail) err = 0.0f;
319 }
320
321 // retry per section 6.5.3.3
322 if (IsFloatSubnormal(s[j]))
323 {
324 int correct3i, correct4i;
325 double correct3 =
326 f->func.f_ffpI(0.0, s2[j], &correct3i);
327 double correct4 =
328 f->func.f_ffpI(-0.0, s2[j], &correct4i);
329 float err2 = Ulp_Error(test, correct3);
330 float err3 = Ulp_Error(test, correct4);
331 int64_t iErr3 = (long long)q2[j] - (long long)correct3i;
332 int64_t iErr4 = (long long)q2[j] - (long long)correct4i;
333 fail = fail
334 && ((!(fabsf(err2) <= float_ulps && iErr3 == 0))
335 && (!(fabsf(err3) <= float_ulps
336 && iErr4 == 0)));
337 if (fabsf(err2) < fabsf(err)) err = err2;
338 if (fabsf(err3) < fabsf(err)) err = err3;
339 if (llabs(iErr3) < llabs(iErr)) iErr = iErr3;
340 if (llabs(iErr4) < llabs(iErr)) iErr = iErr4;
341
342 // retry per section 6.5.3.4
343 if (IsFloatResultSubnormal(correct2, float_ulps)
344 || IsFloatResultSubnormal(correct3, float_ulps))
345 {
346 fail = fail
347 && !(test == 0.0f
348 && (iErr3 == 0 || iErr4 == 0));
349 if (!fail) err = 0.0f;
350 }
351
352 // try with both args as zero
353 if (IsFloatSubnormal(s2[j]))
354 {
355 int correct7i, correct8i;
356 correct3 = f->func.f_ffpI(0.0, 0.0, &correct3i);
357 correct4 = f->func.f_ffpI(-0.0, 0.0, &correct4i);
358 double correct7 =
359 f->func.f_ffpI(0.0, -0.0, &correct7i);
360 double correct8 =
361 f->func.f_ffpI(-0.0, -0.0, &correct8i);
362 err2 = Ulp_Error(test, correct3);
363 err3 = Ulp_Error(test, correct4);
364 float err4 = Ulp_Error(test, correct7);
365 float err5 = Ulp_Error(test, correct8);
366 iErr3 = (long long)q2[j] - (long long)correct3i;
367 iErr4 = (long long)q2[j] - (long long)correct4i;
368 int64_t iErr7 =
369 (long long)q2[j] - (long long)correct7i;
370 int64_t iErr8 =
371 (long long)q2[j] - (long long)correct8i;
372 fail = fail
373 && ((!(fabsf(err2) <= float_ulps && iErr3 == 0))
374 && (!(fabsf(err3) <= float_ulps
375 && iErr4 == 0))
376 && (!(fabsf(err4) <= float_ulps
377 && iErr7 == 0))
378 && (!(fabsf(err5) <= float_ulps
379 && iErr8 == 0)));
380 if (fabsf(err2) < fabsf(err)) err = err2;
381 if (fabsf(err3) < fabsf(err)) err = err3;
382 if (fabsf(err4) < fabsf(err)) err = err4;
383 if (fabsf(err5) < fabsf(err)) err = err5;
384 if (llabs(iErr3) < llabs(iErr)) iErr = iErr3;
385 if (llabs(iErr4) < llabs(iErr)) iErr = iErr4;
386 if (llabs(iErr7) < llabs(iErr)) iErr = iErr7;
387 if (llabs(iErr8) < llabs(iErr)) iErr = iErr8;
388
389 // retry per section 6.5.3.4
390 if (IsFloatResultSubnormal(correct3, float_ulps)
391 || IsFloatResultSubnormal(correct4, float_ulps)
392 || IsFloatResultSubnormal(correct7, float_ulps)
393 || IsFloatResultSubnormal(correct8, float_ulps))
394 {
395 fail = fail
396 && !(test == 0.0f
397 && (iErr3 == 0 || iErr4 == 0
398 || iErr7 == 0 || iErr8 == 0));
399 if (!fail) err = 0.0f;
400 }
401 }
402 }
403 else if (IsFloatSubnormal(s2[j]))
404 {
405 int correct3i, correct4i;
406 double correct3 = f->func.f_ffpI(s[j], 0.0, &correct3i);
407 double correct4 =
408 f->func.f_ffpI(s[j], -0.0, &correct4i);
409 float err2 = Ulp_Error(test, correct3);
410 float err3 = Ulp_Error(test, correct4);
411 int64_t iErr3 = (long long)q2[j] - (long long)correct3i;
412 int64_t iErr4 = (long long)q2[j] - (long long)correct4i;
413 fail = fail
414 && ((!(fabsf(err2) <= float_ulps && iErr3 == 0))
415 && (!(fabsf(err3) <= float_ulps
416 && iErr4 == 0)));
417 if (fabsf(err2) < fabsf(err)) err = err2;
418 if (fabsf(err3) < fabsf(err)) err = err3;
419 if (llabs(iErr3) < llabs(iErr)) iErr = iErr3;
420 if (llabs(iErr4) < llabs(iErr)) iErr = iErr4;
421
422 // retry per section 6.5.3.4
423 if (IsFloatResultSubnormal(correct2, float_ulps)
424 || IsFloatResultSubnormal(correct3, float_ulps))
425 {
426 fail = fail
427 && !(test == 0.0f
428 && (iErr3 == 0 || iErr4 == 0));
429 if (!fail) err = 0.0f;
430 }
431 }
432 }
433 if (fabsf(err) > maxError)
434 {
435 maxError = fabsf(err);
436 maxErrorVal = s[j];
437 }
438 if (llabs(iErr) > maxError2)
439 {
440 maxError2 = llabs(iErr);
441 maxErrorVal2 = s[j];
442 }
443
444 if (fail)
445 {
446 vlog_error("\nERROR: %s%s: {%f, %" PRId64
447 "} ulp error at {%a, %a} "
448 "({0x%8.8x, 0x%8.8x}): *{%a, %d} ({0x%8.8x, "
449 "0x%8.8x}) vs. {%a, %d} ({0x%8.8x, 0x%8.8x})\n",
450 f->name, sizeNames[k], err, iErr,
451 ((float *)gIn)[j], ((float *)gIn2)[j],
452 ((cl_uint *)gIn)[j], ((cl_uint *)gIn2)[j],
453 ((float *)gOut_Ref)[j], ((int *)gOut_Ref2)[j],
454 ((cl_uint *)gOut_Ref)[j],
455 ((cl_uint *)gOut_Ref2)[j], test, q2[j],
456 ((cl_uint *)&test)[0], ((cl_uint *)q2)[j]);
457 return -1;
458 }
459 }
460 }
461
462 if (0 == (i & 0x0fffffff))
463 {
464 if (gVerboseBruteForce)
465 {
466 vlog("base:%14" PRIu64 " step:%10" PRIu64
467 " bufferSize:%10d \n",
468 i, step, BUFFER_SIZE);
469 }
470 else
471 {
472 vlog(".");
473 }
474 fflush(stdout);
475 }
476 }
477
478 if (!gSkipCorrectnessTesting)
479 {
480 if (gWimpyMode)
481 vlog("Wimp pass");
482 else
483 vlog("passed");
484
485 vlog("\t{%8.2f, %" PRId64 "} @ {%a, %a}", maxError, maxError2,
486 maxErrorVal, maxErrorVal2);
487 }
488
489 vlog("\n");
490
491 return CL_SUCCESS;
492 }
493