My Project
Loading...
Searching...
No Matches
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#if !LIMITS_HAS_QUAD
49// provide the numeric limits for the quad precision type
50template <>
51class numeric_limits<quad>
52{
53public:
54 static constexpr bool is_specialized = true;
55
56 static constexpr quad min() throw()
57 { return FLT128_MIN; }
58 static constexpr quad max() throw()
59 { return FLT128_MAX; }
60
61 // number of bits in mantissa
62 static constexpr int digits = FLT128_MANT_DIG;
63 // number of decimal digits
64 static constexpr int digits10 = FLT128_DIG;
65 static constexpr bool is_signed = true;
66 static constexpr bool is_integer = false;
67 static constexpr bool is_exact = false;
68 static constexpr int radix = 0;
69 static constexpr quad epsilon() throw()
70 { return FLT128_EPSILON; }
71 static constexpr quad round_error() throw()
72 { return 0.5; }
73
74 static constexpr int min_exponent = FLT128_MIN_EXP;
75 static constexpr int min_exponent10 = FLT128_MIN_10_EXP;
76 static constexpr int max_exponent = FLT128_MAX_EXP;
77 static constexpr int max_exponent10 = FLT128_MAX_10_EXP;
78
79 static constexpr bool has_infinity = true;
80 static constexpr bool has_quiet_NaN = true;
81 static constexpr bool has_signaling_NaN = true;
82 static constexpr float_denorm_style has_denorm = denorm_present;
83 static constexpr bool has_denorm_loss = false;
84 static constexpr quad infinity() throw()
85 { return __builtin_huge_valq(); }
86 static constexpr quad quiet_NaN() throw()
87 { return __builtin_nan(""); }
88 static constexpr quad signaling_NaN() throw()
89 { return __builtin_nans(""); }
90 static constexpr quad denorm_min() throw()
91 { return FLT128_DENORM_MIN; }
92
93 static constexpr bool is_iec559 = true;
94 static constexpr bool is_bounded = true;
95 static constexpr bool is_modulo = false;
96
97 static constexpr bool traps = std::numeric_limits<double>::traps;
98 static constexpr bool tinyness_before = std::numeric_limits<double>::tinyness_before;
99 static constexpr float_round_style round_style = round_to_nearest;
100};
101#endif // LIMITS_HAS_QUAD
102
103// provide some type traits for the quadruple precision type
104template <>
105struct is_floating_point<quad>
106 : public integral_constant<bool, true>
107{};
108
109template <>
110struct is_arithmetic<quad>
111 : public integral_constant<bool, true>
112{};
113
114template <>
115struct is_fundamental<quad>
116 : public integral_constant<bool, true>
117{};
118
119template <>
120struct is_scalar<quad>
121 : public integral_constant<bool, true>
122{};
123
124template <>
125struct is_pod<quad>
126 : public integral_constant<bool, true>
127{};
128
129template <>
130struct is_signed<quad>
131 : public integral_constant<bool, true>
132{};
133
134
135template <>
136struct is_standard_layout<quad>
137 : public integral_constant<bool, true>
138{};
139
140template <>
141struct is_trivial<quad>
142 : public integral_constant<bool, true>
143{};
144
145/*
146template <>
147struct is_trivially_copyable<quad>
148 : public integral_constant<bool, true>
149{};
150*/
151
152template <class OtherType>
153struct is_assignable<quad, OtherType>
154 : public integral_constant<bool, is_arithmetic<OtherType>::value>
155{};
156
157template <class OtherType>
158struct is_nothrow_assignable<quad, OtherType>
159 : public is_assignable<quad, OtherType>
160{};
161
162/*
163template <class OtherType>
164struct is_trivially_assignable<quad, OtherType>
165 : public integral_constant<bool, is_arithmetic<OtherType>::value>
166{};
167*/
168
169template <>
170struct is_copy_assignable<quad>
171 : public integral_constant<bool, true>
172{};
173
174template <>
175struct is_nothrow_copy_assignable<quad>
176 : public integral_constant<bool, true>
177{};
178
179template <>
180struct is_move_assignable<quad>
181 : public integral_constant<bool, true>
182{};
183
184template <>
185struct is_nothrow_move_assignable<quad>
186 : public integral_constant<bool, true>
187{};
188
189template <>
190struct is_constructible<quad>
191 : public integral_constant<bool, true>
192{};
193
194template <>
195struct is_nothrow_constructible<quad>
196 : public integral_constant<bool, true>
197{};
198
199template <>
200struct is_default_constructible<quad>
201 : public integral_constant<bool, true>
202{};
203
204template <>
205struct is_nothrow_default_constructible<quad>
206 : public integral_constant<bool, true>
207{};
208
209/*
210template <>
211struct is_trivially_default_constructible<quad>
212 : public integral_constant<bool, true>
213{};
214*/
215
216template <>
217struct is_copy_constructible<quad>
218 : public integral_constant<bool, true>
219{};
220
221template <>
222struct is_move_constructible<quad>
223 : public integral_constant<bool, true>
224{};
225
226template <>
227struct is_nothrow_move_constructible<quad>
228 : public integral_constant<bool, true>
229{};
230
231
232template <>
233struct is_destructible<quad>
234 : public integral_constant<bool, true>
235{};
236
237template <>
238struct is_nothrow_destructible<quad>
239 : public integral_constant<bool, true>
240{};
241
242template <class OtherType>
243struct is_convertible<quad, OtherType>
244 : public is_arithmetic<OtherType>
245{ };
246
247inline std::ostream& operator<<(std::ostream& os, const quad& val)
248{
249 if (os.precision() > std::numeric_limits<double>::digits10)
250 throw std::runtime_error("The precision requested for output cannot "
251 "be represented by a double precision floating "
252 "point object");
253
254 return os << static_cast<double>(val);
255}
256
257inline std::istream& operator>>(std::istream& is, quad& val)
258{
259 double tmp;
260 std::istream& ret = (is >> tmp);
261 val = tmp;
262 return ret;
263}
264
265inline quad real(quad val)
266{ return val; }
267
268inline quad real(const std::complex<quad>& val)
269{ return val.real(); }
270
271inline quad imag(quad)
272{ return 0.0; }
273
274inline quad imag(const std::complex<quad>& val)
275{ return val.imag(); }
276
277inline quad abs(quad val)
278{ return (val < 0) ? -val : val; }
279
280inline quad floor(quad val)
281{ return floorq(val); }
282
283inline quad ceil(quad val)
284{ return ceilq(val); }
285
286inline quad max(quad a, quad b)
287{ return (a > b) ? a : b; }
288
289inline quad min(quad a, quad b)
290{ return (a < b) ? a : b; }
291
292inline quad sqrt(quad val)
293{ return sqrtq(val); }
294
295template <class ExpType>
296inline quad pow(quad base, ExpType exp)
297{ return powq(base, static_cast<quad>(exp)); }
298
299template <class BaseType>
300inline quad pow(BaseType base, quad exp)
301{ return powq(static_cast<quad>(base), exp); }
302
303inline quad pow(quad base, quad exp)
304{ return powq(base, exp); }
305
306inline quad exp(quad val)
307{ return expq(val); }
308
309inline quad log(quad val)
310{ return logq(val); }
311
312inline quad sin(quad val)
313{ return sinq(val); }
314
315inline quad cos(quad val)
316{ return cosq(val); }
317
318inline quad tan(quad val)
319{ return tanq(val); }
320
321inline quad atan(quad val)
322{ return atanq(val); }
323
324inline quad atan2(quad a, quad b)
325{ return atan2q(a, b); }
326
327inline quad round(quad val)
328{ return roundq(val); }
329
330inline bool isfinite(quad val)
331{ return finiteq(val); }
332
333inline bool isnan(quad val)
334{ return isnanq(val); }
335
336inline bool isinf(quad val)
337{ return isinfq(val); }
338
339} // namespace std
340
341#if HAVE_DUNE_COMMON
342
343// specialize Dune::className for __float128 since it former does not work properly with
344// __float128 (this is mainly the fault of GCC/libstdc++)
345#include <dune/common/classname.hh>
346
347namespace Dune {
348template <>
349inline std::string className<__float128>()
350{ return "quad"; }
351} // namespace Dune
352
353#endif // HAVE_DUNE_COMMON
354
355#if HAVE_DUNE_FEM
356#include <dune/fem/io/streams/streams_inline.hh>
357
358namespace Dune {
359namespace Fem {
360template <class Traits>
361inline OutStreamInterface<Traits>&
362operator<<(OutStreamInterface<Traits>& out, quad value)
363{
364 out.writeDouble(static_cast<double>(value));
365 return out;
366}
367
368template <class Traits>
369inline InStreamInterface<Traits>&
370operator>>(InStreamInterface<Traits>& in, quad& value)
371{
372 double tmp;
373 in.readDouble(tmp);
374 value = tmp;
375 return in;
376}
377
378}} // namespace Dune, Fem
379#endif // HAVE_DUNE_FEM
380
381#endif // OPM_COMMON_QUAD_HPP