home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / util / scbench / float88.c next >
Encoding:
C/C++ Source or Header  |  1980-01-01  |  7.1 KB  |  315 lines

  1. /*
  2. ** BYTE Small-C Floating-Point Benchmark
  3. ** Version 1 for 8088/8086/80286
  4. ** March, 1988
  5. ** Written in BYTE Small-C
  6. ** Based on Small-C by J.E. Hendrix
  7. **
  8. ** Operation:
  9. ** 1. Initialize the coprocessor.
  10. ** 2. Execute fourbang(), which:
  11. **    a. Generates space for temporaries and constants.
  12. **    b. Turns on stopwatch.
  13. **    c. Executes loop FCOUNT times.  Loop consists of:
  14. **       8 each of floating add, subtract, multiply, divide.
  15. **    d. Turns off stopwatch.
  16. **    e. Calculates time for an empty loop.
  17. **    f. Reports time and result of operations.
  18. ** 3. Executes finteg(), which:
  19. **    a. Generates space for temporaries and constants.
  20. **    b. Turns on stopwatch.
  21. **    c. Executes a trapezoidal integration method for sin(x)
  22. **       from 0 to pi/2.
  23. **    d. Turns off stopwatch.
  24. **    e. Calculates time for an empty loop.
  25. **    f. Reports time and result of operation.
  26. **    g. Turns on stopwatch.
  27. **    h. Executes a trapezoidal integration method for e^(x)
  28. **       from 0 to 1.
  29. **    i. Turns off stopwatch.
  30. **    j. Calculates time for an empty loop.
  31. **    k. Reports time and result of operation.
  32. ** 4. Exits.
  33. **
  34. ** NOTE:
  35. ** Since Small-C does not support floating-point, we simply
  36. ** manipluate floating-point numbers as 4-element integer
  37. ** arrays.  The floating-point library functions handle the
  38. ** actual calls to the coprocessor, including the routine
  39. ** to print out a floating-point number.
  40. **
  41. ** Expected results:
  42. ** For first test: 1.000000000E1  (10.0)
  43. ** For trapezoidal of sine(x): 1.000000000E0  (1.0)
  44. ** For trapezoidal of e^x: 1.718281828E0 (e-1)
  45. */
  46.  
  47. #include stdio.h
  48.  
  49. #define FCOUNT 20000    /* Number of times the four-banger test */
  50.             /* is repeated.                */
  51. #define ICOUNT 32000    /* Stepsize for the integration test    */
  52.  
  53. /* Timer holding variables */
  54. int tblock[4];
  55. int mtblock[4];        /* For empty loop timing */
  56.  
  57. main()
  58. {
  59.     /* Announce yourself */
  60.     printf("BYTE Small-C Floating-Point Coprocessor Benchmark\n\n");
  61.     
  62.     /* Initialize the math coprocessor */
  63.     finit();
  64.  
  65.     /* Do four-banger test */
  66.     fourbang();
  67.  
  68.     /* Do integration */
  69.     finteg();
  70.  
  71.     /* Go home */
  72.     finit();
  73.  
  74.     printf("Press RETURN to exit:");
  75.     fgetc(stdin);
  76.  
  77.     exit(0);
  78. }
  79.  
  80. /*
  81. ** fourbang()
  82. ** Executes a loop of floating add, subtract, multiplies, and divides.
  83. */
  84. fourbang()
  85. {
  86.     int ten[4];    /* Holder for 10 */
  87.     int one[4];    /* Holder for one */
  88.     int temp[4];    /* Temporary storage */
  89.     int i;
  90.  
  91.     /* Announce yourself */
  92.     printf("Basic Math Test (+,-,*,/)\n");
  93.  
  94.     /* First set up constants */
  95.     ten[0]=10;
  96.     ten[1]=ten[2]=ten[3]=0;
  97.     fint2float(ten);
  98.     fconst(1);
  99.     fstore(one);
  100.  
  101.     /* Initialize temp location */
  102.     fload(ten);
  103.     fstore(temp);
  104.  
  105.     gtime(tblock);
  106.     /* Do the operation */
  107.     for(i=0;i<FCOUNT;++i)
  108.     {
  109.         f2add(temp,one,temp);
  110.         f2sub(temp,one,temp);
  111.         f2mult(temp,ten,temp);
  112.         f2div(temp,ten,temp);
  113.         f2sub(temp,one,temp);
  114.         f2mult(temp,ten,temp);
  115.         f2add(temp,ten,temp);
  116.         f2div(temp,ten,temp);
  117.         f2add(temp,one,temp);
  118.         f2sub(temp,one,temp);
  119.         f2mult(temp,ten,temp);
  120.         f2div(temp,ten,temp);
  121.         f2sub(temp,one,temp);
  122.         f2mult(temp,ten,temp);
  123.         f2add(temp,ten,temp);
  124.         f2div(temp,ten,temp);
  125.         f2add(temp,one,temp);
  126.         f2sub(temp,one,temp);
  127.         f2mult(temp,ten,temp);
  128.         f2div(temp,ten,temp);
  129.         f2sub(temp,one,temp);
  130.         f2mult(temp,ten,temp);
  131.         f2add(temp,ten,temp);
  132.         f2div(temp,ten,temp);
  133.         f2add(temp,one,temp);
  134.         f2sub(temp,one,temp);
  135.         f2mult(temp,ten,temp);
  136.         f2div(temp,ten,temp);
  137.         f2sub(temp,one,temp);
  138.         f2mult(temp,ten,temp);
  139.         f2add(temp,ten,temp);
  140.         f2div(temp,ten,temp);
  141.     }
  142.     calctim(tblock);
  143.  
  144.     /* Now calculate an empty loop */
  145.     gtime(mtblock);
  146.     for(i=0;i<FCOUNT;++i) ;
  147.     calctim(mtblock);
  148.  
  149.     /* Report results */
  150.     printf("***Results: (All times are HH:MM:SS:1/100ths\n");
  151.  
  152.     printf("Total time: %d:%d:%d:%d\n",tblock[0],tblock[1],
  153.         tblock[2],tblock[3]);
  154.  
  155.     printf("Empty loop time: %d:%d:%d:%d\n",mtblock[0],mtblock[1],
  156.         mtblock[2],mtblock[3]);
  157.     printf("Value:");
  158.     fltprint(10,temp);
  159.     printf("\n\n");
  160.  
  161.     return;
  162. }
  163.  
  164. /*
  165. ** finteg()
  166. ** Do integration.
  167. **
  168. */
  169.  
  170. finteg()
  171. {
  172.  
  173.     int two[4];        /* Holder for two */
  174.     int pitwo[4];        /* Holder for pi/2 */
  175.     int temp[4];        /* Temp location */
  176.     int sinex[4];        /* Sine value */
  177.     int ex[4];        /* e^x value */
  178.     int accum[4];        /* Accumulator */
  179.     int x[4];        /* Holder for x */
  180.     int i;
  181.  
  182.     /* Announce yourself */
  183.     printf("Trapezoidal rule for sin(x) 0->x->pi/2 \n");
  184.  
  185.     /* Generate 2 */
  186.     two[0]=2;
  187.     two[1]=two[2]=two[3]=0;
  188.     fint2float(two);
  189.  
  190.     /* Generate pi/2 */
  191.     fgetpi2();        /* Get pi over two */
  192.     fstore(pitwo);        /* Store pi over two */
  193.  
  194.     /* Generate stepsize */
  195.     temp[0]=ICOUNT;
  196.     temp[1]=temp[2]=temp[3]=0;
  197.     fint2float(temp);
  198.     f2div(pitwo,temp,temp);    /* Stepsize in temp */
  199.  
  200.     /* Clear accumulator */
  201.     fconst(0);
  202.     fstore(accum);
  203.  
  204.     /* Store x(0) */
  205.     fconst(0);
  206.     fstore(x);
  207.  
  208. /* Do trapezoidal rule for sine(x) */
  209.     gtime(tblock);
  210.     for(i=0;i<ICOUNT+1;++i)
  211.     { 
  212.         fsin(x,sinex);        /* Get sinex */
  213.         f2add(accum,sinex,accum);
  214.         if((i!=0)&&(i!=ICOUNT))
  215.           f2add(accum,sinex,accum);
  216.         f2add(x,temp,x);    /* Increment by step */
  217.     }
  218.     f2mult(accum,temp,accum);    /* Times stepsize */
  219.     f2div(accum,two,accum);        /* Divided by 2 */
  220.  
  221.     /* Calculate time */
  222.     calctim(tblock);
  223.  
  224.     /* Get time for an empty loop */
  225.     gtime(mtblock);
  226.     for(i=0;i<ICOUNT+1;++i) ;
  227.     calctim(mtblock);
  228.  
  229.     /* Report results */
  230.     printf("**** Results: (All times are HH:MM:SS:1/100ths)\n");
  231.  
  232.     printf("Total time:  %d:%d:%d:%d\n",tblock[0],tblock[1],
  233.         tblock[2],tblock[3]);
  234.     printf("Empty loop time: %d:%d:%d:%d\n",mtblock[0],mtblock[1],
  235.         mtblock[2],mtblock[3]);
  236.  
  237.     printf("Value:");
  238.     fltprint(10,accum);
  239.     printf("\n\n");
  240.  
  241. /* Now do trapezoidal rule for e^x */
  242.  
  243.     printf("Trapezoidal rule for e^x  0->x->1\n");
  244.  
  245.     /* Generate stepsize */
  246.     temp[0]=ICOUNT;
  247.     temp[1]=temp[2]=temp[3]=0;
  248.     fint2float(temp);
  249.     fconst(1);
  250.  
  251.     fdiv(temp);
  252.     fstore(temp);        /* Stepsize in temp */
  253.  
  254.     /* Clear accumulator */
  255.     fconst(0);
  256.     fstore(accum);
  257.  
  258.     /* Store x(0) */
  259.     fconst(0);
  260.     fstore(x);
  261.  
  262.     /* Do trapezoidal rule */
  263.     gtime(tblock);
  264.     for(i=0;i<ICOUNT+1;++i)
  265.     { 
  266.         fex(x,ex);        /* Get sinex */
  267.         f2add(accum,ex,accum);
  268.         if((i!=0)&&(i!=ICOUNT))
  269.           f2add(accum,ex,accum);
  270.         f2add(x,temp,x);    /* Increment by step */
  271.     }
  272.     f2mult(accum,temp,accum);    /* Times stepsize */
  273.     f2div(accum,two,accum);        /* Divided by 2 */
  274.  
  275.     /* Calculate time */
  276.     calctim(tblock);
  277.  
  278.     /* Get time for an empty loop */
  279.     gtime(mtblock);
  280.     for(i=0;i<ICOUNT+1;++i) ;
  281.     calctim(mtblock);
  282.  
  283.     /* Report results */
  284.     printf("**** Results: (All times are HH:MM:SS:1/100ths)\n");
  285.  
  286.     printf("Total time:  %d:%d:%d:%d\n",tblock[0],tblock[1],
  287.         tblock[2],tblock[3]);
  288.     printf("Empty loop time: %d:%d:%d:%d\n",mtblock[0],mtblock[1],
  289.         mtblock[2],mtblock[3]);
  290.  
  291.     printf("Value:");
  292.     fltprint(10,accum);
  293.     printf("\n\n");
  294.     
  295.     /* Go home */
  296.     return;
  297. }
  298.  
  299. /*
  300. ** fgetpi2()
  301. ** Puts the value pi/2 on top of the floating-point stack.
  302. */
  303. fgetpi2()
  304. {
  305.     int two[4];    /* Holder for 2 */
  306.  
  307.     two[0]=2;
  308.     two[1]=two[2]=two[3]=0;
  309.     fint2float(two);
  310.  
  311.     fconst(2);    /* Get pi on top of stack */
  312.     fdiv(two);    /* pi/2 now on floating point stack top */
  313.     return;
  314. }
  315.