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 
45 						FloatFormat	(int		minExp,
46 									 int		maxExp,
47 									 int		fractionBits,
48 									 bool		exactPrecision,
49 									 YesNoMaybe	hasSubnormal	= MAYBE,
50 									 YesNoMaybe	hasInf			= MAYBE,
51 									 YesNoMaybe	hasNaN			= MAYBE);
~FloatFormat()52 	virtual				~FloatFormat() {}
53 
getMinExp(void) const54 	int					getMinExp		(void) const { return m_minExp; }
getMaxExp(void) const55 	int					getMaxExp		(void) const { return m_maxExp; }
getMaxValue(void) const56 	double				getMaxValue		(void) const { return m_maxValue; }
getFractionBits(void) const57 	int					getFractionBits	(void) const { return m_fractionBits; }
hasInf(void) const58 	YesNoMaybe			hasInf			(void) const { return m_hasInf; }
hasSubnormal(void) const59 	YesNoMaybe			hasSubnormal	(void) const { return m_hasSubnormal; }
60 
61 	virtual double		ulp				(double x, double count = 1.0) const;
62 	Interval			roundOut		(const Interval& x, bool roundUnderOverflow) const;
63 	virtual double		round			(double d, bool upward) const;
64 	virtual double		roundOut		(double d, bool upward, bool roundUnderOverflow) const;
65 	Interval			convert			(const Interval& x) const;
66 
67 	std::string			floatToHex		(double x) const;
68 	std::string			intervalToHex	(const Interval& interval) const;
69 
70 	static FloatFormat	nativeFloat		(void);
71 	static FloatFormat	nativeDouble	(void);
72 
73 private:
74 	int					exponentShift	(int exp) const;
75 	Interval			clampValue		(double d) const;
76 
77 	int					m_minExp;			// Minimum exponent, inclusive
78 	int					m_maxExp;			// Maximum exponent, inclusive
79 	int					m_fractionBits;		// Number of fractional bits in significand
80 	YesNoMaybe			m_hasSubnormal;		// Does the format support denormalized numbers?
81 	YesNoMaybe			m_hasInf;			// Does the format support infinities?
82 	YesNoMaybe			m_hasNaN;			// Does the format support NaNs?
83 	bool				m_exactPrecision;	// Are larger precisions disallowed?
84 	double				m_maxValue;			// Largest representable finite value.
85 } DE_WARN_UNUSED_TYPE;
86 
87 class NormalizedFormat : public FloatFormat
88 {
89 public:
90 			NormalizedFormat    (int		fractionBits);
~NormalizedFormat()91 			~NormalizedFormat	() {}
92 
93 	double		ulp				(double x, double count = 1.0) const override;
94 	double		round			(double d, bool upward) const override;
95 	double		roundOut		(double d, bool upward, bool roundUnderOverflow) const override;
96 
97 } DE_WARN_UNUSED_TYPE;
98 
99 void		FloatFormat_selfTest	(void);
100 
101 } // tcu
102 
103 #endif // _TCUFLOATFORMAT_HPP
104