dune-common  2.7.1
quadmath.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_QUADMATH_HH
4 #define DUNE_QUADMATH_HH
5 
6 #if HAVE_QUADMATH
7 #include <quadmath.h>
8 
9 #include <cmath>
10 #include <cstddef>
11 #include <cstdint>
12 #include <cstdlib> // abs
13 #include <istream>
14 #include <ostream>
15 #include <type_traits>
16 #include <utility>
17 
20 
21 namespace Dune
22 {
23  namespace Impl
24  {
25  // forward declaration
26  class Float128;
27 
28  } // end namespace Impl
29 
30  using Impl::Float128;
31 
32  // The purpose of this namespace is to move the `<cmath>` function overloads
33  // out of namespace `Dune`, see AlignedNumber in debugalign.hh.
34  namespace Impl
35  {
36  using float128_t = __float128;
37 
39  class Float128
40  {
41  float128_t value_ = 0.0q;
42 
43  public:
44  constexpr Float128() = default;
45  constexpr Float128(const float128_t& value) noexcept
46  : value_(value)
47  {}
48 
49  // constructor from any floating-point or integer type
50  template <class T,
51  std::enable_if_t<std::is_arithmetic<T>::value, int> = 0>
52  constexpr Float128(const T& value) noexcept
53  : value_(value)
54  {}
55 
56  // constructor from pointer to null-terminated byte string
57  explicit Float128(const char* str) noexcept
58  : value_(strtoflt128(str, NULL))
59  {}
60 
61  // accessors
62  constexpr operator float128_t() const noexcept { return value_; }
63 
64  constexpr float128_t const& value() const noexcept { return value_; }
65  constexpr float128_t& value() noexcept { return value_; }
66 
67  // I/O
68  template<class CharT, class Traits>
69  friend std::basic_istream<CharT, Traits>&
70  operator>>(std::basic_istream<CharT, Traits>& in, Float128& x)
71  {
72  std::string buf;
73  buf.reserve(128);
74  in >> buf;
75  x.value() = strtoflt128(buf.c_str(), NULL);
76  return in;
77  }
78 
79  template<class CharT, class Traits>
80  friend std::basic_ostream<CharT, Traits>&
81  operator<<(std::basic_ostream<CharT, Traits>& out, const Float128& x)
82  {
83  const std::size_t bufSize = 128;
84  CharT buf[128];
85 
86  std::string format = "%." + std::to_string(out.precision()) + "Q" +
87  ((out.flags() | std::ios_base::scientific) ? "e" : "f");
88  const int numChars = quadmath_snprintf(buf, bufSize, format.c_str(), x.value());
89  if (std::size_t(numChars) >= bufSize) {
90  DUNE_THROW(Dune::RangeError, "Failed to print Float128 value: buffer overflow");
91  }
92  out << buf;
93  return out;
94  }
95 
96  // Increment, decrement
97  constexpr Float128& operator++() noexcept { ++value_; return *this; }
98  constexpr Float128& operator--() noexcept { --value_; return *this; }
99 
100  constexpr Float128 operator++(int) noexcept { Float128 tmp{*this}; ++value_; return tmp; }
101  constexpr Float128 operator--(int) noexcept { Float128 tmp{*this}; --value_; return tmp; }
102 
103  // unary operators
104  constexpr Float128 operator+() const noexcept { return Float128{+value_}; }
105  constexpr Float128 operator-() const noexcept { return Float128{-value_}; }
106 
107  // assignment operators
108 #define DUNE_ASSIGN_OP(OP) \
109  constexpr Float128& operator OP(const Float128& u) noexcept \
110  { \
111  value_ OP float128_t(u); \
112  return *this; \
113  } \
114  static_assert(true, "Require semicolon to unconfuse editors")
115 
116  DUNE_ASSIGN_OP(+=);
117  DUNE_ASSIGN_OP(-=);
118 
119  DUNE_ASSIGN_OP(*=);
120  DUNE_ASSIGN_OP(/=);
121 
122 #undef DUNE_ASSIGN_OP
123 
124  }; // end class Float128
125 
126  // binary operators:
127  // For symmetry provide overloads with arithmetic types
128  // in the first or second argument.
129 #define DUNE_BINARY_OP(OP) \
130  constexpr Float128 operator OP(const Float128& t, \
131  const Float128& u) noexcept \
132  { \
133  return Float128{float128_t(t) OP float128_t(u)}; \
134  } \
135  template <class T, \
136  std::enable_if_t<std::is_arithmetic<T>::value, int> = 0> \
137  constexpr Float128 operator OP(const T& t, \
138  const Float128& u) noexcept \
139  { \
140  return Float128{float128_t(t) OP float128_t(u)}; \
141  } \
142  template <class U, \
143  std::enable_if_t<std::is_arithmetic<U>::value, int> = 0> \
144  constexpr Float128 operator OP(const Float128& t, \
145  const U& u) noexcept \
146  { \
147  return Float128{float128_t(t) OP float128_t(u)}; \
148  } \
149  static_assert(true, "Require semicolon to unconfuse editors")
150 
151  DUNE_BINARY_OP(+);
152  DUNE_BINARY_OP(-);
153  DUNE_BINARY_OP(*);
154  DUNE_BINARY_OP(/);
155 
156 #undef DUNE_BINARY_OP
157 
158  // logical operators:
159  // For symmetry provide overloads with arithmetic types
160  // in the first or second argument.
161 #define DUNE_BINARY_BOOL_OP(OP) \
162  constexpr bool operator OP(const Float128& t, \
163  const Float128& u) noexcept \
164  { \
165  return float128_t(t) OP float128_t(u); \
166  } \
167  template <class T, \
168  std::enable_if_t<std::is_arithmetic<T>::value, int> = 0> \
169  constexpr bool operator OP(const T& t, \
170  const Float128& u) noexcept \
171  { \
172  return float128_t(t) OP float128_t(u); \
173  } \
174  template <class U, \
175  std::enable_if_t<std::is_arithmetic<U>::value, int> = 0> \
176  constexpr bool operator OP(const Float128& t, \
177  const U& u) noexcept \
178  { \
179  return float128_t(t) OP float128_t(u); \
180  } \
181  static_assert(true, "Require semicolon to unconfuse editors")
182 
183  DUNE_BINARY_BOOL_OP(==);
184  DUNE_BINARY_BOOL_OP(!=);
185  DUNE_BINARY_BOOL_OP(<);
186  DUNE_BINARY_BOOL_OP(>);
187  DUNE_BINARY_BOOL_OP(<=);
188  DUNE_BINARY_BOOL_OP(>=);
189 
190 #undef DUNE_BINARY_BOOL_OP
191 
192  // Overloads for the cmath functions
193 
194  // function with name `name` redirects to quadmath function `func`
195 #define DUNE_UNARY_FUNC(name,func) \
196  inline Float128 name(const Float128& u) noexcept \
197  { \
198  return Float128{func (float128_t(u))}; \
199  } \
200  static_assert(true, "Require semicolon to unconfuse editors")
201 
202  // like DUNE_UNARY_FUNC but with cutom return type
203 #define DUNE_CUSTOM_UNARY_FUNC(type,name,func) \
204  inline type name(const Float128& u) noexcept \
205  { \
206  return (type)(func (float128_t(u))); \
207  } \
208  static_assert(true, "Require semicolon to unconfuse editors")
209 
210  // redirects to quadmath function with two arguments
211 #define DUNE_BINARY_FUNC(name,func) \
212  inline Float128 name(const Float128& t, \
213  const Float128& u) noexcept \
214  { \
215  return Float128{func (float128_t(t), float128_t(u))}; \
216  } \
217  static_assert(true, "Require semicolon to unconfuse editors")
218 
219  DUNE_UNARY_FUNC(abs, fabsq);
220  DUNE_UNARY_FUNC(acos, acosq);
221  DUNE_UNARY_FUNC(acosh, acoshq);
222  DUNE_UNARY_FUNC(asin, asinq);
223  DUNE_UNARY_FUNC(asinh, asinhq);
224  DUNE_UNARY_FUNC(atan, atanq);
225  DUNE_UNARY_FUNC(atanh, atanhq);
226  DUNE_UNARY_FUNC(cbrt, cbrtq);
227  DUNE_UNARY_FUNC(ceil, ceilq);
228  DUNE_UNARY_FUNC(cos, cosq);
229  DUNE_UNARY_FUNC(cosh, coshq);
230  DUNE_UNARY_FUNC(erf, erfq);
231  DUNE_UNARY_FUNC(erfc, erfcq);
232  DUNE_UNARY_FUNC(exp, expq);
233  DUNE_UNARY_FUNC(expm1, expm1q);
234  DUNE_UNARY_FUNC(fabs, fabsq);
235  DUNE_UNARY_FUNC(floor, floorq);
236  DUNE_CUSTOM_UNARY_FUNC(int, ilogb, ilogbq);
237  DUNE_UNARY_FUNC(lgamma, lgammaq);
238  DUNE_CUSTOM_UNARY_FUNC(long long int, llrint, llrintq);
239  DUNE_CUSTOM_UNARY_FUNC(long long int, llround, llroundq);
240  DUNE_UNARY_FUNC(log, logq);
241  DUNE_UNARY_FUNC(log10, log10q);
242  DUNE_UNARY_FUNC(log1p, log1pq);
243  DUNE_UNARY_FUNC(log2, log2q);
244  // DUNE_UNARY_FUNC(logb, logbq); // not available in gcc5
245  DUNE_CUSTOM_UNARY_FUNC(long int, lrint, lrintq);
246  DUNE_CUSTOM_UNARY_FUNC(long int, lround, lroundq);
247  DUNE_UNARY_FUNC(nearbyint, nearbyintq);
248  DUNE_BINARY_FUNC(nextafter, nextafterq);
249  DUNE_BINARY_FUNC(pow, powq); // overload for integer argument see below
250  DUNE_UNARY_FUNC(rint, rintq);
251  DUNE_UNARY_FUNC(round, roundq);
252  DUNE_UNARY_FUNC(sin, sinq);
253  DUNE_UNARY_FUNC(sinh, sinhq);
254  DUNE_UNARY_FUNC(sqrt, sqrtq);
255  DUNE_UNARY_FUNC(tan, tanq);
256  DUNE_UNARY_FUNC(tanh, tanhq);
257  DUNE_UNARY_FUNC(tgamma, tgammaq);
258  DUNE_UNARY_FUNC(trunc, truncq);
259 
260  DUNE_CUSTOM_UNARY_FUNC(bool, isfinite, finiteq);
261  DUNE_CUSTOM_UNARY_FUNC(bool, isinf, isinfq);
262  DUNE_CUSTOM_UNARY_FUNC(bool, isnan, isnanq);
263  DUNE_CUSTOM_UNARY_FUNC(bool, signbit, signbitq);
264 
265 #undef DUNE_UNARY_FUNC
266 #undef DUNE_CUSTOM_UNARY_FUNC
267 #undef DUNE_BINARY_FUNC
268 
269  // like DUNE_BINARY_FUNC but provide overloads with arithmetic
270  // types in the first or second argument.
271 #define DUNE_BINARY_ARITHMETIC_FUNC(name,func) \
272  inline Float128 name(const Float128& t, \
273  const Float128& u) noexcept \
274  { \
275  return Float128{func (float128_t(t), float128_t(u))}; \
276  } \
277  template <class T, \
278  std::enable_if_t<std::is_arithmetic<T>::value, int> = 0> \
279  inline Float128 name(const T& t, \
280  const Float128& u) noexcept \
281  { \
282  return Float128{func (float128_t(t), float128_t(u))}; \
283  } \
284  template <class U, \
285  std::enable_if_t<std::is_arithmetic<U>::value, int> = 0> \
286  inline Float128 name(const Float128& t, \
287  const U& u) noexcept \
288  { \
289  return Float128{func (float128_t(t), float128_t(u))}; \
290  } \
291  static_assert(true, "Require semicolon to unconfuse editors")
292 
293  DUNE_BINARY_ARITHMETIC_FUNC(atan2,atan2q);
294  DUNE_BINARY_ARITHMETIC_FUNC(copysign,copysignq);
295  DUNE_BINARY_ARITHMETIC_FUNC(fdim,fdimq);
296  DUNE_BINARY_ARITHMETIC_FUNC(fmax,fmaxq);
297  DUNE_BINARY_ARITHMETIC_FUNC(fmin,fminq);
298  DUNE_BINARY_ARITHMETIC_FUNC(fmod,fmodq);
299  DUNE_BINARY_ARITHMETIC_FUNC(hypot,hypotq);
300  DUNE_BINARY_ARITHMETIC_FUNC(remainder,remainderq);
301 
302 #undef DUNE_BINARY_ARITHMETIC_FUNC
303 
304  // some more cmath functions with special signature
305 
306  inline Float128 fma(const Float128& t, const Float128& u, const Float128& v)
307  {
308  return Float128{fmaq(float128_t(t),float128_t(u),float128_t(v))};
309  }
310 
311  inline Float128 frexp(const Float128& u, int* p)
312  {
313  return Float128{frexpq(float128_t(u), p)};
314  }
315 
316  inline Float128 ldexp(const Float128& u, int p)
317  {
318  return Float128{ldexpq(float128_t(u), p)};
319  }
320 
321  inline Float128 remquo(const Float128& t, const Float128& u, int* quo)
322  {
323  return Float128{remquoq(float128_t(t), float128_t(u), quo)};
324  }
325 
326  inline Float128 scalbln(const Float128& u, long int e)
327  {
328  return Float128{scalblnq(float128_t(u), e)};
329  }
330 
331  inline Float128 scalbn(const Float128& u, int e)
332  {
333  return Float128{scalbnq(float128_t(u), e)};
334  }
335 
337  // NOTE: This is much faster than a pow(x, Float128(p)) call
338  // NOTE: This is a modified version of boost::math::cstdfloat::detail::pown
339  // (adapted to the type Float128) that is part of the Boost 1.65 Math toolkit 2.8.0
340  // and is implemented by Christopher Kormanyos, John Maddock, and Paul A. Bristow,
341  // distributed under the Boost Software License, Version 1.0
342  // (See http://www.boost.org/LICENSE_1_0.txt)
343  template <class Int,
344  std::enable_if_t<std::is_integral<Int>::value, int> = 0>
345  inline Float128 pow(const Float128& x, const Int p)
346  {
347  static const Float128 max_value = FLT128_MAX;
348  static const Float128 min_value = FLT128_MIN;
349  static const Float128 inf_value = float128_t{1} / float128_t{0};
350 
351  const bool isneg = (x < 0);
352  const bool isnan = (x != x);
353  const bool isinf = (isneg ? bool(-x > max_value) : bool(+x > max_value));
354 
355  if (isnan) { return x; }
356  if (isinf) { return Float128{nanq("")}; }
357 
358  const Float128 abs_x = (isneg ? -x : x);
359  if (p < Int(0)) {
360  if (abs_x < min_value)
361  return (isneg ? -inf_value : +inf_value);
362  else
363  return Float128(1) / pow(x, Int(-p));
364  }
365 
366  if (p == Int(0)) { return Float128(1); }
367  if (p == Int(1)) { return x; }
368  if (abs_x > max_value)
369  return (isneg ? -inf_value : +inf_value);
370 
371  if (p == Int(2)) { return (x * x); }
372  if (p == Int(3)) { return ((x * x) * x); }
373  if (p == Int(4)) { const Float128 x2 = (x * x); return (x2 * x2); }
374 
375  Float128 result = ((p % Int(2)) != Int(0)) ? x : Float128(1);
376  Float128 xn = x; // binary powers of x
377 
378  Int p2 = p;
379  while (Int(p2 /= 2) != Int(0)) {
380  xn *= xn; // Square xn for each binary power
381 
382  const bool has_binary_power = (Int(p2 % Int(2)) != Int(0));
383  if (has_binary_power)
384  result *= xn;
385  }
386 
387  return result;
388  }
389 
390 
391  } // end namespace Impl
392 
393  template <>
394  struct IsNumber<Impl::Float128>
395  : public std::true_type {};
396 
397 } // end namespace Dune
398 
399 namespace std
400 {
401 #ifndef NO_STD_NUMERIC_LIMITS_SPECIALIZATION
402  template <>
403  class numeric_limits<Dune::Impl::Float128>
404  {
405  using Float128 = Dune::Impl::Float128;
406  using float128_t = Dune::Impl::float128_t;
407 
408  public:
409  static constexpr bool is_specialized = true;
410  static constexpr Float128 min() noexcept { return FLT128_MIN; }
411  static constexpr Float128 max() noexcept { return FLT128_MAX; }
412  static constexpr Float128 lowest() noexcept { return -FLT128_MAX; }
413  static constexpr int digits = FLT128_MANT_DIG;
414  static constexpr int digits10 = 34;
415  static constexpr int max_digits10 = 36;
416  static constexpr bool is_signed = true;
417  static constexpr bool is_integer = false;
418  static constexpr bool is_exact = false;
419  static constexpr int radix = 2;
420  static constexpr Float128 epsilon() noexcept { return FLT128_EPSILON; }
421  static constexpr Float128 round_error() noexcept { return float128_t{0.5}; }
422  static constexpr int min_exponent = FLT128_MIN_EXP;
423  static constexpr int min_exponent10 = FLT128_MIN_10_EXP;
424  static constexpr int max_exponent = FLT128_MAX_EXP;
425  static constexpr int max_exponent10 = FLT128_MAX_10_EXP;
426  static constexpr bool has_infinity = true;
427  static constexpr bool has_quiet_NaN = true;
428  static constexpr bool has_signaling_NaN = false;
429  static constexpr float_denorm_style has_denorm = denorm_present;
430  static constexpr bool has_denorm_loss = false;
431  static constexpr Float128 infinity() noexcept { return float128_t{1}/float128_t{0}; }
432  static Float128 quiet_NaN() noexcept { return nanq(""); }
433  static constexpr Float128 signaling_NaN() noexcept { return float128_t{}; }
434  static constexpr Float128 denorm_min() noexcept { return FLT128_DENORM_MIN; }
435  static constexpr bool is_iec559 = true;
436  static constexpr bool is_bounded = false;
437  static constexpr bool is_modulo = false;
438  static constexpr bool traps = false;
439  static constexpr bool tinyness_before = false;
440  static constexpr float_round_style round_style = round_to_nearest;
441  };
442 #endif
443 } // end namespace std
444 
445 #endif // HAVE_QUADMATH
446 #endif // DUNE_QUADMATH_HH
#define DUNE_BINARY_OP(OP)
Definition: debugalign.hh:235
#define DUNE_UNARY_FUNC(name)
#define DUNE_ASSIGN_OP(OP)
Definition: debugalign.hh:194
A few common exception classes.
Traits for type conversions and type information.
Stream & operator>>(Stream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition: streamoperators.hh:41
std::ostream & operator<<(std::ostream &s, const bigunsignedint< k > &x)
Definition: bigunsignedint.hh:273
bigunsignedint< k > operator+(const bigunsignedint< k > &x, std::uintmax_t y)
Definition: bigunsignedint.hh:530
bigunsignedint< k > operator-(const bigunsignedint< k > &x, std::uintmax_t y)
Definition: bigunsignedint.hh:537
I round(const T &val, typename EpsilonType< T >::Type epsilon)
round using epsilon
Definition: float_cmp.cc:309
I trunc(const T &val, typename EpsilonType< T >::Type epsilon)
truncate using epsilon
Definition: float_cmp.cc:405
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignedallocator.hh:14
T max_value(const AlignedNumber< T, align > &val)
Definition: debugalign.hh:468
T min_value(const AlignedNumber< T, align > &val)
Definition: debugalign.hh:474
auto min(const AlignedNumber< T, align > &a, const AlignedNumber< T, align > &b)
Definition: debugalign.hh:434
auto max(const AlignedNumber< T, align > &a, const AlignedNumber< T, align > &b)
Definition: debugalign.hh:412
Default exception class for range errors.
Definition: exceptions.hh:252