view test/float.c @ 282:d61cf7a9b469

first jmp in MIPS
author kono
date Mon, 24 May 2004 07:37:42 +0900
parents ebaec1ae566e
children ec1a68133f6a
line wrap: on
line source

#include "stdio.h"

void test1();
void print(double d);

extern double sin(double);
// extern float fsin(float);
double test2(double f,int i);

float f = 0.3;
double d = 0.3;
float f1 = 0.3;
double d1 = 0.3;
double d2 = -0.2;
float h = 1.0;

int
d2i(double d) {
   return (int)d;
}

double
i2d(int u) {
   return (double)u;
}

unsigned u;
unsigned
d2u(double d) {
   return (unsigned)d;
}

double
u2d(unsigned u) {
   return (double)u;
}

int
main(int ac,char *av[]) {
   double g;
   int i;
   unsigned u;
   double d00 = ac?0.5:3;

   printf("%g\n",d00);

   g = 1.0;
   g = -g;
   printf("%d\ncond0 ",1);
   if(f==f*1.0) printf("t ");
   printf("%d\ncond1 ",f==f*1.0);
   if(d==f*1.0) printf("t ");
   printf("%d\ncond2 ",d==f*1.0);
   if(f==f1) printf("t ");
   printf("%d\ncond3 ",f==f1);
   if(d==d1) printf("t ");
   printf("%d\ncond4 ",d==d2);
   if(d==d2) printf("t ");
   printf("%d\ncond5 ",(d==d2));
   if(d>d1) printf("t ");
   printf("%d\ncond6 ",d>d1);
   if(d>d2) printf("t ");
   printf("%d\ncond7 ",d>d2);
   if(d>=d1) printf("t ");
   printf("%d\ncond8 ",d>=d1);
   if(d>=d2) printf("t ");
   printf("%d\ncond9 ",d>=d2);
   if(d!=d1) printf("t ");
   printf("%d\ncond10 ",d!=d1);
   if(d!=d2) printf("5 ");
   printf("%d\ncond11 ",d!=d2);
   if(d<d1) printf("t ");
   printf("%d\ncond12 ",d<d1);
   if(d<d2) printf("t ");
   printf("%d\ncond13 ",d<d2);
   if(d<=d1) printf("t ");
   printf("%d\ncond14 ",d<=d1);
   if(d<=d2) printf("t ");
   printf("%d\ncond15 ",d<=d2);
   d = 123.4234; f=-234.333;
   i = d;
   d = i;
   i = f;
   f = i;
   printf("\ni=%d d=%g f=%f",i,d,f);
   f = g = d = d1 = d2 = f;
   printf(" i=%d d=%g f=%f g=%g\n",i,d,f,g);

   d = 4204967294.4234; f=4204967294.4234;
   u = d;
   d = u;
   u = f;
   f = u;
   printf("u=%u d=%g f=%f\n",u,d,f);

   print(1.0);
   print(0.1234);
   print(1.234e10);
   print(1.234e-10);

   test1();
   printf("nested call: %g\n",test2(test2(test2(test2(-0.333,3),5),6),7));
   return 0;
}

void
print(double d)
{
    float f;
    int *dd;

    f = d;

    dd = (int*) &d;
    printf("d %g ",d);
    printf("dx %08x %08x\n",*(dd),*(dd+1));

    dd = (int*) &f;
    printf("f %g ",f);
    printf("dx %08x \n",*(dd));
}

double
testd(double i,double j)
{
    return j+1.1+.0e3+12.3e-12;
}

float
testf(float i,float j)
{
    return j+1;
}

