brzeszczot

brzeszczot.git
git clone git://git.lenczewski.org/brzeszczot.git
Log | Files | Refs

common.h (2591B)


      1 #ifndef COMMON_H
      2 #define COMMON_H
      3 
      4 #ifdef __cplusplus
      5 extern "C" {
      6 #endif /* __cplusplus */
      7 
      8 #ifdef __cplusplus
      9 	#include <cassert>
     10 	#include <cerrno>
     11 	#include <cfloat>
     12 	#include <climits>
     13 	#include <csetjmp>
     14 	#include <cstdalign>
     15 	#include <cstdatomic>
     16 	#include <cstdarg>
     17 	#include <cstdbool>
     18 	#include <cstddef>
     19 	#include <cstdint>
     20 	#include <cstdio>
     21 	#include <cstdlib>
     22 	#include <cstdnoreturn>
     23 	#include <cstring>
     24 #else
     25 	#include <assert.h>
     26 	#include <errno.h>
     27 	#include <float.h>
     28 	#include <limits.h>
     29 	#include <setjmp.h>
     30 	#include <stdalign.h>
     31 	#include <stdatomic.h>
     32 	#include <stdarg.h>
     33 	#include <stdbool.h>
     34 	#include <stddef.h>
     35 	#include <stdint.h>
     36 	#include <stdio.h>
     37 	#include <stdlib.h>
     38 	#include <stdnoreturn.h>
     39 	#include <string.h>
     40 #endif
     41 
     42 typedef int8_t b8;
     43 typedef int32_t b32;
     44 
     45 typedef uint8_t u8;
     46 typedef uint16_t u16;
     47 typedef uint32_t u32;
     48 typedef uint64_t u64;
     49 
     50 typedef int8_t s8;
     51 typedef int16_t s16;
     52 typedef int32_t s32;
     53 typedef int64_t s64;
     54 
     55 typedef float f32;
     56 typedef double f64;
     57 
     58 #define KiB 1024ULL
     59 #define MiB 1024 * KiB
     60 #define GiB 1024 * MiB
     61 
     62 #define ARRLEN(arr) (sizeof(arr) / sizeof((arr)[0]))
     63 #define MIN(a, b) ((a) < (b) ? (a) : (b))
     64 #define MAX(a, b) ((a) > (b) ? (a) : (b))
     65 
     66 #define BITS_SET(value, mask) (((value) & mask) == (mask))
     67 
     68 #define RELPTR_MASK(relptr_ty) ((relptr_ty)1 << ((sizeof(relptr_ty) * 8) - 1))
     69 #define RELPTR_NULL (0)
     70 
     71 #define RELPTR_ENC(relptr_ty, ptroff) \
     72 	((relptr_ty)((ptroff) ^ RELPTR_MASK(relptr_ty)))
     73 
     74 #define RELPTR_DEC(relptr_ty, relptr) \
     75 	((relptr_ty)((relptr) ^ RELPTR_MASK(relptr_ty)))
     76 
     77 #define RELPTR_ABS2REL(relptr_ty, base, absptr) \
     78 	((absptr) \
     79 	? RELPTR_ENC(relptr_ty, (u8*)absptr - (u8*)base) \
     80 	: RELPTR_NULL)
     81 
     82 #define RELPTR_REL2ABS(absptr_ty, relptr_ty, base, relptr) \
     83 	((relptr) \
     84 	? ((absptr_ty)((u8*)base + RELPTR_DEC(relptr_ty, relptr))) \
     85 	: NULL)
     86 
     87 #define errloc() fprintf(stderr, "%s:%d:%s: ", __FILE__, __LINE__, __func__);
     88 
     89 #define errlog(...) \
     90 do { \
     91 	errloc(); \
     92 	fprintf(stderr, __VA_ARGS__); \
     93 	fprintf(stderr, "\n"); \
     94 	fflush(stderr); \
     95 } while (0);
     96 
     97 #define unreachable(...) \
     98 do { \
     99 	errloc(); \
    100 	fprintf(stderr, "UNREACHABLE: "); \
    101 	fprintf(stderr, __VA_ARGS__); \
    102 	fprintf(stderr, "\n"); \
    103 	fflush(stderr); \
    104 	abort(); \
    105 } while (0);
    106 
    107 #define unimplemented(...) \
    108 do { \
    109 	errloc(); \
    110 	fprintf(stderr, "UNIMPLEMENTED: "); \
    111 	fprintf(stderr, __VA_ARGS__); \
    112 	fprintf(stderr, "\n"); \
    113 	fflush(stderr); \
    114 	abort(); \
    115 } while (0);
    116 
    117 #ifndef NDEBUG
    118 	#define dbglog(...) errlog(__VA_ARGS__)
    119 #else
    120 	#define dbglog(...)
    121 #endif
    122 
    123 #ifdef __cplusplus
    124 };
    125 #endif /* __cplusplus */
    126 
    127 #endif /* COMMON_H */