-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinline.h
115 lines (102 loc) · 2.84 KB
/
inline.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* include/inline.h
*
* This work was supported by the Director, Office of Science, Division
* of Mathematical, Information, and Computational Sciences of the
* U.S. Department of Energy under contract number DE-AC03-76SF00098.
*
* Copyright (c) 2000-2001
*
* This file contains the basic functions used both by double-double
* and quad-double package. These are declared as inline functions as
* they are the smallest building blocks of the double-double and
* quad-double arithmetic.
*/
#ifndef _INLINE_H_
#define _INLINE_H_
#define _SPLITTER 134217729.0 /* = 2^27 + 1 */
/*********** Basic Functions ************/
/* Computes fl(a+b) and err(a+b). Assumes |a| >= |b|. */
inline double quick_two_sum(double a, double b, double &err) {
double s = a + b;
err = b - (s - a);
return s;
}
/* Computes fl(a-b) and err(a-b). Assumes |a| >= |b| */
inline double quick_two_diff(double a, double b, double &err) {
double s = a - b;
err = (a - s) - b;
return s;
}
/* Computes fl(a+b) and err(a+b). */
inline double two_sum(double a, double b, double &err) {
double s = a + b;
double bb = s - a;
err = (a - (s - bb)) + (b - bb);
return s;
}
/* Computes fl(a-b) and err(a-b). */
inline double two_diff(double a, double b, double &err) {
double s = a - b;
double bb = s - a;
err = (a - (s - bb)) - (b + bb);
return s;
}
#ifndef HAS_FMA
/* Computes high word and lo word of a */
inline void split(double a, double &hi, double &lo) {
double temp;
temp = _SPLITTER * a;
hi = temp - (temp - a);
lo = a - hi;
}
#endif
/* Computes fl(a*b) and err(a*b). */
inline double two_prod(double a, double b, double &err) {
#ifdef HAS_FMA
double p = -a * b;
err = a * b + p;
return -p;
#else
double a_hi, a_lo, b_hi, b_lo;
double p = a * b;
split(a, a_hi, a_lo);
split(b, b_hi, b_lo);
err = ((a_hi * b_hi - p) + a_hi * b_lo + a_lo * b_hi) + a_lo * b_lo;
return p;
#endif
}
/* Computes fl(a*a) and err(a*a). Faster than the above method. */
inline double two_sqr(double a, double &err) {
#ifdef HAS_FMA
double p = -a * a;
err = a * a + p;
return -p;
#else
double hi, lo;
double q = a * a;
split(a, hi, lo);
err = ((hi * hi - q) + 2.0 * hi * lo) + lo * lo;
return q;
#endif
}
/* Computes the nearest integer to d. */
inline double nint(double d) {
if (d == floor(d))
return d;
return floor(d + 0.5);
}
/* Computes the truncated integer. */
inline double aint(double d) {
return (d >= 0.0) ? floor(d) : ceil(d);
}
/* These are provided to give consistent
interface for double with double-double and quad-double. */
inline void sincosh(double t, double &sinh_t, double &cosh_t) {
sinh_t = sinh(t);
cosh_t = cosh(t);
}
inline double sqr(double t) {
return t * t;
}
#endif /* _INLINE_H_ */