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