• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************
2 Copyright (c) 2016 The Khronos Group Inc. All Rights Reserved.
3 
4 This code is protected by copyright laws and contains material proprietary to the Khronos Group, Inc.
5 This is UNPUBLISHED PROPRIETARY SOURCE CODE that may not be disclosed in whole or in part to
6 third parties, and may not be reproduced, republished, distributed, transmitted, displayed,
7 broadcast or otherwise exploited in any manner without the express prior written permission
8 of Khronos Group. The receipt or possession of this code does not convey any rights to reproduce,
9 disclose, or distribute its contents, or to manufacture, use, or sell anything that it may describe,
10 in whole or in part other than under the terms of the Khronos Adopters Agreement
11 or Khronos Conformance Test Source License Agreement as executed between Khronos and the recipient.
12 ******************************************************************/
13 
14 #include "testBase.h"
15 #include "types.hpp"
16 
17 #include <sstream>
18 #include <string>
19 
20 template<typename Tv>
test_negation(cl_device_id deviceID,cl_context context,cl_command_queue queue,const char * Tname,const char * funcName,const std::vector<Tv> & h_in,Tv (* negate)(Tv)=negOp<Tv>)21 int test_negation(cl_device_id deviceID,
22                   cl_context context,
23                   cl_command_queue queue,
24                   const char *Tname,
25                   const char *funcName,
26                   const std::vector<Tv> &h_in,
27                   Tv (*negate)(Tv) = negOp<Tv>)
28 {
29     if(std::string(Tname).find("double") != std::string::npos) {
30         if(!is_extension_available(deviceID, "cl_khr_fp64")) {
31             log_info("Extension cl_khr_fp64 not supported; skipping double tests.\n");
32             return 0;
33         }
34     }
35     if (std::string(Tname).find("half") != std::string::npos)
36     {
37         if (!is_extension_available(deviceID, "cl_khr_fp16"))
38         {
39             log_info(
40                 "Extension cl_khr_fp16 not supported; skipping half tests.\n");
41             return 0;
42         }
43     }
44 
45     cl_int err = CL_SUCCESS;
46     int num = (int)h_in.size();
47     size_t bytes = sizeof(Tv) * num;
48 
49     clMemWrapper in = clCreateBuffer(context, CL_MEM_READ_WRITE, bytes, NULL, &err);
50     SPIRV_CHECK_ERROR(err, "Failed to create  in buffer");
51 
52     err = clEnqueueWriteBuffer(queue, in, CL_TRUE, 0, bytes, &h_in[0], 0, NULL, NULL);
53     SPIRV_CHECK_ERROR(err, "Failed to copy to in buffer");
54 
55     std::string spvStr = std::string(funcName) + "_" + std::string(Tname);
56     const char *spvName = spvStr.c_str();
57 
58     clProgramWrapper prog;
59     err = get_program_with_il(prog, deviceID, context, spvName);
60     SPIRV_CHECK_ERROR(err, "Failed to build program");
61 
62     clKernelWrapper kernel = clCreateKernel(prog, spvName, &err);
63     SPIRV_CHECK_ERROR(err, "Failed to create spv kernel");
64 
65     err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &in);
66     SPIRV_CHECK_ERROR(err, "Failed to set arg 1");
67 
68     size_t global = num;
69     err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, NULL, 0, NULL, NULL);
70     SPIRV_CHECK_ERROR(err, "Failed to enqueue cl kernel");
71 
72     std::vector<Tv> h_out(num);
73     err = clEnqueueReadBuffer(queue, in, CL_TRUE, 0, bytes, &h_out[0], 0, NULL, NULL);
74     SPIRV_CHECK_ERROR(err, "Failed to read from ref");
75 
76     for (int i = 0; i < num; i++) {
77         if (h_out[i] != negate(h_in[i])) {
78             log_error("Values do not match at location %d\n", i);
79             return -1;
80         }
81     }
82     return 0;
83 }
84 
85 #define TEST_NEGATION(TYPE, Tv, OP, FUNC)                                      \
86     TEST_SPIRV_FUNC(OP##_##TYPE)                                               \
87     {                                                                          \
88         int num = 1 << 20;                                                     \
89         std::vector<Tv> in(num);                                               \
90         RandomSeed seed(gRandomSeed);                                          \
91         for (int i = 0; i < num; i++)                                          \
92         {                                                                      \
93             in[i] = genrand<Tv>(seed);                                         \
94         }                                                                      \
95         return test_negation<Tv>(deviceID, context, queue, #TYPE, #OP, in,     \
96                                  FUNC);                                        \
97     }
98 
99 
100 #define TEST_NEG_HALF TEST_NEGATION(half, cl_half, op_neg, negOpHalf)
101 #define TEST_NEG(TYPE)        TEST_NEGATION(TYPE, cl_##TYPE, op_neg, negOp<cl_##TYPE>)
102 #define TEST_NOT(TYPE)        TEST_NEGATION(TYPE, cl_##TYPE, op_not, notOp<cl_##TYPE>)
103 #define TEST_NEG_VEC(TYPE, N) TEST_NEGATION(TYPE##N, cl_##TYPE##N, op_neg, (negOpVec<cl_##TYPE##N, N>))
104 #define TEST_NOT_VEC(TYPE, N) TEST_NEGATION(TYPE##N, cl_##TYPE##N, op_not, (notOpVec<cl_##TYPE##N, N>))
105 
106 TEST_NEG_HALF
107 TEST_NEG(float)
108 TEST_NEG(double)
109 TEST_NEG(int)
110 TEST_NEG(long)
111 TEST_NOT(int)
112 TEST_NOT(long)
113 
114 #ifdef __GNUC__
115 // std::vector<cl_short> is causing compilation errors on GCC 5.3 (works on gcc 4.8)
116 // Needs further investigation
117 TEST_NEGATION(short, short, op_neg, negOp<cl_short>)
118 TEST_NEGATION(short, short, op_not, notOp<cl_short>)
119 #else
120 TEST_NEG(short)
121 TEST_NOT(short)
122 #endif
123 
124 TEST_NEG_VEC(float  , 4)
125 TEST_NEG_VEC(int    , 4)
126 TEST_NOT_VEC(int    , 4)
127