My Project
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 
40 extern "C" {
41 #include <quadmath.h>
42 }
43 
44 typedef __float128 quad;
45 
46 namespace std {
47 
48 // provide the numeric limits for the quad precision type
49 template <>
50 class numeric_limits<quad>
51 {
52 public:
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
102 template <>
103 struct is_floating_point<quad>
104  : public integral_constant<bool, true>
105 {};
106 
107 template <>
108 struct is_arithmetic<quad>
109  : public integral_constant<bool, true>
110 {};
111 
112 template <>
113 struct is_fundamental<quad>
114  : public integral_constant<bool, true>
115 {};
116 
117 template <>
118 struct is_scalar<quad>
119  : public integral_constant<bool, true>
120 {};
121 
122 template <>
123 struct is_pod<quad>
124  : public integral_constant<bool, true>
125 {};
126 
127 template <>
128 struct is_signed<quad>
129  : public integral_constant<bool, true>
130 {};
131 
132 
133 template <>
134 struct is_standard_layout<quad>
135  : public integral_constant<bool, true>
136 {};
137 
138 template <>
139 struct is_trivial<quad>
140  : public integral_constant<bool, true>
141 {};
142 
143 /*
144 template <>
145 struct is_trivially_copyable<quad>
146  : public integral_constant<bool, true>
147 {};
148 */
149 
150 template <class OtherType>
151 struct is_assignable<quad, OtherType>
152  : public integral_constant<bool, is_arithmetic<OtherType>::value>
153 {};
154 
155 template <class OtherType>
156 struct is_nothrow_assignable<quad, OtherType>
157  : public is_assignable<quad, OtherType>
158 {};
159 
160 /*
161 template <class OtherType>
162 struct is_trivially_assignable<quad, OtherType>
163  : public integral_constant<bool, is_arithmetic<OtherType>::value>
164 {};
165 */
166 
167 template <>
168 struct is_copy_assignable<quad>
169  : public integral_constant<bool, true>
170 {};
171 
172 template <>
173 struct is_nothrow_copy_assignable<quad>
174  : public integral_constant<bool, true>
175 {};
176 
177 template <>
178 struct is_move_assignable<quad>
179  : public integral_constant<bool, true>
180 {};
181 
182 template <>
183 struct is_nothrow_move_assignable<quad>
184  : public integral_constant<bool, true>
185 {};
186 
187 template <>
188 struct is_constructible<quad>
189  : public integral_constant<bool, true>
190 {};
191 
192 template <>
193 struct is_nothrow_constructible<quad>
194  : public integral_constant<bool, true>
195 {};
196 
197 template <>
198 struct is_default_constructible<quad>
199  : public integral_constant<bool, true>
200 {};
201 
202 template <>
203 struct is_nothrow_default_constructible<quad>
204  : public integral_constant<bool, true>
205 {};
206 
207 /*
208 template <>
209 struct is_trivially_default_constructible<quad>
210  : public integral_constant<bool, true>
211 {};
212 */
213 
214 template <>
215 struct is_copy_constructible<quad>
216  : public integral_constant<bool, true>
217 {};
218 
219 template <>
220 struct is_move_constructible<quad>
221  : public integral_constant<bool, true>
222 {};
223 
224 template <>
225 struct is_nothrow_move_constructible<quad>
226  : public integral_constant<bool, true>
227 {};
228 
229 
230 template <>
231 struct is_destructible<quad>
232  : public integral_constant<bool, true>
233 {};
234 
235 template <>
236 struct is_nothrow_destructible<quad>
237  : public integral_constant<bool, true>
238 {};
239 
240 template <class OtherType>
241 struct is_convertible<quad, OtherType>
242  : public is_arithmetic<OtherType>
243 { };
244 
245 inline 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 
255 inline 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 
263 inline quad real(quad val)
264 { return val; }
265 
266 inline quad real(const std::complex<quad>& val)
267 { return val.real(); }
268 
269 inline quad imag(quad)
270 { return 0.0; }
271 
272 inline quad imag(const std::complex<quad>& val)
273 { return val.imag(); }
274 
275 inline quad abs(quad val)
276 { return (val < 0) ? -val : val; }
277 
278 inline quad floor(quad val)
279 { return floorq(val); }
280 
281 inline quad ceil(quad val)
282 { return ceilq(val); }
283 
284 inline quad max(quad a, quad b)
285 { return (a > b) ? a : b; }
286 
287 inline quad min(quad a, quad b)
288 { return (a < b) ? a : b; }
289 
290 inline quad sqrt(quad val)
291 { return sqrtq(val); }
292 
293 template <class ExpType>
294 inline quad pow(quad base, ExpType exp)
295 { return powq(base, static_cast<quad>(exp)); }
296 
297 template <class BaseType>
298 inline quad pow(BaseType base, quad exp)
299 { return powq(static_cast<quad>(base), exp); }
300 
301 inline quad pow(quad base, quad exp)
302 { return powq(base, exp); }
303 
304 inline quad exp(quad val)
305 { return expq(val); }
306 
307 inline quad log(quad val)
308 { return logq(val); }
309 
310 inline quad sin(quad val)
311 { return sinq(val); }
312 
313 inline quad cos(quad val)
314 { return cosq(val); }
315 
316 inline quad tan(quad val)
317 { return tanq(val); }
318 
319 inline quad atan(quad val)
320 { return atanq(val); }
321 
322 inline quad atan2(quad a, quad b)
323 { return atan2q(a, b); }
324 
325 inline quad round(quad val)
326 { return roundq(val); }
327 
328 inline bool isfinite(quad val)
329 { return finiteq(val); }
330 
331 inline bool isnan(quad val)
332 { return isnanq(val); }
333 
334 inline 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 
343 namespace Dune {
344 template <>
345 inline 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 
352 namespace Dune {
353 namespace Fem {
354 template <class Traits>
355 inline OutStreamInterface<Traits>&
356 operator<<(OutStreamInterface<Traits>& out, quad value)
357 {
358  out.writeDouble(static_cast<double>(value));
359  return out;
360 }
361 
362 template <class Traits>
363 inline InStreamInterface<Traits>&
364 operator>>(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