First public contribution.
1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\euser\maths\um_sin.cpp
15 // Floating point sine and cosine functions
21 #if defined(__USE_VFP_MATH) && !defined(__CPU_HAS_VFP)
22 #error __USE_VFP_MATH was defined but not __CPU_HAS_VFP - impossible combination, check variant.mmh
26 #ifndef __USE_VFP_MATH
28 LOCAL_D const TUint32 SinCoeffs[] =
30 0x2168C235,0xC90FDAA2,0x80000000, // polynomial approximation to sin(pi*x)
31 0x2DF200BF,0xA55DE731,0x80010001, // for |x| <= 0.25
32 0xAC273AA1,0xA335E33B,0x80000000,
33 0x5AB23F44,0x99696671,0x7FFE0001,
34 0xD585EAFE,0xA83C17D9,0x7FFB0000,
35 0xA30DE7AD,0xF1802BAC,0x7FF70001,
36 0xF57FD821,0xF1F6A1C9,0x7FF30000
39 LOCAL_D const TUint32 CosCoeffs[] =
41 0x00000000,0x80000000,0x7FFF0000, // polynomial approximation to cos(pi*x)
42 0xF22EF286,0x9DE9E64D,0x80010001, // for |x| <= 0.25
43 0xDAD59F90,0x81E0F840,0x80010000,
44 0xE4E45144,0xAAE9E3F1,0x7FFF0001,
45 0x3232D733,0xF0FA8342,0x7FFC0000,
46 0x03E16BB8,0xD368F6A3,0x7FF90001,
47 0x712FD084,0xFCE66DE2,0x7FF50000,
48 0x9E5353EE,0xD94951B0,0x7FF10001
51 LOCAL_D const TUint32 PiInvdata[] = {0x4E44152A,0xA2F9836E,0x7FFD0000}; // 1/pi
52 LOCAL_D const TUint32 Halfdata[] = {0x00000000,0x80000000,0x7FFE0000}; // 0.5
53 LOCAL_D const TUint32 Onedata[] = {0x00000000,0x80000000,0x7FFF0000}; // 1.0
55 LOCAL_C TInt CalcSinCos(TReal& aTrg, TRealX& aSrc, TBool aCos)
57 // Calculate sin(aSrc) if aCos=false or cos(aSrc) if aCos=true
58 // and write result to aTrg.
60 // Divide aSrc by pi and throw away integer part, but change sign
61 // of result if integer part odd. Replace aSrc with remainder.
62 // ( use identities sin(x+n*pi)=(-1)^n*sin(x)
63 // cos(x+n*pi)=(-1)^n*cos(x) )
64 // If aSrc>=0.5 replace aSrc with 1-aSrc, and change sign of result
66 // ( use identities sin(pi-x)=sin(x), cos(pi-x)=-cos(x) )
67 // If aSrc>=0.25 replace aSrc with 0.5-aSrc and swap sin and cos
68 // ( use identities sin(pi/2-x)=cos(x), cos(pi/2-x)=sin(x) )
69 // Use polynomial approximation to evaluate sin(pi*x) or cos(pi*x)
72 const TRealX& One = *(const TRealX*)Onedata;
73 const TRealX& Half = *(const TRealX*)Halfdata;
74 const TRealX& PiInv = *(const TRealX*)PiInvdata;
79 if (n<KMaxTInt && n>KMinTInt)
87 if (aSrc.iExp>=0x7FFE) // if remainder>=pi/2
93 if (aSrc.iExp>=0x7FFD) // if remainder>=pi/4
95 aSrc=Half-aSrc; // take complementary angle
96 aCos=!aCos; // and swap sin and cos
99 Math::PolyX(y,aSrc*aSrc,7,(const TRealX*)CosCoeffs);
102 Math::PolyX(y,aSrc*aSrc,6,(const TRealX*)SinCoeffs);
107 return y.GetTReal(aTrg);
115 EXPORT_C TInt Math::Sin(TReal& aTrg, const TReal& aSrc)
117 Calculates the sine of a number.
119 @param aTrg A reference containing the result.
120 @param aSrc The argument of the sin function in radians.
122 @return KErrNone if successful, otherwise another of
123 the system-wide error codes.
129 r=CalcSinCos(aTrg,x,EFalse);
139 EXPORT_C TInt Math::Cos(TReal& aTrg, const TReal& aSrc)
141 Calculates the cosine of a number.
143 @param aTrg A reference containing the result.
144 @param aSrc The argument of the cos function in radians
146 @return KErrNone if successful, otherwise another of
147 the system-wide error codes.
153 r=CalcSinCos(aTrg,x,ETrue);
160 #else // __USE_VFP_MATH
162 // definitions come from RVCT math library
163 extern "C" TReal sin(TReal);
164 extern "C" TReal cos(TReal);
166 EXPORT_C TInt Math::Sin(TReal& aTrg, const TReal& aSrc)
168 if (aSrc<KMaxTInt && aSrc>KMinTInt)
171 if (Math::IsFinite(aTrg))
178 EXPORT_C TInt Math::Cos(TReal& aTrg, const TReal& aSrc)
180 if (aSrc<KMaxTInt && aSrc>KMinTInt)
183 if (Math::IsFinite(aTrg))