<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
gnuquad.hpp
Go to the documentation of this file.
1
3//
4// Copyright (c) 2021-2023, University of Colorado Denver. All rights reserved.
5//
6// This file is part of <T>LAPACK.
7// <T>LAPACK is free software: you can redistribute it and/or modify it under
8// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
9
10#ifndef TLAPACK_GNUQUAD_HH
11#define TLAPACK_GNUQUAD_HH
12
13#include <quadmath.h>
14
15#include <complex>
16#include <limits>
17#include <ostream>
18
19namespace std {
20
21constexpr __float128 abs(__float128 x); // See include/bits/std_abs.h
22inline __float128 ceil(__float128 x) noexcept { return ceilq(x); }
23inline __float128 floor(__float128 x) noexcept { return floorq(x); }
24inline bool isinf(__float128 x) noexcept { return isinfq(x); }
25inline bool isnan(__float128 x) noexcept { return isnanq(x); }
26inline __float128 log2(__float128 x) noexcept { return log2q(x); }
27inline __float128 max(__float128 x, __float128 y) noexcept
28{
29 return fmaxq(x, y);
30}
31inline __float128 min(__float128 x, __float128 y) noexcept
32{
33 return fminq(x, y);
34}
35inline __float128 pow(int x, __float128 y) noexcept { return powq(x, y); }
36inline __float128 sqrt(__float128 x) noexcept { return sqrtq(x); }
37
38inline __float128 abs(complex<__float128> z)
39{
40 __float128 x = z.real();
41 __float128 y = z.imag();
42 const __float128 __s = max(abs(x), abs(y));
43 if (__s == __float128()) // well ...
44 return __s;
45 x /= __s;
46 y /= __s;
47 return __s * sqrt(x * x + y * y);
48}
49inline complex<__float128> sqrt(complex<__float128> z)
50{
51 const __float128 x = real(z);
52 const __float128 y = imag(z);
53 const __float128 zero(0);
54 const __float128 two(2);
55 const __float128 half(0.5);
56
57 if (x == zero) {
58 const __float128 t = sqrt(half * abs(y));
59 return complex<__float128>(t, (y < zero) ? -t : t);
60 }
61 else {
62 const __float128 t = sqrt(two * (abs(z) + abs(x)));
63 const __float128 u = half * t;
64 return (x > zero)
65 ? complex<__float128>(u, y / t)
66 : complex<__float128>(abs(y) / t, (y < zero) ? -u : u);
67 }
68}
69
70template <>
71struct numeric_limits<__float128> {
72 static constexpr bool is_specialized = true;
73
74 static constexpr __float128 min() noexcept;
75 static constexpr __float128 max() noexcept;
76 static constexpr __float128 lowest() noexcept;
77
78 static constexpr int digits = FLT128_MANT_DIG;
79 static constexpr int digits10 = FLT128_DIG;
80 static constexpr int max_digits10 = (2 + FLT128_MANT_DIG * 643L / 2136);
81
82 static constexpr bool is_signed = true;
83 static constexpr bool is_integer = false;
84 static constexpr bool is_exact = false;
85
86 static constexpr int radix = 2;
87 static constexpr __float128 epsilon() noexcept;
88 static constexpr __float128 round_error() noexcept { return 0.5F; }
89
90 static constexpr int min_exponent = FLT128_MIN_EXP;
91 static constexpr int min_exponent10 = FLT128_MIN_10_EXP;
92 static constexpr int max_exponent = FLT128_MAX_EXP;
93 static constexpr int max_exponent10 = FLT128_MAX_10_EXP;
94
95 static constexpr bool has_infinity = true;
96 static constexpr bool has_quiet_NaN = true;
97 // static constexpr bool has_signaling_NaN = ;
98 static constexpr float_denorm_style has_denorm = denorm_present;
99 static constexpr bool has_denorm_loss = false;
100
101 static constexpr __float128 infinity() noexcept
102 {
103 return __builtin_huge_valq();
104 }
105 static __float128 quiet_NaN() noexcept { return nanq(""); }
106 // static constexpr __float128 signaling_NaN() noexcept {}
107 static constexpr __float128 denorm_min() noexcept;
108
109 // static constexpr bool is_iec559 = ;
110 static constexpr bool is_bounded = true;
111 static constexpr bool is_modulo = false;
112
113 // static constexpr bool traps = ;
114 // static constexpr bool tinyness_before = ;
115 static constexpr float_round_style round_style = round_to_nearest;
116};
117
118inline ostream& operator<<(ostream& out, const __float128& x)
119{
120 char buf[128];
121 quadmath_snprintf(buf, sizeof buf, "%+-#*.20Qe", x);
122 out << buf;
123 return out;
124}
125
126inline istream& operator>>(istream& in, __float128& x)
127{
128 char buf[128];
129 in >> buf;
130 x = strtoflt128(buf, nullptr);
131 return in;
132}
133
134#ifdef __GNUC__
135 #pragma GCC system_header
136#endif
137
138constexpr __float128 numeric_limits<__float128>::min() noexcept
139{
140 return FLT128_MIN;
141}
142constexpr __float128 numeric_limits<__float128>::max() noexcept
143{
144 return FLT128_MAX;
145}
146constexpr __float128 numeric_limits<__float128>::lowest() noexcept
147{
148 return -FLT128_MAX;
149}
150constexpr __float128 numeric_limits<__float128>::epsilon() noexcept
151{
152 return FLT128_EPSILON;
153}
154constexpr __float128 numeric_limits<__float128>::denorm_min() noexcept
155{
156 return FLT128_DENORM_MIN;
157}
158
159} // namespace std
160
161#endif
constexpr bool isnan(const T &x) noexcept
Extends std::isnan() to complex numbers.
Definition utils.hpp:125
constexpr real_type< T > real(const T &x) noexcept
Extends std::real() to real datatypes.
Definition utils.hpp:71
constexpr real_type< T > imag(const T &x) noexcept
Extends std::imag() to real datatypes.
Definition utils.hpp:86
constexpr bool isinf(const T &x) noexcept
Extends std::isinf() to complex numbers.
Definition utils.hpp:117