1*12129755Smartynas /* $OpenBSD: polevll.c,v 1.2 2013/11/12 20:35:09 martynas Exp $ */
249393c00Smartynas
349393c00Smartynas /*
449393c00Smartynas * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
549393c00Smartynas *
649393c00Smartynas * Permission to use, copy, modify, and distribute this software for any
749393c00Smartynas * purpose with or without fee is hereby granted, provided that the above
849393c00Smartynas * copyright notice and this permission notice appear in all copies.
949393c00Smartynas *
1049393c00Smartynas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1149393c00Smartynas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1249393c00Smartynas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1349393c00Smartynas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1449393c00Smartynas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1549393c00Smartynas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1649393c00Smartynas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1749393c00Smartynas */
1849393c00Smartynas
1949393c00Smartynas /* polevll.c
2049393c00Smartynas * p1evll.c
2149393c00Smartynas *
2249393c00Smartynas * Evaluate polynomial
2349393c00Smartynas *
2449393c00Smartynas *
2549393c00Smartynas *
2649393c00Smartynas * SYNOPSIS:
2749393c00Smartynas *
2849393c00Smartynas * int N;
2949393c00Smartynas * long double x, y, coef[N+1], polevl[];
3049393c00Smartynas *
3149393c00Smartynas * y = polevll( x, coef, N );
3249393c00Smartynas *
3349393c00Smartynas *
3449393c00Smartynas *
3549393c00Smartynas * DESCRIPTION:
3649393c00Smartynas *
3749393c00Smartynas * Evaluates polynomial of degree N:
3849393c00Smartynas *
3949393c00Smartynas * 2 N
4049393c00Smartynas * y = C + C x + C x +...+ C x
4149393c00Smartynas * 0 1 2 N
4249393c00Smartynas *
4349393c00Smartynas * Coefficients are stored in reverse order:
4449393c00Smartynas *
4549393c00Smartynas * coef[0] = C , ..., coef[N] = C .
4649393c00Smartynas * N 0
4749393c00Smartynas *
4849393c00Smartynas * The function p1evll() assumes that coef[N] = 1.0 and is
4949393c00Smartynas * omitted from the array. Its calling arguments are
5049393c00Smartynas * otherwise the same as polevll().
5149393c00Smartynas *
5249393c00Smartynas *
5349393c00Smartynas * SPEED:
5449393c00Smartynas *
5549393c00Smartynas * In the interest of speed, there are no checks for out
5649393c00Smartynas * of bounds arithmetic. This routine is used by most of
5749393c00Smartynas * the functions in the library. Depending on available
5849393c00Smartynas * equipment features, the user may wish to rewrite the
5949393c00Smartynas * program in microcode or assembly language.
6049393c00Smartynas *
6149393c00Smartynas */
6249393c00Smartynas
6349393c00Smartynas #include <math.h>
6449393c00Smartynas
65*12129755Smartynas #include "math_private.h"
66*12129755Smartynas
6749393c00Smartynas /*
6849393c00Smartynas * Polynomial evaluator:
6949393c00Smartynas * P[0] x^n + P[1] x^(n-1) + ... + P[n]
7049393c00Smartynas */
7149393c00Smartynas long double
__polevll(long double x,void * PP,int n)7249393c00Smartynas __polevll(long double x, void *PP, int n)
7349393c00Smartynas {
7449393c00Smartynas long double y;
7549393c00Smartynas long double *P;
7649393c00Smartynas
7749393c00Smartynas P = (long double *)PP;
7849393c00Smartynas y = *P++;
7949393c00Smartynas do {
8049393c00Smartynas y = y * x + *P++;
8149393c00Smartynas } while (--n);
8249393c00Smartynas
8349393c00Smartynas return (y);
8449393c00Smartynas }
8549393c00Smartynas
8649393c00Smartynas /*
8749393c00Smartynas * Polynomial evaluator:
8849393c00Smartynas * x^n + P[0] x^(n-1) + P[1] x^(n-2) + ... + P[n]
8949393c00Smartynas */
9049393c00Smartynas long double
__p1evll(long double x,void * PP,int n)9149393c00Smartynas __p1evll(long double x, void *PP, int n)
9249393c00Smartynas {
9349393c00Smartynas long double y;
9449393c00Smartynas long double *P;
9549393c00Smartynas
9649393c00Smartynas P = (long double *)PP;
9749393c00Smartynas n -= 1;
9849393c00Smartynas y = x + *P++;
9949393c00Smartynas do {
10049393c00Smartynas y = y * x + *P++;
10149393c00Smartynas } while (--n);
10249393c00Smartynas
10349393c00Smartynas return (y);
10449393c00Smartynas }
105