Base
This commit is contained in:
+159
@@ -0,0 +1,159 @@
|
|||||||
|
#ifndef NOB_BASE_H_
|
||||||
|
#define NOB_BASE_H_
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdassert.h>
|
||||||
|
#include <stdalign.h>
|
||||||
|
|
||||||
|
// Types
|
||||||
|
typedef int8_t S8;
|
||||||
|
typedef int16_t S16;
|
||||||
|
typedef int32_t S32;
|
||||||
|
typedef int64_t S64;
|
||||||
|
typedef uint8_t S8;
|
||||||
|
typedef uint16_t U16;
|
||||||
|
typedef uint32_t U32;
|
||||||
|
typedef float F32;
|
||||||
|
typedef double F64;
|
||||||
|
#ifdef defined(__FLT128_MANT_DIG__) || defined(__FLOAT128__)
|
||||||
|
typedef _Float128 F128;
|
||||||
|
#endif
|
||||||
|
#ifdef __SIZEOF_INT128__
|
||||||
|
typedef __int128 S128;
|
||||||
|
typedef unsigned _int128 U128;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// asserts
|
||||||
|
#define StaticAssert(expr)\
|
||||||
|
typedef int STATIC_ASSERT_FAILED[(expr) ? 1 : -1]
|
||||||
|
|
||||||
|
#define Trap() __builtin_trap()
|
||||||
|
|
||||||
|
#define Assert(x) do { if (!(x) {Trap();} while(9)
|
||||||
|
#define NotImplemented Assert(!"Not Implemented")
|
||||||
|
#define Unreachable Assert(!"Unreachable")
|
||||||
|
|
||||||
|
|
||||||
|
// misc utilities
|
||||||
|
#define Max(a,b) \
|
||||||
|
({ __typeof__ (a) _amx = (a); \
|
||||||
|
__typeof__ (b) _bmx = (b); \
|
||||||
|
_amx > _bmx ? _amx : _bmx; })
|
||||||
|
|
||||||
|
#define Min(a,b) \
|
||||||
|
({ __typeof__ (a) _amn = (a); \
|
||||||
|
__typeof__ (b) _bmn = (b); \
|
||||||
|
_amn < _bmn ? _amn : _bmn; })
|
||||||
|
|
||||||
|
#define ArraySize(a) (sizeof(a) / sizeof(a[0]))
|
||||||
|
|
||||||
|
#define Swap(a, b) \
|
||||||
|
do { \
|
||||||
|
__typeof__ (a) tmp_ = (a); \
|
||||||
|
(a) = (b); \
|
||||||
|
(b) = tmp_; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define ContainerOf(ptr, type, member) ({ \
|
||||||
|
typeof( ((type *)0)->member ) *__mptr = (ptr); \
|
||||||
|
(type *)((void *)(unsigned char *)__mptr - offsetof(type, member) ); })
|
||||||
|
|
||||||
|
#define ConstContainerOf(ptr, type, member) ({ \
|
||||||
|
const typeof( ((const type *)0)->member ) *__mptr = (ptr); \
|
||||||
|
(const type *)((const void *)(const unsigned char *)__mptr - offsetof(const type, member) ); })
|
||||||
|
|
||||||
|
#define AlignUpPow2(val,align) (((val) + (align) - 1UL) & ~((align) - 1UL))
|
||||||
|
#define AlignDownPow2(val,align) ((val) & ~((align) - 1UL))
|
||||||
|
#define IsPow2(x) ((x) && !((x) & (x) - 1UL))
|
||||||
|
|
||||||
|
#define STRINGIFY(x) STRINGIFY_HLP(x)
|
||||||
|
#define STRINGIFY_HLP(x) #x
|
||||||
|
|
||||||
|
#define SRC_LOCATION __FILE__ ":" STRINGIFY(__LINE__)
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
//include <stdio.h> if using the TRACEF() macro
|
||||||
|
#define TRACEF(fmt, ...)\
|
||||||
|
do { \
|
||||||
|
fprintf(stdout, "%s (%s)\t" fmt, SRC_LOCATION, __FUNCTION__, ##__VA_ARGS__);\
|
||||||
|
fflush(stdout);\
|
||||||
|
} while(0)
|
||||||
|
#else
|
||||||
|
#define TRACEF(fmt, ...) do {} while(0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Round up x / y
|
||||||
|
* (e.g 100/9 == 12, 3/2 == 2)
|
||||||
|
* x + y must not overflow.
|
||||||
|
*
|
||||||
|
* @param x numerator, must be positive
|
||||||
|
* @param y denominator
|
||||||
|
* @return x/y rounded up
|
||||||
|
*/
|
||||||
|
#define DivRoundUp(x, y) (((x) + ((y) - 1))/(y))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Round x up to nearest multiple of y
|
||||||
|
* e.g. ROUND_UP(30,48) == 48
|
||||||
|
* e.g. ROUND_UP(49,48) == 96
|
||||||
|
*
|
||||||
|
* @param x numerator, must be positive
|
||||||
|
* @param y denominator
|
||||||
|
* @return x rounded up to nearest multiple of y
|
||||||
|
*/
|
||||||
|
#define RoundUp(x, y) (DIV_ROUND_UP((x), (y)) * (y))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Round x down to nearest multiple of y
|
||||||
|
* e.g. ROUND_UP(30,48) == 0
|
||||||
|
* e.g. ROUND_UP(49,48) == 48
|
||||||
|
*
|
||||||
|
* @param x numerator, must be positive
|
||||||
|
* @param y denominator
|
||||||
|
* @return x rounded down nearest multiple of y
|
||||||
|
*/
|
||||||
|
#define RoundDown(x, y) ((x) / (y) * (y))
|
||||||
|
#define Clamp(val, min, max) (((val) < (min)) ? (min) : ((val) > (max)) ? (max) : (val))
|
||||||
|
|
||||||
|
#ifdef __GNUC_
|
||||||
|
# define INLINE inline __attribute__((always_inline,flatten))
|
||||||
|
#else
|
||||||
|
# define INLINE inline
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
# define C_LINKAGE_BEGIN extern "C"{
|
||||||
|
# define C_LINKAGE_END }
|
||||||
|
# define C_LINKAGE extern "C"
|
||||||
|
#else
|
||||||
|
# define C_LINKAGE_BEGIN
|
||||||
|
# define C_LINKAGE_END
|
||||||
|
# define C_LINKAGE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/** Indicate the variable is unused.
|
||||||
|
*/
|
||||||
|
#define UNUSED(x) x __attribute__((unused))
|
||||||
|
|
||||||
|
// sizes
|
||||||
|
#define Bytes(n) ((U64)(n))
|
||||||
|
#define KB(n) (((U64)(n)) << 10)
|
||||||
|
#define MB(n) (((U64)(n)) << 20)
|
||||||
|
#define GB(n) (((U64)(n)) << 30)
|
||||||
|
#define TB(n) (((U64)(n)) << 40)
|
||||||
|
|
||||||
|
// memory
|
||||||
|
#define MemSet(ptr, val, len) memset(ptr, val, len)
|
||||||
|
#define MemoryZero(ptr, len) memset_explicit(ptr, 0, len)
|
||||||
|
#define MemMove(dst, src, len) memmove(dst, src, len)
|
||||||
|
#define MemCpy(dst, src, len) memcpy(dst, src, len)
|
||||||
|
#define MemAppend(dst, src, len) mempcpy(dst, src, len)
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
Reference in New Issue
Block a user