os/kernelhwsrv/kerneltest/f32test/bench/t_whet.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1997-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 // f32test\bench\t_whet.cpp
    15 // Whetstone Benchmarks
    16 // 
    17 //
    18 
    19  
    20 #include <e32math.h>
    21 #include <e32std.h>
    22 #include <e32std_private.h>
    23 #include <f32file.h>
    24 #include <e32svr.h>
    25 #include <e32test.h>
    26 #include <hal.h>
    27 #include "../server/t_server.h"
    28 
    29 const TInt KSections=9;
    30 
    31 LOCAL_D TReal64 loop_time[KSections];
    32 LOCAL_D TReal64 loop_mops[KSections];
    33 LOCAL_D TReal64 loop_mflops[KSections];
    34 LOCAL_D TReal64 TimeUsed;
    35 LOCAL_D TReal64 mwips;
    36 LOCAL_D TBufC<50> headings[KSections];
    37 LOCAL_D TReal64 Check;
    38 LOCAL_D TReal64 results[KSections];
    39 
    40 LOCAL_D	TBuf<0x100> buf;
    41 LOCAL_D TBuf8<0x100> buf8;
    42 LOCAL_D const TFileName pathName = _L("C:\\E32-MATH\\");
    43 #ifdef T_WHET_WITH_VFP
    44 LOCAL_D const TFileName fileName = _L("WHETVFP.RES");
    45 #else
    46 LOCAL_D const TFileName fileName = _L("WHET.RES");
    47 #endif
    48 LOCAL_D RFile outfile;
    49 
    50 #ifdef T_WHET_WITH_VFP
    51 GLDEF_D RTest test(_L("TReal64 C/C++ Whetstone Benchmark with VFP"));
    52 #else
    53 GLDEF_D RTest test(_L("TReal64 C/C++ Whetstone Benchmark"));
    54 #endif
    55 
    56 LOCAL_C void appendString(const TDesC& aString)
    57 	{
    58 
    59 	buf=_L("");
    60 	buf.AppendFormat(_L("%S\n"),&aString);
    61 	buf8.Copy(buf);			// Unicode
    62 	outfile.Write(buf8);
    63 	}
    64 
    65 LOCAL_C void pa(TReal64* e,TReal64 t,TReal64 t2)	
    66 	{
    67 	
    68 	TInt j;
    69     for (j=0;j<6;j++)
    70 		{
    71         e[0]=(e[0]+e[1]+e[2]-e[3])*t;
    72         e[1]=(e[0]+e[1]-e[2]+e[3])*t;
    73         e[2]=(e[0]-e[1]+e[2]+e[3])*t;
    74         e[3]=(-e[0]+e[1]+e[2]+e[3])/t2;
    75         }
    76     }
    77 
    78 LOCAL_C void po(TReal64* e1,TInt j,TInt k,TInt l)
    79 	{
    80     
    81 	e1[j]=e1[k];
    82     e1[k]=e1[l];
    83     e1[l]=e1[j];
    84     }
    85 
    86 LOCAL_C void p3(TReal64 *x,TReal64 *y,TReal64 *z,TReal64 t,TReal64 t1,TReal64 t2)
    87 	{
    88     
    89 	*x=*y;
    90     *y=*z;
    91     *x=t*(*x+*y);
    92     *y=t1*(*x+*y);
    93     *z=(*x+*y)/t2;
    94     }
    95 
    96 LOCAL_C void pout(const TDesC& aTitle,TReal64 aOps,TInt aType,TReal64 aChecknum,TReal64 aTime,
    97 				  TInt aCalibrate,TInt aSection)
    98 	{
    99 	
   100     TReal64 mops,mflops;
   101 
   102     Check+=aChecknum;
   103     loop_time[aSection]=aTime;
   104     headings[aSection]=aTitle;
   105     TimeUsed+=aTime;
   106 
   107     if (aCalibrate==1)
   108         results[aSection]=aChecknum;
   109 
   110     if (aCalibrate==0)
   111         {              
   112         test.Printf(_L("%- 20S%- 18g"),&headings[aSection],results[aSection]);    
   113        
   114         if (aType==1)
   115 			{
   116 			if (aTime>0)
   117 				mflops=aOps/(1E+6*aTime);
   118             else
   119 				mflops=0.0;
   120 
   121             loop_mops[aSection]=99999.0;
   122             loop_mflops[aSection]=mflops;
   123             test.Printf(_L("%- 34g%- 7g\n"),loop_mflops[aSection],loop_time[aSection]);                
   124             }
   125 		else
   126 			{
   127             if (aTime>0)
   128 				mops=aOps/(1E+6*aTime);
   129             else
   130 				mops=0.0;
   131 
   132             loop_mops[aSection]=mops;
   133             loop_mflops[aSection]=0.0; 
   134             test.Printf(_L("                 %- 17g%- 7g\n"),loop_mops[aSection],loop_time[aSection]);
   135             }
   136         }     
   137     }
   138 
   139 LOCAL_C void whetstones(TInt xtra,TInt x100,TInt calibrate)
   140 	{
   141 	
   142 	TTime timea,timeb;                 
   143     TReal64 t=0.49999975;
   144 	const TReal64 t0=t;            
   145 
   146     TReal64 t1=0.50000025;
   147     TReal64 t2=2.0;
   148                
   149     Check=0.0;
   150 
   151     TInt n1,n2,n3,n4,n5,n6,n7,n8,n1mult; 
   152     n1 = 12*x100;
   153     n2 = 14*x100;
   154     n3 = 345*x100;
   155     n4 = 210*x100;
   156     n5 = 32*x100;
   157     n6 = 899*x100;
   158     n7 = 616*x100;
   159     n8 = 93*x100;
   160     n1mult = 10;
   161 	
   162 	// Section 1, Array elements
   163     TReal64 e1[] = 
   164 		{1.0,-1.0,-1.0,-1.0};
   165 	TInt i,ix;
   166 
   167     timea.HomeTime();
   168 	for (ix=0; ix<xtra; ix++)
   169 		{
   170 		for(i=0; i<n1*n1mult; i++)
   171 			{
   172 			e1[0]=(e1[0]+e1[1]+e1[2]-e1[3])*t;
   173 			e1[1]=(e1[0]+e1[1]-e1[2]+e1[3])*t;
   174 			e1[2]=(e1[0]-e1[1]+e1[2]+e1[3])*t;
   175 			e1[3]=(-e1[0]+e1[1]+e1[2]+e1[3])*t;
   176 			}
   177         t=1.0-t;
   178 		}
   179     t=t0;                    
   180     timeb.HomeTime();
   181 	TReal64 time=I64REAL(timeb.Int64()-timea.Int64())/(TReal64(n1mult)*1E+6);
   182 
   183     pout(_L("N1 floating point "),TReal64(n1*16)*TReal64(xtra),1,e1[3],time,calibrate,1);
   184 
   185     // Section 2, Array as parameter
   186     
   187 	timea.HomeTime();
   188     for (ix=0; ix<xtra; ix++)
   189 		{
   190 		for(i=0; i<n2; i++)
   191             pa(e1,t,t2);
   192         t=1.0-t;
   193         }
   194 	t= t0; 
   195 	timeb.HomeTime();
   196 	time=I64REAL(timeb.Int64()-timea.Int64())/1E+6;
   197 
   198     pout(_L("N2 floating point "),TReal64(n2*96)*TReal64(xtra),1,e1[3],time,calibrate,2);
   199 
   200     // Section 3, Conditional jumps
   201 	TInt j=1;
   202 
   203     timea.HomeTime();       
   204 	for (ix=0; ix<xtra; ix++)
   205 		{
   206         for(i=0; i<n3; i++)
   207 			{
   208             if(j==1)
   209 				j = 2;
   210             else
   211 				j = 3;
   212             if(j>2)
   213 				j = 0;
   214             else
   215 				j = 1;
   216             if(j<1)
   217 				j = 1;
   218 			else
   219 				j = 0;
   220             }
   221         }
   222 	timeb.HomeTime();
   223 	time=I64REAL(timeb.Int64()-timea.Int64())/1E+6;
   224     
   225 	pout(_L("N3 if then else   "),TReal64(n3*3)*TReal64(xtra),2,TReal64(j),time,calibrate,3);
   226 
   227     // Section 4, Integer arithmetic
   228 	j=1;
   229     TInt k=2;
   230     TInt l=3;
   231     
   232 	timea.HomeTime();
   233     for (ix=0; ix<xtra; ix++)
   234 		{
   235         for(i=0; i<n4; i++)
   236 			{
   237             j=j*(k-j)*(l-k);
   238             k=l*k-(l-j)*k;
   239             l=(l-k)*(k+j);
   240             e1[l-2]=j+k+l;
   241             e1[k-2]=j*k*l;
   242             }
   243         }
   244 	timeb.HomeTime();
   245 	time=I64REAL(timeb.Int64()-timea.Int64())/1E+6;
   246         
   247 	TReal64 x=e1[0]+e1[1];
   248 
   249     pout(_L("N4 fixed point    "),TReal64(n4*15)*TReal64(xtra),2,x,time,calibrate,4);
   250      
   251     // Section 5, Trig functions
   252     x=0.5;
   253 	TReal64 y=0.5;
   254 	TReal64 trg1,trg2,trg3,trg4;
   255     
   256 	timea.HomeTime();
   257 	for (ix=0; ix<xtra; ix++)
   258 		{
   259 		for(i=1; i<n5; i++)
   260 			{
   261 			Math::Sin(trg1,x);
   262 			Math::Cos(trg2,x);
   263 			Math::Cos(trg3,x+y);
   264 			Math::Cos(trg4,x-y);
   265             Math::ATan(x,(t2*trg1*trg2)/(trg3+trg4-1.0));
   266 			x*=t;
   267 			Math::Sin(trg1,y);
   268 			Math::Cos(trg2,y);
   269 			Math::Cos(trg3,x+y);
   270 			Math::Cos(trg4,x-y);
   271 			Math::ATan(y,(t2*trg1*trg2)/(trg3+trg4-1.0));
   272             y*=t;
   273             }
   274         t=1.0-t;
   275         }
   276     t=t0;
   277     timeb.HomeTime();
   278 	time=I64REAL(timeb.Int64()-timea.Int64())/1E+6;
   279 
   280     pout(_L("N5 sin,cos etc.   "),TReal64(n5*26)*TReal64(xtra),2,y,time,calibrate,5);
   281   
   282     // Section 6, Procedure calls
   283     x=1.0;
   284     y=1.0;
   285     TReal64 z=1.0;
   286     
   287 	timea.HomeTime();
   288     for (ix=0; ix<xtra; ix++)
   289 		{
   290 		for(i=0; i<n6; i++)
   291 			p3(&x,&y,&z,t,t1,t2);
   292         }
   293     timeb.HomeTime();
   294 	time=I64REAL(timeb.Int64()-timea.Int64())/1E+6;
   295 
   296     pout(_L("N6 floating point "),TReal64(n6*6)*TReal64(xtra),1,z,time,calibrate,6);
   297   
   298     // Section 7, Array refrences
   299     j = 0;
   300     k = 1;
   301     l = 2;
   302 
   303     TReal64 e2[]=
   304 		{1.0,2.0,3.0};
   305     
   306 	timea.HomeTime();
   307     for (ix=0; ix<xtra; ix++)
   308 		{
   309         for(i=0;i<n7;i++)
   310 			po(&e2[0],j,k,l);
   311         }
   312     timeb.HomeTime();
   313 	time=I64REAL(timeb.Int64()-timea.Int64())/1E+6;
   314 
   315     pout(_L("N7 assignments    "),TReal64(n7*3)*TReal64(xtra),2,e2[2],time,calibrate,7);
   316         
   317 	// Section 8, Standard functions
   318     x=0.75;
   319     
   320 	timea.HomeTime();
   321     for (ix=0; ix<xtra; ix++)
   322 		{
   323         for(i=0; i<n8; i++)
   324 			{
   325 			Math::Log(trg1,x);
   326 			Math::Exp(trg1,trg1/t1);
   327 			Math::Sqrt(x,trg1);
   328 			}
   329 		}
   330 	timeb.HomeTime();
   331 	time=I64REAL(timeb.Int64()-timea.Int64())/1E+6;
   332 
   333 
   334     pout(_L("N8 exp,sqrt etc.  "),TReal64(n8*4)*TReal64(xtra),2,x,time,calibrate,8);
   335     }
   336 
   337 GLDEF_C void CallTestsL(void)
   338 	{
   339 
   340 	TInt calibrate=1;
   341     TInt xtra=1;
   342     TInt section;
   343     TInt x100=100;
   344     TInt duration=100;
   345    
   346     test.Title();
   347 
   348 #ifdef T_WHET_WITH_VFP
   349 	TInt supportedModes;
   350 	if (HAL::Get(HALData::EHardwareFloatingPoint, supportedModes) != KErrNone)
   351 		{
   352 		test.Printf(_L("No VFP hardware, skipping test\n"));
   353 		return;
   354 		}
   355 #endif
   356 
   357 	// connect and make directory for write file
   358 	test.Start(_L("Making directory..."));
   359 	TInt r=TheFs.MkDirAll(pathName);
   360 	test(r==KErrNone || r==KErrAlreadyExists);
   361 	test(TheFs.SetSessionPath(pathName)==KErrNone);
   362 	test(outfile.Replace(TheFs,fileName,EFileWrite)==KErrNone);
   363             
   364 	// calibrate
   365 	test.Next(_L("Calibrating...\n"));
   366 	TInt count=10;
   367 	
   368 	while (count>0)
   369 		{
   370 		TimeUsed=0;     
   371 		whetstones(xtra,x100,calibrate);
   372 
   373 		test.Printf(_L("%g Seconds     %d Passes (x 100)\n"),TimeUsed,xtra);
   374 		calibrate++;
   375 		count--;
   376 
   377 		if (TimeUsed>2)
   378 			count=0;
   379 		else
   380 			xtra*=5;
   381 		}
   382 	
   383 	if (TimeUsed>0) 
   384 		{
   385 		TRealX TimeX(TimeUsed);
   386 		TInt TimeInt=TInt(TimeX);
   387 		xtra*=duration/TimeInt;
   388 		}
   389 	if (xtra<1) 
   390 		xtra=1;
   391        
   392 	calibrate=0;
   393   
   394 	test.Printf(_L("\nUse %d  passes (x 100)\n"),xtra);
   395 
   396 	test.Printf(_L("\nTReal64 C/C++ Whetstone Benchmark\n"));
   397 	test.Printf(_L("\nLoop content        Result            MFLOPS          MOPS             Seconds\n\n"));
   398 
   399 	TimeUsed=0;
   400 	whetstones(xtra,x100,calibrate);
   401 	
   402 	test.Printf(_L("\nMWIPS                                 "));
   403 	if (TimeUsed>0)
   404 		mwips=(TReal64)(xtra*x100)/(10.0*TimeUsed);
   405     else
   406 		mwips=0;
   407     
   408 	test.Printf(_L("%- 17g                 %- 7g\n\n"),mwips,TimeUsed);
   409      
   410 	if (Check==TReal64(0.0)) 
   411 		test.Printf(_L("Wrong answer  "));
   412               
   413 	// Add results to output file whets.res
   414 	appendString(_L("Whetstone TReal64 Benchmark in C/C++\n"));
   415 	appendString(_L("\n\n"));
   416 	appendString(_L("Loop content        Result              MFLOPS           MOPS                 Seconds\n\n")); 
   417                            
   418 	for (section=1; section<KSections; section++)
   419 		{
   420 		buf=_L("");
   421 		buf.AppendFormat(_L("%- 20S%- 20g"),&headings[section],results[section]);
   422 		buf8.Copy(buf);		// Unicode
   423 		outfile.Write(buf8);
   424 
   425 		if (loop_mops[section]==99999.0f)
   426 			{      
   427 			buf=_L("");
   428 			buf.AppendFormat(_L("%- 39g%- 8g\n"),loop_mflops[section],loop_time[section]);
   429 			buf8.Copy(buf);		// Unicode
   430 			outfile.Write(buf8);	
   431 			}
   432 		else
   433 			{
   434 			buf=_L("");			
   435 			buf.AppendFormat(_L("                  %- 21g%- 8g\n"),loop_mops[section],loop_time[section]);
   436 			buf8.Copy(buf);		// Unicode
   437 			outfile.Write(buf8);	
   438 			}
   439 		}
   440 
   441 	buf=_L("");			
   442 	buf.AppendFormat(_L("\nMWIPS                                   %- 18g                     %- 8g\n\n"),mwips,TimeUsed);
   443 	buf8.Copy(buf);			// Unicode
   444 	outfile.Write(buf8);	
   445 
   446 	outfile.Close();
   447 	test.Printf(_L("\nResults are in file whets.res"));
   448 
   449     test.End();
   450 	return;
   451 	}
   452 
   453