12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #ifndef CORE_UTILITY_H
- #define CORE_UTILITY_H
- #include <string.h>
- #include "core/Types.h"
- size_t popCount(u64 u);
- #define interpolate(a, b, factor) ((a) * (1.0f - (factor)) + (b) * (factor))
- #define PI 3.14159265358979323846f
- #define radianToDegree(radians) ((radians) * (180.0f / PI))
- #define degreeToRadian(degrees) ((degrees) * (PI / 180.0f))
- inline size_t maxSize(size_t a, size_t b) {
- return a > b ? a : b;
- }
- inline size_t minSize(size_t a, size_t b) {
- return a < b ? a : b;
- }
- typedef void (*ExitHandler)(int, void*);
- [[noreturn]] void exitWithHandler(const char* file, int line, int value);
- void setExitHandler(ExitHandler h, void* data);
- #define EXIT(exitValue) exitWithHandler(__FILE__, __LINE__, exitValue)
- typedef void (*OutOfMemoryHandler)(void*);
- void setOutOfMemoryHandler(OutOfMemoryHandler h, void* data);
- #ifdef CHECK_MEMORY
- void* coreDebugAllocate(const char* file, int line, size_t n);
- void* coreDebugZeroAllocate(const char* file, int line, size_t n);
- void* coreDebugReallocate(const char* file, int line, void* p, size_t n);
- void coreFreeDebug(const char* file, int line, void* p);
- void printMemoryReport(void);
- #define coreAllocate(n) coreDebugAllocate(__FILE__, __LINE__, n)
- #define coreZeroAllocate(n) coreDebugZeroAllocate(__FILE__, __LINE__, n)
- #define coreReallocate(p, n) coreDebugReallocate(__FILE__, __LINE__, p, n)
- #define coreFree(p) coreFreeDebug(__FILE__, __LINE__, p)
- #else
- void* coreAllocate(size_t n);
- void* coreZeroAllocate(size_t n);
- void* coreReallocate(void* p, size_t n);
- void coreFree(void* p);
- #define printMemoryReport()
- #endif
- bool sleepNanos(i64 nanos);
- i64 getNanos(void);
- #define swap(a, b) \
- do { \
- auto aPointer = (a); \
- auto bPointer = (b); \
- auto tmp = *aPointer; \
- *aPointer = *bPointer; \
- *bPointer = tmp; \
- } while(0)
- #define BUBBLE_SORT(type, Type) void bubbleSort##Type(type* data, size_t n);
- #define BUBBLE_SORT_SOURCE(type, Type, greaterThan) \
- void bubbleSort##Type(type* data, size_t n) { \
- bool swapped = true; \
- while(swapped && n > 0) { \
- swapped = false; \
- n--; \
- for(size_t i = 0; i < n; i++) { \
- if(greaterThan(data[i], data[i + 1])) { \
- swap(data + i, data + i + 1); \
- swapped = true; \
- } \
- } \
- } \
- }
- #endif
|