misc.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef __sti__misc_h__
  2. #define __sti__misc_h__
  3. // Public Domain.
  4. #include <stdlib.h>
  5. #include <math.h> // fmin,fmax
  6. // *performance* time counting functions
  7. // these use CLOCK_MONOTONIC_RAW (>= Linux 2.6.28)
  8. // they do not provide a real unix timestamp, rather a consistent, precise measure of
  9. // some undefined time that is unaffected by system time twiddling
  10. double getCurrentTimePerf(void); // in seconds
  11. double timeSincePerf(double past); // also in seconds
  12. // *actual wall time* functions
  13. // these give unix timestamps
  14. // this function provides the number of seconds since the unix epoch
  15. double getCurrentTimeEpoch(void); // in seconds
  16. // deceptively but consistently named, this function is comparative with stamps
  17. // provided by the previous function
  18. double timeSinceEpoch(double past); // also in seconds
  19. #ifndef STI_HAS_STATIC_nextPOT
  20. #define STI_HAS_STATIC_nextPOT
  21. // super nifty site:
  22. // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  23. inline static size_t nextPOT(size_t in) {
  24. in--;
  25. in |= in >> 1;
  26. in |= in >> 2;
  27. in |= in >> 4;
  28. in |= in >> 8;
  29. in |= in >> 16;
  30. in++;
  31. return in;
  32. }
  33. #endif // STI_HAS_STATIC_nextPOT
  34. #ifndef STI_C3DLAS_NO_CONFLICT
  35. // Random number helpers
  36. inline static float frand(float low, float high) {
  37. return low + ((high - low) * ((float)rand() / (float)RAND_MAX));
  38. }
  39. inline static float frandNorm() {
  40. return ((float)rand() / (float)RAND_MAX);
  41. }
  42. inline static double drand(double low, double high) {
  43. return low + ((high - low) * ((double)rand() / (double)RAND_MAX));
  44. }
  45. inline static double drandNorm() {
  46. return ((double)rand() / (double)RAND_MAX);
  47. }
  48. // clamping
  49. inline static float fclamp(float val, float min, float max) {
  50. return fminf(max, fmaxf(min, val));
  51. }
  52. inline static float fclampNorm(float val) {
  53. return fclamp(val, 0.0f, 1.0f);
  54. }
  55. inline static float dclamp(double val, double min, double max) {
  56. return fmin(max, fmax(min, val));
  57. }
  58. inline static float dclampNorm(double val) {
  59. return fclamp(val, 0.0, 1.0);
  60. }
  61. inline static int iclamp(int val, int min, int max) {
  62. return val > max ? max : (val < min ? min : val);
  63. }
  64. inline static int uiclamp(unsigned int val, unsigned int min, unsigned int max) {
  65. return val > max ? max : (val < min ? min : val);
  66. }
  67. inline static int iclamp64(int64_t val, int64_t min, int64_t max) {
  68. return val > max ? max : (val < min ? min : val);
  69. }
  70. inline static int uclamp64(uint64_t val, uint64_t min, uint64_t max) {
  71. return val > max ? max : (val < min ? min : val);
  72. }
  73. // lerps (linear interpolation)
  74. inline static float flerp(float a, float b, float t) {
  75. return a + ((b - a) * t);
  76. }
  77. inline static float dlerp(double a, double b, double t) {
  78. return a + ((b - a) * t);
  79. }
  80. inline static float flerp2D(float x0y0, float x1y0, float x0y1, float x1y1, float tx, float ty) {
  81. return flerp(flerp(x0y0, x1y0, tx), flerp(x0y1, x1y1, tx), ty);
  82. }
  83. inline static float dlerp2D(double x0y0, double x1y0, double x0y1, double x1y1, double tx, double ty) {
  84. return dlerp(dlerp(x0y0, x1y0, tx), dlerp(x0y1, x1y1, tx), ty);
  85. }
  86. #endif
  87. #endif // __sti__misc_h__