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.
 
 
 

60 lines
1.2 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 <cmath>
  17. #include <cstdlib>
  18. #include <stdint.h>
  19. namespace lol
  20. {
  21. /* Random float value */
  22. static inline float RandF()
  23. {
  24. using namespace std;
  25. return (float)rand() / RAND_MAX;
  26. }
  27. static inline float RandF(float val)
  28. {
  29. return RandF() * val;
  30. }
  31. static inline float RandF(float min, float max)
  32. {
  33. return min + RandF() * (max - min);
  34. }
  35. /* Next power of two. */
  36. template <typename T> static inline T PotUp(T val)
  37. {
  38. val = val - 1;
  39. if (sizeof(val) > 4) val = val | ((uint64_t)val >> 32);
  40. if (sizeof(val) > 2) val = val | ((uint64_t)val >> 16);
  41. if (sizeof(val) > 1) val = val | ((uint64_t)val >> 8);
  42. val = val | ((uint64_t)val >> 4);
  43. val = val | ((uint64_t)val >> 2);
  44. val = val | ((uint64_t)val >> 1);
  45. return val + 1;
  46. }
  47. } /* namespace lol */
  48. #endif // __LOL_NUMERIC_H__