os/kernelhwsrv/kernel/eka/euser/maths/um_atan.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32\euser\maths\um_atan.cpp
sl@0
    15
// Floating point arc tangent
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include "um_std.h"
sl@0
    20
sl@0
    21
#if defined(__USE_VFP_MATH) && !defined(__CPU_HAS_VFP)
sl@0
    22
#error	__USE_VFP_MATH was defined but not __CPU_HAS_VFP - impossible combination, check variant.mmh 
sl@0
    23
#endif
sl@0
    24
sl@0
    25
#ifndef __USE_VFP_MATH
sl@0
    26
sl@0
    27
LOCAL_D const TUint32 ArctanCoeffs[] =
sl@0
    28
	{
sl@0
    29
	0x00000000,0x80000000,0x7FFF0000,	// polynomial approximation to arctan(x)
sl@0
    30
	0xAA84D6EE,0xAAAAAAAA,0x7FFD0001,	// for -(sqr2-1) <= x <= (sqr2-1)
sl@0
    31
	0x89C77453,0xCCCCCCCC,0x7FFC0000,
sl@0
    32
	0xEBC0261C,0x9249247B,0x7FFC0001,
sl@0
    33
	0x940BC4DB,0xE38E3121,0x7FFB0000,
sl@0
    34
	0x141C32F1,0xBA2DBF36,0x7FFB0001,
sl@0
    35
	0xA90615E7,0x9D7C807E,0x7FFB0000,
sl@0
    36
	0x1C632E93,0x87F6A873,0x7FFB0001,
sl@0
    37
	0x310FCFFD,0xE8BE5D0A,0x7FFA0000,
sl@0
    38
	0x92289F15,0xB17B930B,0x7FFA0001,
sl@0
    39
	0x546FE7CE,0xABDE562D,0x7FF90000
sl@0
    40
	};
sl@0
    41
sl@0
    42
LOCAL_D const TUint32 Sqr2m1data[] = {0xE7799211,0xD413CCCF,0x7FFD0000};		// sqr2-1
sl@0
    43
LOCAL_D const TUint32 Sqr2p1data[] = {0xFCEF3242,0x9A827999,0x80000000};		// sqr2+1
sl@0
    44
LOCAL_D const TUint32 Onedata[] = {0x00000000,0x80000000,0x7FFF0000};			// 1.0
sl@0
    45
LOCAL_D const TUint32 PiBy8data[] = {0x2168C235,0xC90FDAA2,0x7FFD0000};			// pi/8
sl@0
    46
LOCAL_D const TUint32 PiBy2data[] = {0x2168C235,0xC90FDAA2,0x7FFF0000};			// pi/2
sl@0
    47
LOCAL_D const TUint32 ThreePiBy8data[] = {0x990E91A8,0x96CBE3F9,0x7FFF0000};	// 3*pi/8
sl@0
    48
sl@0
    49
LOCAL_C void Arctan(TRealX& y, TRealX& x)
sl@0
    50
	{
sl@0
    51
	// Calculate arctan(x), write result to y
sl@0
    52
	// Algorithm:
sl@0
    53
	//		If x>1, replace x with 1/x and subtract result from pi/2
sl@0
    54
	//			( use identity tan(pi/2-x)=1/tan(x) )
sl@0
    55
	//		If x>sqr(2)-1, replace x with (x-(sqr(2)-1))/(1-(sqr2-1)x)
sl@0
    56
	//			( use identity tan(x-a)=(tanx-tana)/(1-tana.tanx)
sl@0
    57
	//			  where a=pi/8, tan a = sqr2-1
sl@0
    58
	//			and add pi/8 to result
sl@0
    59
	//		Use polynomial approximation to calculate arctan(x) for
sl@0
    60
	//		x in the interval [0,sqr2-1]
sl@0
    61
sl@0
    62
	const TRealX& Sqr2m1 = *(const TRealX*)Sqr2m1data;
sl@0
    63
	const TRealX& Sqr2p1 = *(const TRealX*)Sqr2p1data;
sl@0
    64
	const TRealX& One = *(const TRealX*)Onedata;
sl@0
    65
	const TRealX& PiBy8 = *(const TRealX*)PiBy8data;
sl@0
    66
	const TRealX& PiBy2 = *(const TRealX*)PiBy2data;
sl@0
    67
	const TRealX& ThreePiBy8 = *(const TRealX*)ThreePiBy8data;
sl@0
    68
sl@0
    69
	TInt section=0;
sl@0
    70
	TInt8 sign=x.iSign;
sl@0
    71
	x.iSign=0;
sl@0
    72
	if (x>Sqr2p1)
sl@0
    73
		{
sl@0
    74
		x=One/x;
sl@0
    75
		section=3;
sl@0
    76
		}
sl@0
    77
	else if (x>One)
sl@0
    78
		{
sl@0
    79
		x=(One-Sqr2m1*x)/(x+Sqr2m1);
sl@0
    80
		section=2;
sl@0
    81
		}
sl@0
    82
	else if (x>Sqr2m1)
sl@0
    83
		{
sl@0
    84
		x=(x-Sqr2m1)/(One+Sqr2m1*x);
sl@0
    85
		section=1;
sl@0
    86
		}
sl@0
    87
	Math::PolyX(y,x*x,10,(const TRealX*)ArctanCoeffs);
sl@0
    88
	y*=x;
sl@0
    89
	if (section==1)
sl@0
    90
		y+=PiBy8;
sl@0
    91
	else if (section==2)
sl@0
    92
		y=ThreePiBy8-y;
sl@0
    93
	else if (section==3)
sl@0
    94
		y=PiBy2-y;
sl@0
    95
	y.iSign=sign;
sl@0
    96
	}
