#include <exec/exec.h>
#include <functions.h>
#include <math.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <stdio.h>

/*
	BenchMark.C
	Written by

	Heiner Hückstädt
	Gellertstraße 12
	D-5090 Leverkusen 1

	Public Domain	


	Compile with Manx V3.6

		cc -s Benchmark.c
		ln Benchmark.o -lm -lc

	

*/ 

float sinus,cosinus;
struct Library *MathTransBase;
struct Library *FPU,*OLD;


float SPAtan();
float SPSin();
float SPCos();
float SPTan();
float SPSincos();
float SPSinh();
float SPCosh();
float SPTanh();
float SPExp();
float SPLog();
float SPPow();
float SPSqrt();
ULONG SPTieee();
float SPFieee();
float SPAsin();
float SPAcos();
float SPLog10();
 
#define DUMMY 2.0
#define MAXLOOPS 25000

BPTR fh;
long time1[3];
long time2[3];
char mess[100];
long ieee;

#define TIMESTART DateStamp(&time1[0])
#define TIMEEND DateStamp(&time2[0])
#define SHOWTIME Time(&time1[0],&time2[0])
#define WAITKEY	 Print("\nAny key to continue (ESC to quit)\n");\
		 Read(fh,mess,1L);\
		 if(mess[0] == 0x1b) goto quit_and_die

long strlen();

float fputime,oldtime,fpu_all,old_all;


void Print(text)
register char *text;
{
 Write(fh,text,strlen(text));
}


float Time(v1,v2)
long *v1;
long *v2;
{
    float time;
    long sek;
    long dummy;

    dummy = v2[1]-v1[1];

    if (dummy == 0) sek = v2[2]-v1[2];
    else sek = (dummy*3000)-v1[2]+v2[2];

    time = ((float)sek)/50.0;
    sprintf(mess,"%d Loops in %.2f seconds\n",MAXLOOPS,time);
    Write(fh,mess,strlen(mess));
    return(time);	
}
void Performance()
{
  float factor;

  factor = oldtime/fputime;
  if(factor < 1.0)
	{
	 sprintf(mess,"OH BOY.. This function performs %.2f times slower\n",fputime/oldtime);
	 Write(fh,mess,strlen(mess));
	}	
  else
	{
	 sprintf(mess,"GOODIE GOODIE.. This function performs %.2f times faster\n",factor);
	 Write(fh,mess,strlen(mess));
	}	
  if(fabs(1.0-factor) < 0.05)
	{
	 sprintf(mess,"HMMM.. Seems that there is quite no difference\n");
	 Write(fh,mess,strlen(mess));
	}
}

main()
{
 register int i;
 
 fpu_all = old_all = 0.0;

 if((fh = Open("RAW:100/30/500/130/FPU-Mathtrans Benchmark  © HH 1990",MODE_NEWFILE)))
  { 
   FPU = (struct Library *)OpenLibrary("mathtrans.library",0L);
   OLD = (struct Library *)OpenLibrary("old-trans.library",0L);

/*	The old-trans.library is a renamed copy of the original
	mathtrans.library used only for comparision.
*/

   if(FPU && OLD)
	{

	 Print("Atan (2.0) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPAtan(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FPU : Atan = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPAtan(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FFP : Atan = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("Sin (2.0) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPSin(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FPU : Sin = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPSin(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FFP : Sin = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("Cos (2.0) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPCos(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FPU : Cos = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPCos(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FFP : Cos = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("Sincos (2.0) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPSincos(&cosinus,DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FPU : Sinus = %f Cosinus = %f\n",sinus,cosinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPSincos(&cosinus,DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FFP : Sinus = %f Cosinus = %f\n",sinus,cosinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("Sinh (2.0) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPSinh(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FPU : Sinh = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPSinh(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FFP : Sinh = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("Cosh (2.0) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPCosh(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FPU : Cosh = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPCosh(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FFP : Cosh = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("Tanh (2.0) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPTanh(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FPU : Tanh = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPTanh(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FFP : Tanh = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("Exp (2.0) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPExp(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FPU : Exp = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPExp(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FFP : Exp = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("Log (2.0) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPLog(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FPU : Log = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPLog(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FFP : Log = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

/*	Mysterious	SPPow does not compute ?? */

	 Print("Pow (4.0,2.0) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = pow(4.0,DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FPU : Pow = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = pow(4.0,DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FFP : Pow = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("Sqrt (2.0) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPSqrt(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FPU : Sqrt = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPSqrt(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FFP : Sqrt = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("Tieee (2.0) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  ieee = SPTieee(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FPU : Tieee = $%lx\n",ieee);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  ieee = SPTieee(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FFP : Tieee = $%lx\n",ieee);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("Fieee (2.0) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPFieee(ieee); 
	 TIMEEND;
	 sprintf(mess,"FPU : Fieee = %f FFP $%lx\n",sinus,sinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPFieee(ieee); 
	 TIMEEND;
	 sprintf(mess,"FFP : Fieee = %f FFP $%lx\n",sinus,sinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("Asin (0.5) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPAsin(0.5); 
	 TIMEEND;
	 sprintf(mess,"FPU : Asin = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPAsin(0.5); 
	 TIMEEND;
	 sprintf(mess,"FFP : Asin = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("Acos (0.5) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPAcos(0.5); 
	 TIMEEND;
	 sprintf(mess,"FPU : Acos = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPAcos(0.5); 
	 TIMEEND;
	 sprintf(mess,"FFP : Acos = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("Log10 (2.0) ..\n");
 	 MathTransBase = FPU;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPLog10(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FPU : Log10 = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 fputime = SHOWTIME;
	 MathTransBase = OLD;
	 TIMESTART;
	 for(i=0;i<MAXLOOPS;i++)  sinus = SPLog10(DUMMY); 
	 TIMEEND;
	 sprintf(mess,"FFP : Log10 = %f\n",sinus);
	 Write(fh,mess,strlen(mess));
	 oldtime = SHOWTIME;
	 Performance();
	 fpu_all += fputime;
	 old_all += oldtime;
	 WAITKEY;

	 Print("\nBenchmark terminated\n");
	 sprintf(mess,"Elapsed benchmark time FPU :%.2f seconds\n",fpu_all);
	 Write(fh,mess,strlen(mess));
	 sprintf(mess,"Elapsed benchmark time FFP :%.2f seconds\n",old_all);
	 Write(fh,mess,strlen(mess));
	 sprintf(mess,"FPU => FFP      %.2f => 1.0\n",old_all/fpu_all,1.0);
	 Write(fh,mess,strlen(mess));
	  
	 Print("\nPress Return to quit..\n");
	 Read(fh,mess,1L);

quit_and_die:
 	 MathTransBase = FPU;
	 CloseLibrary(MathTransBase);
	 CloseLibrary(OLD);
	}
     else
	{
	 Print("Error\n");
	 sprintf(mess,"Mathtrans Base $%lx\nOld-Trans Base $%lx\n",FPU,OLD);
	 Write(fh,mess,strlen(mess));
	 Read(fh,mess,1L);
   	}
     Close(fh);	
   }
}

