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 #ifndef UTILITY_H
17 #define UTILITY_H
18
19 #include "harness/compat.h"
20 #include "harness/rounding_mode.h"
21 #include "harness/fpcontrol.h"
22 #include "harness/testHarness.h"
23 #include "harness/ThreadPool.h"
24 #include "harness/conversions.h"
25
26 #define BUFFER_SIZE (1024 * 1024 * 2)
27 #define EMBEDDED_REDUCTION_FACTOR (64)
28
29 #if defined(__GNUC__)
30 #define UNUSED __attribute__((unused))
31 #else
32 #define UNUSED
33 #endif
34
35 struct Func;
36
37 extern int gWimpyReductionFactor;
38
39 #define VECTOR_SIZE_COUNT 6
40 extern const char *sizeNames[VECTOR_SIZE_COUNT];
41 extern const int sizeValues[VECTOR_SIZE_COUNT];
42
43 extern cl_device_id gDevice;
44 extern cl_context gContext;
45 extern cl_command_queue gQueue;
46 extern void *gIn;
47 extern void *gIn2;
48 extern void *gIn3;
49 extern void *gOut_Ref;
50 extern void *gOut_Ref2;
51 extern void *gOut[VECTOR_SIZE_COUNT];
52 extern void *gOut2[VECTOR_SIZE_COUNT];
53 extern cl_mem gInBuffer;
54 extern cl_mem gInBuffer2;
55 extern cl_mem gInBuffer3;
56 extern cl_mem gOutBuffer[VECTOR_SIZE_COUNT];
57 extern cl_mem gOutBuffer2[VECTOR_SIZE_COUNT];
58 extern int gSkipCorrectnessTesting;
59 extern int gForceFTZ;
60 extern int gFastRelaxedDerived;
61 extern int gWimpyMode;
62 extern int gIsInRTZMode;
63 extern int gInfNanSupport;
64 extern int gIsEmbedded;
65 extern int gVerboseBruteForce;
66 extern uint32_t gMaxVectorSizeIndex;
67 extern uint32_t gMinVectorSizeIndex;
68 extern cl_device_fp_config gFloatCapabilities;
69
70 #define LOWER_IS_BETTER 0
71 #define HIGHER_IS_BETTER 1
72
73 #include "harness/errorHelpers.h"
74
75 #if defined(_MSC_VER)
76 // Deal with missing scalbn on windows
77 #define scalbnf(_a, _i) ldexpf(_a, _i)
78 #define scalbn(_a, _i) ldexp(_a, _i)
79 #define scalbnl(_a, _i) ldexpl(_a, _i)
80 #endif
81
82 float Abs_Error(float test, double reference);
83 float Ulp_Error(float test, double reference);
84 float Bruteforce_Ulp_Error_Double(double test, long double reference);
85
86 int MakeKernel(const char **c, cl_uint count, const char *name, cl_kernel *k,
87 cl_program *p, bool relaxedMode);
88 int MakeKernels(const char **c, cl_uint count, const char *name,
89 cl_uint kernel_count, cl_kernel *k, cl_program *p,
90 bool relaxedMode);
91
92 // used to convert a bucket of bits into a search pattern through double
DoubleFromUInt32(uint32_t bits)93 inline double DoubleFromUInt32(uint32_t bits)
94 {
95 union {
96 uint64_t u;
97 double d;
98 } u;
99
100 // split 0x89abcdef to 0x89abc00000000def
101 u.u = bits & 0xfffU;
102 u.u |= (uint64_t)(bits & ~0xfffU) << 32;
103
104 // sign extend the leading bit of def segment as sign bit so that the middle
105 // region consists of either all 1s or 0s
106 u.u -= (bits & 0x800U) << 1;
107
108 // return result
109 return u.d;
110 }
111
112 void _LogBuildError(cl_program p, int line, const char *file);
113 #define LogBuildError(program) _LogBuildError(program, __LINE__, __FILE__)
114
115 // The spec is fairly clear that we may enforce a hard cutoff to prevent
116 // premature flushing to zero.
117 // However, to avoid conflict for 1.0, we are letting results at TYPE_MIN +
118 // ulp_limit to be flushed to zero.
IsFloatResultSubnormal(double x,float ulps)119 inline int IsFloatResultSubnormal(double x, float ulps)
120 {
121 x = fabs(x) - MAKE_HEX_DOUBLE(0x1.0p-149, 0x1, -149) * (double)ulps;
122 return x < MAKE_HEX_DOUBLE(0x1.0p-126, 0x1, -126);
123 }
124
IsFloatResultSubnormalAbsError(double x,float abs_err)125 inline int IsFloatResultSubnormalAbsError(double x, float abs_err)
126 {
127 x = x - abs_err;
128 return x < MAKE_HEX_DOUBLE(0x1.0p-126, 0x1, -126);
129 }
130
IsDoubleResultSubnormal(long double x,float ulps)131 inline int IsDoubleResultSubnormal(long double x, float ulps)
132 {
133 x = fabsl(x) - MAKE_HEX_LONG(0x1.0p-1074, 0x1, -1074) * (long double)ulps;
134 return x < MAKE_HEX_LONG(0x1.0p-1022, 0x1, -1022);
135 }
136
IsFloatInfinity(double x)137 inline int IsFloatInfinity(double x)
138 {
139 union {
140 cl_float d;
141 cl_uint u;
142 } u;
143 u.d = (cl_float)x;
144 return ((u.u & 0x7fffffffU) == 0x7F800000U);
145 }
146
IsFloatMaxFloat(double x)147 inline int IsFloatMaxFloat(double x)
148 {
149 union {
150 cl_float d;
151 cl_uint u;
152 } u;
153 u.d = (cl_float)x;
154 return ((u.u & 0x7fffffffU) == 0x7F7FFFFFU);
155 }
156
IsFloatNaN(double x)157 inline int IsFloatNaN(double x)
158 {
159 union {
160 cl_float d;
161 cl_uint u;
162 } u;
163 u.d = (cl_float)x;
164 return ((u.u & 0x7fffffffU) > 0x7F800000U);
165 }
166
167 cl_uint RoundUpToNextPowerOfTwo(cl_uint x);
168
169 // Windows (since long double got deprecated) sets the x87 to 53-bit precision
170 // (that's x87 default state). This causes problems with the tests that
171 // convert long and ulong to float and double or otherwise deal with values
172 // that need more precision than 53-bit. So, set the x87 to 64-bit precision.
Force64BitFPUPrecision(void)173 inline void Force64BitFPUPrecision(void)
174 {
175 #if __MINGW32__
176 // The usual method is to use _controlfp as follows:
177 // #include <float.h>
178 // _controlfp(_PC_64, _MCW_PC);
179 //
180 // _controlfp is available on MinGW32 but not on MinGW64. Instead of having
181 // divergent code just use inline assembly which works for both.
182 unsigned short int orig_cw = 0;
183 unsigned short int new_cw = 0;
184 __asm__ __volatile__("fstcw %0" : "=m"(orig_cw));
185 new_cw = orig_cw | 0x0300; // set precision to 64-bit
186 __asm__ __volatile__("fldcw %0" ::"m"(new_cw));
187 #elif defined(_WIN32) && defined(__INTEL_COMPILER)
188 // Unfortunately, usual method (`_controlfp( _PC_64, _MCW_PC );') does *not*
189 // work on win.x64: > On the x64 architecture, changing the floating point
190 // precision is not supported. (Taken from
191 // http://msdn.microsoft.com/en-us/library/e9b52ceh%28v=vs.100%29.aspx)
192 int cw;
193 __asm { fnstcw cw }
194 ; // Get current value of FPU control word.
195 cw = cw & 0xfffffcff
196 | (3 << 8); // Set Precision Control to Double Extended Precision.
197 __asm { fldcw cw }
198 ; // Set new value of FPU control word.
199 #else
200 /* Implement for other platforms if needed */
201 #endif
202 }
203
204 void memset_pattern4(void *dest, const void *src_pattern, size_t bytes);
205
206 union int32f_t {
207 int32_t i;
208 float f;
209 };
210
211 union int64d_t {
212 int64_t l;
213 double d;
214 };
215
216 void MulD(double *rhi, double *rlo, double u, double v);
217 void AddD(double *rhi, double *rlo, double a, double b);
218 void MulDD(double *rhi, double *rlo, double xh, double xl, double yh,
219 double yl);
220 void AddDD(double *rhi, double *rlo, double xh, double xl, double yh,
221 double yl);
222 void DivideDD(double *chi, double *clo, double a, double b);
223 int compareFloats(float x, float y);
224 int compareDoubles(double x, double y);
225
226 void logFunctionInfo(const char *fname, unsigned int float_size,
227 unsigned int isFastRelaxed);
228
229 float getAllowedUlpError(const Func *f, const bool relaxed);
230
getTestScale(size_t typeSize)231 inline cl_uint getTestScale(size_t typeSize)
232 {
233 if (gWimpyMode)
234 {
235 return (cl_uint)typeSize * 2 * gWimpyReductionFactor;
236 }
237 else if (gIsEmbedded)
238 {
239 return EMBEDDED_REDUCTION_FACTOR;
240 }
241 else
242 {
243 return 1;
244 }
245 }
246
getTestStep(size_t typeSize,size_t bufferSize)247 inline uint64_t getTestStep(size_t typeSize, size_t bufferSize)
248 {
249 if (gWimpyMode)
250 {
251 return (1ULL << 32) * gWimpyReductionFactor / (512);
252 }
253 else if (gIsEmbedded)
254 {
255 return (BUFFER_SIZE / typeSize) * EMBEDDED_REDUCTION_FACTOR;
256 }
257 else
258 {
259 return bufferSize / typeSize;
260 }
261 }
262
263 #endif /* UTILITY_H */
264