1 #ifndef _TCUINTERVAL_HPP
2 #define _TCUINTERVAL_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 Interval arithmetic and floating point precisions.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "tcuDefs.hpp"
27 
28 #include "deMath.h"
29 
30 #include <iostream>
31 #include <limits>
32 #include <cmath>
33 
34 #define TCU_INFINITY	(::std::numeric_limits<float>::infinity())
35 #define TCU_NAN			(::std::numeric_limits<float>::quiet_NaN())
36 
37 namespace tcu
38 {
39 
40 // RAII context for temporarily changing the rounding mode
41 class ScopedRoundingMode
42 {
43 public:
ScopedRoundingMode(deRoundingMode mode)44 							ScopedRoundingMode	(deRoundingMode mode)
45 								: m_oldMode (deGetRoundingMode()) { deSetRoundingMode(mode); }
46 
ScopedRoundingMode(void)47 							ScopedRoundingMode	(void) : m_oldMode (deGetRoundingMode()) {}
48 
~ScopedRoundingMode(void)49 							~ScopedRoundingMode	(void)	{ deSetRoundingMode(m_oldMode); }
50 
51 private:
52 							ScopedRoundingMode	(const ScopedRoundingMode&);
53 	ScopedRoundingMode&		operator=			(const ScopedRoundingMode&);
54 
55 	const deRoundingMode	m_oldMode;
56 };
57 
58 class Interval
59 {
60 public:
61 				// Empty interval.
Interval(void)62 				Interval			(void)
63 					: m_hasNaN		(false)
64 					, m_lo			(TCU_INFINITY)
65 					, m_hi			(-TCU_INFINITY)
66 					, m_warningLo	(-TCU_INFINITY)
67 					, m_warningHi	(TCU_INFINITY) {}
68 
69 				// Intentionally not explicit. Conversion from double to Interval is common
70 				// and reasonable.
Interval(double val)71 				Interval			(double val)
72 					: m_hasNaN		(!!deIsNaN(val))
73 					, m_lo			(m_hasNaN ? TCU_INFINITY : val)
74 					, m_hi			(m_hasNaN ? -TCU_INFINITY : val)
75 					, m_warningLo	(-TCU_INFINITY)
76 					, m_warningHi	(TCU_INFINITY) {}
77 
78 
Interval(bool hasNaN_,double lo_,double hi_)79 				Interval(bool hasNaN_, double lo_, double hi_)
80 					: m_hasNaN(hasNaN_), m_lo(lo_), m_hi(hi_), m_warningLo(-TCU_INFINITY), m_warningHi(TCU_INFINITY) {}
81 
Interval(bool hasNaN_,double lo_,double hi_,double wlo_,double whi_)82 				Interval(bool hasNaN_, double lo_, double hi_, double wlo_, double whi_)
83 					: m_hasNaN(hasNaN_), m_lo(lo_), m_hi(hi_), m_warningLo(wlo_), m_warningHi(whi_) {}
84 
Interval(const Interval & a,const Interval & b)85 				Interval		(const Interval& a, const Interval& b)
86 					: m_hasNaN		(a.m_hasNaN || b.m_hasNaN)
87 					, m_lo			(de::min(a.lo(), b.lo()))
88 					, m_hi			(de::max(a.hi(), b.hi()))
89 					, m_warningLo	(de::min(a.warningLo(), b.warningLo()))
90 					, m_warningHi	(de::max(a.warningHi(), b.warningHi())) {}
91 
length(void) const92 	double		length			(void) const { return m_hi - m_lo; }
lo(void) const93 	double		lo				(void) const { return m_lo; }
hi(void) const94 	double		hi				(void) const { return m_hi; }
warningLo(void) const95 	double		warningLo		(void) const { return m_warningLo; }
warningHi(void) const96 	double		warningHi		(void) const { return m_warningHi; }
hasNaN(void) const97 	bool		hasNaN			(void) const { return m_hasNaN; }
nan(void) const98 	Interval	nan				(void) const { return m_hasNaN ? TCU_NAN : Interval(); }
empty(void) const99 	bool		empty			(void) const { return m_lo > m_hi; }
isFinite(void) const100 	bool		isFinite		(void) const { return m_lo > -TCU_INFINITY && m_hi < TCU_INFINITY; }
isOrdinary(void) const101 	bool		isOrdinary		(void) const { return !hasNaN() && !empty() && isFinite(); }
102 
warning(double lo_,double hi_)103 	void		warning			(double lo_, double hi_)
104 	{
105 		m_warningLo = lo_;
106 		m_warningHi = hi_;
107 	}
108 
operator |(const Interval & other) const109 	Interval	operator|		(const Interval& other) const
110 	{
111 		return Interval(m_hasNaN || other.m_hasNaN,
112 						de::min(m_lo, other.m_lo),
113 						de::max(m_hi, other.m_hi),
114 						de::min(m_warningLo, other.m_warningLo),
115 						de::max(m_warningHi, other.m_warningHi));
116 	}
117 
operator |=(const Interval & other)118 	Interval&	operator|=		(const Interval& other)
119 	{
120 		return (*this = *this | other);
121 	}
122 
operator &(const Interval & other) const123 	Interval	operator&		(const Interval& other) const
124 	{
125 		return Interval(m_hasNaN && other.m_hasNaN,
126 						de::max(m_lo, other.m_lo),
127 						de::min(m_hi, other.m_hi),
128 						de::max(m_warningLo, other.m_warningLo),
129 						de::min(m_warningHi, other.m_warningHi));
130 	}
131 
operator &=(const Interval & other)132 	Interval&	operator&=		(const Interval& other)
133 	{
134 		return (*this = *this & other);
135 	}
136 
contains(const Interval & other) const137 	bool		contains		(const Interval& other) const
138 	{
139 		return (other.lo() >= lo() && other.hi() <= hi() &&
140 				(!other.hasNaN() || hasNaN()));
141 	}
142 
containsWarning(const Interval & other) const143 	bool		containsWarning(const Interval& other) const
144 	{
145 		return (other.lo() >= warningLo() && other.hi() <= warningHi() &&
146 			(!other.hasNaN() || hasNaN()));
147 	}
148 
intersects(const Interval & other) const149 	bool		intersects		(const Interval& other) const
150 	{
151 		return ((other.hi() >= lo() && other.lo() <= hi()) ||
152 				(other.hasNaN() && hasNaN()));
153 	}
154 
operator -(void) const155 	Interval	operator-		(void) const
156 	{
157 		return Interval(hasNaN(), -hi(), -lo(), -warningHi(), -warningLo());
158 	}
159 
unbounded(bool nan=false)160 	static Interval	unbounded	(bool nan = false)
161 	{
162 		return Interval(nan, -TCU_INFINITY, TCU_INFINITY);
163 	}
164 
midpoint(void) const165 	double		midpoint		(void) const
166 	{
167 		const double	h = hi();
168 		const double	l = lo();
169 
170 		if (h == -l)
171 			return 0.0;
172 		if (l == -TCU_INFINITY)
173 			return -TCU_INFINITY;
174 		if (h == TCU_INFINITY)
175 			return TCU_INFINITY;
176 
177 		const bool		negativeH = ::std::signbit(h);
178 		const bool		negativeL = ::std::signbit(l);
179 		double			ret;
180 
181 		if (negativeH != negativeL)
182 		{
183 			// Different signs. Adding both values should be safe.
184 			ret = (h + l) * 0.5;
185 		}
186 		else
187 		{
188 			// Same sign. Substracting low from high should be safe.
189 			ret = l + (h - l) * 0.5;
190 		}
191 
192 		return ret;
193 	}
194 
operator ==(const Interval & other) const195 	bool		operator==		(const Interval& other) const
196 	{
197 		return ((m_hasNaN == other.m_hasNaN) &&
198 				((empty() && other.empty()) ||
199 				 (m_lo == other.m_lo && m_hi == other.m_hi)));
200 	}
201 
202 private:
203 	bool		m_hasNaN;
204 	double		m_lo;
205 	double		m_hi;
206 	double		m_warningLo;
207 	double		m_warningHi;
208 } DE_WARN_UNUSED_TYPE;
209 
operator +(const Interval & x)210 inline Interval	operator+	(const Interval& x) { return x; }
211 Interval		exp2		(const Interval& x);
212 Interval		exp			(const Interval& x);
213 int				sign		(const Interval& x);
214 Interval		abs			(const Interval& x);
215 Interval		inverseSqrt	(const Interval& x);
216 
217 Interval		operator+	(const Interval& x,		const Interval& y);
218 Interval		operator-	(const Interval& x,		const Interval& y);
219 Interval		operator*	(const Interval& x,		const Interval& y);
220 Interval		operator/	(const Interval& nom,	const Interval& den);
221 
operator +=(Interval & x,const Interval & y)222 inline Interval& operator+=	(Interval& x,	const Interval& y) { return (x = x + y); }
operator -=(Interval & x,const Interval & y)223 inline Interval& operator-=	(Interval& x,	const Interval& y) { return (x = x - y); }
operator *=(Interval & x,const Interval & y)224 inline Interval& operator*=	(Interval& x,	const Interval& y) { return (x = x * y); }
operator /=(Interval & x,const Interval & y)225 inline Interval& operator/=	(Interval& x,	const Interval& y) { return (x = x / y); }
226 
227 std::ostream&	operator<<	(std::ostream& os, const Interval& interval);
228 
229 #define TCU_SET_INTERVAL_BOUNDS(DST, VAR, SETLOW, SETHIGH) do	\
230 {																\
231 	::tcu::ScopedRoundingMode	VAR##_ctx_;						\
232 	::tcu::Interval&			VAR##_dst_	= (DST);			\
233 	::tcu::Interval				VAR##_lo_;						\
234 	::tcu::Interval				VAR##_hi_;						\
235 																\
236 	{															\
237 		::tcu::Interval&	VAR = VAR##_lo_;					\
238 		::deSetRoundingMode(DE_ROUNDINGMODE_TO_NEGATIVE_INF);	\
239 		SETLOW;													\
240 	}															\
241 	{															\
242 		::tcu::Interval&	VAR = VAR##_hi_;					\
243 		::deSetRoundingMode(DE_ROUNDINGMODE_TO_POSITIVE_INF);	\
244 		SETHIGH;												\
245 	}															\
246 																\
247 	VAR##_dst_ = VAR##_lo_ | VAR##_hi_;							\
248 } while (::deGetFalse())
249 
250 #define TCU_SET_INTERVAL(DST, VAR, BODY)						\
251 	TCU_SET_INTERVAL_BOUNDS(DST, VAR, BODY, BODY)
252 
253 //! Set the interval DST to the image of BODY on ARG, assuming that BODY on
254 //! ARG is a monotone function. In practice, BODY is evaluated on both the
255 //! upper and lower bound of ARG, and DST is set to the union of these
256 //! results. While evaluating BODY, PARAM is bound to the bound of ARG, and
257 //! the output of BODY should be stored in VAR.
258 #define TCU_INTERVAL_APPLY_MONOTONE1(DST, PARAM, ARG, VAR, BODY) do		\
259 	{																	\
260 	const ::tcu::Interval&	VAR##_arg_		= (ARG);					\
261 	::tcu::Interval&		VAR##_dst_		= (DST);					\
262 	::tcu::Interval			VAR##_lo_;									\
263 	::tcu::Interval			VAR##_hi_;									\
264 	if (VAR##_arg_.empty())												\
265 		VAR##_dst_ = Interval();										\
266 	else																\
267 	{																	\
268 		{																\
269 			const double		PARAM	= VAR##_arg_.lo();				\
270 			::tcu::Interval&	VAR		= VAR##_lo_;					\
271 			BODY;														\
272 		}																\
273 		{																\
274 			const double		PARAM	= VAR##_arg_.hi();				\
275 			::tcu::Interval&	VAR		= VAR##_hi_;					\
276 			BODY;														\
277 		}																\
278 		VAR##_dst_ = VAR##_lo_ | VAR##_hi_;								\
279 	}																	\
280 	if (VAR##_arg_.hasNaN())											\
281 		VAR##_dst_ |= TCU_NAN;											\
282 } while (::deGetFalse())
283 
284 #define TCU_INTERVAL_APPLY_MONOTONE2(DST, P0, A0, P1, A1, VAR, BODY)	\
285 	TCU_INTERVAL_APPLY_MONOTONE1(										\
286 		DST, P0, A0, tmp2_,												\
287 		TCU_INTERVAL_APPLY_MONOTONE1(tmp2_, P1, A1, VAR, BODY))
288 
289 #define TCU_INTERVAL_APPLY_MONOTONE3(DST, P0, A0, P1, A1, P2, A2, VAR, BODY) \
290 	TCU_INTERVAL_APPLY_MONOTONE1(										\
291 		DST, P0, A0, tmp3_,												\
292 		TCU_INTERVAL_APPLY_MONOTONE2(tmp3_, P1, A1, P2, A2, VAR, BODY))
293 
294 typedef double		DoubleFunc1			(double);
295 typedef double		DoubleFunc2			(double, double);
296 typedef double		DoubleFunc3			(double, double, double);
297 typedef Interval	DoubleIntervalFunc1	(double);
298 typedef Interval	DoubleIntervalFunc2	(double, double);
299 typedef Interval	DoubleIntervalFunc3	(double, double, double);
300 
301 Interval	applyMonotone	(DoubleFunc1&			func,
302 							 const Interval&		arg0);
303 Interval	applyMonotone	(DoubleFunc2&			func,
304 							 const Interval&		arg0,
305 							 const Interval&		arg1);
306 Interval	applyMonotone	(DoubleIntervalFunc1&	func,
307 							 const Interval&		arg0);
308 Interval	applyMonotone	(DoubleIntervalFunc2&	func,
309 							 const Interval&		arg0,
310 							 const Interval&		arg1);
311 
312 
313 } // tcu
314 
315 #endif // _TCUINTERVAL_HPP
316