2 /* { dg-options "-O2 -lm" } */
3 /* { dg-options "-O2 -msse2 -mfpmath=sse" { target { { i?86-*-* x86_64-*-* } && ia32 } } } */
4 /* { dg-require-effective-target sse2_runtime { target { { i?86-*-* x86_64-*-* } && ia32 } } } */
6 extern double fabs (double);
7 extern void abort (void);
9 const int MAX_ITERATIONS = 50;
10 const double SMALL_ENOUGH = 1.0e-10;
11 const double RELERROR = 1.0e-12;
21 polyeval (double x, int n, double *Coeffs)
27 for (i = n - 1; i >= 0; i--)
28 val = val * x + Coeffs[i];
34 regula_falsa (int order, double *coef, double a, double b, double *val)
37 double fa, fb, x, fx, lfx;
39 fa = polyeval (a, order, coef);
40 fb = polyeval (b, order, coef);
45 if (fabs (fa) < SMALL_ENOUGH)
51 if (fabs (fb) < SMALL_ENOUGH)
59 for (its = 0; its < MAX_ITERATIONS; its++)
61 x = (fb * a - fa * b) / (fb - fa);
62 fx = polyeval (x, order, coef);
63 if (fabs (x) > RELERROR)
65 if (fabs (fx / x) < RELERROR)
73 if (fabs (fx) < RELERROR)
115 if (fabs (b - a) < RELERROR)
128 numchanges (int np, polynomial * sseq, double a)
135 lf = polyeval (a, sseq[0].ord, sseq[0].coef);
137 for (s = sseq + 1; s <= sseq + np; s++)
139 f = polyeval (a, s->ord, s->coef);
140 if (lf == 0.0 || lf * f < 0)
150 sbisect (int np, polynomial * sseq, double min_value, double max_value,
151 int atmin, int atmax, double *roots)
154 int n1, n2, its, atmid;
156 if ((atmin - atmax) == 1)
158 if (regula_falsa (sseq->ord, sseq->coef, min_value, max_value, roots))
162 for (its = 0; its < MAX_ITERATIONS; its++)
164 mid = (min_value + max_value) / 2;
165 atmid = numchanges (np, sseq, mid);
166 if ((atmid < atmax) || (atmid > atmin))
169 if (fabs (mid) > RELERROR)
171 if (fabs ((max_value - min_value) / mid) < RELERROR)
179 if (fabs (max_value - min_value) < RELERROR)
186 if ((atmin - atmid) == 0)
197 for (its = 0; its < MAX_ITERATIONS; its++)
199 mid = (min_value + max_value) / 2;
200 atmid = numchanges (np, sseq, mid);
201 if ((atmid < atmax) || (atmid > atmin))
204 if (fabs (mid) > RELERROR)
206 if (fabs ((max_value - min_value) / mid) < RELERROR)
214 if (fabs (max_value - min_value) < RELERROR)
224 if ((n1 != 0) && (n2 != 0))
226 n1 = sbisect (np, sseq, min_value, mid, atmin, atmid, roots);
227 n2 = sbisect (np, sseq, mid, max_value, atmid, atmax, &roots[n1]);
245 polynomial sseq[7] = {
246 {6, {0.15735259075109281, -5.1185263411378736, 1.8516070705868664,
247 7.348009172322695, -2.2152395279161343, -2.7543325329350692, 1.0}},
248 {5, {-0.8530877235229789, 0.61720235686228875, 3.6740045861613475,
249 -1.4768263519440896, -2.2952771107792245, 1.0}},
250 {4, {0.13072124257049417, 2.2220687798791126, -1.6299431586726509,
251 -1.6718404582408546, 1.0}},
252 {3, {0.86776597575462633, -2.1051099695282511, -0.49008580100694688,
254 {2, {-11.117984175064155, 10.89886635045883, 1.0}},
255 {1, {0.94453099602191237, -1.0}},
256 {0, {-0.068471716890574186}}
262 nroots = sbisect (6, sseq, 0.0, 10000000.0, 5, 1, roots);