1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #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* 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 coreReallocate(p, n) coreDebugReallocate(__FILE__, __LINE__, p, n)
- #define coreFree(p) coreFreeDebug(__FILE__, __LINE__, p)
- #else
- void* coreAllocate(size_t n);
- void* coreReallocate(void* p, size_t n);
- void coreFree(void* p);
- #define printMemoryReport()
- #endif
- bool sleepNanos(i64 nanos);
- i64 getNanos(void);
- // TODO: replace typeof with auto when available
- #define swap(a, b) \
- do { \
- typeof(a) aPointer = (a); \
- typeof(b) bPointer = (b); \
- typeof(*aPointer) 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
|