• 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 #pragma once
15 #include <CL/cl.h>
16 
17 #if defined(_MSC_VER) || defined(_WIN32)
18 #define PACKED(__STRUCT__) __pragma(pack(push, 1)) __STRUCT__ __pragma(pack(pop))
19 #elif defined(__GNUC__) || defined(__clang__)
20 #define PACKED(__STRUCT__) __STRUCT__ __attribute__((packed))
21 #endif
22 
23 template<typename T, int n>
isVectorNotEqual(const T & lhs,const T & rhs)24 inline bool isVectorNotEqual(const T &lhs, const T &rhs)
25 {
26     bool result = false;
27     for (int i = 0; !result && i < n; i++) {
28         result |= lhs.s[i] != rhs.s[i];
29     }
30     return result;
31 }
32 
33 #define VEC_NOT_EQ_FUNC(TYPE, N)                                    \
34     inline bool operator!=(const TYPE##N &lhs, const TYPE##N &rhs)  \
35     {                                                               \
36         return isVectorNotEqual<TYPE##N, N>(lhs, rhs);                    \
37     }                                                               \
38 
39 VEC_NOT_EQ_FUNC(cl_int, 2)
40 VEC_NOT_EQ_FUNC(cl_int, 4)
41 VEC_NOT_EQ_FUNC(cl_uint, 4)
42 VEC_NOT_EQ_FUNC(cl_float, 2)
43 VEC_NOT_EQ_FUNC(cl_float, 4)
44 VEC_NOT_EQ_FUNC(cl_double, 2)
45 VEC_NOT_EQ_FUNC(cl_double, 4)
46 
47 template<typename T>
isNotEqual(const T & lhs,const T & rhs)48 bool isNotEqual(const T &lhs, const T &rhs)
49 {
50     return lhs != rhs;
51 }
52 
53 // Can replace the following with tuples if c++11 can be used
54 template<typename T>
55 struct AbstractStruct1
56 {
57     T val;
58 };
59 
60 template<typename T>
operator !=(const AbstractStruct1<T> & lhs,const AbstractStruct1<T> & rhs)61 inline bool operator != (const AbstractStruct1<T> &lhs, const AbstractStruct1<T> &rhs)
62 {
63     return lhs.val != rhs.val;
64 }
65 
66 template<typename T0, typename T1>
67 struct AbstractStruct2
68 {
69     T0 val0;
70     T1 val1;
71 };
72 
73 
74 template<typename T0, typename T1>
operator !=(const AbstractStruct2<T0,T1> & lhs,const AbstractStruct2<T0,T1> & rhs)75 inline bool operator != (const AbstractStruct2<T0, T1> &lhs,
76                          const AbstractStruct2<T0, T1> &rhs)
77 {
78     return lhs.val0 != rhs.val0 || lhs.val1 != rhs.val1;
79 }
80 
81 
82 template<typename T> struct is_double { static const bool value = false; };
83 template<> struct  is_double<cl_double> { static const bool value = true; };
84 template<> struct  is_double<cl_double2> { static const bool value = true; };
85 
86 template<typename T>
genrandReal(RandomSeed & seed)87 T genrandReal(RandomSeed &seed)
88 {
89     return genrand_real1(seed);
90 }
91 
92 template<typename T, int N>
genrandRealVec(RandomSeed & seed)93 T genrandRealVec(RandomSeed &seed)
94 {
95     T res;
96     for (int i = 0; i < N; i++) {
97         res.s[i] = genrand_real1(seed);
98     }
99     return res;
100 }
101 
102 #define GENRAND_REAL_FUNC(TYPE, N)                                      \
103     template<> inline TYPE##N genrandReal<TYPE##N>(RandomSeed &seed)    \
104     {                                                                   \
105         return genrandRealVec<TYPE##N, N>(seed);                        \
106     }                                                                   \
107 
108 GENRAND_REAL_FUNC(cl_float, 2)
109 GENRAND_REAL_FUNC(cl_float, 4)
110 GENRAND_REAL_FUNC(cl_double, 2)
111 GENRAND_REAL_FUNC(cl_double, 4)
112 
genrandReal(RandomSeed & seed)113 template<> inline cl_half genrandReal<cl_half>(RandomSeed &seed)
114 {
115     return (cl_half)(genrand_int32(seed) % 2048);
116 }
117 
118 template<typename T>
genrand(RandomSeed & seed)119 T genrand(RandomSeed &seed)
120 {
121     return genrandReal<T>(seed);
122 }
123 
genrand(RandomSeed & seed)124 template<> inline cl_int genrand<cl_int>(RandomSeed &seed)
125 {
126     return genrand_int32(seed);
127 }
128 
genrand(RandomSeed & seed)129 template<> inline cl_long genrand<cl_long>(RandomSeed &seed)
130 {
131     return genrand_int32(seed);
132 }
133 
genrand(RandomSeed & seed)134 template<> inline cl_short genrand<cl_short>(RandomSeed &seed)
135 {
136     return genrand_int32(seed);
137 }
138 
139 #define GENRAND_INT_VEC(T, N)                               \
140     template<> inline T##N genrand<T##N>(RandomSeed &seed)  \
141     {                                                       \
142         T##N res;                                           \
143         for (int i = 0; i < N; i++) {                       \
144             res.s[i] = (T)genrand_int32(seed);              \
145         }                                                   \
146         return res;                                         \
147     }                                                       \
148 
149 GENRAND_INT_VEC(cl_int, 4)
150 GENRAND_INT_VEC(cl_uint, 4)
151 GENRAND_INT_VEC(cl_long, 2)
152 GENRAND_INT_VEC(cl_char, 16)
153 
154 template<typename Tv>
negOp(Tv in)155 Tv negOp(Tv in)
156 {
157     return -in;
158 }
159 
160 template<typename Tv>
notOp(Tv in)161 Tv notOp(Tv in)
162 {
163     return ~in;
164 }
165 
166 template<typename Tv, int N>
negOpVec(Tv in)167 Tv negOpVec(Tv in)
168 {
169     Tv out;
170     for (int i = 0; i < N; i++) {
171         out.s[i] = -in.s[i];
172     }
173     return out;
174 }
175 
176 template<typename Tv, int N>
notOpVec(Tv in)177 Tv notOpVec(Tv in)
178 {
179     Tv out;
180     for (int i = 0; i < N; i++) {
181         out.s[i] = ~in.s[i];
182     }
183     return out;
184 }
185