• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,
72                                    ExplicitType inType, bool saturate,
73                                    RoundingType roundType,
74                                    ExplicitType outType);
75 
76 extern void generate_random_data(ExplicitType type, size_t count, MTdata d,
77                                  void *outData);
78 extern void *create_random_data(ExplicitType type, MTdata d, size_t count);
79 
80 extern cl_long read_upscale_signed(void *inRaw, ExplicitType inType);
81 extern cl_ulong read_upscale_unsigned(void *inRaw, ExplicitType inType);
82 extern float read_as_float(void *inRaw, ExplicitType inType);
83 
84 extern float get_random_float(float low, float high, MTdata d);
85 extern double get_random_double(double low, double high, MTdata d);
86 extern float any_float(MTdata d);
87 extern double any_double(MTdata d);
88 
89 extern int random_in_range(int minV, int maxV, MTdata d);
90 
91 size_t get_random_size_t(size_t low, size_t high, MTdata d);
92 
93 // Note: though this takes a double, this is for use with single precision tests
IsFloatSubnormal(float x)94 static inline int IsFloatSubnormal(float x)
95 {
96 #if 2 == FLT_RADIX
97     // Do this in integer to avoid problems with FTZ behavior
98     union {
99         float d;
100         uint32_t u;
101     } u;
102     u.d = fabsf(x);
103     return (u.u - 1) < 0x007fffffU;
104 #else
105     // rely on floating point hardware for non-radix2 non-IEEE-754 hardware --
106     // will fail if you flush subnormals to zero
107     return fabs(x) < (double)FLT_MIN && x != 0.0;
108 #endif
109 }
110 
IsDoubleSubnormal(double x)111 static inline int IsDoubleSubnormal(double x)
112 {
113 #if 2 == FLT_RADIX
114     // Do this in integer to avoid problems with FTZ behavior
115     union {
116         double d;
117         uint64_t u;
118     } u;
119     u.d = fabs(x);
120     return (u.u - 1) < 0x000fffffffffffffULL;
121 #else
122     // rely on floating point hardware for non-radix2 non-IEEE-754 hardware --
123     // will fail if you flush subnormals to zero
124     return fabs(x) < (double)DBL_MIN && x != 0.0;
125 #endif
126 }
127 
IsHalfSubnormal(cl_half x)128 static inline int IsHalfSubnormal(cl_half x)
129 {
130     // this relies on interger overflow to exclude 0 as a subnormal
131     return ((x & 0x7fffU) - 1U) < 0x03ffU;
132 }
133 
134 #endif // _conversions_h
135