Advertisement
Guest User

Untitled

a guest
Mar 11th, 2013
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <math.h>
  5. #include <time.h>
  6.  
  7. #define NUM_TESTLOOPS   10000
  8. #define NUM_INNERLOOPS  10000
  9.  
  10. #define PRECISION   1024 // ~ 3 digits
  11.  
  12. // floating/fixed point conversion
  13. inline int32_t FIXED(float x) { return x * PRECISION; }
  14. inline float FLOAT(int32_t x) { return (float)x / PRECISION; }
  15.  
  16. // testing values
  17. float value[4];
  18. int32_t ivalue[4];
  19.  
  20. // timing
  21. float _time()   { return (float)clock() / CLOCKS_PER_SEC; }
  22.  
  23. // perform add, subtract, divide, and multiply with floats
  24. float op_floating(float v)
  25. {
  26.     int x;
  27.  
  28.     // do work with floats
  29.     for(x = 0; x < NUM_INNERLOOPS; x++)
  30.     {
  31.         v += value[0];
  32.         v -= value[1];
  33.         v *= value[2];
  34.         v /= value[3];
  35.     }
  36.  
  37.     return v;
  38. }
  39.  
  40. // perform add, sub, mul, div, return converted to float
  41. int op_fixed(int iv)
  42. {
  43.     int x;
  44.  
  45.     // do work with ints
  46.     for(x = 0; x < NUM_INNERLOOPS; x++)
  47.     {
  48.         iv += ivalue[0];
  49.         iv -= ivalue[1];
  50.         iv = ((int64_t)iv*ivalue[2])/PRECISION;
  51.         iv = ((int64_t)iv*PRECISION)/ivalue[3];
  52.     }
  53.  
  54.     return iv;
  55. }
  56.  
  57. int main(int argc, char **argv)
  58. {
  59.     int x, iresult = 0;
  60.     float t1, t2, result = 0;
  61.  
  62.     printf("------------------------------------------\n");
  63.     printf("Simplistic test: fixed vs floating point arithmetic\n");
  64.     printf("------------------------------------------\n\n");
  65.  
  66.     // generate work values
  67.     unsigned seed = time(0); // N.B. not all seed values give valid results
  68.     srand(seed);
  69.     printf("Seed: %x\n", seed);
  70.     float start = 100*((float)rand()/RAND_MAX);
  71.     int istart = FIXED(start);
  72.     printf("Start: %f\tVals:", start);
  73.     for(x = 0; x < 4; x++)
  74.     {
  75.         value[x] = 5*((float)rand()/RAND_MAX);
  76.         ivalue[x] = FIXED(value[x]);
  77.         printf(" %3.2f",value[x]);
  78.     }
  79.  
  80.     // timed floating point arithmetic test
  81.  
  82.     printf("\n\nbeginning timed floating-point work\n");
  83.     t1 = _time();
  84.  
  85.     // do expensive floating-point work
  86.     for(x = 0; x < NUM_TESTLOOPS; x++)
  87.         result += op_floating(start);
  88.  
  89.     t1 = _time() - t1;
  90.     float fresult = op_floating(start);
  91.     printf("floating-point test time: %2.3fs\nresult = %f\n\n", t1, fresult);
  92.  
  93.     // timed fixed-point arithmetic test
  94.  
  95.     printf("beginning timed fixed-point work\n");
  96.     t2 = _time();
  97.  
  98.     // do expensive fixed-point work
  99.     for(x = 0; x < NUM_TESTLOOPS; x++)
  100.         iresult += op_fixed(istart);
  101.  
  102.     t2 = _time() - t2;
  103.     float firesult = FLOAT(op_fixed(istart));
  104.     printf("fixed-point test time: %2.3fs\nresult = %f\n\n", t2, firesult);
  105.  
  106.     double relativeError = fabs(fresult/(double)firesult - 1.0);
  107.     printf("relative error: %f\n", relativeError);
  108.     printf("relative time: %f\n", t2/t1);
  109.     return result-iresult;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement