-
Notifications
You must be signed in to change notification settings - Fork 37
/
macros.h
81 lines (64 loc) · 2.91 KB
/
macros.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma once
#if !defined(__has_include)
#define __has_include(...) 1
#endif
#define DBGPRINTF_NOPFX(V...) if (DEBUG_MESSAGES>0) printf(V)
#define DBGPRINTF(V...) if (DEBUG_MESSAGES>0) printf(PFX V)
#define DBGPRINTF_E(V...) if (DEBUG_MESSAGES>0) printf(EPFX V)
// Macros to make a string from a macro
#define STRINGIFY_(M) #M
#define STRINGIFY(M) STRINGIFY_(M)
// Macros for bit masks
#undef _BV
#define _BV(n) (1<<(n))
#define TEST(n,b) (!!((n)&_BV(b)))
#define SET_BIT_TO(N,B,TF) do{ if (TF) SBI(N,B); else CBI(N,B); }while(0)
//
// Primitives supporting precompiler REPEAT
//
#define FIRST(a,...) a
#define SECOND(a,b,...) b
#define THIRD(a,b,c,...) c
#define IS_PROBE(V...) SECOND(V, 0)
#define _NUM_ARGS(_,Z,Y,X,W,V,U,T,S,R,Q,P,O,N,M,L,K,J,I,H,G,F,E,D,C,B,A,OUT,...) OUT
#define NUM_ARGS(V...) _NUM_ARGS(0,V,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
// Macros to chain up to 12 conditions
#define _DO_1(W,C,A) (_##W##_1(A))
#define _DO_2(W,C,A,B) (_##W##_1(A) C _##W##_1(B))
#define _DO_3(W,C,A,V...) (_##W##_1(A) C _DO_2(W,C,V))
#define _DO_4(W,C,A,V...) (_##W##_1(A) C _DO_3(W,C,V))
#define _DO_5(W,C,A,V...) (_##W##_1(A) C _DO_4(W,C,V))
#define _DO_6(W,C,A,V...) (_##W##_1(A) C _DO_5(W,C,V))
#define _DO_7(W,C,A,V...) (_##W##_1(A) C _DO_6(W,C,V))
#define _DO_8(W,C,A,V...) (_##W##_1(A) C _DO_7(W,C,V))
#define _DO_9(W,C,A,V...) (_##W##_1(A) C _DO_8(W,C,V))
#define _DO_10(W,C,A,V...) (_##W##_1(A) C _DO_9(W,C,V))
#define _DO_11(W,C,A,V...) (_##W##_1(A) C _DO_10(W,C,V))
#define _DO_12(W,C,A,V...) (_##W##_1(A) C _DO_11(W,C,V))
#define __DO_N(W,C,N,V...) _DO_##N(W,C,V)
#define _DO_N(W,C,N,V...) __DO_N(W,C,N,V)
#define DO(W,C,V...) _DO_N(W,C,NUM_ARGS(V),V)
// Macros to support option testing
#define _CAT(a,V...) a##V
#define CAT(a,V...) _CAT(a,V)
#define _ISENA_ ~,1
#define _ISENA_1 ~,1
#define _ISENA_0x1 ~,1
#define _ISENA_true ~,1
#define _ISENA(V...) IS_PROBE(V)
#define _ENA_1(O) _ISENA(CAT(_IS,CAT(ENA_, O)))
#define _DIS_1(O) NOT(_ENA_1(O))
#define ENABLED(V...) DO(ENA,&&,V)
#define DISABLED(V...) DO(DIS,&&,V)
#define TERN(O,A,B) _TERN(_ENA_1(O),B,A) // OPTION converted to '0' or '1'
#define TERN0(O,A) _TERN(_ENA_1(O),0,A) // OPTION converted to A or '0'
#define TERN1(O,A) _TERN(_ENA_1(O),1,A) // OPTION converted to A or '1'
#define TERN_(O,A) _TERN(_ENA_1(O),,A) // OPTION converted to A or '<nul>'
#define _TERN(E,V...) __TERN(_CAT(T_,E),V) // Prepend 'T_' to get 'T_0' or 'T_1'
#define __TERN(T,V...) ___TERN(_CAT(_NO,T),V) // Prepend '_NO' to get '_NOT_0' or '_NOT_1'
#define ___TERN(P,V...) THIRD(P,V) // If first argument has a comma, A. Else B.
#define ANY(V...) !DISABLED(V)
#define NONE(V...) DISABLED(V)
#define ALL(V...) ENABLED(V)
#define BOTH(V1,V2) ALL(V1,V2)
#define EITHER(V1,V2) ANY(V1,V2)