xref: /aosp_15_r20/external/deqp/framework/common/tcuFloatFormat.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 #ifndef _TCUFLOATFORMAT_HPP
2 #define _TCUFLOATFORMAT_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements Quality Program Tester Core
5  * ----------------------------------------
6  *
7  * Copyright 2014 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Adjustable-precision floating point operations.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "tcuDefs.hpp"
27 #include "tcuInterval.hpp"
28 
29 #include <string>
30 
31 namespace tcu
32 {
33 
34 enum YesNoMaybe
35 {
36     NO,
37     MAYBE,
38     YES
39 };
40 
41 class FloatFormat
42 {
43 public:
44     FloatFormat(int minExp, int maxExp, int fractionBits, bool exactPrecision, YesNoMaybe hasSubnormal = MAYBE,
45                 YesNoMaybe hasInf = MAYBE, YesNoMaybe hasNaN = MAYBE);
~FloatFormat()46     virtual ~FloatFormat()
47     {
48     }
49 
getMinExp(void) const50     int getMinExp(void) const
51     {
52         return m_minExp;
53     }
getMaxExp(void) const54     int getMaxExp(void) const
55     {
56         return m_maxExp;
57     }
getMaxValue(void) const58     double getMaxValue(void) const
59     {
60         return m_maxValue;
61     }
getFractionBits(void) const62     int getFractionBits(void) const
63     {
64         return m_fractionBits;
65     }
hasInf(void) const66     YesNoMaybe hasInf(void) const
67     {
68         return m_hasInf;
69     }
hasSubnormal(void) const70     YesNoMaybe hasSubnormal(void) const
71     {
72         return m_hasSubnormal;
73     }
74 
75     virtual double ulp(double x, double count = 1.0) const;
76     Interval roundOut(const Interval &x, bool roundUnderOverflow) const;
77     virtual double round(double d, bool upward) const;
78     virtual double roundOut(double d, bool upward, bool roundUnderOverflow) const;
79     Interval convert(const Interval &x) const;
80 
81     std::string floatToHex(double x) const;
82     std::string intervalToHex(const Interval &interval) const;
83 
84     static FloatFormat nativeFloat(void);
85     static FloatFormat nativeDouble(void);
86 
87 private:
88     int exponentShift(int exp) const;
89     Interval clampValue(double d) const;
90 
91     int m_minExp;              // Minimum exponent, inclusive
92     int m_maxExp;              // Maximum exponent, inclusive
93     int m_fractionBits;        // Number of fractional bits in significand
94     YesNoMaybe m_hasSubnormal; // Does the format support denormalized numbers?
95     YesNoMaybe m_hasInf;       // Does the format support infinities?
96     YesNoMaybe m_hasNaN;       // Does the format support NaNs?
97     bool m_exactPrecision;     // Are larger precisions disallowed?
98     double m_maxValue;         // Largest representable finite value.
99 } DE_WARN_UNUSED_TYPE;
100 
101 class NormalizedFormat : public FloatFormat
102 {
103 public:
104     NormalizedFormat(int fractionBits);
~NormalizedFormat()105     ~NormalizedFormat()
106     {
107     }
108 
109     double ulp(double x, double count = 1.0) const override;
110     double round(double d, bool upward) const override;
111     double roundOut(double d, bool upward, bool roundUnderOverflow) const override;
112 
113 } DE_WARN_UNUSED_TYPE;
114 
115 void FloatFormat_selfTest(void);
116 
117 } // namespace tcu
118 
119 #endif // _TCUFLOATFORMAT_HPP
120