kernel/eka/euser/us_gcc.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\us_gcc.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "us_std.h"
       
    19 #include <e32math.h>
       
    20 
       
    21 extern "C" {
       
    22 
       
    23 EXPORT_C TInt64 __fixsfdi(TReal32 a1)
       
    24 //
       
    25 // Convert float to long long
       
    26 //
       
    27 	{
       
    28 	const TRealX f(a1);
       
    29 	const TInt64 ret = f;
       
    30 	return ret;
       
    31 	}
       
    32 
       
    33 }	// end of extern "C" declaration
       
    34 
       
    35 #ifndef __REALS_MACHINE_CODED__
       
    36 LOCAL_C void MathException(TInt aErrType) 
       
    37 //
       
    38 // Decides on type of maths exception according to error and raises exception.
       
    39 // Added by AnnW, December 1996
       
    40 //
       
    41 	{
       
    42 
       
    43 	TExcType excType=EExcGeneral;
       
    44 
       
    45 	switch (aErrType)
       
    46 		{
       
    47 	case KErrArgument:				// error due to invalid operation
       
    48 		excType=EExcFloatInvalidOperation;
       
    49 		break;
       
    50 	case KErrDivideByZero:
       
    51 		excType=EExcFloatDivideByZero;
       
    52 		break;
       
    53 	case KErrOverflow:
       
    54 		excType=EExcFloatOverflow;
       
    55 		break;
       
    56 	case KErrUnderflow:
       
    57 		excType=EExcFloatUnderflow;
       
    58 		break;
       
    59 /*
       
    60 	// also errors due to inexact result
       
    61 	case KErrInexact:		// const not defined yet
       
    62 		excType=EExcFloatInexact;
       
    63 		break;
       
    64 */
       
    65 	
       
    66 	default:
       
    67 		// Unknown error
       
    68 		Panic(EMathUnknownError);
       
    69 		}
       
    70 
       
    71 	User::RaiseException(excType);
       
    72 	}
       
    73 
       
    74 EXPORT_C TReal32 __addsf3(TReal32 a1,TReal32 a2)
       
    75 //
       
    76 // Add two floats
       
    77 //
       
    78     {
       
    79 
       
    80     TRealX x1=a1;
       
    81     TRealX x2=a2;
       
    82 
       
    83 	TRealX res;
       
    84 	TReal32 trg;
       
    85 	x1.Add(res,x2);	// need not check error because no underflow and others will not be lost in conversion
       
    86 	TInt ret=res.GetTReal(trg);
       
    87 	if (ret!=KErrNone)
       
    88 		MathException(ret);
       
    89 
       
    90 	return(trg);
       
    91     }
       
    92 
       
    93 EXPORT_C TReal64 __adddf3(TReal64 a1,TReal64 a2)
       
    94 //
       
    95 // Add two doubles
       
    96 //
       
    97     {
       
    98 
       
    99     TRealX x1=a1;
       
   100     TRealX x2=a2;
       
   101 
       
   102 	TRealX res;
       
   103 	TReal64 trg;
       
   104 	x1.Add(res,x2);	// need not check error because no underflow and others will not be lost in conversion
       
   105 	TInt ret=res.GetTReal(trg);
       
   106 	if (ret!=KErrNone)
       
   107 		MathException(ret);
       
   108 
       
   109 	return(trg);
       
   110     }
       
   111 
       
   112 EXPORT_C TReal32 __subsf3(TReal32 a1,TReal32 a2)
       
   113 //
       
   114 // Subtract two floats
       
   115 //
       
   116     {
       
   117 
       
   118     TRealX x1=a1;
       
   119     TRealX x2=a2;
       
   120 
       
   121 	TRealX res;
       
   122 	TReal32 trg;
       
   123 	x1.Sub(res,x2);	// need not check error because no underflow and others will not be lost in conversion
       
   124 	TInt ret=res.GetTReal(trg);
       
   125 	if (ret!=KErrNone)
       
   126 		MathException(ret);
       
   127 
       
   128 	return(trg);
       
   129 	}
       
   130 
       
   131 EXPORT_C TReal64 __subdf3(TReal64 a1,TReal64 a2)
       
   132 //
       
   133 // Subtract two doubles
       
   134 //
       
   135     {
       
   136 
       
   137     TRealX x1=a1;
       
   138     TRealX x2=a2;
       
   139 
       
   140 	TRealX res;
       
   141 	TReal64 trg;
       
   142 	x1.Sub(res,x2);	// need not check error because no underflow and others will not be lost in conversion
       
   143 	TInt ret=res.GetTReal(trg);
       
   144 	if (ret!=KErrNone)
       
   145 		MathException(ret);
       
   146 
       
   147 	return(trg);    
       
   148     }
       
   149 
       
   150 EXPORT_C TInt __cmpsf3(TReal32 a1,TReal32 a2)
       
   151 //
       
   152 // Compare two floats
       
   153 //
       
   154     {
       
   155 
       
   156     TRealX x1=a1;
       
   157     TRealX x2=a2;
       
   158     if (x1<x2)
       
   159         return(-1);
       
   160     if (x1>x2)
       
   161         return(1);
       
   162     return(0);
       
   163     }
       
   164 
       
   165 EXPORT_C TInt __cmpdf3(TReal64 a1,TReal64 a2)
       
   166 //
       
   167 // Compare two doubles
       
   168 //
       
   169     {
       
   170 
       
   171     TRealX x1=a1;
       
   172     TRealX x2=a2;
       
   173     if (x1<x2)
       
   174         return(-1);
       
   175     if (x1>x2)
       
   176         return(1);
       
   177     return(0);
       
   178     }
       
   179 
       
   180 EXPORT_C TInt __eqsf2(TReal32 a1,TReal32 a2)
       
   181 //
       
   182 // Compare if two floats are equal
       
   183 //
       
   184     {
       
   185 
       
   186     TRealX x1=a1;
       
   187     TRealX x2=a2;
       
   188     return (x1==x2 ? 0 : 1);
       
   189     }
       
   190 
       
   191 EXPORT_C TInt __eqdf2(TReal64 a1,TReal64 a2)
       
   192 //
       
   193 // Compare if two doubles are equal
       
   194 //
       
   195     {
       
   196 
       
   197     TRealX x1=a1;
       
   198     TRealX x2=a2;
       
   199     return (x1==x2 ? 0 : 1);
       
   200     }
       
   201 
       
   202 EXPORT_C TInt __nesf2(TReal32 a1,TReal32 a2)
       
   203 //
       
   204 // Compare if two floats are not equal
       
   205 //
       
   206     {
       
   207 
       
   208     TRealX x1=a1;
       
   209     TRealX x2=a2;
       
   210     return(x1!=x2 ? 1 : 0);
       
   211     }
       
   212 
       
   213 EXPORT_C TInt __nedf2(TReal64 a1,TReal64 a2)
       
   214 //
       
   215 // Compare if two doubles are not equal
       
   216 //
       
   217     {
       
   218 
       
   219     TRealX x1=a1;
       
   220     TRealX x2=a2;
       
   221     return(x1!=x2 ? 1 : 0);
       
   222     }
       
   223 
       
   224 EXPORT_C TInt __gtsf2(TReal32 a1,TReal32 a2)
       
   225 //
       
   226 // Compare if one float is greater than another
       
   227 //
       
   228     {
       
   229 
       
   230     TRealX x1=a1;
       
   231     TRealX x2=a2;
       
   232     return(x1>x2 ? +1 : -1);
       
   233     }
       
   234 
       
   235 EXPORT_C TInt __gtdf2(TReal64 a1,TReal64 a2)
       
   236 //
       
   237 // Compare if one double is greater than another
       
   238 //
       
   239     {
       
   240 
       
   241     TRealX x1=a1;
       
   242     TRealX x2=a2;
       
   243     return(x1>x2 ? +1 : -1);
       
   244     }
       
   245 
       
   246 EXPORT_C TInt __gesf2(TReal32 a1,TReal32 a2)
       
   247 //
       
   248 // Compare if one float is greater than or equal to another
       
   249 //
       
   250     {
       
   251 
       
   252     TRealX x1=a1;
       
   253     TRealX x2=a2;
       
   254     return(x1>=x2 ? 1 : -1);
       
   255     }
       
   256 
       
   257 EXPORT_C TInt __gedf2(TReal64 a1,TReal64 a2)
       
   258 //
       
   259 // Compare if one double is greater than or equal to another
       
   260 //
       
   261     {
       
   262 
       
   263     TRealX x1=a1;
       
   264     TRealX x2=a2;
       
   265     return(x1>=x2 ? 1 : -1);
       
   266     }
       
   267 
       
   268 EXPORT_C TInt __ltsf2(TReal32 a1,TReal32 a2)
       
   269 //
       
   270 // Compare if one float is less than another
       
   271 //
       
   272     {
       
   273 
       
   274     TRealX x1=a1;
       
   275     TRealX x2=a2;
       
   276     return(x1<x2 ? -1 : +1);
       
   277     }
       
   278 
       
   279 EXPORT_C TInt __ltdf2(TReal64 a1,TReal64 a2)
       
   280 //
       
   281 // Compare if one double is less than another
       
   282 //
       
   283     {
       
   284 
       
   285     TRealX x1=a1;
       
   286     TRealX x2=a2;
       
   287     return(x1<x2 ? -1 : +1);
       
   288     }
       
   289 
       
   290 EXPORT_C TInt __lesf2(TReal32 a1,TReal32 a2)
       
   291 //
       
   292 // Compare if one float is less than or equal to another
       
   293 //
       
   294     {
       
   295 
       
   296     TRealX x1=a1;
       
   297     TRealX x2=a2;
       
   298     return(x1<=x2 ? -1 : +1);
       
   299     }
       
   300 
       
   301 EXPORT_C TInt __ledf2(TReal64 a1,TReal64 a2)
       
   302 //
       
   303 // Compare if one double is less than or equal to another
       
   304 //
       
   305     {
       
   306 
       
   307     TRealX x1=a1;
       
   308     TRealX x2=a2;
       
   309     return(x1<=x2 ? -1 : +1);
       
   310     }
       
   311 
       
   312 EXPORT_C TReal32 __mulsf3(TReal32 a1,TReal32 a2)
       
   313 //
       
   314 // Multiply two floats
       
   315 //
       
   316     {
       
   317 
       
   318     TRealX x1=a1;
       
   319     TRealX x2=a2;
       
   320 
       
   321 	TRealX res;
       
   322 	TReal32 trg;
       
   323 	TInt ret=x1.Mult(res,x2);
       
   324 	if (ret==KErrNone)		// must check this because if underflow, error is lost in conversion
       
   325 		ret=res.GetTReal(trg);
       
   326 	if (ret!=KErrNone)
       
   327 		MathException(ret);
       
   328 	
       
   329 	return((TReal32)res);
       
   330     }
       
   331 
       
   332 EXPORT_C TReal64 __muldf3(TReal64 a1,TReal64 a2)
       
   333 //
       
   334 // Multiply two doubles
       
   335 //
       
   336     {
       
   337 
       
   338     TRealX x1=a1;
       
   339     TRealX x2=a2;
       
   340 
       
   341 	TRealX res;
       
   342 	TReal64 trg;
       
   343 	TInt ret=x1.Mult(res,x2);
       
   344 	if (ret==KErrNone)		// must check this because if underflow, error is lost in conversion
       
   345 		ret=res.GetTReal(trg);
       
   346 	if (ret!=KErrNone)
       
   347 		MathException(ret);
       
   348 
       
   349 	return((TReal64)res);
       
   350     }
       
   351 
       
   352 EXPORT_C TReal32 __divsf3(TReal32 a1,TReal32 a2)
       
   353 //
       
   354 // Divide two floats
       
   355 //
       
   356     {
       
   357 
       
   358     TRealX x1=a1;
       
   359     TRealX x2=a2;
       
   360 
       
   361 	TRealX res;
       
   362 	TReal32 trg;
       
   363 	TInt ret=x1.Div(res,x2);
       
   364 	if (ret==KErrNone)		// must check this because if underflow, error is lost in conversion
       
   365 		ret=res.GetTReal(trg);
       
   366 	if (ret!=KErrNone)
       
   367 		MathException(ret);
       
   368 
       
   369 	return((TReal32)res);
       
   370     }
       
   371 
       
   372 EXPORT_C TReal64 __divdf3(TReal64 a1,TReal64 a2)
       
   373 	//
       
   374 	// Divide two doubles
       
   375 	//
       
   376 	{
       
   377 
       
   378 	TRealX x1=a1;
       
   379 	TRealX x2=a2;
       
   380 
       
   381 	TRealX res;
       
   382 	TReal64 trg;
       
   383 	TInt ret=x1.Div(res,x2);
       
   384 	if (ret==KErrNone)		// must check this because if underflow, error is lost in conversion
       
   385 		ret=res.GetTReal(trg);
       
   386 	if (ret!=KErrNone)
       
   387 		MathException(ret);
       
   388 	
       
   389 	return((TReal64)res);
       
   390 	}
       
   391 
       
   392 EXPORT_C TReal32 __negsf2(TReal32 a1)
       
   393 //
       
   394 // Negate a float
       
   395 //
       
   396     {
       
   397 
       
   398     TRealX x1=a1;
       
   399     return((TReal32)-x1);
       
   400     }
       
   401 
       
   402 EXPORT_C TReal64 __negdf2(TReal64 a1)
       
   403 //
       
   404 // Negate a double
       
   405 //
       
   406     {
       
   407 
       
   408     TRealX x1=a1;
       
   409     return((TReal64)-x1);
       
   410     }
       
   411 
       
   412 EXPORT_C TReal32 __floatsisf(TInt a1)
       
   413 //
       
   414 // Convert int to float
       
   415 //
       
   416     {
       
   417 
       
   418     return((TReal32)TRealX(a1));
       
   419     }
       
   420 
       
   421 EXPORT_C TReal64 __floatsidf(TInt a1)
       
   422 //
       
   423 // Convert int to double
       
   424 //
       
   425     {
       
   426 
       
   427     return((TReal64)TRealX(a1));
       
   428     }
       
   429 
       
   430 EXPORT_C TInt __fixsfsi(TReal32 a1)
       
   431 //
       
   432 // Convert float to int
       
   433 //
       
   434     {
       
   435 
       
   436     return((TInt)TRealX(a1));
       
   437     }
       
   438 
       
   439 EXPORT_C TInt __fixdfsi(TReal64 a1)
       
   440 //
       
   441 // Convert double to int
       
   442 //
       
   443     {
       
   444 
       
   445     return((TInt)TRealX(a1));
       
   446     }
       
   447 
       
   448 EXPORT_C TReal64 __extendsfdf2(TReal32 a1)
       
   449 //
       
   450 // Convert a float to a double
       
   451 //
       
   452     {
       
   453 
       
   454     return((TReal64)TRealX(a1));
       
   455     }
       
   456 
       
   457 EXPORT_C TReal32 __truncdfsf2(TReal64 a1)
       
   458 //
       
   459 // Convert a double to a float
       
   460 // Raises an exception if conversion results in an error
       
   461 //
       
   462     {
       
   463 
       
   464 	TRealX x1=a1;
       
   465 
       
   466 	TInt ret;
       
   467 	TReal32 trg;
       
   468 	if ((ret=x1.GetTReal(trg))!=KErrNone)
       
   469 		MathException(ret);
       
   470 
       
   471     return(trg);
       
   472     }
       
   473 #endif //__REALS_MACHINE_CODED__