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