| 1 |
/*! \file arithmetic.hpp |
| 2 |
\brief higher precision arithmetic for Kernel Detection |
| 3 |
\author Atsushi Suzuki, Laboratoire Jacques-Louis Lions |
| 4 |
\date Jul. 17th 2015 |
| 5 |
\date Nov. 30th 2016 |
| 6 |
*/ |
| 7 |
|
| 8 |
// This file is part of Dissection |
| 9 |
// |
| 10 |
// Dissection is free software: you can redistribute it and/or modify |
| 11 |
// it under the terms of the GNU General Public License as published by |
| 12 |
// the Free Software Foundation, either version 3 of the License, or |
| 13 |
// (at your option) any later version. |
| 14 |
// |
| 15 |
// Linking Dissection statically or dynamically with other modules is making |
| 16 |
// a combined work based on Disssection. Thus, the terms and conditions of |
| 17 |
// the GNU General Public License cover the whole combination. |
| 18 |
// |
| 19 |
// As a special exception, the copyright holders of Dissection give you |
| 20 |
// permission to combine Dissection program with free software programs or |
| 21 |
// libraries that are released under the GNU LGPL and with independent modules |
| 22 |
// that communicate with Dissection solely through the Dissection-fortran |
| 23 |
// interface. You may copy and distribute such a system following the terms of |
| 24 |
// the GNU GPL for Dissection and the licenses of the other code concerned, |
| 25 |
// provided that you include the source code of that other code when and as |
| 26 |
// the GNU GPL requires distribution of source code and provided that you do |
| 27 |
// not modify the Dissection-fortran interface. |
| 28 |
// |
| 29 |
// Note that people who make modified versions of Dissection are not obligated |
| 30 |
// to grant this special exception for their modified versions; it is their |
| 31 |
// choice whether to do so. The GNU General Public License gives permission to |
| 32 |
// release a modified version without this exception; this exception also makes |
| 33 |
// it possible to release a modified version which carries forward this |
| 34 |
// exception. If you modify the Dissection-fortran interface, this exception |
| 35 |
// does not apply to your modified version of Dissection, and you must remove |
| 36 |
// this exception when you distribute your modified version. |
| 37 |
// |
| 38 |
// This exception is an additional permission under section 7 of the GNU |
| 39 |
// General Public License, version 3 ("GPLv3") |
| 40 |
// |
| 41 |
// Dissection is distributed in the hope that it will be useful, |
| 42 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 43 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 44 |
// GNU General Public License for more details. |
| 45 |
// |
| 46 |
// You should have received a copy of the GNU General Public License |
| 47 |
// along with Dissection. If not, see <http://www.gnu.org/licenses/>. |
| 48 |
// |
| 49 |
|
| 50 |
#ifndef _COMPILER_ARITHMETIC_H |
| 51 |
# define _COMPILER_ARITHMETIC_H |
| 52 |
# include "Compiler/OptionCompiler.hpp" |
| 53 |
#include <float.h> |
| 54 |
#include <string> |
| 55 |
#include <cstdio> |
| 56 |
|
| 57 |
// scalbn modification for LLVM is taken from |
| 58 |
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0415r0.html |
| 59 |
template <class T> |
| 60 |
T scalbn(T e, int exp) { |
| 61 |
#ifndef __clang__ |
| 62 |
return scalbn(e, exp); |
| 63 |
#else |
| 64 |
if (!exp) return e; |
| 65 |
T mult(1.0); |
| 66 |
if (exp > 0) { |
| 67 |
mult = FLT_RADIX; |
| 68 |
-- exp; |
| 69 |
} else { |
| 70 |
++ exp; |
| 71 |
exp = -exp; |
| 72 |
mult /= FLT_RADIX; |
| 73 |
} |
| 74 |
|
| 75 |
while (exp > 0) { |
| 76 |
if (!(exp & 1)) { |
| 77 |
mult *= mult; |
| 78 |
exp >>= 1; |
| 79 |
} else { |
| 80 |
e *= mult; |
| 81 |
-- exp; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return e; |
| 86 |
#endif |
| 87 |
} |
| 88 |
|
| 89 |
#include <complex> |
| 90 |
using std::complex; |
| 91 |
|
| 92 |
#ifdef SX_ACE |
| 93 |
#define LONG_DOUBLE |
| 94 |
#endif |
| 95 |
|
| 96 |
template<typename T, typename Z> |
| 97 |
inline T machine_epsilon() { return T(DBL_EPSILON); }; |
| 98 |
|
| 99 |
template<> |
| 100 |
inline double machine_epsilon<double, double>() { return DBL_EPSILON; } |
| 101 |
|
| 102 |
template<typename T> |
| 103 |
inline std::string tostring(const T &x) { std::string dummy; return dummy; }; |
| 104 |
|
| 105 |
template<typename T> |
| 106 |
inline T sqrt(const T &x) { }; |
| 107 |
|
| 108 |
#ifdef DD_REAL |
| 109 |
#include "qd/dd_real.h" |
| 110 |
|
| 111 |
typedef dd_real quadruple; // implementation of quadruple precision |
| 112 |
|
| 113 |
#include "qd/qd_real.h" |
| 114 |
|
| 115 |
typedef qd_real octruple; // implementation of octruple precision |
| 116 |
|
| 117 |
inline double quad2double(const quadruple &x) { return x.x[0]; } |
| 118 |
|
| 119 |
inline double oct2double(const octruple &x) { return x.x[0]; } |
| 120 |
|
| 121 |
inline quadruple oct2quad(const octruple &x) { return dd_real(x.x[0], x.x[1]); } |
| 122 |
|
| 123 |
template<> |
| 124 |
inline quadruple machine_epsilon<quadruple, quadruple>() { // argument is dummy |
| 125 |
return quadruple(dd_real::_eps); // to define type |
| 126 |
} |
| 127 |
|
| 128 |
template<> |
| 129 |
inline quadruple machine_epsilon<quadruple, double>() { // argument is dummy |
| 130 |
return quadruple(DBL_EPSILON); |
| 131 |
} |
| 132 |
|
| 133 |
template<> |
| 134 |
inline double machine_epsilon<double, quadruple>() { // argument is dummy |
| 135 |
return double(dd_real::_eps); // to define type |
| 136 |
} |
| 137 |
|
| 138 |
template<> |
| 139 |
inline std::string tostring<quadruple>(const quadruple &x) { |
| 140 |
return x.to_string(dd_real::_ndigits); |
| 141 |
} |
| 142 |
|
| 143 |
template<> |
| 144 |
inline std::string tostring<complex<quadruple> >(const complex<quadruple> &x) { |
| 145 |
return "( " + x.real().to_string(dd_real::_ndigits) + " " |
| 146 |
+ x.imag().to_string(dd_real::_ndigits) + " )"; |
| 147 |
} |
| 148 |
|
| 149 |
template<> |
| 150 |
inline double sqrt<double> (const double &x) {return sqrt(x); } |
| 151 |
|
| 152 |
template<> |
| 153 |
inline quadruple sqrt<quadruple> (const quadruple &x) {return sqrt(x); } |
| 154 |
|
| 155 |
template<> |
| 156 |
inline octruple sqrt<octruple> (const octruple &x) {return sqrt(x); } |
| 157 |
|
| 158 |
|
| 159 |
template<> |
| 160 |
inline float sqrt<float> (const float &x) {return sqrtf(x); } |
| 161 |
|
| 162 |
#else |
| 163 |
#ifdef LONG_DOUBLE |
| 164 |
typedef long double quadruple; |
| 165 |
inline double quad2double(const quadruple &x) { return (double)x; } |
| 166 |
|
| 167 |
template<> |
| 168 |
inline quadruple machine_epsilon<quadruple, quadruple>() { |
| 169 |
return (long double)1.93e-34; // need to be updated for LONG DOUBLE |
| 170 |
} |
| 171 |
|
| 172 |
template<> |
| 173 |
inline quadruple machine_epsilon<quadruple, double>() { |
| 174 |
return (long double)DBL_EPSILON; |
| 175 |
} |
| 176 |
#if ((!defined(SX_ACE)) && (!defined(_MSC_VER))) |
| 177 |
inline long double fabs(const long double x) { |
| 178 |
return (x > 0.0L ? x : (-x)); |
| 179 |
} |
| 180 |
#endif |
| 181 |
|
| 182 |
template<> |
| 183 |
inline double sqrt<double> (const double &x) {return sqrt(x); } |
| 184 |
|
| 185 |
template<> |
| 186 |
inline quadruple sqrt<quadruple> (const quadruple &x) {return sqrtl(x); } |
| 187 |
|
| 188 |
emplate<> |
| 189 |
inline float sqrt<float> (const float &x) {return sqrtf(x); } |
| 190 |
|
| 191 |
#else |
| 192 |
#ifdef FAST_DD |
| 193 |
#include <fast_dd.h> |
| 194 |
typedef dd_real quadruple; // implementation of quadruple precisio |
| 195 |
|
| 196 |
inline double quad2double(const quadruple &x) { return to_double(x); } |
| 197 |
|
| 198 |
|
| 199 |
template<> |
| 200 |
inline quadruple machine_epsilon<quadruple, double>() { // argument is dummy |
| 201 |
return dd_real(1.93e-34); // need to be updated for LONG DOUBLE |
| 202 |
} |
| 203 |
|
| 204 |
template<> |
| 205 |
inline std::string tostring<quadruple>(const quadruple &x) { |
| 206 |
char buf[256]; |
| 207 |
sprintf(buf, "%24.16e", quad2double(x)); |
| 208 |
return std::string(buf); |
| 209 |
} |
| 210 |
template<> |
| 211 |
inline std::string tostring<complex<quadruple> >(const complex<quadruple> &x) { |
| 212 |
char buf[256]; |
| 213 |
sprintf(buf, "( %24.16e 24.16e )", |
| 214 |
quad2double(x.real()), quad2double(x.imag())); |
| 215 |
return std::string(buf); |
| 216 |
} |
| 217 |
// atan2 is not defined in fast_dd : approximated by double! |
| 218 |
inline quadruple atan2(const quadruple &y, const quadruple &x) { |
| 219 |
double t; |
| 220 |
t = atan2(quad2double(y), quad2double(x)); |
| 221 |
return dd_real(t); |
| 222 |
} |
| 223 |
#else // #ifdef FAST_DD |
| 224 |
#include <quadmath.h> |
| 225 |
typedef __float128 quadruple; |
| 226 |
inline double quad2double(const quadruple &x) { return (double)x; } |
| 227 |
template<> |
| 228 |
inline std::string tostring<quadruple> (const quadruple &x) { |
| 229 |
char buf[256]; |
| 230 |
quadmath_snprintf(buf, 256, "%.32Qe", x); |
| 231 |
return std::string(buf); |
| 232 |
} |
| 233 |
template<> |
| 234 |
inline std::string tostring<complex<quadruple> >(const complex<quadruple> &x) { |
| 235 |
char buf[256]; |
| 236 |
quadmath_snprintf(buf, 256, "%.32Qe %.32Qe", x.real(), x.imag()); |
| 237 |
return std::string(buf); |
| 238 |
} |
| 239 |
template<> |
| 240 |
inline double sqrt<double> (const double &x) {return sqrt(x); } |
| 241 |
|
| 242 |
template<> |
| 243 |
inline quadruple sqrt<quadruple> (const quadruple &x) {return sqrtq(x); } |
| 244 |
|
| 245 |
template<> |
| 246 |
inline float sqrt<float> (const float &x) {return sqrtf(x); } |
| 247 |
|
| 248 |
#endif |
| 249 |
#endif |
| 250 |
#endif |
| 251 |
|
| 252 |
template<> |
| 253 |
inline std::string tostring<double>(const double &x) { |
| 254 |
char buf[256]; |
| 255 |
sprintf(buf, "%24.16e", x); |
| 256 |
return std::string(buf); |
| 257 |
} |
| 258 |
template<> |
| 259 |
inline std::string tostring<complex<double> >(const complex<double> &x) { |
| 260 |
char buf[256]; |
| 261 |
sprintf(buf, "(%24.16e %24.16e)", x.real(), x.imag()); |
| 262 |
return std::string(buf); |
| 263 |
} |
| 264 |
|
| 265 |
template<> |
| 266 |
inline std::string tostring<float>(const float &x) { |
| 267 |
char buf[256]; |
| 268 |
sprintf(buf, "%16.8e", x); |
| 269 |
return std::string(buf); |
| 270 |
} |
| 271 |
template<> |
| 272 |
inline std::string tostring<complex<float> >(const complex<float> &x) { |
| 273 |
char buf[256]; |
| 274 |
sprintf(buf, "(%16.8e %16.8e)", x.real(), x.imag()); |
| 275 |
return std::string(buf); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
#ifndef NO_OCTRUPLE |
| 280 |
template<> |
| 281 |
inline std::string tostring<octruple>(const octruple &x) { return x.to_string(); } |
| 282 |
#endif |
| 283 |
|
| 284 |
template<typename Z, typename T> |
| 285 |
inline Z conv_prec(const T &x){ |
| 286 |
fprintf(stderr, |
| 287 |
"%s %d : specialized template is not implemented\n", |
| 288 |
__FILE__, __LINE__); |
| 289 |
return Z(0.0); |
| 290 |
}; |
| 291 |
|
| 292 |
template<> |
| 293 |
inline double conv_prec<double, quadruple>(const quadruple &y) |
| 294 |
{ |
| 295 |
return quad2double(y); |
| 296 |
} |
| 297 |
|
| 298 |
template<> |
| 299 |
inline quadruple conv_prec<quadruple, double>(const double &y) |
| 300 |
{ |
| 301 |
return quadruple(y); |
| 302 |
} |
| 303 |
|
| 304 |
template<> |
| 305 |
inline quadruple conv_prec<quadruple, quadruple>(const quadruple &y) |
| 306 |
{ |
| 307 |
return y; |
| 308 |
} |
| 309 |
|
| 310 |
template<> |
| 311 |
inline complex<quadruple> conv_prec<complex<quadruple>, complex<quadruple> >(const complex<quadruple> &y) |
| 312 |
{ |
| 313 |
return y; |
| 314 |
} |
| 315 |
|
| 316 |
#ifndef NO_OCTRUPLE |
| 317 |
template<> |
| 318 |
inline octruple conv_prec<octruple, quadruple>(const quadruple &y) |
| 319 |
{ |
| 320 |
return octruple(y); |
| 321 |
} |
| 322 |
#endif |
| 323 |
|
| 324 |
template<> |
| 325 |
inline complex<quadruple> conv_prec<complex<quadruple>, double>(const double &y) |
| 326 |
{ |
| 327 |
return complex<quadruple>(quadruple(y), quadruple(0.0)); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
template<> |
| 332 |
inline complex<quadruple> conv_prec<complex<quadruple>,complex<double> > |
| 333 |
(const complex<double> &y) |
| 334 |
{ |
| 335 |
return complex<quadruple>(quadruple(y.real()), quadruple(y.imag())); |
| 336 |
} |
| 337 |
|
| 338 |
#ifndef NO_OCTRUPLE |
| 339 |
template<> |
| 340 |
inline complex<octruple> conv_prec<complex<octruple>, quadruple> |
| 341 |
(const quadruple &y) |
| 342 |
{ |
| 343 |
return complex<octruple>(octruple(y), octruple(0.0)); |
| 344 |
} |
| 345 |
template<> |
| 346 |
inline complex<octruple> conv_prec<complex<octruple>, complex<quadruple> >(const complex<quadruple> &y) |
| 347 |
{ |
| 348 |
return complex<octruple>(octruple(y.real()), octruple(y.imag())); |
| 349 |
} |
| 350 |
#endif |
| 351 |
|
| 352 |
template<> |
| 353 |
inline complex<double> conv_prec<complex<double>, complex<quadruple> >(const complex<quadruple> &x) { |
| 354 |
return complex<double>(quad2double(x.real()), quad2double(x.imag())); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
#ifndef NO_OCTRUPLE |
| 359 |
template<> |
| 360 |
inline quadruple conv_prec<quadruple, octruple>(const octruple &x) { |
| 361 |
return oct2quad(x); |
| 362 |
} |
| 363 |
template<> |
| 364 |
inline complex<quadruple> conv_prec<complex<quadruple>, complex<octruple> >(const complex<octruple> &x) { |
| 365 |
return complex<quadruple>(oct2quad(x.real()), oct2quad(x.imag())); |
| 366 |
} |
| 367 |
#endif |
| 368 |
|
| 369 |
template<> |
| 370 |
inline double conv_prec<double, double>(const double &x) { |
| 371 |
return x; |
| 372 |
} |
| 373 |
|
| 374 |
template<> |
| 375 |
inline complex<double> conv_prec<complex<double>, |
| 376 |
complex<double> >(const complex<double> &x) { |
| 377 |
return x; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
template<> |
| 382 |
inline float conv_prec<float, double>(const double &y) |
| 383 |
{ |
| 384 |
return (float)y; |
| 385 |
} |
| 386 |
|
| 387 |
template<> |
| 388 |
inline double conv_prec<double, float>(const float &y) |
| 389 |
{ |
| 390 |
return (double)y; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
template<typename T> |
| 395 |
inline double todouble(const T &x){ return double(x); }; |
| 396 |
|
| 397 |
template<> |
| 398 |
inline double todouble<double>(const double &x) { return x; } |
| 399 |
template<> |
| 400 |
inline double todouble<float>(const float &x) { return x; } |
| 401 |
template<> |
| 402 |
inline double todouble<quadruple>(const quadruple &x) { return quad2double(x); } |
| 403 |
template<> |
| 404 |
inline double todouble<complex<double> >(const complex<double> &x) { return x.real(); } |
| 405 |
template<> |
| 406 |
inline double todouble<complex<float> >(const complex<float> &x) { return x.real(); } |
| 407 |
|
| 408 |
template<> |
| 409 |
inline double todouble<complex<quadruple> >(const complex<quadruple> &x) { |
| 410 |
return quad2double(x.real()); |
| 411 |
} |
| 412 |
#ifndef NO_OCTRUPLE |
| 413 |
template<> |
| 414 |
inline double todouble<octruple>(const octruple &x) { return oct2double(x); } |
| 415 |
|
| 416 |
template<> |
| 417 |
inline double todouble<complex<octruple> >(const complex<octruple> &x) { |
| 418 |
return oct2double(x.real()); |
| 419 |
} |
| 420 |
#endif |
| 421 |
|
| 422 |
|
| 423 |
template<typename T> |
| 424 |
void printscalar(const bool verbose, FILE *fp, T x); |
| 425 |
template<> |
| 426 |
void printscalar<double>(const bool verbose, FILE *fp, double x); |
| 427 |
template<> |
| 428 |
void printscalar<float>(const bool verbose, FILE *fp, float x); |
| 429 |
template<> |
| 430 |
void printscalar<quadruple>(const bool verbose, FILE *fp, quadruple x); |
| 431 |
template<> |
| 432 |
void printscalar<complex<double> >(const bool verbose, |
| 433 |
FILE *fp, complex<double> x); |
| 434 |
template<> |
| 435 |
void printscalar<complex<float> >(const bool verbose, |
| 436 |
FILE *fp, complex<float> x); |
| 437 |
template<> |
| 438 |
void printscalar<complex<quadruple> >(const bool verbose, |
| 439 |
FILE *fp, complex<quadruple> x); |
| 440 |
|
| 441 |
template<typename T, typename U> |
| 442 |
T tocomplex(const U &x); |
| 443 |
template<> |
| 444 |
double tocomplex<double, double>(const double &x); |
| 445 |
template<> |
| 446 |
float tocomplex<float, float>(const float &x); |
| 447 |
template<> |
| 448 |
quadruple tocomplex<quadruple, quadruple>(const quadruple &x); |
| 449 |
template<> |
| 450 |
complex<double> tocomplex<complex<double>, double>(const double &x); |
| 451 |
template<> |
| 452 |
complex<quadruple> tocomplex<complex<quadruple>, quadruple>(const quadruple &x); |
| 453 |
|
| 454 |
|
| 455 |
template<typename T, typename U> |
| 456 |
inline T tocomplex(const U &x) |
| 457 |
{ |
| 458 |
fprintf(stderr, "%s %d : specilized template is not defined\n", |
| 459 |
__FILE__, __LINE__); |
| 460 |
return T(0.0); |
| 461 |
} |
| 462 |
|
| 463 |
template<> |
| 464 |
inline double tocomplex<double, double>(const double &x){ return x; } |
| 465 |
|
| 466 |
template<> |
| 467 |
inline float tocomplex<float, float>(const float &x){ return x; } |
| 468 |
|
| 469 |
template<> |
| 470 |
inline quadruple tocomplex<quadruple, quadruple>(const quadruple &x) |
| 471 |
{ |
| 472 |
return x; |
| 473 |
} |
| 474 |
|
| 475 |
template<> |
| 476 |
inline complex<double> tocomplex<complex<double>, double>(const double &x) |
| 477 |
{ |
| 478 |
return std::complex<double>(x, 0.0); |
| 479 |
} |
| 480 |
|
| 481 |
template<> |
| 482 |
inline complex<quadruple> tocomplex<complex<quadruple>, |
| 483 |
quadruple>(const quadruple &x) |
| 484 |
{ |
| 485 |
quadruple zero(0.0); |
| 486 |
return std::complex<quadruple>(x, zero); |
| 487 |
} |
| 488 |
|
| 489 |
#ifndef NO_OCTRUPLE |
| 490 |
template<> |
| 491 |
inline octruple tocomplex<octruple>(const octruple &x) { return x; } |
| 492 |
|
| 493 |
template<> |
| 494 |
inline complex<octruple> tocomplex<complex<octruple> >(const octruple &x) { |
| 495 |
octruple zero(0.0); |
| 496 |
return std::complex<octruple>(x, zero); |
| 497 |
} |
| 498 |
#endif |
| 499 |
|
| 500 |
#endif |