0
|
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".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
// e32\euser\maths\um_sqrt.cpp
|
|
15 |
// Square root.
|
|
16 |
//
|
|
17 |
//
|
|
18 |
|
|
19 |
#include "um_std.h"
|
|
20 |
|
|
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
|
|
23 |
#endif
|
|
24 |
|
|
25 |
|
|
26 |
#ifndef __USE_VFP_MATH
|
|
27 |
|
|
28 |
#ifndef __REALS_MACHINE_CODED__
|
|
29 |
LOCAL_D const TUint32 KConstAdata[] = {0x00000000,0xD5A9A805,0x7FFD0000};
|
|
30 |
LOCAL_D const TUint32 KConstBdata[] = {0x00000000,0x9714B9CB,0x7FFE0000};
|
|
31 |
LOCAL_D const TUint32 Sqr2Invdata[] = {0xF9DE6484,0xB504F333,0x7FFE0000}; // 1/sqr2
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
EXPORT_C TInt Math::Sqrt(TReal& aTrg,const TReal &aSrc)
|
|
37 |
/**
|
|
38 |
Calculates the square root of a number.
|
|
39 |
|
|
40 |
@param aTrg A reference containing the result.
|
|
41 |
@param aSrc The number whose square-root is required.
|
|
42 |
|
|
43 |
@return KErrNone if successful, otherwise another of
|
|
44 |
the system-wide error codes.
|
|
45 |
*/
|
|
46 |
//
|
|
47 |
// Fast sqrt routine. See Software manual by W.J.Cody & W.Waite Chapter 4.
|
|
48 |
//
|
|
49 |
{
|
|
50 |
const TRealX& KConstA=*(const TRealX*)KConstAdata;
|
|
51 |
const TRealX& KConstB=*(const TRealX*)KConstBdata;
|
|
52 |
const TRealX& Sqr2Inv=*(const TRealX*)Sqr2Invdata;
|
|
53 |
|
|
54 |
TRealX x;
|
|
55 |
TInt r=x.Set(aSrc);
|
|
56 |
if (x.IsZero())
|
|
57 |
{
|
|
58 |
aTrg=aSrc;
|
|
59 |
return(KErrNone);
|
|
60 |
}
|
|
61 |
if (r==KErrArgument || x.iSign&1)
|
|
62 |
{
|
|
63 |
SetNaN(aTrg);
|
|
64 |
return(KErrArgument);
|
|
65 |
}
|
|
66 |
if (r==KErrOverflow) // positive infinity
|
|
67 |
{
|
|
68 |
aTrg=aSrc;
|
|
69 |
return(r);
|
|
70 |
}
|
|
71 |
TInt n=x.iExp-0x7FFE;
|
|
72 |
x.iExp=0x7FFE;
|
|
73 |
TRealX y=KConstB*x+KConstA;
|
|
74 |
y=y+(x/y);
|
|
75 |
y.iExp--;
|
|
76 |
y=y+(x/y);
|
|
77 |
y.iExp--;
|
|
78 |
y=y+(x/y);
|
|
79 |
y.iExp--;
|
|
80 |
if (n&1)
|
|
81 |
{
|
|
82 |
y*=Sqr2Inv;
|
|
83 |
n++;
|
|
84 |
}
|
|
85 |
y.iExp=TUint16(TInt(y.iExp)+(n>>1));
|
|
86 |
return y.GetTReal(aTrg);
|
|
87 |
}
|
|
88 |
#endif
|
|
89 |
|
|
90 |
#endif // !__USE_VFP_MATH - VFP version is in assembler
|