5#ifndef DUNE_QUADMATH_HH
6#define DUNE_QUADMATH_HH
38 using float128_t = __float128;
43 float128_t value_ = 0.0q;
46 constexpr Float128() =
default;
47 constexpr Float128(
const float128_t& value) noexcept
53 std::enable_if_t<std::is_arithmetic<T>::value,
int> = 0>
54 constexpr Float128(
const T& value) noexcept
59 explicit Float128(
const char* str) noexcept
60 : value_(strtoflt128(str, NULL))
64 constexpr operator float128_t() const noexcept {
return value_; }
66 constexpr float128_t
const& value() const noexcept {
return value_; }
67 constexpr float128_t& value() noexcept {
return value_; }
70 template<
class CharT,
class Traits>
71 friend std::basic_istream<CharT, Traits>&
72 operator>>(std::basic_istream<CharT, Traits>& in, Float128& x)
77 x.value() = strtoflt128(buf.c_str(), NULL);
81 template<
class CharT,
class Traits>
82 friend std::basic_ostream<CharT, Traits>&
83 operator<<(std::basic_ostream<CharT, Traits>& out,
const Float128& x)
85 const std::size_t bufSize = 128;
88 std::string format =
"%." + std::to_string(out.precision()) +
"Q" +
89 ((out.flags() | std::ios_base::scientific) ?
"e" :
"f");
90 const int numChars = quadmath_snprintf(buf, bufSize, format.c_str(), x.value());
91 if (std::size_t(numChars) >= bufSize) {
99 constexpr Float128& operator++() noexcept { ++value_;
return *
this; }
100 constexpr Float128& operator--() noexcept { --value_;
return *
this; }
102 constexpr Float128 operator++(
int)
noexcept { Float128 tmp{*
this}; ++value_;
return tmp; }
103 constexpr Float128 operator--(
int)
noexcept { Float128 tmp{*
this}; --value_;
return tmp; }
106 constexpr Float128 operator+() const noexcept {
return Float128{+value_}; }
107 constexpr Float128 operator-() const noexcept {
return Float128{-value_}; }
110#define DUNE_ASSIGN_OP(OP) \
111 constexpr Float128& operator OP(const Float128& u) noexcept \
113 value_ OP float128_t(u); \
116 static_assert(true, "Require semicolon to unconfuse editors")
131#define DUNE_BINARY_OP(OP) \
132 constexpr Float128 operator OP(const Float128& t, \
133 const Float128& u) noexcept \
135 return Float128{float128_t(t) OP float128_t(u)}; \
137 constexpr Float128 operator OP(const float128_t& t, \
138 const Float128& u) noexcept \
140 return Float128{t OP float128_t(u)}; \
142 constexpr Float128 operator OP(const Float128& t, \
143 const float128_t& u) noexcept \
145 return Float128{float128_t(t) OP u}; \
148 std::enable_if_t<std::is_arithmetic<T>::value, int> = 0> \
149 constexpr Float128 operator OP(const T& t, \
150 const Float128& u) noexcept \
152 return Float128{float128_t(t) OP float128_t(u)}; \
155 std::enable_if_t<std::is_arithmetic<U>::value, int> = 0> \
156 constexpr Float128 operator OP(const Float128& t, \
157 const U& u) noexcept \
159 return Float128{float128_t(t) OP float128_t(u)}; \
161 static_assert(true, "Require semicolon to unconfuse editors")
173#define DUNE_BINARY_BOOL_OP(OP) \
174 constexpr bool operator OP(const Float128& t, \
175 const Float128& u) noexcept \
177 return float128_t(t) OP float128_t(u); \
180 std::enable_if_t<std::is_arithmetic<T>::value, int> = 0> \
181 constexpr bool operator OP(const T& t, \
182 const Float128& u) noexcept \
184 return float128_t(t) OP float128_t(u); \
187 std::enable_if_t<std::is_arithmetic<U>::value, int> = 0> \
188 constexpr bool operator OP(const Float128& t, \
189 const U& u) noexcept \
191 return float128_t(t) OP float128_t(u); \
193 static_assert(true, "Require semicolon to unconfuse editors")
195 DUNE_BINARY_BOOL_OP(==);
196 DUNE_BINARY_BOOL_OP(!=);
197 DUNE_BINARY_BOOL_OP(<);
198 DUNE_BINARY_BOOL_OP(>);
199 DUNE_BINARY_BOOL_OP(<=);
200 DUNE_BINARY_BOOL_OP(>=);
202#undef DUNE_BINARY_BOOL_OP
207#define DUNE_UNARY_FUNC(name,func) \
208 inline Float128 name(const Float128& u) noexcept \
210 return Float128{func (float128_t(u))}; \
212 static_assert(true, "Require semicolon to unconfuse editors")
215#define DUNE_CUSTOM_UNARY_FUNC(type,name,func) \
216 inline type name(const Float128& u) noexcept \
218 return (type)(func (float128_t(u))); \
220 static_assert(true, "Require semicolon to unconfuse editors")
223#define DUNE_BINARY_FUNC(name,func) \
224 inline Float128 name(const Float128& t, \
225 const Float128& u) noexcept \
227 return Float128{func (float128_t(t), float128_t(u))}; \
229 static_assert(true, "Require semicolon to unconfuse editors")
231 DUNE_UNARY_FUNC(abs, fabsq);
232 DUNE_UNARY_FUNC(acos, acosq);
233 DUNE_UNARY_FUNC(acosh, acoshq);
234 DUNE_UNARY_FUNC(asin, asinq);
235 DUNE_UNARY_FUNC(asinh, asinhq);
236 DUNE_UNARY_FUNC(atan, atanq);
237 DUNE_UNARY_FUNC(atanh, atanhq);
238 DUNE_UNARY_FUNC(cbrt, cbrtq);
239 DUNE_UNARY_FUNC(ceil, ceilq);
240 DUNE_UNARY_FUNC(cos, cosq);
241 DUNE_UNARY_FUNC(cosh, coshq);
242 DUNE_UNARY_FUNC(erf, erfq);
243 DUNE_UNARY_FUNC(erfc, erfcq);
244 DUNE_UNARY_FUNC(exp, expq);
245 DUNE_UNARY_FUNC(expm1, expm1q);
246 DUNE_UNARY_FUNC(fabs, fabsq);
247 DUNE_UNARY_FUNC(floor, floorq);
248 DUNE_CUSTOM_UNARY_FUNC(
int, ilogb, ilogbq);
249 DUNE_UNARY_FUNC(lgamma, lgammaq);
250 DUNE_CUSTOM_UNARY_FUNC(
long long int, llrint, llrintq);
251 DUNE_CUSTOM_UNARY_FUNC(
long long int, llround, llroundq);
252 DUNE_UNARY_FUNC(log, logq);
253 DUNE_UNARY_FUNC(log10, log10q);
254 DUNE_UNARY_FUNC(log1p, log1pq);
255 DUNE_UNARY_FUNC(log2, log2q);
257 DUNE_CUSTOM_UNARY_FUNC(
long int, lrint, lrintq);
258 DUNE_CUSTOM_UNARY_FUNC(
long int, lround, lroundq);
259 DUNE_UNARY_FUNC(nearbyint, nearbyintq);
260 DUNE_BINARY_FUNC(nextafter, nextafterq);
261 DUNE_BINARY_FUNC(pow, powq);
262 DUNE_UNARY_FUNC(rint, rintq);
263 DUNE_UNARY_FUNC(
round, roundq);
264 DUNE_UNARY_FUNC(sin, sinq);
265 DUNE_UNARY_FUNC(sinh, sinhq);
266 DUNE_UNARY_FUNC(sqrt, sqrtq);
267 DUNE_UNARY_FUNC(tan, tanq);
268 DUNE_UNARY_FUNC(tanh, tanhq);
269 DUNE_UNARY_FUNC(tgamma, tgammaq);
270 DUNE_UNARY_FUNC(
trunc, truncq);
272 DUNE_CUSTOM_UNARY_FUNC(
bool, isfinite, finiteq);
273 DUNE_CUSTOM_UNARY_FUNC(
bool, isinf, isinfq);
274 DUNE_CUSTOM_UNARY_FUNC(
bool, isnan, isnanq);
275 DUNE_CUSTOM_UNARY_FUNC(
bool, signbit, signbitq);
277#undef DUNE_UNARY_FUNC
278#undef DUNE_CUSTOM_UNARY_FUNC
279#undef DUNE_BINARY_FUNC
283#define DUNE_BINARY_ARITHMETIC_FUNC(name,func) \
284 inline Float128 name(const Float128& t, \
285 const Float128& u) noexcept \
287 return Float128{func (float128_t(t), float128_t(u))}; \
290 std::enable_if_t<std::is_arithmetic<T>::value, int> = 0> \
291 inline Float128 name(const T& t, \
292 const Float128& u) noexcept \
294 return Float128{func (float128_t(t), float128_t(u))}; \
297 std::enable_if_t<std::is_arithmetic<U>::value, int> = 0> \
298 inline Float128 name(const Float128& t, \
299 const U& u) noexcept \
301 return Float128{func (float128_t(t), float128_t(u))}; \
303 static_assert(true, "Require semicolon to unconfuse editors")
305 DUNE_BINARY_ARITHMETIC_FUNC(atan2,atan2q);
306 DUNE_BINARY_ARITHMETIC_FUNC(copysign,copysignq);
307 DUNE_BINARY_ARITHMETIC_FUNC(fdim,fdimq);
308 DUNE_BINARY_ARITHMETIC_FUNC(fmax,fmaxq);
309 DUNE_BINARY_ARITHMETIC_FUNC(fmin,fminq);
310 DUNE_BINARY_ARITHMETIC_FUNC(fmod,fmodq);
311 DUNE_BINARY_ARITHMETIC_FUNC(hypot,hypotq);
312 DUNE_BINARY_ARITHMETIC_FUNC(remainder,remainderq);
314#undef DUNE_BINARY_ARITHMETIC_FUNC
318 inline Float128 fma(
const Float128& t,
const Float128& u,
const Float128& v)
320 return Float128{fmaq(float128_t(t),float128_t(u),float128_t(v))};
323 inline Float128 frexp(
const Float128& u,
int* p)
325 return Float128{frexpq(float128_t(u), p)};
328 inline Float128 ldexp(
const Float128& u,
int p)
330 return Float128{ldexpq(float128_t(u), p)};
333 inline Float128 remquo(
const Float128& t,
const Float128& u,
int* quo)
335 return Float128{remquoq(float128_t(t), float128_t(u), quo)};
338 inline Float128 scalbln(
const Float128& u,
long int e)
340 return Float128{scalblnq(float128_t(u), e)};
343 inline Float128 scalbn(
const Float128& u,
int e)
345 return Float128{scalbnq(float128_t(u), e)};
356 std::enable_if_t<std::is_integral<Int>::value,
int> = 0>
357 inline Float128 pow(
const Float128& x,
const Int p)
359 static const Float128 max_value = FLT128_MAX;
360 static const Float128 min_value = FLT128_MIN;
361 static const Float128 inf_value = float128_t{1} / float128_t{0};
363 const bool isneg = (x < 0);
364 const bool isnan = (x != x);
365 const bool isinf = (isneg ? bool(-x > max_value) : bool(+x > max_value));
367 if (isnan) {
return x; }
368 if (isinf) {
return Float128{nanq(
"")}; }
370 const Float128 abs_x = (isneg ? -x : x);
372 if (abs_x < min_value)
373 return (isneg ? -inf_value : +inf_value);
375 return Float128(1) / pow(x, Int(-p));
378 if (p == Int(0)) {
return Float128(1); }
379 if (p == Int(1)) {
return x; }
380 if (abs_x > max_value)
381 return (isneg ? -inf_value : +inf_value);
383 if (p == Int(2)) {
return (x * x); }
384 if (p == Int(3)) {
return ((x * x) * x); }
385 if (p == Int(4)) {
const Float128 x2 = (x * x);
return (x2 * x2); }
387 Float128 result = ((p % Int(2)) != Int(0)) ? x : Float128(1);
391 while (Int(p2 /= 2) != Int(0)) {
394 const bool has_binary_power = (Int(p2 % Int(2)) != Int(0));
395 if (has_binary_power)
406 struct IsNumber<Impl::Float128>
407 :
public std::true_type {};
413#ifndef NO_STD_NUMERIC_LIMITS_SPECIALIZATION
415 class numeric_limits<
Dune::Impl::Float128>
417 using Float128 = Dune::Impl::Float128;
418 using float128_t = Dune::Impl::float128_t;
421 static constexpr bool is_specialized =
true;
422 static constexpr Float128
min() noexcept {
return FLT128_MIN; }
423 static constexpr Float128
max() noexcept {
return FLT128_MAX; }
424 static constexpr Float128 lowest() noexcept {
return -FLT128_MAX; }
425 static constexpr int digits = FLT128_MANT_DIG;
426 static constexpr int digits10 = 34;
427 static constexpr int max_digits10 = 36;
428 static constexpr bool is_signed =
true;
429 static constexpr bool is_integer =
false;
430 static constexpr bool is_exact =
false;
431 static constexpr int radix = 2;
432 static constexpr Float128 epsilon() noexcept {
return FLT128_EPSILON; }
433 static constexpr Float128 round_error() noexcept {
return float128_t{0.5}; }
434 static constexpr int min_exponent = FLT128_MIN_EXP;
435 static constexpr int min_exponent10 = FLT128_MIN_10_EXP;
436 static constexpr int max_exponent = FLT128_MAX_EXP;
437 static constexpr int max_exponent10 = FLT128_MAX_10_EXP;
438 static constexpr bool has_infinity =
true;
439 static constexpr bool has_quiet_NaN =
true;
440 static constexpr bool has_signaling_NaN =
false;
441 static constexpr float_denorm_style has_denorm = denorm_present;
442 static constexpr bool has_denorm_loss =
false;
443 static constexpr Float128 infinity() noexcept {
return float128_t{1}/float128_t{0}; }
444 static Float128 quiet_NaN() noexcept {
return nanq(
""); }
445 static constexpr Float128 signaling_NaN() noexcept {
return float128_t{}; }
446 static constexpr Float128 denorm_min() noexcept {
return FLT128_DENORM_MIN; }
447 static constexpr bool is_iec559 =
true;
448 static constexpr bool is_bounded =
false;
449 static constexpr bool is_modulo =
false;
450 static constexpr bool traps =
false;
451 static constexpr bool tinyness_before =
false;
452 static constexpr float_round_style round_style = round_to_nearest;
Default exception class for range errors.
Definition: exceptions.hh:254
A few common exception classes.
Stream & operator>>(Stream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition: streamoperators.hh:43
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
I round(const T &val, typename EpsilonType< T >::Type epsilon)
round using epsilon
Definition: float_cmp.cc:311
I trunc(const T &val, typename EpsilonType< T >::Type epsilon)
truncate using epsilon
Definition: float_cmp.cc:407
auto min(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::min()
Definition: defaults.hh:89
auto max(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::max()
Definition: defaults.hh:81
Dune namespace.
Definition: alignedallocator.hh:13
Traits for type conversions and type information.