Debian Codegraph @

3rdparty/dissection/src/qd/dd_real.h

raw
1 /*
2 * include/dd_real.h
3 *
4 * This work was supported by the Director, Office of Science, Division
5 * of Mathematical, Information, and Computational Sciences of the
6 * U.S. Department of Energy under contract number DE-AC03-76SF00098.
7 *
8 * Copyright (c) 2000-2007
9 *
10 * Double-double precision (>= 106-bit significand) floating point
11 * arithmetic package based on David Bailey's Fortran-90 double-double
12 * package, with some changes. See
13 *
14 * http://www.nersc.gov/~dhbailey/mpdist/mpdist.html
15 *
16 * for the original Fortran-90 version.
17 *
18 * Overall structure is similar to that of Keith Brigg's C++ double-double
19 * package. See
20 *
21 * http://www-epidem.plansci.cam.ac.uk/~kbriggs/doubledouble.html
22 *
23 * for more details. In particular, the fix for x86 computers is borrowed
24 * from his code.
25 *
26 * Yozo Hida
27 */
28 // operator int(), dd_real copysign(), fmax(), lobg(), scalbn() are added
29 // for complex class of LLVM Clang++ : 23 Jul.2015 Atsushi Suzuki
30
31 #ifndef _QD_DD_REAL_H
32 #define _QD_DD_REAL_H
33
34 #include <cmath>
35 #include <iostream>
36 #include <string>
37 #include <limits>
38 #include <qd/qd_config.h>
39 #include <qd/fpu.h>
40
41 // Some compilers define isnan, isfinite, and isinf as macros, even for
42 // C++ codes, which cause havoc when overloading these functions. We undef
43 // them here.
44 #ifdef isnan
45 #undef isnan
46 #endif
47
48 #ifdef isfinite
49 #undef isfinite
50 #endif
51
52 #ifdef isinf
53 #undef isinf
54 #endif
55
56 #ifdef max
57 #undef max
58 #endif
59
60 #ifdef min
61 #undef min
62 #endif
63
64 struct QD_API dd_real {
65 double x[2];
66
67 dd_real(double hi, double lo) { x[0] = hi; x[1] = lo; }
68 dd_real() {x[0] = 0.0; x[1] = 0.0; }
69 dd_real(double h) { x[0] = h; x[1] = 0.0; }
70 dd_real(int h) {
71 x[0] = (static_cast<double>(h));
72 x[1] = 0.0;
73 }
74
75 dd_real (const char *s);
76 explicit dd_real (const double *d) {
77 x[0] = d[0]; x[1] = d[1];
78 }
79
80 static void error(const char *msg);
81
82 double _hi() const { return x[0]; }
83 double _lo() const { return x[1]; }
84
85 static const dd_real _2pi;
86 static const dd_real _pi;
87 static const dd_real _3pi4;
88 static const dd_real _pi2;
89 static const dd_real _pi4;
90 static const dd_real _e;
91 static const dd_real _log2;
92 static const dd_real _log10;
93 static const dd_real _nan;
94 static const dd_real _inf;
95
96 static const double _eps;
97 static const double _min_normalized;
98 static const dd_real _max;
99 static const dd_real _safe_max;
100 static const int _ndigits;
101
102 bool isnan() const { return QD_ISNAN(x[0]) || QD_ISNAN(x[1]); }
103 bool isfinite() const { return QD_ISFINITE(x[0]); }
104 bool isinf() const { return QD_ISINF(x[0]); }
105
106 static dd_real add(double a, double b);
107 static dd_real ieee_add(const dd_real &a, const dd_real &b);
108 static dd_real sloppy_add(const dd_real &a, const dd_real &b);
109
110 dd_real &operator+=(double a);
111 dd_real &operator+=(const dd_real &a);
112
113 static dd_real sub(double a, double b);
114
115 dd_real &operator-=(double a);
116 dd_real &operator-=(const dd_real &a);
117
118 dd_real operator-() const;
119 dd_real operator+() const;
120
121 static dd_real mul(double a, double b);
122
123 dd_real &operator*=(double a);
124 dd_real &operator*=(const dd_real &a);
125
126 static dd_real div(double a, double b);
127 static dd_real sloppy_div(const dd_real &a, const dd_real &b);
128 static dd_real accurate_div(const dd_real &a, const dd_real &b);
129
130 dd_real &operator/=(double a);
131 dd_real &operator/=(const dd_real &a);
132
133 dd_real &operator=(double a);
134 dd_real &operator=(const char *s);
135
136 dd_real operator^(int n);
137 static dd_real sqr(double d);
138
139 static dd_real sqrt(double a);
140
141 bool is_zero() const;
142 bool is_one() const;
143 bool is_positive() const;
144 bool is_negative() const;
145
146 static dd_real rand(void);
147
148 void to_digits(char *s, int &expn, int precision = _ndigits) const;
149 void write(char *s, int len, int precision = _ndigits,
150 bool showpos = false, bool uppercase = false) const;
151 std::string to_string(int precision = _ndigits, int width = 0,
152 std::ios_base::fmtflags fmt = static_cast<std::ios_base::fmtflags>(0),
153 bool showpos = false, bool uppercase = false, char fill = ' ') const;
154 int read(const char *s, dd_real &a);
155
156 /* Debugging Methods */
157 void dump(const std::string &name = "", std::ostream &os = std::cerr) const;
158 void dump_bits(const std::string &name = "",
159 std::ostream &os = std::cerr) const;
160
161 static dd_real debug_rand();
162
163 // added for complex class of LLVM Clang++: 23 Jul.2015 Atsushi Suzuki
164 operator int() { return int(x[0]); }
165 operator int() const { return int(x[0]); }
166 };
167
168
169 namespace std {
170 template <>
171 class numeric_limits<dd_real> : public numeric_limits<double> {
172 public:
173 inline static double epsilon() { return dd_real::_eps; }
174 inline static dd_real max() { return dd_real::_max; }
175 inline static dd_real safe_max() { return dd_real::_safe_max; }
176 inline static double min() { return dd_real::_min_normalized; }
177 static const int digits = 104;
178 static const int digits10 = 31;
179 };
180 }
181
182 QD_API dd_real ddrand(void);
183 QD_API dd_real sqrt(const dd_real &a);
184
185 QD_API dd_real polyeval(const dd_real *c, int n, const dd_real &x);
186 QD_API dd_real polyroot(const dd_real *c, int n,
187 const dd_real &x0, int max_iter = 32, double thresh = 0.0);
188
189 QD_API inline bool isnan(const dd_real &a) { return a.isnan(); }
190 QD_API inline bool isfinite(const dd_real &a) { return a.isfinite(); }
191 QD_API inline bool isinf(const dd_real &a) { return a.isinf(); }
192
193 /* Computes dd * d where d is known to be a power of 2. */
194 QD_API dd_real mul_pwr2(const dd_real &dd, double d);
195
196 QD_API dd_real operator+(const dd_real &a, double b);
197 QD_API dd_real operator+(double a, const dd_real &b);
198 QD_API dd_real operator+(const dd_real &a, const dd_real &b);
199
200 QD_API dd_real operator-(const dd_real &a, double b);
201 QD_API dd_real operator-(double a, const dd_real &b);
202 QD_API dd_real operator-(const dd_real &a, const dd_real &b);
203
204 QD_API dd_real operator*(const dd_real &a, double b);
205 QD_API dd_real operator*(double a, const dd_real &b);
206 QD_API dd_real operator*(const dd_real &a, const dd_real &b);
207
208 QD_API dd_real operator/(const dd_real &a, double b);
209 QD_API dd_real operator/(double a, const dd_real &b);
210 QD_API dd_real operator/(const dd_real &a, const dd_real &b);
211
212 QD_API dd_real inv(const dd_real &a);
213
214 QD_API dd_real rem(const dd_real &a, const dd_real &b);
215 QD_API dd_real drem(const dd_real &a, const dd_real &b);
216 QD_API dd_real divrem(const dd_real &a, const dd_real &b, dd_real &r);
217
218 QD_API dd_real pow(const dd_real &a, int n);
219 QD_API dd_real pow(const dd_real &a, const dd_real &b);
220 QD_API dd_real npwr(const dd_real &a, int n);
221 QD_API dd_real sqr(const dd_real &a);
222
223 QD_API dd_real sqrt(const dd_real &a);
224 QD_API dd_real nroot(const dd_real &a, int n);
225
226 QD_API bool operator==(const dd_real &a, int b);
227 QD_API bool operator==(int a, const dd_real &b);
228 QD_API bool operator==(const dd_real &a, double b);
229 QD_API bool operator==(double a, const dd_real &b);
230 QD_API bool operator==(const dd_real &a, const dd_real &b);
231
232 QD_API bool operator<=(const dd_real &a, int b);
233 QD_API bool operator<=(int a, const dd_real &b);
234 QD_API bool operator<=(const dd_real &a, double b);
235 QD_API bool operator<=(double a, const dd_real &b);
236 QD_API bool operator<=(const dd_real &a, const dd_real &b);
237
238 QD_API bool operator>=(const dd_real &a, int b);
239 QD_API bool operator>=(int a, const dd_real &b);
240 QD_API bool operator>=(const dd_real &a, double b);
241 QD_API bool operator>=(double a, const dd_real &b);
242 QD_API bool operator>=(const dd_real &a, const dd_real &b);
243
244 QD_API bool operator<(const dd_real &a, int b);
245 QD_API bool operator<(int a, const dd_real &b);
246 QD_API bool operator<(const dd_real &a, double b);
247 QD_API bool operator<(double a, const dd_real &b);
248 QD_API bool operator<(const dd_real &a, const dd_real &b);
249
250 QD_API bool operator>(const dd_real &a, int b);
251 QD_API bool operator>(int a, const dd_real &b);
252 QD_API bool operator>(const dd_real &a, double b);
253 QD_API bool operator>(double a, const dd_real &b);
254 QD_API bool operator>(const dd_real &a, const dd_real &b);
255
256 QD_API bool operator!=(const dd_real &a, int b);
257 QD_API bool operator!=(int a, const dd_real &b);
258 QD_API bool operator!=(const dd_real &a, double b);
259 QD_API bool operator!=(double a, const dd_real &b);
260 QD_API bool operator!=(const dd_real &a, const dd_real &b);
261
262 QD_API dd_real nint(const dd_real &a);
263 QD_API dd_real floor(const dd_real &a);
264 QD_API dd_real ceil(const dd_real &a);
265 QD_API dd_real aint(const dd_real &a);
266
267 QD_API dd_real ddrand(void);
268
269 double to_double(const dd_real &a);
270 int to_int(const dd_real &a);
271
272 // QD_API dd_real exp(const dd_real &a);
273 QD_API dd_real ldexp(const dd_real &a, int exp);
274 QD_API dd_real log(const dd_real &a);
275 QD_API dd_real log10(const dd_real &a);
276
277 QD_API dd_real sin(const dd_real &a);
278 QD_API dd_real cos(const dd_real &a);
279 QD_API dd_real tan(const dd_real &a);
280 QD_API void sincos(const dd_real &a, dd_real &sin_a, dd_real &cos_a);
281
282 QD_API dd_real asin(const dd_real &a);
283 QD_API dd_real acos(const dd_real &a);
284 QD_API dd_real atan(const dd_real &a);
285 QD_API dd_real atan2(const dd_real &y, const dd_real &x);
286
287 QD_API dd_real sinh(const dd_real &a);
288 QD_API dd_real cosh(const dd_real &a);
289 QD_API dd_real tanh(const dd_real &a);
290 QD_API void sincosh(const dd_real &a,
291 dd_real &sinh_a, dd_real &cosh_a);
292
293 QD_API dd_real asinh(const dd_real &a);
294 QD_API dd_real acosh(const dd_real &a);
295 QD_API dd_real atanh(const dd_real &a);
296
297 QD_API dd_real fabs(const dd_real &a);
298 QD_API dd_real abs(const dd_real &a); /* same as fabs */
299
300 QD_API dd_real fmod(const dd_real &a, const dd_real &b);
301
302 QD_API std::ostream& operator<<(std::ostream &s, const dd_real &a);
303 QD_API std::istream& operator>>(std::istream &s, dd_real &a);
304 #ifdef QD_INLINE
305 #include <qd/dd_inline.h>
306 #endif
307
308 // added for complex class of LLVM Clang++ : 23 Jul.2015 Atsushi Suzuki
309 inline dd_real copysign(const dd_real &x, const dd_real &y) {
310 return (y.x[0] < 0.0) ? ((x.x[0] < 0.0) ? x : (-x)) : ((x.x[0] < 0.0) ? (-x) : x);
311 }
312
313 inline dd_real fmax(const dd_real &x, const dd_real &y) {
314 return x.x[0] < y.x[0] ? y : x;
315 }
316
317 inline dd_real logb(const dd_real &y) {
318 return dd_real(logb(y.x[0]));
319 }
320 #if 0
321 inline dd_real scalbn(const dd_real &x, int n) {
322 return dd_real(scalb(x.x[0], n));
323 }
324 #endif
325 #endif /* _QD_DD_REAL_H */
326