sl@0
    97
sl@0
    98
sl@0
    99
sl@0
   100
sl@0
   101
EXPORT_C TInt Math::ATan(TReal& aTrg, const TReal& aSrc)
sl@0
   102
/**
sl@0
   103
Calculates the principal value of the inverse tangent of a number.
sl@0
   104
sl@0
   105
@param aTrg A reference containing the result in radians,
sl@0
   106
            a value between -pi/2 and +pi/2.
sl@0
   107
@param aSrc The argument of the arctan function,
sl@0
   108
            a value between +infinity and +infinity.
sl@0
   109
sl@0
   110
@return KErrNone if successful, otherwise another of
sl@0
   111
        the system-wide error codes. 
sl@0
   112
*/
sl@0
   113
	{
sl@0
   114
	TRealX x;
sl@0
   115
	TInt r=x.Set(aSrc);
sl@0
   116
	if (r==KErrNone)
sl@0
   117
		{
sl@0
   118
		TRealX y;
sl@0
   119
		Arctan(y,x);
sl@0
   120
		return y.GetTReal(aTrg);
sl@0
   121
		}
sl@0
   122
	if (r==KErrArgument)
sl@0
   123
		{
sl@0
   124
		SetNaN(aTrg);
sl@0
   125
		return KErrArgument;
sl@0
   126
		}
sl@0
   127
	aTrg=KPiBy2;		// arctan(+/- infinity) = +/- pi/2
sl@0
   128
	if (x.iSign&1)
sl@0
   129
		aTrg=-aTrg;
sl@0
   130
	return KErrNone;
sl@0
   131
	}
sl@0
   132
sl@0
   133
LOCAL_D const TUint32 Pidata[] = {0x2168C235,0xC90FDAA2,0x80000000};
sl@0
   134
LOCAL_D const TUint32 PiBy4data[] = {0x2168C235,0xC90FDAA2,0x7FFE0000};
sl@0
   135
LOCAL_D const TUint32 MinusPiBy4data[] = {0x2168C235,0xC90FDAA2,0x7FFE0001};
sl@0
   136
LOCAL_D const TUint32 ThreePiBy4data[] = {0x990E91A8,0x96CBE3F9,0x80000000};
sl@0
   137
LOCAL_D const TUint32 MinusThreePiBy4data[] = {0x990E91A8,0x96CBE3F9,0x80000001};
sl@0
   138
LOCAL_D const TUint32 Zerodata[] = {0x00000000,0x00000000,0x00000000};
sl@0
   139
sl@0
   140
sl@0
   141
sl@0
   142
sl@0
   143
EXPORT_C TInt Math::ATan(TReal &aTrg,const TReal &aY,const TReal &aX)
sl@0
   144
