quad.hpp
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18
19 Consult the COPYING file in the top-level source directory of this
20 module for the precise wording of the license and the list of
21 copyright holders.
22*/
29#if !defined OPM_COMMON_QUAD_HPP && HAVE_QUAD
30#define OPM_COMMON_QUAD_HPP
31
32#include <cmath>
33#include <complex>
34#include <string>
35#include <stdexcept>
36#include <limits>
37#include <iostream>
38#include <type_traits>
39
40extern "C" {
41#include <quadmath.h>
42}
43
44typedef __float128 quad;
45
46namespace std {
47
48// provide the numeric limits for the quad precision type
49template <>
50class numeric_limits<quad>
51{
52public:
53 static constexpr bool is_specialized = true;
54
55 static constexpr quad min() throw()
56 { return FLT128_MIN; }
57 static constexpr quad max() throw()
58 { return FLT128_MAX; }
59
60 // number of bits in mantissa
61 static constexpr int digits = FLT128_MANT_DIG;
62 // number of decimal digits
63 static constexpr int digits10 = FLT128_DIG;
64 static constexpr bool is_signed = true;
65 static constexpr bool is_integer = false;
66 static constexpr bool is_exact = false;
67 static constexpr int radix = 0;
68 static constexpr quad epsilon() throw()
69 { return FLT128_EPSILON; }
70 static constexpr quad round_error() throw()
71 { return 0.5; }
72
73 static constexpr int min_exponent = FLT128_MIN_EXP;
74 static constexpr int min_exponent10 = FLT128_MIN_10_EXP;
75 static constexpr int max_exponent = FLT128_MAX_EXP;
76 static constexpr int max_exponent10 = FLT128_MAX_10_EXP;
77
78 static constexpr bool has_infinity = true;
79 static constexpr bool has_quiet_NaN = true;
80 static constexpr bool has_signaling_NaN = true;
81 static constexpr float_denorm_style has_denorm = denorm_present;
82 static constexpr bool has_denorm_loss = false;
83 static constexpr quad infinity() throw()
84 { return __builtin_huge_valq(); }
85 static constexpr quad quiet_NaN() throw()
86 { return __builtin_nan(""); }
87 static constexpr quad signaling_NaN() throw()
88 { return __builtin_nans(""); }
89 static constexpr quad denorm_min() throw()
90 { return FLT128_DENORM_MIN; }
91
92 static constexpr bool is_iec559 = true;
93 static constexpr bool is_bounded = true;
94 static constexpr bool is_modulo = false;
95
96 static constexpr bool traps = std::numeric_limits<double>::traps;
97 static constexpr bool tinyness_before = std::numeric_limits<double>::tinyness_before;
98 static constexpr float_round_style round_style = round_to_nearest;
99};
100
101// provide some type traits for the quadruple precision type
102template <>
103struct is_floating_point<quad>
104 : public integral_constant<bool, true>
105{};
106
107template <>
108struct is_arithmetic<quad>
109 : public integral_constant<bool, true>
110{};
111
112template <>
113struct is_fundamental<quad>
114 : public integral_constant<bool, true>
115{};
116
117template <>
118struct is_scalar<quad>
119 : public integral_constant<bool, true>
120{};
121
122template <>
123struct is_pod<quad>
124 : public integral_constant<bool, true>
125{};
126
127template <>
128struct is_signed<quad>
129 : public integral_constant<bool, true>
130{};
131
132
133template <>
134struct is_standard_layout<quad>
135 : public integral_constant<bool, true>
136{};
137
138template <>
139struct is_trivial<quad>
140 : public integral_constant<bool, true>
141{};
142
143/*
144template <>
145struct is_trivially_copyable<quad>
146 : public integral_constant<bool, true>
147{};
148*/
149
150template <class OtherType>
151struct is_assignable<quad, OtherType>
152 : public integral_constant<bool, is_arithmetic<OtherType>::value>
153{};
154
155template <class OtherType>
156struct is_nothrow_assignable<quad, OtherType>
157 : public is_assignable<quad, OtherType>
158{};
159
160/*
161template <class OtherType>
162struct is_trivially_assignable<quad, OtherType>
163 : public integral_constant<bool, is_arithmetic<OtherType>::value>
164{};
165*/
166
167template <>
168struct is_copy_assignable<quad>
169 : public integral_constant<bool, true>
170{};
171
172template <>
173struct is_nothrow_copy_assignable<quad>
174 : public integral_constant<bool, true>
175{};
176
177template <>
178struct is_move_assignable<quad>
179 : public integral_constant<bool, true>
180{};
181
182template <>
183struct is_nothrow_move_assignable<quad>
184 : public integral_constant<bool, true>
185{};
186
187template <>
188struct is_constructible<quad>
189 : public integral_constant<bool, true>
190{};
191
192template <>
193struct is_nothrow_constructible<quad>
194 : public integral_constant<bool, true>
195{};
196
197template <>
198struct is_default_constructible<quad>
199 : public integral_constant<bool, true>
200{};
201
202template <>
203struct is_nothrow_default_constructible<quad>
204 : public integral_constant<bool, true>
205{};
206
207/*
208template <>
209struct is_trivially_default_constructible<quad>
210 : public integral_constant<bool, true>
211{};
212*/
213
214template <>
215struct is_copy_constructible<quad>
216 : public integral_constant<bool, true>
217{};
218
219template <>
220struct is_move_constructible<quad>
221 : public integral_constant<bool, true>
222{};
223
224template <>
225struct is_nothrow_move_constructible<quad>
226 : public integral_constant<bool, true>
227{};
228
229
230template <>
231struct is_destructible<quad>
232 : public integral_constant<bool, true>
233{};
234
235template <>
236struct is_nothrow_destructible<quad>
237 : public integral_constant<bool, true>
238{};
239
240template <class OtherType>
241struct is_convertible<quad, OtherType>
242 : public is_arithmetic<OtherType>
243{ };
244
245inline std::ostream& operator<<(std::ostream& os, const quad& val)
246{
247 if (os.precision() > std::numeric_limits<double>::digits10)
248 throw std::runtime_error("The precision requested for output cannot "
249 "be represented by a double precision floating "
250 "point object");
251
252 return os << static_cast<double>(val);
253}
254
255inline std::istream& operator>>(std::istream& is, quad& val)
256{
257 double tmp;
258 std::istream& ret = (is >> tmp);
259 val = tmp;
260 return ret;
261}
262
263inline quad real(quad val)
264{ return val; }
265
266inline quad real(const std::complex<quad>& val)
267{ return val.real(); }
268
269inline quad imag(quad)
270{ return 0.0; }
271
272inline quad imag(const std::complex<quad>& val)
273{ return val.imag(); }
274
275inline quad abs(quad val)
276{ return (val < 0) ? -val : val; }
277
278inline quad floor(quad val)
279{ return floorq(val); }
280
281inline quad ceil(quad val)
282{ return ceilq(val); }
283
284inline quad max(quad a, quad b)
285{ return (a > b) ? a : b; }
286
287inline quad min(quad a, quad b)
288{ return (a < b) ? a : b; }
289
290inline quad sqrt(quad val)
291{ return sqrtq(val); }
292
293template <class ExpType>
294inline quad pow(quad base, ExpType exp)
295{ return powq(base, static_cast<quad>(exp)); }
296
297template <class BaseType>
298inline quad pow(BaseType base, quad exp)
299{ return powq(static_cast<quad>(base), exp); }
300
301inline quad pow(quad base, quad exp)
302{ return powq(base, exp); }
303
304inline quad exp(quad val)
305{ return expq(val); }
306
307inline quad log(quad val)
308{ return logq(val); }
309
310inline quad sin(quad val)
311{ return sinq(val); }
312
313inline quad cos(quad val)
314{ return cosq(val); }
315
316inline quad tan(quad val)
317{ return tanq(val); }
318
319inline quad atan(quad val)
320{ return atanq(val); }
321
322inline quad atan2(quad a, quad b)
323{ return atan2q(a, b); }
324
325inline quad round(quad val)
326{ return roundq(val); }
327
328inline bool isfinite(quad val)
329{ return finiteq(val); }
330
331inline bool isnan(quad val)
332{ return isnanq(val); }
333
334inline bool isinf(quad val)
335{ return isinfq(val); }
336
337} // namespace std
338
339// specialize Dune::className for __float128 since it former does not work properly with
340// __float128 (this is mainly the fault of GCC/libstdc++)
341#include <dune/common/classname.hh>
342
343namespace Dune {
344template <>
345inline std::string className<__float128>()
346{ return "quad"; }
347} // namespace Dune
348
349#if HAVE_DUNE_FEM
350#include <dune/fem/io/streams/streams_inline.hh>
351
352namespace Dune {
353namespace Fem {
354template <class Traits>
355inline OutStreamInterface<Traits>&
356operator<<(OutStreamInterface<Traits>& out, quad value)
357{
358 out.writeDouble(static_cast<double>(value));
359 return out;
360}
361
362template <class Traits>
363inline InStreamInterface<Traits>&
364operator>>(InStreamInterface<Traits>& in, quad& value)
365{
366 double tmp;
367 in.readDouble(tmp);
368 value = tmp;
369 return in;
370}
371
372}} // namespace Dune, Fem
373#endif // HAVE_DUNE_FEM
374
375#endif // OPM_COMMON_QUAD_HPP
std::ostream & operator<<(std::ostream &os, const Opm::TridiagonalMatrix< Scalar > &mat)
Definition: TridiagonalMatrix.hpp:849
Definition: Evaluation.hpp:620
Evaluation cos(const Evaluation &value)
Definition: MathToolbox.hpp:383
bool isfinite(const Evaluation &value)
Definition: MathToolbox.hpp:420
Evaluation sin(const Evaluation &value)
Definition: MathToolbox.hpp:367
Evaluation atan(const Evaluation &value)
Definition: MathToolbox.hpp:358
Evaluation exp(const Evaluation &value)
Definition: MathToolbox.hpp:403
Evaluation sqrt(const Evaluation &value)
Definition: MathToolbox.hpp:399
ReturnEval_< Evaluation1, Evaluation2 >::type min(const Evaluation1 &arg1, const Evaluation2 &arg2)
Definition: MathToolbox.hpp:346
bool isnan(const Evaluation &value)
Definition: MathToolbox.hpp:424
ReturnEval_< Evaluation1, Evaluation2 >::type max(const Evaluation1 &arg1, const Evaluation2 &arg2)
Definition: MathToolbox.hpp:341
Evaluation log(const Evaluation &value)
Definition: MathToolbox.hpp:407
Evaluation tan(const Evaluation &value)
Definition: MathToolbox.hpp:354
ReturnEval_< Evaluation1, Evaluation2 >::type atan2(const Evaluation1 &value1, const Evaluation2 &value2)
Definition: MathToolbox.hpp:363
Evaluation abs(const Evaluation &value)
Definition: MathToolbox.hpp:350
ReturnEval_< Evaluation1, Evaluation2 >::type pow(const Evaluation1 &base, const Evaluation2 &exp)
Definition: MathToolbox.hpp:416