-
Notifications
You must be signed in to change notification settings - Fork 4
/
libeft.hxx
296 lines (224 loc) · 5.99 KB
/
libeft.hxx
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
This file is part of LibEFT, an error-free transformations library
Parts of this file originally come from Verrou
Copyright (C) 2014-2017
F. Févotte <[email protected]>
B. Lathuilière <[email protected]>
EDF SA
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#ifndef HXX_LIBEFT
#define HXX_LIBEFT
#include <functional>
#include <cmath>
#include <xmmintrin.h>
#ifdef EFT_VERROU
#include <valgrind/verrou.h>
#endif // EFT_VERROU
#ifdef EFT_FMA
#include <immintrin.h>
#include <fmaintrin.h>
#endif // EFT_FMA
namespace EFT {
// * Helper functions
// ** Stop/start instrumenting
inline void stopInstr () {
#ifdef EFT_VERROU
VERROU_STOP_INSTRUMENTATION;
#endif // EFT_VERROU
}
inline void startInstr () {
#ifdef EFT_VERROU
VERROU_START_INSTRUMENTATION;
#endif // EFT_VERROU
}
#ifdef EFT_FMA
// ** Fused Multiply-Add (FMA) using intrinsics
// return a * b + c
inline double fma (const double& a, const double& b, const double& c){
double d;
__m128d ai, bi,ci,di;
ai = _mm_load_sd(&a);
bi = _mm_load_sd(&b);
ci = _mm_load_sd(&c);
di=_mm_fmadd_sd(ai,bi,ci);
d=_mm_cvtsd_f64(di);
return d;
}
inline float fma (const float& a, const float& b, const float& c){
float d;
__m128 ai, bi,ci,di;
ai = _mm_load_ss(&a);
bi = _mm_load_ss(&b);
ci = _mm_load_ss(&c);
di=_mm_fmadd_ss(ai,bi,ci);
d=_mm_cvtss_f32(di);
return d;
}
#endif // EFT_FMA
// ** Data types for standard arithmetic operations using intrinsics
// this prevents harmful compiler optimizations where needed
template <typename Real>
class Intrinsic {};
template <>
class Intrinsic<double> {
public:
Intrinsic (const double &x)
: _reg (_mm_load_sd(&x))
{}
Intrinsic (__m128d reg)
: _reg (reg)
{}
Intrinsic ()
: _reg()
{}
Intrinsic operator- (const Intrinsic& y) const {
return _mm_sub_sd(_reg, y._reg);
}
Intrinsic operator+ (const Intrinsic& y) const {
return _mm_add_sd(_reg, y._reg);
}
Intrinsic operator* (const Intrinsic& y) const {
return _mm_mul_sd(_reg, y._reg);
}
double val () const {
return _mm_cvtsd_f64 (_reg);
}
private:
__m128d _reg;
};
template <>
class Intrinsic<float> {
public:
Intrinsic (const float &x)
: _reg (_mm_load_ss(&x))
{}
Intrinsic (__m128 reg)
: _reg (reg)
{}
Intrinsic ()
: _reg()
{}
Intrinsic operator- (const Intrinsic& y) const {
return _mm_sub_ss(_reg, y._reg);
}
Intrinsic operator+ (const Intrinsic& y) const {
return _mm_add_ss(_reg, y._reg);
}
Intrinsic operator* (const Intrinsic& y) const {
return _mm_mul_ss(_reg, y._reg);
}
float val () const {
return _mm_cvtss_f32 (_reg);
}
private:
__m128 _reg;
};
// ** Split
// Splitting factor used by split
template<class Real> constexpr const Real splitFactor = 0. / 0.; // NaN
// Double precision: ((2^27)+1), where 27 = sup(53/2)
template<> constexpr const double splitFactor<double> = 134217729;
// Single precision: ((2^12)+1), where 12 = 24/2
template<> constexpr const float splitFactor<float> = 4097;
// a = x + y
// x = hi(a)
// y = lo(a)
template <typename Real>
static inline void split(const Intrinsic<Real>& ia, Intrinsic<Real>& ix, Intrinsic<Real>& iy){
typedef const Intrinsic<Real> I;
stopInstr();
I ic = I(EFT::splitFactor<Real>) * ia;
ix = ic - (ic-ia);
iy = ia - ix;
startInstr();
}
// * Base blocks for compensated algorithms
// ** TwoSum
template <typename Real>
void fastTwoSum (/* IN */ const Real &a, const Real &b,
/* OUT */ Real &x, Real &e) {
// Fast2Sum algorithm from
// T. J. Dekker. "A Floating-Point Technique for Extending the Available Precision". 1971
Real A, B;
if (std::abs(a) > std::abs(b)) {
A = a;
B = b;
} else {
A = b;
B = a;
}
typedef const Intrinsic<Real> I;
I iA(A), iB(B);
I ix = iA + iB;
I iz = ix - iA;
I ie = iB - iz;
x = ix.val();
e = ie.val();
}
template <typename Real>
void twoSum (/* IN */ const Real &a, const Real &b,
/* OUT */ Real &x, Real &e) {
typedef const Intrinsic<Real> I;
I ia(a), ib(b);
I ix = ia + ib;
I iz = ix - ia;
I ie = (ia - (ix-iz)) + (ib-iz);
x = ix.val();
e = ie.val();
}
// ** TwoProd
// a * b = x + e
// x = rnd (a * b)
template <typename Real>
void twoProd (/* IN */ const Real &a, const Real &b,
/* OUT */ Real &x, Real &e) {
const Real c = a * b;
#ifdef EFT_FMA
e = EFT::fma(a, b, -c);
#else
typedef Intrinsic<Real> I;
const I ia(a), ib(b), ic(c);
I ia1, ia2, ib1, ib2;
EFT::split<Real> (ia, ia1, ia2);
EFT::split<Real> (ib, ib1, ib2);
I itmp = (ia1*ib1-ic) + ia1*ib2 + ia2*ib1;
const Real a2(ia2.val()), b2(ib2.val());
e = itmp.val() + a2*b2;
#endif
x = c;
}
// ** TwoProdSum
// a * b + c approx= x + e
template <typename Real>
void twoProdSum (/* IN */ const Real & a, const Real & b, const Real & c,
/* OUT */ Real & x, Real & e) {
Real ph, pl; EFT::twoProd (a, b, ph, pl);
Real uh, ul; EFT::twoSum (c, ph, uh, ul);
#if 0 //def EFT_FMA
// ErrFmaAppr from
// S. Boldo, JM. Muller. "Exact and Approximated Error of the FMA". 2011
x = EFT::fma (a, b, c);
typedef const Intrinsic<Real> I;
I ix(x), iuh(uh), iul(ul), ipl(pl);
I ie = (iuh-ix) + (ipl+iul);
e = ie.val();
#else
x = uh;
e = pl + ul;
#endif // EFT_FMA
}
}
#endif // ndef HXX_LIBEFT