-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEstrinPoly.cpp
371 lines (304 loc) · 7.81 KB
/
EstrinPoly.cpp
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include<algorithm>
template<typename T, int N>
class HornerPoly {
public:
HornerPoly(){}
HornerPoly(std::initializer_list<T> il) : p(std::begin(il)+1), c0(*il.begin()){}
HornerPoly(T const coeff[N+1]) : p(coeff+1), c0(*(coeff)){};
T operator()(T x) const { return c0 + x*p(x); }
private:
HornerPoly<T,N-1> p;
T c0;
};
template<typename T>
class HornerPoly<T,0> {
public:
HornerPoly(){}
HornerPoly(T coeff) : c0(coeff){};
HornerPoly(T const * coeff) : c0(*coeff){};
T operator()(T) const { return c0; }
private:
T c0;
};
namespace padeDetails {
template<typename T>
struct Value {
Value(){}
Value(T ip, T iq): p(ip),q(iq){}
void prod(Value<T> & c, T x) const {c.p=c.p*x+p; c.q=c.q*x+q;}
T div() const {return p/q;}
T p,q;
};
}
template<typename T, int N>
class PadePoly {
public:
using ValueType=padeDetails::Value<T>;
PadePoly(){}
PadePoly(std::initializer_list<T> il) : pp(std::begin(il)+2), c0(*il.begin(),*(il.begin()+1)){}
PadePoly(T const coeff[2*(N+1)]) : pp(coeff+2), c0(*(coeff),*(coeff+1)) {};
void value(ValueType & c,T x) const { pp.value(c,x); c0.prod(c,x) ;}
// T operator()(T x) const { ValueType v; value(v,x); return v.div(); }
T p (T x) const { return c0.p + x*pp.p(x); }
T q (T x) const { return c0.q + x*pp.q(x); }
T operator()(T x) const { return p(x)/q(x); }
private:
PadePoly<T,N-1> pp;
ValueType c0;
};
template<typename T>
class PadePoly<T,0> {
public:
using ValueType=padeDetails::Value<T>;
PadePoly(){}
PadePoly(std::initializer_list<T> il) : c0(*il.begin(),*(il.begin()+1)){}
PadePoly(T const * coeff) : c0(*coeff,*(coeff+1)){};
void value(ValueType & c, T) const { c=c0;}
T operator()(T) const { return c0.div(); }
T p(T) const { return c0.p; }
T q(T) const { return c0.q; }
private:
ValueType c0;
};
namespace estrinDetails {
constexpr int ilog2(int i, int n=0) { return i<=1 ? n : ilog2(i/2,n+1); };
template<typename T, int Q>
inline T xq(T x) { return xq<T,(Q/2)>(x)*xq<T,(Q/2)>(x); }
template<>
float xq<float,1>(float x) { return x; }
template<>
double xq<double,1>(double x) { return x; }
}
template<typename T, int N>
class EstrinPoly {
private:
static constexpr int K = estrinDetails::ilog2(N);
static constexpr int M = ((N>>K)<<K)-1;
public:
EstrinPoly(){}
EstrinPoly(std::initializer_list<T> il) : p1(il),p2(il.begin()+M+1) {}
EstrinPoly(T const coeff[N+1]) : p1(coeff), p2(coeff+M+1){};
T operator()(T x) const { T xx = estrinDetails::xq<T,M>(x*x); return p1(x) + xx*p2(x);}
private:;
EstrinPoly<T,M> p1;
EstrinPoly<T,N-M-1> p2;
};
template<typename T>
class EstrinPoly<T,0> {
public:
EstrinPoly(){}
EstrinPoly(T const * c0) : coeff(*c0){}
T operator()(T) const { return coeff;}
private:
T coeff;
};
template<typename T>
class EstrinPoly<T,1> {
public:
EstrinPoly(){}
EstrinPoly(std::initializer_list<T> il) : coeff{*il.begin(),*(il.begin()+1)}{}
EstrinPoly(T const c[2]) : coeff{c[0],c[1]}{}
T operator()(T x) const { return coeff[0]+ x*coeff[1];}
private:
T coeff[2];
};
/*
template<typename T>
class EstrinPoly<T,2> {
public:
T operator()(T x) const { return p1(x) + p2(x)*(x*x);}
EstrinPoly<T,1> p1;
EstrinPoly<T,0> p2;
};
template<typename T>
class EstrinPoly<T,3> {
public:
T operator()(T x) const { return p1(x) +p2(x)*(x*x);}
EstrinPoly<T,1> p1;
EstrinPoly<T,1> p2;
};
template<typename T>
class EstrinPoly<T,4> {
public:
EstrinPoly(){}
EstrinPoly(T coeff[5]) : p1(coeff), p2(coeff+4){};
T operator()(T x) const { return p1(x) + p2(x)*(x*x)*(x*x);}
EstrinPoly<T,3> p1;
EstrinPoly<T,0> p2;
};
template<typename T>
class EstrinPoly<T,5> {
public:
T operator()(T x) const { return p1(x) + p2(x)*(x*x)*(x*x);}
EstrinPoly<T,3> p1;
EstrinPoly<T,1> p2;
};
template<typename T>
class EstrinPoly<T,6> {
public:
T operator()(T x) const { return p1(x) + p2(x)*(x*x)*(x*x);}
EstrinPoly<T,3> p1;
EstrinPoly<T,2> p2;
};
template<typename T>
class EstrinPoly<T,7> {
public:
T operator()(T x) const { return p1(x) + p2(x)*(x*x)*(x*x);}
EstrinPoly<T,3> p1;
EstrinPoly<T,3> p2;
};
template<typename T>
class EstrinPoly<T,8> {
public:
T operator()(T x) const { T x4=(x*x)*(x*x); return p1(x) + p2(x)*x4*x4;}
EstrinPoly<T,7> p1;
EstrinPoly<T,0> p2;
};
*/
#include<iostream>
namespace justcomp {
typedef float Float;
// typedef double Float;
Float coeff[] ={1.f,3.f,-3.5f,0.45f,-1.1f,-0.45f,0.3f,-0.23f,0.95f,0.72f, 0.0912f,0.03f,-0.12f,0.67f, 0.11f, -0.023f, 0.056f, 0.011f,
1.f,3.f,-3.5f,0.45f,-1.1f,-0.45f,0.3f,-0.23f,0.95f,0.72f, 0.0912f,0.03f,-0.12f,0.67f, 0.11f, -0.023f, 0.056f, 0.011f};
constexpr int NN=1024*1024;
Float a[NN], b[NN];
template<int DEGREE>
void horner(HornerPoly<Float,DEGREE> const & p) {
for (int i=0; i!=NN; ++i)
b[i] = p(a[i]);
}
template<int DEGREE>
void estrin(EstrinPoly<Float,DEGREE> const & p) {
for (int i=0; i!=NN; ++i)
b[i] = p(a[i]);
}
template<int DEGREE>
void pade(PadePoly<Float,DEGREE> const & p) {
for (int i=0; i!=NN; ++i)
b[i] = p(a[i]);
}
}
// performance test
#include <x86intrin.h>
inline volatile unsigned long long rdtsc() {
return __rdtsc();
}
template<int DEGREE, int WHAT>
struct Measure{
inline void operator()(unsigned long long & t) const;
};
template<int DEGREE>
struct Measure<DEGREE,0> {
typedef justcomp::Float Float;
Measure() : p(justcomp::coeff){}
HornerPoly<Float,DEGREE> p;
inline
void operator()(unsigned long long & t) const{
t -= rdtsc();
justcomp::horner<DEGREE>(p);
t += rdtsc();
}
};
template<int DEGREE>
struct Measure<DEGREE,1> {
typedef justcomp::Float Float;
Measure() : p(justcomp::coeff){}
EstrinPoly<Float,DEGREE> p;
inline
void operator()(unsigned long long & t) const{
t -= rdtsc();
justcomp::estrin<DEGREE>(p);
t += rdtsc();
}
};
template<int DEGREE>
struct Measure<DEGREE,2> {
typedef justcomp::Float Float;
Measure() : p(justcomp::coeff){}
PadePoly<Float,DEGREE> p;
inline
void operator()(unsigned long long & t) const{
t -= rdtsc();
justcomp::pade<DEGREE>(p);
t += rdtsc();
}
};
double show() {
unsigned long long t=0;
Measure<5,1> measure;
measure(t);
return t;
}
template<int DEGREE, int WHAT>
void perf() {
Measure<DEGREE,WHAT> measure;
unsigned long long t=0;
union { float f;unsigned int i;} x;
float sum=0;
long long ntot=0;
x.f=1.0; // should be 0 but
while (x.f<32) { // this is 5*2^23 tests
++ntot;
int i=0;
while(i<justcomp::NN) {
x.i++;
justcomp::a[i++]=x.f;
justcomp::a[i++]= (WHAT<2) ? -x.f : 1.f/x.f;
}
measure(t);
for (int i=0; i!=justcomp::NN; ++i)
sum += justcomp::b[i];
}
const char * what[]={"Horner","Estrin","Pade"};
std::cout << "time for " << what[WHAT] << " degree " << DEGREE << " is "<< double(t)/double(justcomp::NN*ntot) << std::endl;
std::cout << "sum= " << sum << std::endl;;
}
#include<cassert>
int main() {
for (int i=0; i!=32; ++i) {
int k = estrinDetails::ilog2(i);
std::cout << i<< ":"<< k <<"," << ((i>>(k))<<(k)) <<" ";
}
std::cout << std::endl;
HornerPoly<float,1> h1({-1.f,1.f});
EstrinPoly<float,1> p1({-1.f,1.f});
HornerPoly<float,3> h3({-1.f,1.f,0.5f,-0.5f});
EstrinPoly<float,3> p3({-1.f,1.f,0.5f,-0.5f});
assert(h1(1.f)==p1(1.f));
assert(h1(2.f)==p1(2.f));
assert(h1(-2.f)==p1(-2.f));
assert(h1(1.f)==0);
assert(h3(0.f)==-1.f);
assert(p3(0.f)==-1.f);
assert(h3(1.f)==0);
assert(p3(1.f)==0);
perf<0,0>();
perf<0,1>();
perf<0,2>();
perf<1,0>();
perf<1,1>();
perf<1,2>();
perf<3,0>();
perf<3,1>();
perf<3,2>();
perf<4,0>();
perf<4,1>();
perf<4,2>();
perf<5,0>();
perf<5,1>();
perf<5,2>();
perf<7,0>();
perf<7,1>();
perf<7,2>();
perf<11,0>();
perf<11,1>();
perf<11,2>();
perf<14,0>();
perf<14,1>();
perf<14,2>();
perf<18,0>();
perf<18,1>();
perf<18,2>();
return 0;
}