| 1 |
/* |
| 2 |
* include/inline.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-2001 |
| 9 |
* |
| 10 |
* This file contains the basic functions used both by double-double |
| 11 |
* and quad-double package. These are declared as inline functions as |
| 12 |
* they are the smallest building blocks of the double-double and |
| 13 |
* quad-double arithmetic. |
| 14 |
*/ |
| 15 |
#ifndef _QD_INLINE_H |
| 16 |
#define _QD_INLINE_H |
| 17 |
|
| 18 |
#define _QD_SPLITTER 134217729.0 // = 2^27 + 1 |
| 19 |
#define _QD_SPLIT_THRESH 6.69692879491417e+299 // = 2^996 |
| 20 |
|
| 21 |
#ifdef QD_VACPP_BUILTINS_H |
| 22 |
/* For VisualAge C++ __fmadd */ |
| 23 |
#include <builtins.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <cmath> |
| 27 |
#include <limits> |
| 28 |
|
| 29 |
namespace qd { |
| 30 |
|
| 31 |
static const double _d_nan = std::numeric_limits<double>::quiet_NaN(); |
| 32 |
static const double _d_inf = std::numeric_limits<double>::infinity(); |
| 33 |
|
| 34 |
/*********** Basic Functions ************/ |
| 35 |
/* Computes fl(a+b) and err(a+b). Assumes |a| >= |b|. */ |
| 36 |
inline double quick_two_sum(double a, double b, volatile double &err) { |
| 37 |
volatile double s = a + b; |
| 38 |
volatile double ss = s - a; |
| 39 |
err = b - ss; |
| 40 |
// err = b - (s - a); |
| 41 |
return s; |
| 42 |
} |
| 43 |
|
| 44 |
/* Computes fl(a-b) and err(a-b). Assumes |a| >= |b| */ |
| 45 |
inline double quick_two_diff(double a, double b, volatile double &err) { |
| 46 |
volatile double s = a - b; |
| 47 |
volatile double ss = a - s; |
| 48 |
err = ss - b; |
| 49 |
// err = (a - s) - b; |
| 50 |
return s; |
| 51 |
} |
| 52 |
|
| 53 |
/* Computes fl(a+b) and err(a+b). */ |
| 54 |
inline double two_sum(double a, double b, volatile double &err) { |
| 55 |
volatile double s = a + b; |
| 56 |
volatile double bb = s - a; |
| 57 |
volatile double sbb = s - bb; |
| 58 |
volatile double bbb = b - bb; |
| 59 |
err = (a - sbb) + bbb; |
| 60 |
// err = (a - (s - bb)) + (b - bb); |
| 61 |
return s; |
| 62 |
} |
| 63 |
|
| 64 |
/* Computes fl(a-b) and err(a-b). */ |
| 65 |
inline double two_diff(double a, double b, volatile double &err) { |
| 66 |
volatile double s = a - b; |
| 67 |
volatile double bb = s - a; |
| 68 |
volatile double sbb = s - bb; |
| 69 |
volatile double bbb = b + bb; |
| 70 |
err = (a - sbb) - bbb; |
| 71 |
return s; |
| 72 |
} |
| 73 |
|
| 74 |
#ifndef QD_FMS |
| 75 |
/* Computes high word and lo word of a */ |
| 76 |
inline void split(double a, double &hi, double &lo) { |
| 77 |
volatile double temp; |
| 78 |
if (a > _QD_SPLIT_THRESH || a < -_QD_SPLIT_THRESH) { |
| 79 |
a *= 3.7252902984619140625e-09; // 2^-28 |
| 80 |
temp = _QD_SPLITTER * a; |
| 81 |
volatile double tempa = temp - a; |
| 82 |
hi = temp - tempa; |
| 83 |
lo = a - hi; |
| 84 |
hi *= 268435456.0; // 2^28 |
| 85 |
lo *= 268435456.0; // 2^28 |
| 86 |
} else { |
| 87 |
temp = _QD_SPLITTER * a; |
| 88 |
volatile double tempa = temp - a; |
| 89 |
hi = temp - tempa; |
| 90 |
lo = a - hi; |
| 91 |
} |
| 92 |
} |
| 93 |
#endif |
| 94 |
|
| 95 |
/* Computes fl(a*b) and err(a*b). */ |
| 96 |
inline double two_prod(double a, double b, volatile double &err) { |
| 97 |
#ifdef QD_FMS |
| 98 |
volatile double p = a * b; |
| 99 |
err = QD_FMS(a, b, p); |
| 100 |
return p; |
| 101 |
#else |
| 102 |
double a_hi, a_lo, b_hi, b_lo; |
| 103 |
volatile double p = a * b; |
| 104 |
split(a, a_hi, a_lo); |
| 105 |
split(b, b_hi, b_lo); |
| 106 |
err = ((a_hi * b_hi - p) + a_hi * b_lo + a_lo * b_hi) + a_lo * b_lo; |
| 107 |
return p; |
| 108 |
#endif |
| 109 |
} |
| 110 |
|
| 111 |
/* Computes fl(a*a) and err(a*a). Faster than the above method. */ |
| 112 |
inline double two_sqr(double a, volatile double &err) { |
| 113 |
#ifdef QD_FMS |
| 114 |
volatile double p = a * a; |
| 115 |
err = QD_FMS(a, a, p); |
| 116 |
return p; |
| 117 |
#else |
| 118 |
double hi, lo; |
| 119 |
volatile double q = a * a; |
| 120 |
split(a, hi, lo); |
| 121 |
err = ((hi * hi - q) + 2.0 * hi * lo) + lo * lo; |
| 122 |
return q; |
| 123 |
#endif |
| 124 |
} |
| 125 |
|
| 126 |
/* Computes the nearest integer to d. */ |
| 127 |
inline double nint(double d) { |
| 128 |
if (d == std::floor(d)) |
| 129 |
return d; |
| 130 |
return std::floor(d + 0.5); |
| 131 |
} |
| 132 |
|
| 133 |
/* Computes the truncated integer. */ |
| 134 |
inline double aint(double d) { |
| 135 |
return (d >= 0.0) ? std::floor(d) : std::ceil(d); |
| 136 |
} |
| 137 |
|
| 138 |
/* These are provided to give consistent |
| 139 |
interface for double with double-double and quad-double. */ |
| 140 |
inline void sincosh(double t, double &sinh_t, double &cosh_t) { |
| 141 |
sinh_t = std::sinh(t); |
| 142 |
cosh_t = std::cosh(t); |
| 143 |
} |
| 144 |
|
| 145 |
inline double sqr(double t) { |
| 146 |
return t * t; |
| 147 |
} |
| 148 |
|
| 149 |
inline double to_double(double a) { return a; } |
| 150 |
inline int to_int(double a) { return static_cast<int>(a); } |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
#endif /* _QD_INLINE_H */ |