// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#include <stdlib.h>
#include <time.h>

#include <iostream>

using namespace std;

#include "fp.h"
#define PRINT(z) cout << #z << ": " << (z) << endl;

#define TEST(nam,v1,v2) \
void nam() \
{ \
   class BigFP16 a = v1; \
   PRINT(a); \
   class BigFP16 b = v2; \
   PRINT(b); \
   class BigFP16 c; \
   PRINT(c); \
   PRINT(a+b); \
   c = a+b; \
   PRINT(c); \
   PRINT(a-b); \
   c = a-b; \
   PRINT(c); \
   PRINT(a*b); \
   c = a*b; \
   PRINT(c); \
   PRINT(b/a); \
   c = b/a; \
   PRINT(c); \
   PRINT(2*a+1); \
   c = 2*a+1; \
   PRINT(c); \
   cout << "----" << endl; \
   return; \
}

TEST(test1,1,2)
TEST(test2,-1,-2)
TEST(test3,12,44)
TEST(test4,-2,8)
TEST(test5,2,-8)

void testf(double x)
{
   class BigFP16 xx = x;
   PRINT(xx);
   PRINT(sqrt(xx));
   PRINT(pow(xx,5));
   PRINT(pow(xx,0.5));
   PRINT(exp(xx));
   PRINT(log(xx));
   PRINT(cos(xx));
   PRINT(sin(xx));
   PRINT(tan(xx));
   return;
}

void testcpu()
{
   class BigFP32 a = 12345.67890;
   class BigFP32 b = 987.65;
   class BigFP32 c;
   clock_t t;
   int i;
   t = clock();
   for(i=0;i<10000;i++) c = a+b;
   cerr << "addition " << ((1.0*(clock()-t))/CLOCKS_PER_SEC) << endl;
   t = clock();
   for(i=0;i<10000;i++) c = a-b;
   cerr << "subtraction " << ((1.0*(clock()-t))/CLOCKS_PER_SEC) << endl;
   t = clock();
   for(i=0;i<10000;i++) c = a*b;
   cerr << "multiplication " << ((1.0*(clock()-t))/CLOCKS_PER_SEC) << endl;
   t = clock();
   for(i=0;i<100;i++) c = a/b;
   cerr << "division " << ((100.0*(clock()-t))/CLOCKS_PER_SEC) << endl;
   t = clock();
   for(i=0;i<10;i++) c = sqrt(a);
   cerr << "sqrt " << ((1000.0*(clock()-t))/CLOCKS_PER_SEC) << endl;
   t = clock();
   for(i=0;i<1;i++) c = pow(a,4);
   cerr << "pow int " << ((10000.0*(clock()-t))/CLOCKS_PER_SEC) << endl;
   t = clock();
   for(i=0;i<1;i++) c = pow(a,0.5);
   cerr << "pow double " << ((10000.0*(clock()-t))/CLOCKS_PER_SEC) << endl;
   t = clock();
   for(i=0;i<1;i++) c = exp(b/100);
   cerr << "exp " << ((10000.0*(clock()-t))/CLOCKS_PER_SEC) << endl;
   t = clock();
   for(i=0;i<1;i++) c = log(a);
   cerr << "log " << ((10000.0*(clock()-t))/CLOCKS_PER_SEC) << endl;
   t = clock();
   for(i=0;i<1;i++) c = cos(b);
   cerr << "cos " << ((10000.0*(clock()-t))/CLOCKS_PER_SEC) << endl;
   t = clock();
   for(i=0;i<1;i++) c = sin(b);
   cerr << "sin " << ((10000.0*(clock()-t))/CLOCKS_PER_SEC) << endl;
   t = clock();
   for(i=0;i<1;i++) c = tan(b);
   cerr << "tan " << ((10000.0*(clock()-t))/CLOCKS_PER_SEC) << endl;
   return;
}

int main()
{
   test1();
   test2();
   test3();
   test4();
   test5();
   testf(2.5);
   testf(25);
   testcpu();
   exit(EXIT_SUCCESS);
}

