• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 #include "tcuInterval.hpp"
29 
30 #include <string>
31 
32 namespace tcu
33 {
34 
35 enum YesNoMaybe
36 {
37 	NO,
38 	MAYBE,
39 	YES
40 };
41 
42 class FloatFormat
43 {
44 public:
45 
46 						FloatFormat	(int		minExp,
47 									 int		maxExp,
48 									 int		fractionBits,
49 									 bool		exactPrecision,
50 									 YesNoMaybe	hasSubnormal	= MAYBE,
51 									 YesNoMaybe	hasInf			= MAYBE,
52 									 YesNoMaybe	hasNaN			= MAYBE);
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; }
hasSubnormal(void) const58 	YesNoMaybe			hasSubnormal	(void) const { return m_hasSubnormal; }
59 
60 	double				ulp				(double x, double count = 1.0) const;
61 	Interval			roundOut		(const Interval& x, bool roundUnderOverflow) const;
62 	double				round			(double d, bool upward) const;
63 	double				roundOut		(double d, bool upward, bool roundUnderOverflow) const;
64 	Interval			convert			(const Interval& x) const;
65 
66 	std::string			floatToHex		(double x) const;
67 	std::string 		intervalToHex	(const Interval& interval) const;
68 
69 	static FloatFormat	nativeFloat		(void);
70 	static FloatFormat	nativeDouble	(void);
71 
72 private:
73 	int					exponentShift 	(int exp) const;
74 	Interval			clampValue		(double d) const;
75 
76 	int					m_minExp;			// Minimum exponent, inclusive
77 	int					m_maxExp;			// Maximum exponent, inclusive
78 	int					m_fractionBits;		// Number of fractional bits in significand
79 	YesNoMaybe			m_hasSubnormal;		// Does the format support denormalized numbers?
80 	YesNoMaybe			m_hasInf;			// Does the format support infinities?
81 	YesNoMaybe			m_hasNaN;			// Does the format support NaNs?
82 	bool				m_exactPrecision;	// Are larger precisions disallowed?
83 	double				m_maxValue;			// Largest representable finite value.
84 };
85 
86 void		FloatFormat_selfTest	(void);
87 
88 } // tcu
89 
90 #endif // _TCUFLOATFORMAT_HPP
91