kernel/eka/euser/maths/um_frac.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     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_frac.cpp
       
    15 // Writes the fractional part of aTrg to aSrc
       
    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 #ifndef __USE_VFP_MATH
       
    26 
       
    27 #ifndef __REALS_MACHINE_CODED__
       
    28 EXPORT_C TInt Math::Frac(TReal &aTrg,const TReal &aSrc)
       
    29 /**
       
    30 Calculates the fractional part of a number.
       
    31 
       
    32 The fractional part is that after a decimal point.
       
    33 Truncation is toward zero, so that
       
    34 Frac(2.4)=0.4, Frac(2)=0, Frac(-1)=0, Frac(-1.4)=0.4.
       
    35 
       
    36 @param aTrg A reference containing the result.
       
    37 @param aSrc The number whose fractional part is required. 
       
    38 
       
    39 @return KErrNone if successful, otherwise another of
       
    40         the system-wide error codes.
       
    41 */
       
    42 	{
       
    43 	TRealX f;
       
    44 	TInt ret=f.Set(aSrc);
       
    45 	if (ret!=KErrNone)
       
    46 		{
       
    47 		if (ret==KErrArgument)
       
    48 			SetNaN(aTrg);
       
    49 		if (ret==KErrOverflow)
       
    50 			SetZero(aTrg,f.iSign&1);
       
    51 		return(ret);
       
    52 		}
       
    53 	TInt intbits=f.iExp-0x7FFE;
       
    54 	if (intbits<=0)	 // aSrc is already a fraction
       
    55 		{
       
    56 		aTrg=aSrc;
       
    57 		return(KErrNone);
       
    58 		}
       
    59 	if (intbits>KMantissaBits)
       
    60 		{
       
    61 		SetZero(aTrg,f.iSign&1);
       
    62 		return(KErrNone);
       
    63 		}
       
    64 
       
    65 	// calculate integer part and subtract
       
    66 	// this means that the subtraction normalises the result
       
    67 	TRealX g=f;
       
    68 
       
    69 	TUint64 mask = ~(UI64LIT(0));
       
    70 	mask <<= (64 - intbits);
       
    71 
       
    72 	g.iMantHi &= static_cast<TUint32>(mask >> 32);
       
    73 	g.iMantLo &= static_cast<TUint32>(mask);
       
    74 
       
    75 	f-=g;
       
    76 	f.GetTReal(aTrg);
       
    77 	return(KErrNone);
       
    78 	}
       
    79 
       
    80 #endif //__REALS_MACHINE_CODED__
       
    81 
       
    82 #else // __USE_VFP_MATH
       
    83 
       
    84 // definitions come from RVCT math library
       
    85 extern "C" TReal modf(TReal,TReal*);
       
    86 
       
    87 EXPORT_C TInt Math::Frac(TReal& aTrg, const TReal& aSrc)
       
    88 	{
       
    89 	if (Math::IsNaN(aSrc))
       
    90 		{
       
    91 		SetNaN(aTrg);
       
    92 		return KErrArgument;
       
    93 		}
       
    94 	if (Math::IsInfinite(aSrc))
       
    95 		{
       
    96 		SetZero(aTrg);
       
    97 		return KErrOverflow;
       
    98 		}
       
    99 
       
   100 	TReal temp;
       
   101 	aTrg = modf(aSrc,&temp);
       
   102 	return KErrNone;
       
   103 	}
       
   104 
       
   105 #endif