Utility.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef CORE_UTILITY_H
  2. #define CORE_UTILITY_H
  3. #include <string.h>
  4. #include "core/Types.h"
  5. size_t popCount(u64 u);
  6. #define interpolate(a, b, factor) ((a) * (1.0f - (factor)) + (b) * (factor))
  7. #define PI 3.14159265358979323846f
  8. #define radianToDegree(radians) ((radians) * (180.0f / PI))
  9. #define degreeToRadian(degrees) ((degrees) * (PI / 180.0f))
  10. #define MIN_MAX(type, name) \
  11. inline type max##name(type a, type b) { \
  12. return a > b ? a : b; \
  13. } \
  14. inline type min##name(type a, type b) { \
  15. return a < b ? a : b; \
  16. } \
  17. inline type clamp##name(type t, type from, type to) { \
  18. return max##name(min##name(t, to), from); \
  19. }
  20. MIN_MAX(size_t, Size)
  21. MIN_MAX(u32, U32)
  22. MIN_MAX(i32, I32)
  23. MIN_MAX(float, Float)
  24. typedef void (*ExitHandler)(int, void*);
  25. [[noreturn]] void exitWithHandler(const char* file, int line, int value);
  26. void setExitHandler(ExitHandler h, void* data);
  27. #define EXIT(exitValue) exitWithHandler(__FILE__, __LINE__, exitValue)
  28. typedef void (*OutOfMemoryHandler)(void*);
  29. void setOutOfMemoryHandler(OutOfMemoryHandler h, void* data);
  30. #ifdef CHECK_MEMORY
  31. void* coreDebugAllocate(const char* file, int line, size_t n);
  32. void* coreDebugZeroAllocate(const char* file, int line, size_t n);
  33. void* coreDebugReallocate(const char* file, int line, void* p, size_t n);
  34. void coreFreeDebug(const char* file, int line, void* p);
  35. void printMemoryReport(void);
  36. #define coreAllocate(n) coreDebugAllocate(__FILE__, __LINE__, n)
  37. #define coreZeroAllocate(n) coreDebugZeroAllocate(__FILE__, __LINE__, n)
  38. #define coreReallocate(p, n) coreDebugReallocate(__FILE__, __LINE__, p, n)
  39. #define coreFree(p) coreFreeDebug(__FILE__, __LINE__, p)
  40. #else
  41. void* coreAllocate(size_t n);
  42. void* coreZeroAllocate(size_t n);
  43. void* coreReallocate(void* p, size_t n);
  44. void coreFree(void* p);
  45. #define printMemoryReport()
  46. #endif
  47. bool sleepMillis(i64 millis);
  48. bool sleepNanos(i64 nanos);
  49. i64 getNanos(void);
  50. #define swap(a, b) \
  51. do { \
  52. auto aPointer = (a); \
  53. auto bPointer = (b); \
  54. auto tmp = *aPointer; \
  55. *aPointer = *bPointer; \
  56. *bPointer = tmp; \
  57. } while(0)
  58. #define BUBBLE_SORT(type, Type) void bubbleSort##Type(type* data, size_t n);
  59. #define BUBBLE_SORT_SOURCE(type, Type, greaterThan) \
  60. void bubbleSort##Type(type* data, size_t n) { \
  61. bool swapped = true; \
  62. while(swapped && n > 0) { \
  63. swapped = false; \
  64. n--; \
  65. for(size_t i = 0; i < n; i++) { \
  66. if(greaterThan(data[i], data[i + 1])) { \
  67. swap(data + i, data + i + 1); \
  68. swapped = true; \
  69. } \
  70. } \
  71. } \
  72. }
  73. #endif