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
36 cl_int err = CL_SUCCESS;
37 int num = (int)h_in.size();
38 size_t bytes = sizeof(Tv) * num;
39
40 clMemWrapper in = clCreateBuffer(context, CL_MEM_READ_WRITE, bytes, NULL, &err);
41 SPIRV_CHECK_ERROR(err, "Failed to create in buffer");
42
43 err = clEnqueueWriteBuffer(queue, in, CL_TRUE, 0, bytes, &h_in[0], 0, NULL, NULL);
44 SPIRV_CHECK_ERROR(err, "Failed to copy to in buffer");
45
46 cl_uint bits = sizeof(void *) * 8;
47 std::string spvStr = std::string(funcName) + "_" + std::string(Tname);
48 const char *spvName = spvStr.c_str();
49
50 clProgramWrapper prog;
51 err = get_program_with_il(prog, deviceID, context, spvName);
52 SPIRV_CHECK_ERROR(err, "Failed to build program");
53
54 clKernelWrapper kernel = clCreateKernel(prog, spvName, &err);
55 SPIRV_CHECK_ERROR(err, "Failed to create spv kernel");
56
57 err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &in);
58 SPIRV_CHECK_ERROR(err, "Failed to set arg 1");
59
60 size_t global = num;
61 err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, NULL, 0, NULL, NULL);
62 SPIRV_CHECK_ERROR(err, "Failed to enqueue cl kernel");
63
64 std::vector<Tv> h_out(num);
65 err = clEnqueueReadBuffer(queue, in, CL_TRUE, 0, bytes, &h_out[0], 0, NULL, NULL);
66 SPIRV_CHECK_ERROR(err, "Failed to read from ref");
67
68 for (int i = 0; i < num; i++) {
69 if (h_out[i] != negate(h_in[i])) {
70 log_error("Values do not match at location %d\n", i);
71 return -1;
72 }
73 }
74 return 0;
75 }
76
77 #define TEST_NEGATION(TYPE, Tv, OP, FUNC) \
78 TEST_SPIRV_FUNC(OP##_##TYPE) \
79 { \
80 int num = 1 << 20; \
81 std::vector<Tv> in(num); \
82 RandomSeed seed(gRandomSeed); \
83 for (int i = 0; i < num; i++) { \
84 in[i] = genrand<Tv>(seed); \
85 } \
86 return test_negation<Tv>(deviceID, \
87 context, \
88 queue, \
89 #TYPE, \
90 #OP, \
91 in, FUNC); \
92 } \
93
94
95 #define TEST_NEG(TYPE) TEST_NEGATION(TYPE, cl_##TYPE, op_neg, negOp<cl_##TYPE>)
96 #define TEST_NOT(TYPE) TEST_NEGATION(TYPE, cl_##TYPE, op_not, notOp<cl_##TYPE>)
97 #define TEST_NEG_VEC(TYPE, N) TEST_NEGATION(TYPE##N, cl_##TYPE##N, op_neg, (negOpVec<cl_##TYPE##N, N>))
98 #define TEST_NOT_VEC(TYPE, N) TEST_NEGATION(TYPE##N, cl_##TYPE##N, op_not, (notOpVec<cl_##TYPE##N, N>))
99
100 TEST_NEG(float)
101 TEST_NEG(double)
102 TEST_NEG(int)
103 TEST_NEG(long)
104 TEST_NOT(int)
105 TEST_NOT(long)
106
107 #ifdef __GNUC__
108 // std::vector<cl_short> is causing compilation errors on GCC 5.3 (works on gcc 4.8)
109 // Needs further investigation
110 TEST_NEGATION(short, short, op_neg, negOp<cl_short>)
111 TEST_NEGATION(short, short, op_not, notOp<cl_short>)
112 #else
113 TEST_NEG(short)
114 TEST_NOT(short)
115 #endif
116
117 TEST_NEG_VEC(float , 4)
118 TEST_NEG_VEC(int , 4)
119 TEST_NOT_VEC(int , 4)
120