2 /* { dg-options "-O2 -lm" } */
3 /* { dg-options "-O2 -msse2 -mfpmath=sse" { target { { i?86-*-* x86_64-*-* } && ilp32 } } } */
10 extern double fabs (double);
11 extern void abort (void);
13 const int MAX_ITERATIONS = 50;
14 const double SMALL_ENOUGH = 1.0e-10;
15 const double RELERROR = 1.0e-12;
25 polyeval (double x, int n, double *Coeffs)
31 for (i = n - 1; i >= 0; i--)
32 val = val * x + Coeffs[i];
38 regula_falsa (int order, double *coef, double a, double b, double *val)
41 double fa, fb, x, fx, lfx;
43 fa = polyeval (a, order, coef);
44 fb = polyeval (b, order, coef);
49 if (fabs (fa) < SMALL_ENOUGH)
55 if (fabs (fb) < SMALL_ENOUGH)
63 for (its = 0; its < MAX_ITERATIONS; its++)
65 x = (fb * a - fa * b) / (fb - fa);
66 fx = polyeval (x, order, coef);
67 if (fabs (x) > RELERROR)
69 if (fabs (fx / x) < RELERROR)
77 if (fabs (fx) < RELERROR)
119 if (fabs (b - a) < RELERROR)
132 numchanges (int np, polynomial * sseq, double a)
139 lf = polyeval (a, sseq[0].ord, sseq[0].coef);
141 for (s = sseq + 1; s <= sseq + np; s++)
143 f = polyeval (a, s->ord, s->coef);
144 if (lf == 0.0 || lf * f < 0)
154 sbisect (int np, polynomial * sseq, double min_value, double max_value,
155 int atmin, int atmax, double *roots)
158 int n1, n2, its, atmid;
160 if ((atmin - atmax) == 1)
162 if (regula_falsa (sseq->ord, sseq->coef, min_value, max_value, roots))
166 for (its = 0; its < MAX_ITERATIONS; its++)
168 mid = (min_value + max_value) / 2;
169 atmid = numchanges (np, sseq, mid);
170 if ((atmid < atmax) || (atmid > atmin))
173 if (fabs (mid) > RELERROR)
175 if (fabs ((max_value - min_value) / mid) < RELERROR)
183 if (fabs (max_value - min_value) < RELERROR)
190 if ((atmin - atmid) == 0)
201 for (its = 0; its < MAX_ITERATIONS; its++)
203 mid = (min_value + max_value) / 2;
204 atmid = numchanges (np, sseq, mid);
205 if ((atmid < atmax) || (atmid > atmin))
208 if (fabs (mid) > RELERROR)
210 if (fabs ((max_value - min_value) / mid) < RELERROR)
218 if (fabs (max_value - min_value) < RELERROR)
228 if ((n1 != 0) && (n2 != 0))
230 n1 = sbisect (np, sseq, min_value, mid, atmin, atmid, roots);
231 n2 = sbisect (np, sseq, mid, max_value, atmid, atmax, &roots[n1]);
249 polynomial sseq[7] = {
250 {6, {0.15735259075109281, -5.1185263411378736, 1.8516070705868664,
251 7.348009172322695, -2.2152395279161343, -2.7543325329350692, 1.0}},
252 {5, {-0.8530877235229789, 0.61720235686228875, 3.6740045861613475,
253 -1.4768263519440896, -2.2952771107792245, 1.0}},
254 {4, {0.13072124257049417, 2.2220687798791126, -1.6299431586726509,
255 -1.6718404582408546, 1.0}},
256 {3, {0.86776597575462633, -2.1051099695282511, -0.49008580100694688,
258 {2, {-11.117984175064155, 10.89886635045883, 1.0}},
259 {1, {0.94453099602191237, -1.0}},
260 {0, {-0.068471716890574186}}
267 unsigned int eax, ebx, ecx, edx;
269 if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
272 if (!(edx & bit_SSE2))
276 nroots = sbisect (6, sseq, 0.0, 10000000.0, 5, 1, roots);