1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kernel/eka/euser/maths/um_ln.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,143 @@
1.4 +// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32\euser\maths\um_ln.cpp
1.18 +// Natural log.
1.19 +//
1.20 +//
1.21 +
1.22 +#include "um_std.h"
1.23 +
1.24 +#if defined(__USE_VFP_MATH) && !defined(__CPU_HAS_VFP)
1.25 +#error __USE_VFP_MATH was defined but not __CPU_HAS_VFP - impossible combination, check variant.mmh
1.26 +#endif
1.27 +
1.28 +
1.29 +#ifndef __USE_VFP_MATH
1.30 +
1.31 +LOCAL_D const TUint32 ArtanhCoeffs[] =
1.32 + {
1.33 + 0x5C17F0BC,0xB8AA3B29,0x80010000, // polynomial approximation to (4/ln2)artanh(x)
1.34 + 0xD02489EE,0xF6384EE1,0x7FFF0000, // for |x| <= (sqr2-1)/(sqr2+1)
1.35 + 0x7008CA5F,0x93BB6287,0x7FFF0000,
1.36 + 0xE32D1D6B,0xD30BB16D,0x7FFE0000,
1.37 + 0x461D071E,0xA4257CE2,0x7FFE0000,
1.38 + 0xC3B0EC87,0x8650D459,0x7FFE0000,
1.39 + 0x53BEC0CD,0xE23137E3,0x7FFD0000,
1.40 + 0xC523F21B,0xDAF79221,0x7FFD0000
1.41 + };
1.42 +
1.43 +LOCAL_D const TUint32 Ln2By2data[] = {0xD1CF79AC,0xB17217F7,0x7FFD0000}; // (ln2)/2
1.44 +LOCAL_D const TUint32 Sqr2data[] = {0xF9DE6484,0xB504F333,0x7FFF0000}; // sqr2
1.45 +LOCAL_D const TUint32 Sqr2Invdata[] = {0xF9DE6484,0xB504F333,0x7FFE0000}; // 1/sqr2
1.46 +LOCAL_D const TUint32 Onedata[] = {0x00000000,0x80000000,0x7FFF0000}; // 1.0
1.47 +
1.48 +
1.49 +
1.50 +
1.51 +EXPORT_C TInt Math::Ln(TReal& aTrg, const TReal& aSrc)
1.52 +/**
1.53 +Calculates the natural logarithm of a number.
1.54 +
1.55 +@param aTrg A reference containing the result.
1.56 +@param aSrc The number whose natural logarithm is required.
1.57 +
1.58 +@return KErrNone if successful, otherwise another of
1.59 + the system-wide error codes.
1.60 +*/
1.61 + {
1.62 + // Calculate ln(aSrc) and write to aTrg
1.63 + // Algorithm:
1.64 + // Calculate log2(aSrc) and multiply by ln2
1.65 + // log2(aSrc)=log2(2^e.m) e=exponent of aSrc, m=mantissa 1<=m<2
1.66 + // log2(aSrc)=e+log2(m)
1.67 + // If e=-1 (0.5<=aSrc<1), let x=aSrc else let x=mantissa(aSrc)
1.68 + // If x>Sqr2, replace x with x/Sqr2
1.69 + // If x<Sqr2/2, replace x with x*Sqr2
1.70 + // Replace x with (x-1)/(x+1)
1.71 + // Use polynomial to calculate artanh(x) for |x| <= (sqr2-1)/(sqr2+1)
1.72 + // ( use identity ln(x) = 2artanh((x-1)/(x+1)) )
1.73 +
1.74 + TRealX x;
1.75 + const TRealX& Ln2By2=*(const TRealX*)Ln2By2data;
1.76 + const TRealX& Sqr2=*(const TRealX*)Sqr2data;
1.77 + const TRealX& Sqr2Inv=*(const TRealX*)Sqr2Invdata;
1.78 + const TRealX& One=*(const TRealX*)Onedata;
1.79 +
1.80 + TInt r=x.Set(aSrc);
1.81 + if (r==KErrNone)
1.82 + {
1.83 + if (x.iExp==0)
1.84 + {
1.85 + SetInfinite(aTrg,1);
1.86 + return KErrOverflow;
1.87 + }
1.88 + if (x.iSign&1)
1.89 + {
1.90 + SetNaN(aTrg);
1.91 + return KErrArgument;
1.92 + }
1.93 + TInt n=(x.iExp-0x7FFF)<<1;
1.94 + x.iExp=0x7FFF;
1.95 + if (n!=-2)
1.96 + {
1.97 + if (x>Sqr2)
1.98 + {
1.99 + x*=Sqr2Inv;
1.100 + n++;
1.101 + }
1.102 + }
1.103 + else
1.104 + {
1.105 + n=0;
1.106 + x.iExp=0x7FFE;
1.107 + if (x<Sqr2Inv)
1.108 + {
1.109 + x*=Sqr2;
1.110 + n--;
1.111 + }
1.112 + }
1.113 + x=(x-One)/(x+One); // ln(x)=2artanh((x-1)/(x+1))
1.114 + TRealX y;
1.115 + PolyX(y,x*x,7,(const TRealX*)ArtanhCoeffs);
1.116 + y*=x;
1.117 + y+=TRealX(n);
1.118 + y*=Ln2By2;
1.119 + return y.GetTReal(aTrg);
1.120 + }
1.121 + if (r==KErrArgument || (r==KErrOverflow && (x.iSign&1)))
1.122 + {
1.123 + SetNaN(aTrg);
1.124 + return KErrArgument;
1.125 + }
1.126 + SetInfinite(aTrg,0);
1.127 + return KErrOverflow;
1.128 + }
1.129 +
1.130 +#else // __USE_VFP_MATH
1.131 +
1.132 +// definitions come from RVCT math library
1.133 +extern "C" TReal log(TReal);
1.134 +
1.135 +EXPORT_C TInt Math::Ln(TReal& aTrg, const TReal& aSrc)
1.136 + {
1.137 + aTrg = log(aSrc);
1.138 + if (Math::IsFinite(aTrg))
1.139 + return KErrNone;
1.140 + if (Math::IsInfinite(aTrg))
1.141 + return KErrOverflow;
1.142 + SetNaN(aTrg);
1.143 + return KErrArgument;
1.144 + }
1.145 +
1.146 +#endif