• 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 VEC_NOT_EQ_FUNC(cl_half, 2)
47 VEC_NOT_EQ_FUNC(cl_half, 4)
48 
49 template<typename T>
isNotEqual(const T & lhs,const T & rhs)50 bool isNotEqual(const T &lhs, const T &rhs)
51 {
52     return lhs != rhs;
53 }
54 
55 // Can replace the following with tuples if c++11 can be used
56 template<typename T>
57 struct AbstractStruct1
58 {
59     T val;
60 };
61 
62 template<typename T>
operator !=(const AbstractStruct1<T> & lhs,const AbstractStruct1<T> & rhs)63 inline bool operator != (const AbstractStruct1<T> &lhs, const AbstractStruct1<T> &rhs)
64 {
65     return lhs.val != rhs.val;
66 }
67 
68 template<typename T0, typename T1>
69 struct AbstractStruct2
70 {
71     T0 val0;
72     T1 val1;
73 };
74 
75 
76 template<typename T0, typename T1>
operator !=(const AbstractStruct2<T0,T1> & lhs,const AbstractStruct2<T0,T1> & rhs)77 inline bool operator != (const AbstractStruct2<T0, T1> &lhs,
78                          const AbstractStruct2<T0, T1> &rhs)
79 {
80     return lhs.val0 != rhs.val0 || lhs.val1 != rhs.val1;
81 }
82 
83 
84 template<typename T> struct is_double { static const bool value = false; };
85 template<> struct  is_double<cl_double> { static const bool value = true; };
86 template<> struct  is_double<cl_double2> { static const bool value = true; };
87 
88 template<typename T>
genrandReal(RandomSeed & seed)89 T genrandReal(RandomSeed &seed)
90 {
91     return genrand_real1(seed);
92 }
93 
94 template<typename T, int N>
genrandRealVec(RandomSeed & seed)95 T genrandRealVec(RandomSeed &seed)
96 {
97     T res;
98     for (int i = 0; i < N; i++) {
99         res.s[i] = genrand_real1(seed);
100     }
101     return res;
102 }
103 
104 #define GENRAND_REAL_FUNC(TYPE, N)                                      \
105     template<> inline TYPE##N genrandReal<TYPE##N>(RandomSeed &seed)    \
106     {                                                                   \
107         return genrandRealVec<TYPE##N, N>(seed);                        \
108     }                                                                   \
109 
110 GENRAND_REAL_FUNC(cl_float, 2)
111 GENRAND_REAL_FUNC(cl_float, 4)
112 GENRAND_REAL_FUNC(cl_double, 2)
113 GENRAND_REAL_FUNC(cl_double, 4)
114 GENRAND_REAL_FUNC(cl_half, 2)
115 GENRAND_REAL_FUNC(cl_half, 4)
116 GENRAND_REAL_FUNC(cl_half, 8)
117 
genrandReal(RandomSeed & seed)118 template<> inline cl_half genrandReal<cl_half>(RandomSeed &seed)
119 {
120     return (cl_half)(genrand_int32(seed) % 2048);
121 }
122 
123 template<typename T>
genrand(RandomSeed & seed)124 T genrand(RandomSeed &seed)
125 {
126     return genrandReal<T>(seed);
127 }
128 
genrand(RandomSeed & seed)129 template<> inline cl_int genrand<cl_int>(RandomSeed &seed)
130 {
131     return genrand_int32(seed);
132 }
133 
genrand(RandomSeed & seed)134 template<> inline cl_long genrand<cl_long>(RandomSeed &seed)
135 {
136     return genrand_int32(seed);
137 }
138 
genrand(RandomSeed & seed)139 template<> inline cl_short genrand<cl_short>(RandomSeed &seed)
140 {
141     return genrand_int32(seed);
142 }
143 
144 #define GENRAND_INT_VEC(T, N)                               \
145     template<> inline T##N genrand<T##N>(RandomSeed &seed)  \
146     {                                                       \
147         T##N res;                                           \
148         for (int i = 0; i < N; i++) {                       \
149             res.s[i] = (T)genrand_int32(seed);              \
150         }                                                   \
151         return res;                                         \
152     }                                                       \
153 
154 GENRAND_INT_VEC(cl_int, 4)
155 GENRAND_INT_VEC(cl_uint, 4)
156 GENRAND_INT_VEC(cl_long, 2)
157 GENRAND_INT_VEC(cl_char, 16)
158 
159 template<typename Tv>
negOp(Tv in)160 Tv negOp(Tv in)
161 {
162     return -in;
163 }
164 
negOpHalf(cl_half v)165 inline cl_half negOpHalf(cl_half v) { return v ^ 0x8000; }
166 
167 template<typename Tv>
notOp(Tv in)168 Tv notOp(Tv in)
169 {
170     return ~in;
171 }
172 
173 template<typename Tv, int N>
negOpVec(Tv in)174 Tv negOpVec(Tv in)
175 {
176     Tv out;
177     for (int i = 0; i < N; i++) {
178         out.s[i] = -in.s[i];
179     }
180     return out;
181 }
182 
183 template<typename Tv, int N>
notOpVec(Tv in)184 Tv notOpVec(Tv in)
185 {
186     Tv out;
187     for (int i = 0; i < N; i++) {
188         out.s[i] = ~in.s[i];
189     }
190     return out;
191 }
192