You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

67 lines
1.4 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. //
  11. // The Matrix classes
  12. // ------------------
  13. //
  14. #if !defined __LOL_NUMERIC_H__
  15. #define __LOL_NUMERIC_H__
  16. #include <cstdlib>
  17. #include <stdint.h>
  18. namespace lol
  19. {
  20. /* Random float value */
  21. static inline float RandF()
  22. {
  23. using namespace std;
  24. return (float)rand() / RAND_MAX;
  25. }
  26. static inline float RandF(float val)
  27. {
  28. return RandF() * val;
  29. }
  30. static inline float RandF(float min, float max)
  31. {
  32. return min + RandF() * (max - min);
  33. }
  34. /* Next power of two. */
  35. template <typename T> static inline T PotUp(T val)
  36. {
  37. val = val - 1;
  38. if (sizeof(val) > 4) val = val | ((uint64_t)val >> 32);
  39. if (sizeof(val) > 2) val = val | ((uint64_t)val >> 16);
  40. if (sizeof(val) > 1) val = val | ((uint64_t)val >> 8);
  41. val = val | ((uint64_t)val >> 4);
  42. val = val | ((uint64_t)val >> 2);
  43. val = val | ((uint64_t)val >> 1);
  44. return val + 1;
  45. }
  46. //Lerp for float
  47. template <typename T1, typename T2, typename Tf> static inline T1 damp(const T1 &a, const T2 &b, const Tf &x, const Tf &dt)
  48. {
  49. if (dt <= .0f)
  50. return a;
  51. return lol::lerp(a, b, dt / (dt + x));
  52. }
  53. } /* namespace lol */
  54. #endif // __LOL_NUMERIC_H__