123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #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))
- #define MIN_MAX(type, name) \
- inline type max##name(type a, type b) { \
- return a > b ? a : b; \
- } \
- inline type min##name(type a, type b) { \
- return a < b ? a : b; \
- } \
- inline type clamp##name(type t, type from, type to) { \
- return max##name(min##name(t, to), from); \
- }
- MIN_MAX(size_t, Size)
- MIN_MAX(u32, U32)
- MIN_MAX(i32, I32)
- MIN_MAX(float, Float)
- 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 sleepMillis(i64 millis);
- 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
|