1/*
2 * Copyright (C) 2004-2010 NXP Software
3 * Copyright (C) 2010 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "LVM_Types.h"
19#include "LVM_Macros.h"
20#include "ScalarArithmetic.h"
21
22/*-------------------------------------------------------------------------*/
23/* FUNCTION:                                                               */
24/*   LVM_Polynomial                                                        */
25/*                                                                         */
26/* DESCRIPTION:                                                            */
27/*   This function performs polynomial expansion                           */
28/*  Y = (A0 + A1*X + A2*X2 + A3*X3 + �.. + AN*xN) << AN+1                  */
29/*                                                                         */
30/*  LVM_INT32 LVM_Polynomial(LVM_UINT16    N,                              */
31/*                           LVM_INT32    *pCoefficients,                  */
32/*                           LVM_INT32    X)                               */
33/*                                                                         */
34/* PARAMETERS:                                                             */
35/*                                                                         */
36/*  N                is the polynomial order                               */
37/*  pCoefficients    is the ptr to polynomial coefficients A0,A1.. in Q.31 */
38/*  X                is the input variable                                 */
39/*                                                                         */
40/* RETURNS:                                                                */
41/*   The result of the polynomial expansion in Q1.31 format                */
42/*-------------------------------------------------------------------------*/
43#ifdef BUILD_FLOAT
44LVM_FLOAT LVM_Polynomial(LVM_UINT16    N,
45                         LVM_FLOAT    *pCoefficients,
46                         LVM_FLOAT    X)
47{
48    LVM_INT32 i;
49    LVM_FLOAT Y,A,XTemp,Temp,sign;
50
51    Y = *pCoefficients; /* Y=A0*/
52    pCoefficients++;
53
54    if(X == -1.0f)
55    {
56        Temp = -1;
57        sign = Temp;
58        for(i = 1; i <= N; i++)
59        {
60            Y += ((*pCoefficients) * sign);
61            pCoefficients++;
62            sign *= Temp;
63        }
64
65
66    }
67    else
68    {
69        XTemp = X;
70        for(i = N-1; i >= 0; i--)
71        {
72            A = *pCoefficients;
73            pCoefficients++;
74
75            Temp = A * XTemp;
76            Y += Temp;
77
78            Temp = XTemp * X;
79            XTemp = Temp;
80        }
81    }
82    return Y;
83}
84#else
85LVM_INT32 LVM_Polynomial(LVM_UINT16    N,
86                         LVM_INT32    *pCoefficients,
87                         LVM_INT32    X)
88{
89    LVM_INT32 i;
90    LVM_INT32 Y,A,XTemp,Temp,sign;
91
92    Y=*pCoefficients; /* Y=A0*/
93    pCoefficients++;
94
95    if((LVM_UINT32)X==0x80000000)
96    {
97        Temp=-1;
98        sign=Temp;
99        for(i=1;i<=N;i++)
100        {
101            Y+=((*pCoefficients)*sign);
102            pCoefficients++;
103            sign*=Temp;
104        }
105
106
107    }
108    else
109    {
110        XTemp=X;
111        for(i=N-1;i>=0;i--)
112        {
113            A=*pCoefficients;
114            pCoefficients++;
115
116            MUL32x32INTO32(A,XTemp,Temp,31)
117            Y+=Temp;
118
119            MUL32x32INTO32(XTemp,X,Temp,31)
120            XTemp=Temp;
121        }
122    }
123    A=*pCoefficients;
124    pCoefficients++;
125
126    if(A<0)
127    {
128        A=Abs_32(A);
129        Y=Y>>A;
130    }
131    else
132    {
133        Y = Y<<A;
134    }
135    return Y;
136}
137#endif
138