Advertisement
Guest User

Interpolation Algorithm Benchmark v2

a guest
Jan 5th, 2014
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdint>
  2. #include <ctime>
  3.  
  4. #include <random>
  5. #include <functional>
  6. #include <iostream>
  7.  
  8. // Eternity definitions
  9. #define fixed_t int32_t
  10. #define angle_t uint32_t
  11. #define ANG180  0x80000000
  12.  
  13. #define LLANG360 4294967296LL
  14.  
  15. static inline angle_t lerpAngle(fixed_t lerp, angle_t astart, angle_t aend)
  16. {
  17.    int64_t start = astart;
  18.    int64_t end   = aend;
  19.    int64_t value = abs(start - end);
  20.    if(value > ANG180)
  21.    {
  22.       if(end > start)
  23.          start += LLANG360;
  24.       else
  25.          end += LLANG360;
  26.    }
  27.    value = start + ((end - start) * lerp / 65536);
  28.    if(value >= 0 && value < LLANG360)
  29.       return (angle_t)value;
  30.    else
  31.       return (angle_t)(value % LLANG360);
  32. }
  33.  
  34. #define DBLANG360 4294967296.0
  35.  
  36. static inline angle_t lerpAngleDbl(fixed_t lerp, angle_t astart, angle_t aend)
  37. {
  38.    double startf = (double)astart;
  39.    double endf   = (double)aend;
  40.    double diff   = fabs(startf - endf);
  41.    if(diff > ANG180)
  42.    {
  43.       if(endf > startf)
  44.          startf += DBLANG360;
  45.       else
  46.          endf += DBLANG360;
  47.    }
  48.  
  49.    double value = startf + ((endf - startf) * lerp / 65536.0);
  50.    if(value >= 0.0 && value < DBLANG360)
  51.       return (angle_t)(value);
  52.    else
  53.       return (angle_t)(fmod(value, DBLANG360));
  54. }
  55.  
  56. int main()
  57. {
  58.     // Create function objects to generate uint32_t and int32_t numbers randomly
  59.     auto unsignedrand = std::bind(std::uniform_int_distribution<uint32_t>(),
  60.                           std::mt19937(std::random_device()()));
  61.     auto signedrand = std::bind(std::uniform_int_distribution<int32_t>(),
  62.                           std::mt19937(std::random_device()()));
  63.  
  64.     time_t pre_timer = 0;
  65.     time_t post_timer = 0;
  66.  
  67.     time(&pre_timer);
  68.     for(uint32_t i = 0; i < 300000000; i++)
  69.     {
  70.         lerpAngleDbl(signedrand(), unsignedrand(), unsignedrand());
  71.     }
  72.     time(&post_timer);
  73.     std::cout << difftime(post_timer,pre_timer) << " seconds elapsed for double.\n";
  74.  
  75.     // Reset timer
  76.     pre_timer = post_timer = 0;
  77.  
  78.     time(&pre_timer);
  79.     for(uint32_t i = 0; i < 300000000; i++)
  80.     {
  81.         lerpAngle(signedrand(), unsignedrand(), unsignedrand());
  82.     }
  83.     time(&post_timer);
  84.     std::cout << difftime(post_timer,pre_timer) << " seconds elapsed for int.\n";
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement