2013-09-29 00:21:18 +04:00
|
|
|
/* include/c/defs.h
|
|
|
|
**
|
|
|
|
** This file is in the public domain.
|
|
|
|
*/
|
2014-11-05 04:09:17 +03:00
|
|
|
/** Loobeans - inverse booleans to match nock.
|
|
|
|
**/
|
|
|
|
# define c3y 0
|
|
|
|
# define c3n 1
|
|
|
|
|
2014-11-05 04:18:47 +03:00
|
|
|
# define _(x) (c3y == (x))
|
|
|
|
# define __(x) ((x) ? c3y : c3n)
|
|
|
|
# define c3a(x, y) __(_(x) && _(y))
|
|
|
|
# define c3o(x, y) __(_(x) || _(y))
|
2014-11-05 04:09:17 +03:00
|
|
|
|
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
/** Random useful C macros.
|
|
|
|
**/
|
|
|
|
/* Assert. Good to capture.
|
|
|
|
*/
|
2018-12-11 01:04:48 +03:00
|
|
|
# define c3_assert(x) \
|
|
|
|
do { \
|
|
|
|
if (!(x)) { \
|
|
|
|
fprintf(stderr, \
|
|
|
|
"\rAssertion '%s' failed " \
|
|
|
|
"in %s:%d\n", \
|
|
|
|
#x, __FILE__, __LINE__); \
|
|
|
|
c3_cooked(); \
|
|
|
|
assert(x); \
|
|
|
|
} \
|
|
|
|
} while(0)
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
/* Stub.
|
|
|
|
*/
|
|
|
|
# define c3_stub (assert(!"stub"), 0)
|
|
|
|
|
|
|
|
/* Size in words.
|
|
|
|
*/
|
|
|
|
# define c3_wiseof(x) (((sizeof (x)) + 3) >> 2)
|
|
|
|
|
2014-08-22 10:14:37 +04:00
|
|
|
/* Bit counting.
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
2014-11-05 04:09:17 +03:00
|
|
|
# define c3_bits_word(w) ((w) ? (32 - __builtin_clz(w)) : 0)
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
/* Min and max.
|
|
|
|
*/
|
|
|
|
# define c3_max(x, y) ( ((x) > (y)) ? (x) : (y) )
|
|
|
|
# define c3_min(x, y) ( ((x) < (y)) ? (x) : (y) )
|
|
|
|
|
2014-08-26 23:11:59 +04:00
|
|
|
/* Rotate.
|
|
|
|
*/
|
|
|
|
# define c3_rotw(r, x) ( ((x) << (r)) | ((x) >> (32 - (r))) )
|
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
/* Emergency stdio fix.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
c3_cooked();
|
|
|
|
|
|
|
|
/* Short integers.
|
|
|
|
*/
|
|
|
|
# define c3_s1(a) ( (a) )
|
|
|
|
# define c3_s2(a, b) ( ((b) << 8) | c3_s1(a) )
|
|
|
|
# define c3_s3(a, b, c) ( ((c) << 16) | c3_s2(a, b) )
|
|
|
|
# define c3_s4(a, b, c, d) ( ((d) << 24) | c3_s3(a, b, c) )
|
|
|
|
|
|
|
|
# define c3_s5(a, b, c, d, e) \
|
|
|
|
( ((uint64_t)c3_s1(e) << 32ULL) | c3_s4(a, b, c, d) )
|
|
|
|
# define c3_s6(a, b, c, d, e, f) \
|
|
|
|
( ((uint64_t)c3_s2(e, f) << 32ULL) | c3_s4(a, b, c, d) )
|
|
|
|
# define c3_s7(a, b, c, d, e, f, g) \
|
|
|
|
( ((uint64_t)c3_s3(e, f, g) << 32ULL) | c3_s4(a, b, c, d) )
|
|
|
|
# define c3_s8(a, b, c, d, e, f, g, h) \
|
|
|
|
( ((uint64_t)c3_s4(e, f, g, h) << 32ULL) | c3_s4(a, b, c, d) )
|
2014-03-07 23:39:30 +04:00
|
|
|
|
2014-04-19 01:58:51 +04:00
|
|
|
/* Byte-order twiddling.
|
|
|
|
*/
|
|
|
|
# define c3_flip32(w) \
|
|
|
|
( (((w) >> 24) & 0xff) \
|
|
|
|
| (((w) >> 16) & 0xff) << 8 \
|
|
|
|
| (((w) >> 8) & 0xff) << 16 \
|
|
|
|
| ( (w) & 0xff) << 24 )
|
|
|
|
|
2014-03-07 23:39:30 +04:00
|
|
|
/* Logging shorthand.
|
|
|
|
*/
|
|
|
|
# define c3_log_every(n, args...) \
|
|
|
|
do { \
|
|
|
|
static c3_w cnt_w = 0; \
|
|
|
|
\
|
2014-03-08 06:50:01 +04:00
|
|
|
if ( 0 == cnt_w % (n) ) { \
|
2014-03-07 23:39:30 +04:00
|
|
|
uL(fprintf(uH, args)); \
|
|
|
|
} \
|
|
|
|
cnt_w = (cnt_w + 1) % (n); \
|
|
|
|
} while (0)
|
2014-11-05 04:09:17 +03:00
|
|
|
|
2018-12-28 04:22:55 +03:00
|
|
|
/* Asserting allocators.
|
|
|
|
*/
|
|
|
|
# define c3_malloc(s) ({ \
|
|
|
|
void* rut = malloc(s); \
|
|
|
|
if ( 0 == rut ) { \
|
|
|
|
c3_assert(!"memory lost"); \
|
|
|
|
} \
|
|
|
|
rut;})
|
|
|
|
# define c3_calloc(s) ({ \
|
|
|
|
void* rut = calloc(1,s); \
|
|
|
|
if ( 0 == rut ) { \
|
|
|
|
c3_assert(!"memory lost"); \
|
|
|
|
} \
|
|
|
|
rut;})
|
|
|
|
# define c3_realloc(a, b) ({ \
|
|
|
|
void* rut = realloc(a, b); \
|
|
|
|
if ( 0 == rut ) { \
|
|
|
|
c3_assert(!"memory lost"); \
|
|
|
|
} \
|
|
|
|
rut;})
|