Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\euser\maths\um_int.cpp
15 // Writes the integer part aTrg to aSrc (aSrc is either TReal,TInt16 orTInt32)
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
25 #ifndef __USE_VFP_MATH
27 #ifndef __REALS_MACHINE_CODED__
28 EXPORT_C TInt Math::Int(TReal &aTrg,const TReal &aSrc)
30 Calculates the integer part of a number.
32 The integer part is that before a decimal point.
33 Truncation is toward zero, so that
34 int(2.4)=2, int(2)=2, int(-1)=-1, int(-1.4)=-1, int(-1.999)=-1.
37 @param aTrg A reference containing the result.
38 @param aSrc The number whose integer part is required.
40 @return KErrNone if successful, otherwise another of
41 the system-wide error codes.
51 TInt intbits=f.iExp-0x7FFE; // number of integer bits in mantissa
54 SetZero(aTrg,f.iSign&1); // no integer part
57 if (intbits>=KMantissaBits)
59 aTrg=aSrc; // fractional part is outside range of significance
63 TUint64 mask = ~(UI64LIT(0));
64 mask <<= (64 - intbits);
66 f.iMantHi &= static_cast<TUint32>(mask >> 32);
67 f.iMantLo &= static_cast<TUint32>(mask);
76 EXPORT_C TInt Math::Int(TInt16 &aTrg,const TReal &aSrc)
78 Calculates the integer part of a number.
80 The integer part is that before a decimal point.
81 Truncation is toward zero, so that
82 int(2.4)=2, int(2)=2, int(-1)=-1, int(-1.4)=-1, int(-1.999)=-1.
84 This function is suitable when the result is known to be small enough
85 for a 16-bit signed integer.
87 @param aTrg A reference containing the result.
88 @param aSrc The number whose integer part is required.
90 @return KErrNone if successful, otherwise another of
91 the system-wide error codes.
94 // If the integer part of aSrc is in the range -32768 to +32767
95 // inclusive, write the integer part to the TInt16 at aTrg
96 // Negative numbers are rounded towards zero.
97 // If an overflow or underflow occurs, aTrg is set to the max/min value
101 TInt ret=f.Set(aSrc);
103 if (ret==KErrArgument)
109 TInt intbits=f.iExp-0x7FFE; // number of integer bits in mantissa
119 aTrg=(TInt16)((f.iSign&1) ? KMinTInt16 : KMaxTInt16);
120 return((f.iSign&1) ? KErrUnderflow : KErrOverflow);
123 TUint val = f.iMantHi >> (32 - intbits);
125 if ((f.iSign&1)==0 && val>(TUint)KMaxTInt16)
127 aTrg=TInt16(KMaxTInt16);
128 return(KErrOverflow);
131 if ((f.iSign&1) && val>(TUint)(KMaxTInt16+1))
133 aTrg=TInt16(KMinTInt16);
134 return(KErrUnderflow);
137 aTrg = (f.iSign&1) ? (TInt16)(-(TInt)val) : (TInt16)val;
145 EXPORT_C TInt Math::Int(TInt32 &aTrg,const TReal &aSrc)
147 Calculates the integer part of a number.
149 The integer part is that before a decimal point.
150 Truncation is toward zero, so that
151 int(2.4)=2, int(2)=2, int(-1)=-1, int(-1.4)=-1, int(-1.999)=-1.
153 This function is suitable when the result is known to be small enough
154 for a 32-bit signed integer.
156 @param aTrg A reference containing the result.
157 @param aSrc The number whose integer part is required.
159 @return KErrNone if successful, otherwise another of
160 the system-wide error codes.
163 // If the integer part of the float is in the range -2147483648 to +2147483647
164 // inclusive, write the integer part to the TInt32 at aTrg
165 // Negative numbers are rounded towards zero.
166 // If an overflow or underflow occurs, aTrg is set to the max/min value
170 TInt ret=f.Set(aSrc);
172 if (ret==KErrArgument)
178 TInt intbits=f.iExp-0x7FFE; // number of integer bits in mantissa
188 aTrg=((f.iSign&1) ? KMinTInt32 : KMaxTInt32);
189 return((f.iSign&1) ? KErrUnderflow : KErrOverflow);
192 TUint val = f.iMantHi >> (32 - intbits);
194 if ((f.iSign&1)==0 && val>(TUint)KMaxTInt32)
197 return(KErrOverflow);
200 if ((f.iSign&1) && val>((TUint)KMaxTInt32+1))
203 return(KErrUnderflow);
206 aTrg=(f.iSign&1) ? -(TInt32)val : val;
211 #endif //__REALS_MACHINE_CODED__
213 #else // __USE_VFP_MATH
215 // definitions come from RVCT math library
216 extern "C" TReal modf(TReal,TReal*);
218 EXPORT_C TInt Math::Int(TReal& aTrg, const TReal& aSrc)
220 if (Math::IsNaN(aSrc))
225 if (Math::IsInfinite(aSrc))
235 EXPORT_C TInt Math::Int(TInt32& aTrg, const TReal& aSrc)
238 TInt r = Math::Int(aIntPart,aSrc);
244 if (aIntPart>KMaxTInt32)
249 if (aIntPart<KMinTInt32)
252 return KErrUnderflow;
258 EXPORT_C TInt Math::Int(TInt16& aTrg, const TReal& aSrc)
261 TInt r = Math::Int(aIntPart,aSrc);
267 if (aIntPart>KMaxTInt16)
272 if (aIntPart<KMinTInt16)
275 return KErrUnderflow;