Using loop unrolling to reduce the CPE of Horner Method enter image description here
I have to optimize a question using Horner Method. Its original solution was:
void poly(const double a[], double x, long degree, double *result) {
long i;
double r = a[degree];
for (i = degree - 1; i >= 0; i--) {
r = a[i] + r * x;
}
*result = r;
return;
}
I used loop unrolling to reduce its CPE, but the results are always different from the original scheme (>=1e-10).I hope the error can be less than 1e-10. Here is a portion of the code I wrote
for (i = degree-1 ; i >= 4; i-=4) {
double m1 = a[i - 2] * x;
double m2 = a[i - 1] * xx[2];
double m3 = a[i] * xx[3];
double m4 = r1 * xx[4];
r1 = a[i - 3] + m1 + m2 + m3 + m4;
}
double rest = a[i];
for (long j = i - 1; j >=0;j--){
rest = a[j] +x* rest;
}
*result = r1 + rest;
Why did this result occur? Is there an error in unfolding or is it limited by the accuracy of the double? thanks.