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 "function_list.h"
18 #include "test_functions.h"
19 #include "utility.h"
20
21 #include <climits>
22 #include <cstring>
23
BuildKernel(const char * name,int vectorSize,cl_kernel * k,cl_program * p,bool relaxedMode)24 static int BuildKernel(const char *name, int vectorSize, cl_kernel *k,
25 cl_program *p, bool relaxedMode)
26 {
27 const char *c[] = { "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n",
28 "__kernel void math_kernel",
29 sizeNames[vectorSize],
30 "( __global double",
31 sizeNames[vectorSize],
32 "* out, __global int",
33 sizeNames[vectorSize],
34 "* out2, __global double",
35 sizeNames[vectorSize],
36 "* in1, __global double",
37 sizeNames[vectorSize],
38 "* in2 )\n"
39 "{\n"
40 " size_t i = get_global_id(0);\n"
41 " out[i] = ",
42 name,
43 "( in1[i], in2[i], out2 + i );\n"
44 "}\n" };
45
46 const char *c3[] = {
47 "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n",
48 "__kernel void math_kernel",
49 sizeNames[vectorSize],
50 "( __global double* out, __global int* out2, __global double* in, "
51 "__global double* in2)\n"
52 "{\n"
53 " size_t i = get_global_id(0);\n"
54 " if( i + 1 < get_global_size(0) )\n"
55 " {\n"
56 " double3 d0 = vload3( 0, in + 3 * i );\n"
57 " double3 d1 = vload3( 0, in2 + 3 * i );\n"
58 " int3 i0 = 0xdeaddead;\n"
59 " d0 = ",
60 name,
61 "( d0, d1, &i0 );\n"
62 " vstore3( d0, 0, out + 3*i );\n"
63 " vstore3( i0, 0, out2 + 3*i );\n"
64 " }\n"
65 " else\n"
66 " {\n"
67 " size_t parity = i & 1; // Figure out how many elements are "
68 "left over after BUFFER_SIZE % (3*sizeof(float)). Assume power of two "
69 "buffer size \n"
70 " double3 d0;\n"
71 " double3 d1;\n"
72 " int3 i0 = 0xdeaddead;\n"
73 " switch( parity )\n"
74 " {\n"
75 " case 1:\n"
76 " d0 = (double3)( in[3*i], NAN, NAN ); \n"
77 " d1 = (double3)( in2[3*i], NAN, NAN ); \n"
78 " break;\n"
79 " case 0:\n"
80 " d0 = (double3)( in[3*i], in[3*i+1], NAN ); \n"
81 " d1 = (double3)( in2[3*i], in2[3*i+1], NAN ); \n"
82 " break;\n"
83 " }\n"
84 " d0 = ",
85 name,
86 "( d0, d1, &i0 );\n"
87 " switch( parity )\n"
88 " {\n"
89 " case 0:\n"
90 " out[3*i+1] = d0.y; \n"
91 " out2[3*i+1] = i0.y; \n"
92 " // fall through\n"
93 " case 1:\n"
94 " out[3*i] = d0.x; \n"
95 " out2[3*i] = i0.x; \n"
96 " break;\n"
97 " }\n"
98 " }\n"
99 "}\n"
100 };
101
102 const char **kern = c;
103 size_t kernSize = sizeof(c) / sizeof(c[0]);
104
105 if (sizeValues[vectorSize] == 3)
106 {
107 kern = c3;
108 kernSize = sizeof(c3) / sizeof(c3[0]);
109 }
110
111 char testName[32];
112 snprintf(testName, sizeof(testName) - 1, "math_kernel%s",
113 sizeNames[vectorSize]);
114
115 return MakeKernel(kern, (cl_uint)kernSize, testName, k, p, relaxedMode);
116 }
117
118 typedef struct BuildKernelInfo
119 {
120 cl_uint offset; // the first vector size to build
121 cl_kernel *kernels;
122 cl_program *programs;
123 const char *nameInCode;
124 bool relaxedMode; // Whether to build with -cl-fast-relaxed-math.
125 } BuildKernelInfo;
126
BuildKernelFn(cl_uint job_id,cl_uint thread_id UNUSED,void * p)127 static cl_int BuildKernelFn(cl_uint job_id, cl_uint thread_id UNUSED, void *p)
128 {
129 BuildKernelInfo *info = (BuildKernelInfo *)p;
130 cl_uint i = info->offset + job_id;
131 return BuildKernel(info->nameInCode, i, info->kernels + i,
132 info->programs + i, info->relaxedMode);
133 }
134
135 typedef struct ComputeReferenceInfoD_
136 {
137 const double *x;
138 const double *y;
139 double *r;
140 int *i;
141 long double (*f_ffpI)(long double, long double, int *);
142 cl_uint lim;
143 cl_uint count;
144 } ComputeReferenceInfoD;
145
ReferenceD(cl_uint jid,cl_uint tid,void * userInfo)146 static cl_int ReferenceD(cl_uint jid, cl_uint tid, void *userInfo)
147 {
148 ComputeReferenceInfoD *cri = (ComputeReferenceInfoD *)userInfo;
149 cl_uint lim = cri->lim;
150 cl_uint count = cri->count;
151 cl_uint off = jid * count;
152 const double *x = cri->x + off;
153 const double *y = cri->y + off;
154 double *r = cri->r + off;
155 int *i = cri->i + off;
156 long double (*f)(long double, long double, int *) = cri->f_ffpI;
157
158 if (off + count > lim) count = lim - off;
159
160 Force64BitFPUPrecision();
161
162 for (cl_uint j = 0; j < count; ++j)
163 r[j] = (double)f((long double)x[j], (long double)y[j], i + j);
164
165 return CL_SUCCESS;
166 }
167
TestFunc_DoubleI_Double_Double(const Func * f,MTdata d,bool relaxedMode)168 int TestFunc_DoubleI_Double_Double(const Func *f, MTdata d, bool relaxedMode)
169 {
170 int error;
171 cl_program programs[VECTOR_SIZE_COUNT];
172 cl_kernel kernels[VECTOR_SIZE_COUNT];
173 float maxError = 0.0f;
174 int64_t maxError2 = 0;
175 int ftz = f->ftz || gForceFTZ;
176 double maxErrorVal = 0.0f;
177 double maxErrorVal2 = 0.0f;
178 uint64_t step = getTestStep(sizeof(double), BUFFER_SIZE);
179
180 logFunctionInfo(f->name, sizeof(cl_double), relaxedMode);
181
182 cl_uint threadCount = GetThreadCount();
183
184 Force64BitFPUPrecision();
185
186 int testingRemquo = !strcmp(f->name, "remquo");
187
188 // Init the kernels
189 {
190 BuildKernelInfo build_info = { gMinVectorSizeIndex, kernels, programs,
191 f->nameInCode, relaxedMode };
192 if ((error = ThreadPool_Do(BuildKernelFn,
193 gMaxVectorSizeIndex - gMinVectorSizeIndex,
194 &build_info)))
195 return error;
196 }
197
198 for (uint64_t i = 0; i < (1ULL << 32); i += step)
199 {
200 // Init input array
201 double *p = (double *)gIn;
202 double *p2 = (double *)gIn2;
203 for (size_t j = 0; j < BUFFER_SIZE / sizeof(double); j++)
204 {
205 p[j] = DoubleFromUInt32(genrand_int32(d));
206 p2[j] = DoubleFromUInt32(genrand_int32(d));
207 }
208
209 if ((error = clEnqueueWriteBuffer(gQueue, gInBuffer, CL_FALSE, 0,
210 BUFFER_SIZE, gIn, 0, NULL, NULL)))
211 {
212 vlog_error("\n*** Error %d in clEnqueueWriteBuffer ***\n", error);
213 return error;
214 }
215
216 if ((error = clEnqueueWriteBuffer(gQueue, gInBuffer2, CL_FALSE, 0,
217 BUFFER_SIZE, gIn2, 0, NULL, NULL)))
218 {
219 vlog_error("\n*** Error %d in clEnqueueWriteBuffer2 ***\n", error);
220 return error;
221 }
222
223 // write garbage into output arrays
224 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
225 {
226 uint32_t pattern = 0xffffdead;
227 memset_pattern4(gOut[j], &pattern, BUFFER_SIZE);
228 if ((error =
229 clEnqueueWriteBuffer(gQueue, gOutBuffer[j], CL_FALSE, 0,
230 BUFFER_SIZE, gOut[j], 0, NULL, NULL)))
231 {
232 vlog_error("\n*** Error %d in clEnqueueWriteBuffer2(%d) ***\n",
233 error, j);
234 goto exit;
235 }
236
237 memset_pattern4(gOut2[j], &pattern, BUFFER_SIZE);
238 if ((error = clEnqueueWriteBuffer(gQueue, gOutBuffer2[j], CL_FALSE,
239 0, BUFFER_SIZE, gOut2[j], 0, NULL,
240 NULL)))
241 {
242 vlog_error("\n*** Error %d in clEnqueueWriteBuffer2b(%d) ***\n",
243 error, j);
244 goto exit;
245 }
246 }
247
248 // Run the kernels
249 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
250 {
251 size_t vectorSize = sizeof(cl_double) * sizeValues[j];
252 size_t localCount = (BUFFER_SIZE + vectorSize - 1)
253 / vectorSize; // BUFFER_SIZE / vectorSize rounded up
254 if ((error = clSetKernelArg(kernels[j], 0, sizeof(gOutBuffer[j]),
255 &gOutBuffer[j])))
256 {
257 LogBuildError(programs[j]);
258 goto exit;
259 }
260 if ((error = clSetKernelArg(kernels[j], 1, sizeof(gOutBuffer2[j]),
261 &gOutBuffer2[j])))
262 {
263 LogBuildError(programs[j]);
264 goto exit;
265 }
266 if ((error = clSetKernelArg(kernels[j], 2, sizeof(gInBuffer),
267 &gInBuffer)))
268 {
269 LogBuildError(programs[j]);
270 goto exit;
271 }
272 if ((error = clSetKernelArg(kernels[j], 3, sizeof(gInBuffer2),
273 &gInBuffer2)))
274 {
275 LogBuildError(programs[j]);
276 goto exit;
277 }
278
279 if ((error =
280 clEnqueueNDRangeKernel(gQueue, kernels[j], 1, NULL,
281 &localCount, NULL, 0, NULL, NULL)))
282 {
283 vlog_error("FAILED -- could not execute kernel\n");
284 goto exit;
285 }
286 }
287
288 // Get that moving
289 if ((error = clFlush(gQueue))) vlog("clFlush failed\n");
290
291 // Calculate the correctly rounded reference result
292 double *s = (double *)gIn;
293 double *s2 = (double *)gIn2;
294
295 if (threadCount > 1)
296 {
297 ComputeReferenceInfoD cri;
298 cri.x = s;
299 cri.y = s2;
300 cri.r = (double *)gOut_Ref;
301 cri.i = (int *)gOut_Ref2;
302 cri.f_ffpI = f->dfunc.f_ffpI;
303 cri.lim = BUFFER_SIZE / sizeof(double);
304 cri.count = (cri.lim + threadCount - 1) / threadCount;
305 ThreadPool_Do(ReferenceD, threadCount, &cri);
306 }
307 else
308 {
309 double *r = (double *)gOut_Ref;
310 int *r2 = (int *)gOut_Ref2;
311 for (size_t j = 0; j < BUFFER_SIZE / sizeof(double); j++)
312 r[j] = (double)f->dfunc.f_ffpI(s[j], s2[j], r2 + j);
313 }
314
315 // Read the data back
316 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
317 {
318 if ((error =
319 clEnqueueReadBuffer(gQueue, gOutBuffer[j], CL_TRUE, 0,
320 BUFFER_SIZE, gOut[j], 0, NULL, NULL)))
321 {
322 vlog_error("ReadArray failed %d\n", error);
323 goto exit;
324 }
325 if ((error =
326 clEnqueueReadBuffer(gQueue, gOutBuffer2[j], CL_TRUE, 0,
327 BUFFER_SIZE, gOut2[j], 0, NULL, NULL)))
328 {
329 vlog_error("ReadArray2 failed %d\n", error);
330 goto exit;
331 }
332 }
333
334 if (gSkipCorrectnessTesting) break;
335
336 // Verify data
337 uint64_t *t = (uint64_t *)gOut_Ref;
338 int32_t *t2 = (int32_t *)gOut_Ref2;
339 for (size_t j = 0; j < BUFFER_SIZE / sizeof(double); j++)
340 {
341 for (auto k = gMinVectorSizeIndex; k < gMaxVectorSizeIndex; k++)
342 {
343 uint64_t *q = (uint64_t *)gOut[k];
344 int32_t *q2 = (int32_t *)gOut2[k];
345
346 // Check for exact match to correctly rounded result
347 if (t[j] == q[j] && t2[j] == q2[j]) continue;
348
349 // Check for paired NaNs
350 if ((t[j] & 0x7fffffffffffffffUL) > 0x7ff0000000000000UL
351 && (q[j] & 0x7fffffffffffffffUL) > 0x7ff0000000000000UL
352 && t2[j] == q2[j])
353 continue;
354
355 double test = ((double *)q)[j];
356 int correct2 = INT_MIN;
357 long double correct = f->dfunc.f_ffpI(s[j], s2[j], &correct2);
358 float err = Bruteforce_Ulp_Error_Double(test, correct);
359 int64_t iErr;
360
361 // in case of remquo, we only care about the sign and last
362 // seven bits of integer as per the spec.
363 if (testingRemquo)
364 iErr = (long long)(q2[j] & 0x0000007f)
365 - (long long)(correct2 & 0x0000007f);
366 else
367 iErr = (long long)q2[j] - (long long)correct2;
368
369 // For remquo, if y = 0, x is infinite, or either is NaN
370 // then the standard either neglects to say what is returned
371 // in iptr or leaves it undefined or implementation defined.
372 int iptrUndefined = fabs(((double *)gIn)[j]) == INFINITY
373 || ((double *)gIn2)[j] == 0.0 || isnan(((double *)gIn2)[j])
374 || isnan(((double *)gIn)[j]);
375 if (iptrUndefined) iErr = 0;
376
377 int fail = !(fabsf(err) <= f->double_ulps && iErr == 0);
378 if (ftz && fail)
379 {
380 // retry per section 6.5.3.2
381 if (IsDoubleResultSubnormal(correct, f->double_ulps))
382 {
383 fail = fail && !(test == 0.0f && iErr == 0);
384 if (!fail) err = 0.0f;
385 }
386
387 // retry per section 6.5.3.3
388 if (IsDoubleSubnormal(s[j]))
389 {
390 int correct3i, correct4i;
391 long double correct3 =
392 f->dfunc.f_ffpI(0.0, s2[j], &correct3i);
393 long double correct4 =
394 f->dfunc.f_ffpI(-0.0, s2[j], &correct4i);
395 float err2 =
396 Bruteforce_Ulp_Error_Double(test, correct3);
397 float err3 =
398 Bruteforce_Ulp_Error_Double(test, correct4);
399 int64_t iErr3 = (long long)q2[j] - (long long)correct3i;
400 int64_t iErr4 = (long long)q2[j] - (long long)correct4i;
401 fail = fail
402 && ((!(fabsf(err2) <= f->double_ulps && iErr3 == 0))
403 && (!(fabsf(err3) <= f->double_ulps
404 && iErr4 == 0)));
405 if (fabsf(err2) < fabsf(err)) err = err2;
406 if (fabsf(err3) < fabsf(err)) err = err3;
407 if (llabs(iErr3) < llabs(iErr)) iErr = iErr3;
408 if (llabs(iErr4) < llabs(iErr)) iErr = iErr4;
409
410 // retry per section 6.5.3.4
411 if (IsDoubleResultSubnormal(correct2, f->double_ulps)
412 || IsDoubleResultSubnormal(correct3,
413 f->double_ulps))
414 {
415 fail = fail
416 && !(test == 0.0f
417 && (iErr3 == 0 || iErr4 == 0));
418 if (!fail) err = 0.0f;
419 }
420
421 // try with both args as zero
422 if (IsDoubleSubnormal(s2[j]))
423 {
424 int correct7i, correct8i;
425 correct3 = f->dfunc.f_ffpI(0.0, 0.0, &correct3i);
426 correct4 = f->dfunc.f_ffpI(-0.0, 0.0, &correct4i);
427 long double correct7 =
428 f->dfunc.f_ffpI(0.0, -0.0, &correct7i);
429 long double correct8 =
430 f->dfunc.f_ffpI(-0.0, -0.0, &correct8i);
431 err2 = Bruteforce_Ulp_Error_Double(test, correct3);
432 err3 = Bruteforce_Ulp_Error_Double(test, correct4);
433 float err4 =
434 Bruteforce_Ulp_Error_Double(test, correct7);
435 float err5 =
436 Bruteforce_Ulp_Error_Double(test, correct8);
437 iErr3 = (long long)q2[j] - (long long)correct3i;
438 iErr4 = (long long)q2[j] - (long long)correct4i;
439 int64_t iErr7 =
440 (long long)q2[j] - (long long)correct7i;
441 int64_t iErr8 =
442 (long long)q2[j] - (long long)correct8i;
443 fail = fail
444 && ((!(fabsf(err2) <= f->double_ulps
445 && iErr3 == 0))
446 && (!(fabsf(err3) <= f->double_ulps
447 && iErr4 == 0))
448 && (!(fabsf(err4) <= f->double_ulps
449 && iErr7 == 0))
450 && (!(fabsf(err5) <= f->double_ulps
451 && iErr8 == 0)));
452 if (fabsf(err2) < fabsf(err)) err = err2;
453 if (fabsf(err3) < fabsf(err)) err = err3;
454 if (fabsf(err4) < fabsf(err)) err = err4;
455 if (fabsf(err5) < fabsf(err)) err = err5;
456 if (llabs(iErr3) < llabs(iErr)) iErr = iErr3;
457 if (llabs(iErr4) < llabs(iErr)) iErr = iErr4;
458 if (llabs(iErr7) < llabs(iErr)) iErr = iErr7;
459 if (llabs(iErr8) < llabs(iErr)) iErr = iErr8;
460
461 // retry per section 6.5.3.4
462 if (IsDoubleResultSubnormal(correct3,
463 f->double_ulps)
464 || IsDoubleResultSubnormal(correct4,
465 f->double_ulps)
466 || IsDoubleResultSubnormal(correct7,
467 f->double_ulps)
468 || IsDoubleResultSubnormal(correct8,
469 f->double_ulps))
470 {
471 fail = fail
472 && !(test == 0.0f
473 && (iErr3 == 0 || iErr4 == 0
474 || iErr7 == 0 || iErr8 == 0));
475 if (!fail) err = 0.0f;
476 }
477 }
478 }
479 else if (IsDoubleSubnormal(s2[j]))
480 {
481 int correct3i, correct4i;
482 long double correct3 =
483 f->dfunc.f_ffpI(s[j], 0.0, &correct3i);
484 long double correct4 =
485 f->dfunc.f_ffpI(s[j], -0.0, &correct4i);
486 float err2 =
487 Bruteforce_Ulp_Error_Double(test, correct3);
488 float err3 =
489 Bruteforce_Ulp_Error_Double(test, correct4);
490 int64_t iErr3 = (long long)q2[j] - (long long)correct3i;
491 int64_t iErr4 = (long long)q2[j] - (long long)correct4i;
492 fail = fail
493 && ((!(fabsf(err2) <= f->double_ulps && iErr3 == 0))
494 && (!(fabsf(err3) <= f->double_ulps
495 && iErr4 == 0)));
496 if (fabsf(err2) < fabsf(err)) err = err2;
497 if (fabsf(err3) < fabsf(err)) err = err3;
498 if (llabs(iErr3) < llabs(iErr)) iErr = iErr3;
499 if (llabs(iErr4) < llabs(iErr)) iErr = iErr4;
500
501 // retry per section 6.5.3.4
502 if (IsDoubleResultSubnormal(correct2, f->double_ulps)
503 || IsDoubleResultSubnormal(correct3,
504 f->double_ulps))
505 {
506 fail = fail
507 && !(test == 0.0f
508 && (iErr3 == 0 || iErr4 == 0));
509 if (!fail) err = 0.0f;
510 }
511 }
512 }
513 if (fabsf(err) > maxError)
514 {
515 maxError = fabsf(err);
516 maxErrorVal = s[j];
517 }
518 if (llabs(iErr) > maxError2)
519 {
520 maxError2 = llabs(iErr);
521 maxErrorVal2 = s[j];
522 }
523
524 if (fail)
525 {
526 vlog_error(
527 "\nERROR: %sD%s: {%f, %lld} ulp error at {%.13la, "
528 "%.13la} ({ 0x%16.16llx, 0x%16.16llx}): *{%.13la, "
529 "%d} ({ 0x%16.16llx, 0x%8.8x}) vs. {%.13la, %d} ({ "
530 "0x%16.16llx, 0x%8.8x})\n",
531 f->name, sizeNames[k], err, iErr, ((double *)gIn)[j],
532 ((double *)gIn2)[j], ((cl_ulong *)gIn)[j],
533 ((cl_ulong *)gIn2)[j], ((double *)gOut_Ref)[j],
534 ((int *)gOut_Ref2)[j], ((cl_ulong *)gOut_Ref)[j],
535 ((cl_uint *)gOut_Ref2)[j], test, q2[j],
536 ((cl_ulong *)q)[j], ((cl_uint *)q2)[j]);
537 error = -1;
538 goto exit;
539 }
540 }
541 }
542
543 if (0 == (i & 0x0fffffff))
544 {
545 if (gVerboseBruteForce)
546 {
547 vlog("base:%14u step:%10zu bufferSize:%10zd \n", i, step,
548 BUFFER_SIZE);
549 }
550 else
551 {
552 vlog(".");
553 }
554 fflush(stdout);
555 }
556 }
557
558 if (!gSkipCorrectnessTesting)
559 {
560 if (gWimpyMode)
561 vlog("Wimp pass");
562 else
563 vlog("passed");
564
565 vlog("\t{%8.2f, %lld} @ {%a, %a}", maxError, maxError2, maxErrorVal,
566 maxErrorVal2);
567 }
568
569 vlog("\n");
570
571 exit:
572 // Release
573 for (auto k = gMinVectorSizeIndex; k < gMaxVectorSizeIndex; k++)
574 {
575 clReleaseKernel(kernels[k]);
576 clReleaseProgram(programs[k]);
577 }
578
579 return error;
580 }
581