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