1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Interval arithmetic.
22 *//*--------------------------------------------------------------------*/
24 #include "tcuInterval.hpp"
35 Interval applyMonotone (DoubleFunc1& func, const Interval& arg0)
38 TCU_INTERVAL_APPLY_MONOTONE1(ret, x, arg0, val,
39 TCU_SET_INTERVAL(val, point, point = func(x)));
43 Interval applyMonotone (DoubleIntervalFunc1& func, const Interval& arg0)
45 return Interval(func(arg0.lo()), func(arg0.hi()));
48 Interval applyMonotone (DoubleFunc2& func, const Interval& arg0, const Interval& arg1)
52 TCU_INTERVAL_APPLY_MONOTONE2(ret, x, arg0, y, arg1, val,
53 TCU_SET_INTERVAL(val, point, point = func(x, y)));
58 Interval applyMonotone (DoubleIntervalFunc2& func, const Interval& arg0, const Interval& arg1)
60 double lo0 = arg0.lo(), hi0 = arg0.hi(), lo1 = arg1.lo(), hi1 = arg1.hi();
61 return Interval(Interval(func(lo0, lo1), func(lo0, hi1)),
62 Interval(func(hi0, lo1), func(hi0, hi1)));
65 Interval operator+ (const Interval& x, const Interval& y)
69 if (!x.empty() && !y.empty())
70 TCU_SET_INTERVAL_BOUNDS(ret, p, p = x.lo() + y.lo(), p = x.hi() + y.hi());
71 if (x.hasNaN() || y.hasNaN())
77 Interval operator- (const Interval& x, const Interval& y)
81 TCU_INTERVAL_APPLY_MONOTONE2(ret, xp, x, yp, y, val,
82 TCU_SET_INTERVAL(val, point, point = xp - yp));
86 Interval operator* (const Interval& x, const Interval& y)
90 TCU_INTERVAL_APPLY_MONOTONE2(ret, xp, x, yp, y, val,
91 TCU_SET_INTERVAL(val, point, point = xp * yp));
95 Interval operator/ (const Interval& nom, const Interval& den)
97 if (den.contains(0.0))
99 // \todo [2014-03-21 lauri] Non-inf endpoint when one den endpoint is
100 // zero and nom doesn't cross zero?
101 return Interval::unbounded();
107 TCU_INTERVAL_APPLY_MONOTONE2(ret, nomp, nom, denp, den, val,
108 TCU_SET_INTERVAL(val, point, point = nomp / denp));
113 static double negate (double x)
118 Interval operator- (const Interval& x)
120 return applyMonotone(negate, x);
123 Interval exp2 (const Interval& x)
125 return applyMonotone(std::pow, 2.0, x);
128 Interval exp (const Interval& x)
130 return applyMonotone(std::exp, x);
133 Interval sqrt (const Interval& x)
135 return applyMonotone(std::sqrt, x);
138 Interval inverseSqrt (const Interval& x)
140 return 1.0 / sqrt(x);
143 Interval abs (const Interval& x)
145 const Interval mono = applyMonotone(std::abs, x);
148 return Interval(0.0, mono);
153 std::ostream& operator<< (std::ostream& os, const Interval& interval)
155 if (interval.empty())
156 if (interval.hasNaN())
161 os << (interval.hasNaN() ? "~" : "")
162 << "[" << interval.lo() << ", " << interval.hi() << "]";