void
test1()
{
    float f;
    float f1;
    double g;
    double g1;
    float *pf;
    float *pf1;
    double *pg;
    double *pg1;
    int n = 1;

    printf("simple double ");
    f = 1.3;

    g = 1.0;
    g = g+g;
    printf("%d:%g\t",n++,g);
    g1 = g*g;
    printf("%d:%g\t",n++,g1);
    g = g/g1;
    printf("%d:%g\t",n++,g);
    g = g-g1;
    printf("%d:%g\t",n++,g);
    g = sin(g1);
    printf("%d:%g\t",n++,g);
    g = testd(g,g1);
    printf("%d:%g\t",n++,g);
    printf("\n");

    printf("simple float ");
    f = f+f;
    printf("%d:%g\t",n++,f);
    f1 = f*f;
    printf("%d:%g\t",n++,f1);
    f = f/f1;
    printf("%d:%g\t",n++,f);
    f = f-f1;
    printf("%d:%g\t",n++,f);
    f = sin(f1);
    printf("%d:%g\t",n++,f);
    printf("\n");

    printf("post/pre increment ");
    g1 = g;
    printf("%d:%g\t",n++,g1++ - ++g);

    f1 = f;
    printf("%d:%g\t",n++,f1++ - ++f);

    g1 = g;
    printf("%d:%g\t",n++,g1-- - --g);

    f1 = f;
    printf("%d:%g\t",n++,f1-- - --f);

    printf("\n");
    printf("simple calc ");

    f=0.13; g=-0.56; f1=-0.13; g1=0.56;

    g = f+f;
    printf("%d:%g\t",n++,g);
    f = g*g;
    printf("%d:%g\t",n++,f);
    g = g*g+f*f-g1*g1;
    printf("%d:%g\t",n++,g);
    printf("\n");

    printf("float argument ");
    f = testf(f,f1);
    printf("%d:%g\t",n++,f);

    printf("\nindirect  ");
    n=1;
    f = 1.3; pf=&f; pf1=&f1;

    g = 1.0; pg=&g; pg1=&g1;
    *pg = *pg+ *pg;
    printf("%d:%g\t",n++,*pg);
    *pg1 = *pg**pg;
    printf("%d:%g\t",n++,*pg1);
    *pg = *pg/ *pg1;
    printf("%d:%g\t",n++,*pg);
    *pg = *pg-*pg1;
    printf("%d:%g\t",n++,*pg);
    *pg = sin(*pg1);
    printf("%d:%g\t",n++,*pg);
    *pg = testd(*pg,*pg1);
    printf("%d:%g\t",n++,*pg);
    printf("\n");

    *pf = *pf+*pf;
    printf("%d:%g\t",n++,*pf);
    *pf1 = *pf**pf;
    printf("%d:%g\t",n++,*pf1);
    *pf = *pf/ *pf1;
    printf("%d:%g\t",n++,*pf);
    *pf = *pf-*pf1;
    printf("%d:%g\t",n++,*pf);
    *pf = sin(*pf1);
    printf("%d:%g\t",n++,*pf);
    printf("\n");

    printf("indirect post/pre ");
    *pg1 = *pg;
    printf("%d:%g\t",n++,(*pg1)++ - ++(*pg));

    *pf1 = *pf;
    printf("%d:%g\t",n++,(*pf1)++ - ++(*pf));

    *pg1 = *pg;
    printf("%d:%g\t",n++, (*pg1)-- - --(*pg));

    *pf1 = *pf;
    printf("%d:%g\t",n++, (*pf1)-- - --(*pf));
    printf("\n");

    *pf=0.13; *pg=-0.56; *pf1=-0.13; *pg1=0.56;

    *pg = *pf+*pf;
    printf("%d:%g\t",n++,*pg);
    *pf = *pg**pg;
    printf("%d:%g\t",n++,*pf);
    *pg = *pg**pg+*pf**pf-*pg1**pg1;
    printf("%d:%g\t",n++,*pg);
    printf("\n");

    printf("float argument ");

    *pf = testf(*pf,*pf1);
    printf("%d:%g\t",n++,*pf);


    printf("\nassop ");
    n=1;
    f = 1.3;
    g = 1.0;

    g *= 2*g;
    printf("%d:%g\t",n++,g);
    g /= 2*g;
    printf("%d:%g\t",n++,g);
    g -= 2*g;
    printf("%d:%g\t",n++,g);
    g += 2*g;
    printf("%d:%g\t",n++,g);

    f *= 2*g;
    printf("%d:%g\t",n++,f);
    f /= 2*g;
    printf("%d:%g\t",n++,f);
    f -= 2*g;
    printf("%d:%g\t",n++,f);
    f += 2*g;
    printf("%d:%g\t",n++,f);
    printf("\n");

    n=1;
    f = 1.3;
    g = 1.0;

    printf("indirect assop ");
    *pg *= 2**pg;
    printf("%d:%g\t",n++,*pg);
    *pg /= 2**pg;
    printf("%d:%g\t",n++,*pg);
    *pg -= 2**pg;
    printf("%d:%g\t",n++,*pg);
    *pg += 2**pg;
    printf("%d:%g\t",n++,*pg);

    *pf *= 2**pg;
    printf("%d:%g\t",n++,*pf);
    *pf /= 2**pg;
    printf("%d:%g\t",n++,*pf);
    *pf -= 2**pg;
    printf("%d:%g\t",n++,*pf);
    *pf += 2**pg;
    printf("%d:%g\t",n++,*pf);
    printf("\n");


    return;
}

double
test2(double f,int i)
{
    double g,h;
  
    if (i<=0) return f;
#if 1
    printf("rec: %d %g\n",i,f);
#endif
    g =  f*2;
    h =  f-0.5;
    return h/3-(3.0-(g+3)*test2(f*0.5,i-1)/(h-1));
}