os/kernelhwsrv/kernel/eka/euser/maths/um_sqrt.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kernel/eka/euser/maths/um_sqrt.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,90 @@
     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_sqrt.cpp
    1.18 +// Square root.
    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 +#ifndef __REALS_MACHINE_CODED__
    1.32 +LOCAL_D const TUint32 KConstAdata[] = {0x00000000,0xD5A9A805,0x7FFD0000};
    1.33 +LOCAL_D const TUint32 KConstBdata[] = {0x00000000,0x9714B9CB,0x7FFE0000};
    1.34 +LOCAL_D const TUint32 Sqr2Invdata[] = {0xF9DE6484,0xB504F333,0x7FFE0000};		// 1/sqr2
    1.35 +
    1.36 +
    1.37 +
    1.38 +
    1.39 +EXPORT_C TInt Math::Sqrt(TReal& aTrg,const TReal &aSrc)
    1.40 +/**
    1.41 +Calculates the square root of a number.
    1.42 +
    1.43 +@param aTrg A reference containing the result. 
    1.44 +@param aSrc The number whose square-root is required.
    1.45 +
    1.46 +@return KErrNone if successful, otherwise another of
    1.47 +        the system-wide error codes. 
    1.48 +*/
    1.49 +//
    1.50 +// Fast sqrt routine. See Software manual by W.J.Cody & W.Waite Chapter 4.
    1.51 +//
    1.52 +	{
    1.53 +	const TRealX& KConstA=*(const TRealX*)KConstAdata;
    1.54 +	const TRealX& KConstB=*(const TRealX*)KConstBdata;
    1.55 +	const TRealX& Sqr2Inv=*(const TRealX*)Sqr2Invdata;
    1.56 +
    1.57 +	TRealX x;
    1.58 +	TInt r=x.Set(aSrc);
    1.59 +	if (x.IsZero())
    1.60 +		{
    1.61 +		aTrg=aSrc;
    1.62 +		return(KErrNone);
    1.63 +		}   
    1.64 +	if (r==KErrArgument || x.iSign&1)
    1.65 +		{
    1.66 +		SetNaN(aTrg);
    1.67 +		return(KErrArgument);
    1.68 +		}
    1.69 +	if (r==KErrOverflow)	// positive infinity
    1.70 +		{
    1.71 +		aTrg=aSrc;
    1.72 +		return(r);
    1.73 +		}
    1.74 +	TInt n=x.iExp-0x7FFE;
    1.75 +	x.iExp=0x7FFE;
    1.76 +	TRealX y=KConstB*x+KConstA;
    1.77 +	y=y+(x/y);
    1.78 +	y.iExp--;
    1.79 +	y=y+(x/y);
    1.80 +	y.iExp--;
    1.81 +	y=y+(x/y);
    1.82 +	y.iExp--;
    1.83 +	if (n&1)
    1.84 +		{
    1.85 +		y*=Sqr2Inv;
    1.86 +		n++;
    1.87 +		}
    1.88 +	y.iExp=TUint16(TInt(y.iExp)+(n>>1));
    1.89 +	return y.GetTReal(aTrg);
    1.90 +	}
    1.91 +#endif
    1.92 +
    1.93 +#endif // !__USE_VFP_MATH - VFP version is in assembler