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 _conversions_h
17 #define _conversions_h
18
19 #include "compat.h"
20
21 #include "errorHelpers.h"
22 #include "mt19937.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27
28 /* Note: the next three all have to match in size and order!! */
29
30 enum ExplicitTypes
31 {
32 kBool = 0,
33 kChar,
34 kUChar,
35 kUnsignedChar,
36 kShort,
37 kUShort,
38 kUnsignedShort,
39 kInt,
40 kUInt,
41 kUnsignedInt,
42 kLong,
43 kULong,
44 kUnsignedLong,
45 kFloat,
46 kHalf,
47 kDouble,
48 kNumExplicitTypes
49 };
50
51 typedef enum ExplicitTypes ExplicitType;
52
53 enum RoundingTypes
54 {
55 kRoundToEven = 0,
56 kRoundToZero,
57 kRoundToPosInf,
58 kRoundToNegInf,
59 kRoundToNearest,
60
61 kNumRoundingTypes,
62
63 kDefaultRoundingType = kRoundToNearest
64 };
65
66 typedef enum RoundingTypes RoundingType;
67
68 extern void print_type_to_string(ExplicitType type, void *data, char* string);
69 extern size_t get_explicit_type_size( ExplicitType type );
70 extern const char * get_explicit_type_name( ExplicitType type );
71 extern void convert_explicit_value( void *inRaw, void *outRaw, ExplicitType inType, bool saturate, RoundingType roundType, ExplicitType outType );
72
73 extern void generate_random_data( ExplicitType type, size_t count, MTdata d, void *outData );
74 extern void * create_random_data( ExplicitType type, MTdata d, size_t count );
75
76 extern cl_long read_upscale_signed( void *inRaw, ExplicitType inType );
77 extern cl_ulong read_upscale_unsigned( void *inRaw, ExplicitType inType );
78 extern float read_as_float( void *inRaw, ExplicitType inType );
79
80 extern float get_random_float(float low, float high, MTdata d);
81 extern double get_random_double(double low, double high, MTdata d);
82 extern float any_float( MTdata d );
83 extern double any_double( MTdata d );
84
85 extern int random_in_range( int minV, int maxV, MTdata d );
86
87 size_t get_random_size_t(size_t low, size_t high, MTdata d);
88
89 // Note: though this takes a double, this is for use with single precision tests
IsFloatSubnormal(float x)90 static inline int IsFloatSubnormal( float x )
91 {
92 #if 2 == FLT_RADIX
93 // Do this in integer to avoid problems with FTZ behavior
94 union{ float d; uint32_t u;}u;
95 u.d = fabsf(x);
96 return (u.u-1) < 0x007fffffU;
97 #else
98 // rely on floating point hardware for non-radix2 non-IEEE-754 hardware -- will fail if you flush subnormals to zero
99 return fabs(x) < (double) FLT_MIN && x != 0.0;
100 #endif
101 }
102
IsDoubleSubnormal(double x)103 static inline int IsDoubleSubnormal( double x )
104 {
105 #if 2 == FLT_RADIX
106 // Do this in integer to avoid problems with FTZ behavior
107 union{ double d; uint64_t u;}u;
108 u.d = fabs( x);
109 return (u.u-1) < 0x000fffffffffffffULL;
110 #else
111 // rely on floating point hardware for non-radix2 non-IEEE-754 hardware -- will fail if you flush subnormals to zero
112 return fabs(x) < (double) DBL_MIN && x != 0.0;
113 #endif
114 }
115
IsHalfSubnormal(cl_half x)116 static inline int IsHalfSubnormal( cl_half x )
117 {
118 return ( ( x & 0x7fffU ) - 1U ) < 0x03ffU;
119 }
120
121 #endif // _conversions_h
122
123
124