Startle
Useful and efficient algorithms and facilities
|
Generally useful macros. More...
#include "startle/dispatch.h"
Go to the source code of this file.
Macros | |
#define | _CONCAT(x, y) x##y |
concatenate tokens. More... | |
#define | CONCAT(x, y) _CONCAT(x, y) |
#define | _CONCAT_UNDERSCORE(x, y) x##_##y |
#define | CONCAT_UNDERSCORE(x, y) _CONCAT_UNDERSCORE(x, y) |
#define | UNIQUE CONCAT(__unique_, __LINE__) |
Make a unique identifier. More... | |
#define | _STRINGIFY(x) #x |
Convert a token to a string. More... | |
#define | STRINGIFY(x) _STRINGIFY(x) |
#define | sizeof_field(s, f) sizeof(((s *)0)->f) |
Size of a field. More... | |
#define | offsetof(s, f) ((uintptr_t)&(((s *)0)->f)) |
Offset of a field. More... | |
#define | WIDTH(a) (sizeof((a)[0])) |
Number of bytes per array element. More... | |
#define | LENGTH(a) (sizeof(a) / WIDTH(a)) |
Number of array elements. More... | |
#define | RANGEUP(i, lower, upper) for(size_t i = (lower), __upper = (upper); i < __upper; i++) |
Iterate i from lower up to upper-1 . More... | |
#define | RANGEDOWN(i, lower, upper) for(size_t i = (upper), __lower = (lower); i-- > __lower; ) |
Iterate i from upper-1 down to lower . More... | |
#define | COUNTDOWN(i, n) RANGEDOWN(i, 0, n) |
Iterate i from n-1 to 0 . More... | |
#define | COUNTUP(i, n) RANGEUP(i, 0, n) |
Iterate i from 0 to n-1 . More... | |
#define | LOOP(n) COUNTDOWN(UNIQUE, n) |
Iterate n times. More... | |
#define | FOREACH(i, a) COUNTUP(i, LENGTH(a)) |
Iterate i over each index of the array a . More... | |
#define | FORMAP(i, m) for(size_t i = 1; i <= *map_cnt(m); i++) |
Iterate i over each index of map m . More... | |
#define | BITSET(name, size) uint8_t name[((size)+7)/8] = {0} |
Declare a bit set named name to store size bits. More... | |
#define | BITSET_INDEX(name, array) BITSET(name, LENGTH(array)) |
Declare a bit set to hold a bit for each element of the array. More... | |
#define | static_assert(expr, msg) _Static_assert(expr, msg) |
#define | SEG(x) {(x), sizeof(x) - 1} |
String segment initializer. More... | |
#define | printseg(pre, seg, fmt, ...) |
printf that prepends a string segment More... | |
#define | LIST_ADD(f, l, v) |
Add to tail of an intrusive list. More... | |
#define | CONS(f, l, v) |
Add to head of an intrusive list. More... | |
#define | min(a, b) |
#define | max(a, b) |
#define | csub(a, b) |
Non-negative saturating subtraction. More... | |
#define | zero(a) memset(&(a), 0, sizeof(a)) |
Zero an array or struct. More... | |
#define | show(x) printf(#x " = %d\n", (int)(x)) |
Trace an integer variable. More... | |
#define | FLAG(s, flag) (((s).flags & (flag)) != 0) |
Return true if the flag is set. More... | |
#define | NOT_FLAG(s, flag) (((s).flags & (flag)) == 0) |
Return true if the flag is NOT set. More... | |
#define | FLAG_SET(s, flag) ((s).flags |= (flag)) |
#define | FLAG_CLEAR(s, flag) ((s).flags &= ~(flag)) |
#define | FLAG_SET_TO(s, flag, val) ((val) ? FLAG_SET(s, flag) : FLAG_CLEAR(s, flag)) |
#define | ARRAY_SHIFTR(array, offset, length) memmove(&(array) + (offset), &(array), (length) * sizeof(array)) |
Shift elements in the array to the right. More... | |
#define | ARRAY_SHIFTL(array, offset, length) memmove(&(array), &(array) + (offset), (length) * sizeof(array)) |
Shift elements in the array to the left. More... | |
#define | ARRAY_COPY(dst, src, length) memcpy(&(dst), &(src), (length) * sizeof(dst)) |
#define | COLOR(c, str) "\x1b[" CONCAT(COLOR_, c) "m" str "\x1b[0m" |
#define | COLORs(str) "\x1b[%sm" str "\x1b[0m" |
#define | COLOR_red "37;41" |
#define | COLOR_blue "37;44" |
#define | COLOR_gray "38;5;8" |
#define | COLOR_normal "0" |
#define | MARK(x) COLOR(red, x) |
#define | NOTE(x) COLOR(blue, x) |
#define | FADE(x) COLOR(gray, x) |
#define | TODO MARK("TODO") |
#define | HACK MARK("HACK") |
#define | DISABLE(...) |
#define | unlikely(c) (__builtin_expect((c), 0)) |
Hint that the condition is unlikely. More... | |
#define | likely(c) (__builtin_expect((c), 1)) |
Hint that the condition is likely. More... | |
#define | TEST(name) int test_##name() |
Define a test. More... | |
#define | FORMAT(name, c) void format_##name(intptr_t i) |
Define a new format string specifier (for logging). More... | |
Generally useful macros.
#define _CONCAT | ( | x, | |
y | |||
) | x##y |
concatenate tokens.
#define _STRINGIFY | ( | x | ) | #x |
Convert a token to a string.
#define ARRAY_SHIFTL | ( | array, | |
offset, | |||
length | |||
) | memmove(&(array), &(array) + (offset), (length) * sizeof(array)) |
Shift elements in the array to the left.
#define ARRAY_SHIFTR | ( | array, | |
offset, | |||
length | |||
) | memmove(&(array) + (offset), &(array), (length) * sizeof(array)) |
Shift elements in the array to the right.
#define BITSET | ( | name, | |
size | |||
) | uint8_t name[((size)+7)/8] = {0} |
Declare a bit set named name
to store size
bits.
Declare a bit set to hold a bit for each element of the array.
#define CONS | ( | f, | |
l, | |||
v | |||
) |
Add to head of an intrusive list.
f | field to store link. |
l | pointer to list head pointer to update. |
v | struct to add. |
#define COUNTDOWN | ( | i, | |
n | |||
) | RANGEDOWN(i, 0, n) |
Iterate i
from n-1
to 0
.
#define COUNTUP | ( | i, | |
n | |||
) | RANGEUP(i, 0, n) |
Iterate i
from 0
to n-1
.
#define csub | ( | a, | |
b | |||
) |
Non-negative saturating subtraction.
#define DISABLE | ( | ... | ) |
#define FLAG | ( | s, | |
flag | |||
) | (((s).flags & (flag)) != 0) |
Return true
if the flag is set.
#define FORMAP | ( | i, | |
m | |||
) | for(size_t i = 1; i <= *map_cnt(m); i++) |
Iterate i
over each index of map m
.
#define FORMAT | ( | name, | |
c | |||
) | void format_##name(intptr_t i) |
Define a new format string specifier (for logging).
#define LENGTH | ( | a | ) | (sizeof(a) / WIDTH(a)) |
Number of array elements.
#define likely | ( | c | ) | (__builtin_expect((c), 1)) |
Hint that the condition is likely.
if likely(...) { ...
#define LIST_ADD | ( | f, | |
l, | |||
v | |||
) |
Add to tail of an intrusive list.
f | field to store link. |
l | pointer to list tail pointer to update. |
v | struct to add. |
#define max | ( | a, | |
b | |||
) |
#define min | ( | a, | |
b | |||
) |
#define NOT_FLAG | ( | s, | |
flag | |||
) | (((s).flags & (flag)) == 0) |
Return true
if the flag is NOT set.
#define offsetof | ( | s, | |
f | |||
) | ((uintptr_t)&(((s *)0)->f)) |
Offset of a field.
#define printseg | ( | pre, | |
seg, | |||
fmt, | |||
... | |||
) |
printf that prepends a string segment
pre | print this string first |
seg | string segment to print next |
fmt | format string for remaining arguments |
#define RANGEDOWN | ( | i, | |
lower, | |||
upper | |||
) | for(size_t i = (upper), __lower = (lower); i-- > __lower; ) |
Iterate i
from upper-1
down to lower
.
#define RANGEUP | ( | i, | |
lower, | |||
upper | |||
) | for(size_t i = (lower), __upper = (upper); i < __upper; i++) |
Iterate i
from lower
up to upper-1
.
#define SEG | ( | x | ) | {(x), sizeof(x) - 1} |
String segment initializer.
Example: seg_t s = SEG("Hello");
#define show | ( | x | ) | printf(#x " = %d\n", (int)(x)) |
Trace an integer variable.
show(name)
will print name = <value>
#define sizeof_field | ( | s, | |
f | |||
) | sizeof(((s *)0)->f) |
Size of a field.
#define TEST | ( | name | ) | int test_##name() |
Define a test.
#define UNIQUE CONCAT(__unique_, __LINE__) |
Make a unique identifier.
#define unlikely | ( | c | ) | (__builtin_expect((c), 0)) |
Hint that the condition is unlikely.
if unlikely(...) { ...
#define WIDTH | ( | a | ) | (sizeof((a)[0])) |
Number of bytes per array element.
#define zero | ( | a | ) | memset(&(a), 0, sizeof(a)) |
Zero an array or struct.