/**
sl@0
   145
Calculates the angle between the x-axis and a line drawn from the origin
sl@0
   146
to a point represented by its (x,y) co-ordinates.
sl@0
   147
sl@0
   148
The co-ordinates are passed as arguments to the function.
sl@0
   149
This function returns the same result as arctan(y/x), but:
sl@0
   150
sl@0
   151
1. it adds +/-pi to the result, if x is negative
sl@0
   152
sl@0
   153
2. it sets the result to +/-pi/2, if x is zero but y is non-zero.
sl@0
   154
sl@0
   155
@param aTrg A reference containing the result in radians,
sl@0
   156
            a value between -pi exclusive and +pi inclusive.
sl@0
   157
@param aY   The y argument of the arctan(y/x) function. 
sl@0
   158
@param aX   The x argument of the arctan(y/x) function.
sl@0
   159
sl@0
   160
@return KErrNone if successful, otherwise another of
sl@0
   161
        the system-wide error codes. 
sl@0
   162
*/
sl@0
   163
	{
sl@0
   164
	const TRealX& Zero=*(const TRealX*)Zerodata;
sl@0
   165
	const TRealX& Pi=*(const TRealX*)Pidata;
sl@0
   166
	const TRealX& PiBy4=*(const TRealX*)PiBy4data;
sl@0
   167
	const TRealX& MinusPiBy4=*(const TRealX*)MinusPiBy4data;
sl@0
   168
	const TRealX& ThreePiBy4=*(const TRealX*)ThreePiBy4data;
sl@0
   169
	const TRealX& MinusThreePiBy4=*(const TRealX*)MinusThreePiBy4data;
sl@0
   170
sl@0
   171
	TRealX x, y;
sl@0
   172
	TInt rx=x.Set(aX);
sl@0
   173
	TInt ry=y.Set(aY);
sl@0
   174
	if (rx!=KErrArgument && ry!=KErrArgument)
sl@0
   175
		{
sl@0
   176
		if (x.iExp==0)
sl@0
   177
			x.iSign=0;
sl@0
   178
		TRealX q;
sl@0
   179
		TInt rq=y.Div(q,x);
sl@0
   180
		if (rq!=KErrArgument)
sl@0
   181
			{
sl@0
   182
			TRealX arg;
sl@0
   183
			Arctan(arg,q);
sl@0
   184
			if (x<Zero)
sl@0
   185
				{
sl@0
   186
				if (y>=Zero)
sl@0
   187
					arg+=Pi;
sl@0
   188
				else
sl@0
   189
					arg-=Pi;
sl@0
   190
				}
sl@0
   191
			aTrg=arg;
sl@0
   192
			return KErrNone;
sl@0
   193
			}
sl@0
   194
		if (!x.IsZero())
sl@0
   195
			{
sl@0
   196
			// Both x and y must be infinite
sl@0
   197
			TInt quadrant=((y.iSign & 1)<<1) + (x.iSign&1);
sl@0
   198
			TRealX arg;
sl@0
   199
			if (quadrant==0)
sl@0
   200
				arg=PiBy4;
sl@0
   201
			else if (quadrant==1)
sl@0
   202
				arg=ThreePiBy4;
sl@0
   203
			else if (quadrant==3)
sl@0
   204
				arg=MinusThreePiBy4;
sl@0
   205
			else
sl@0
   206
				arg=MinusPiBy4;
sl@0
   207
			aTrg=(TReal)arg;
sl@0
   208
			return KErrNone;
sl@0
   209
			}
sl@0
   210
		}
sl@0
   211
	SetNaN(aTrg);
sl@0
   212
	return KErrArgument;
sl@0
   213
	}
sl@0
   214
sl@0
   215
#else // __USE_VFP_MATH
sl@0
   216
sl@0
   217
LOCAL_D const TUint32 PiBy4data[] = {0x54442D18,0x3FE921FB};
sl@0
   218
LOCAL_D const TUint32 MinusPiBy4data[] = {0x54442D18,0xBFE921FB};
sl@0
   219
LOCAL_D const TUint32 ThreePiBy4data[] = {0x7F3321D2,0x4002D97C};
sl@0
   220
LOCAL_D const TUint32 MinusThreePiBy4data[] = {0x7F3321D2,0xC002D97C};
sl@0
   221
sl@0
   222
// definitions come from RVCT math library
sl@0
   223
extern "C" TReal atan(TReal);
sl@0
   224
extern "C" TReal atan2(TReal,TReal);
sl@0
   225
sl@0
   226
EXPORT_C TInt Math::ATan(TReal& aTrg, const TReal& aSrc)
sl@0
   227
	{
sl@0
   228
	aTrg = atan(aSrc);
sl@0
   229
	if (Math::IsFinite(aTrg))
sl@0
   230
		return KErrNone;
sl@0
   231
	SetNaN(aTrg);
sl@0
   232
	return KErrArgument;
sl@0
   233
	}
sl@0
   234
sl@0
   235
EXPORT_C TInt Math::ATan(TReal &aTrg,const TReal &aY,const TReal &aX)
sl@0
   236
	{
sl@0
   237
	aTrg = atan2(aY,aX);
sl@0
   238
	if (Math::IsFinite(aTrg))
sl@0
   239
		return KErrNone;
sl@0
   240
	
sl@0
   241
	// Return is a NaN, but ARM implementation returns NaN for atan(inf/inf)
sl@0
   242
	// whereas implementation above returns multiples of pi/4 - fix up here
sl@0
   243
	SReal64 *pY=(SReal64 *)&aY;
sl@0
   244
	SReal64 *pX=(SReal64 *)&aX;
sl@0
   245
	
sl@0
   246
	if (   pY->msm==0 && pY->lsm==0 && pY->exp==KTReal64SpecialExponent
sl@0
   247
		&& pX->msm==0 && pX->lsm==0 && pX->exp==KTReal64SpecialExponent)
sl@0
   248
		{
sl@0
   249
		TInt quadrant=((pY->sign)<<1) + (pX->sign);
sl@0
   250
		if (quadrant==0)
sl@0
   251
			aTrg=*(const TReal*)PiBy4data;
sl@0
   252
		else if (quadrant==1)
sl@0
   253
			aTrg=*(const TReal*)ThreePiBy4data;
sl@0
   254
		else if (quadrant==3)
sl@0
   255
			aTrg=*(const TReal*)MinusThreePiBy4data;
sl@0
   256
		else
sl@0
   257
			aTrg=*(const TReal*)MinusPiBy4data;
sl@0
   258
		return KErrNone;
sl@0
   259
		}
sl@0
   260
sl@0
   261
	// If we get here then the args weren't inf/inf so one of them must've
sl@0
   262
	// been a NaN to start with
sl@0
   263
	SetNaN(aTrg);
sl@0
   264
	return KErrArgument;
sl@0
   265
	}
sl@0
   266
sl@0
   267
#endif