Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdint>
- #include <ctime>
- #include <random>
- #include <functional>
- #include <iostream>
- // Eternity definitions
- #define fixed_t int32_t
- #define angle_t uint32_t
- #define ANG180 0x80000000
- #define LLANG360 4294967296LL
- static inline angle_t lerpAngle(fixed_t lerp, angle_t astart, angle_t aend)
- {
- int64_t start = astart;
- int64_t end = aend;
- int64_t value = abs(start - end);
- if(value > ANG180)
- {
- if(end > start)
- start += LLANG360;
- else
- end += LLANG360;
- }
- value = start + ((end - start) * lerp / 65536);
- if(value >= 0 && value < LLANG360)
- return (angle_t)value;
- else
- return (angle_t)(value % LLANG360);
- }
- #define DBLANG360 4294967296.0
- static inline angle_t lerpAngleDbl(fixed_t lerp, angle_t astart, angle_t aend)
- {
- double startf = (double)astart;
- double endf = (double)aend;
- double diff = fabs(startf - endf);
- if(diff > ANG180)
- {
- if(endf > startf)
- startf += DBLANG360;
- else
- endf += DBLANG360;
- }
- double value = startf + ((endf - startf) * lerp / 65536.0);
- if(value >= 0.0 && value < DBLANG360)
- return (angle_t)(value);
- else
- return (angle_t)(fmod(value, DBLANG360));
- }
- int main()
- {
- // Create function objects to generate uint32_t and int32_t numbers randomly
- auto unsignedrand = std::bind(std::uniform_int_distribution<uint32_t>(),
- std::mt19937(std::random_device()()));
- auto signedrand = std::bind(std::uniform_int_distribution<int32_t>(),
- std::mt19937(std::random_device()()));
- time_t pre_timer = 0;
- time_t post_timer = 0;
- time(&pre_timer);
- for(uint32_t i = 0; i < 300000000; i++)
- {
- lerpAngleDbl(signedrand(), unsignedrand(), unsignedrand());
- }
- time(&post_timer);
- std::cout << difftime(post_timer,pre_timer) << " seconds elapsed for double.\n";
- // Reset timer
- pre_timer = post_timer = 0;
- time(&pre_timer);
- for(uint32_t i = 0; i < 300000000; i++)
- {
- lerpAngle(signedrand(), unsignedrand(), unsignedrand());
- }
- time(&post_timer);
- std::cout << difftime(post_timer,pre_timer) << " seconds elapsed for int.\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement