My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

macros.h 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #if !defined(__has_include)
  24. #define __has_include(...) 1
  25. #endif
  26. #define ABCE 4
  27. #define XYZE 4
  28. #define ABC 3
  29. #define XYZ 3
  30. #define XY 2
  31. #define _AXIS(A) (A##_AXIS)
  32. #define _XMIN_ 100
  33. #define _YMIN_ 200
  34. #define _ZMIN_ 300
  35. #define _XMAX_ 101
  36. #define _YMAX_ 201
  37. #define _ZMAX_ 301
  38. #define _XDIAG_ 102
  39. #define _YDIAG_ 202
  40. #define _ZDIAG_ 302
  41. #define _E0DIAG_ 400
  42. #define _E1DIAG_ 401
  43. #define _E2DIAG_ 402
  44. #define _E3DIAG_ 403
  45. #define _E4DIAG_ 404
  46. #define _E5DIAG_ 405
  47. #define _E6DIAG_ 406
  48. #define _E7DIAG_ 407
  49. #define _FORCE_INLINE_ __attribute__((__always_inline__)) __inline__
  50. #define FORCE_INLINE __attribute__((always_inline)) inline
  51. #define NO_INLINE __attribute__((noinline))
  52. #define _UNUSED __attribute__((unused))
  53. #define _O0 __attribute__((optimize("O0")))
  54. #define _Os __attribute__((optimize("Os")))
  55. #define _O1 __attribute__((optimize("O1")))
  56. #define _O2 __attribute__((optimize("O2")))
  57. #define _O3 __attribute__((optimize("O3")))
  58. #define IS_CONSTEXPR(...) __builtin_constant_p(__VA_ARGS__) // Only valid solution with C++14. Should use std::is_constant_evaluated() in C++20 instead
  59. #ifndef UNUSED
  60. #define UNUSED(x) ((void)(x))
  61. #endif
  62. // Clock speed factors
  63. #if !defined(CYCLES_PER_MICROSECOND) && !defined(__STM32F1__)
  64. #define CYCLES_PER_MICROSECOND (F_CPU / 1000000UL) // 16 or 20 on AVR
  65. #endif
  66. // Nanoseconds per cycle
  67. #define NANOSECONDS_PER_CYCLE (1000000000.0 / F_CPU)
  68. // Macros to make a string from a macro
  69. #define STRINGIFY_(M) #M
  70. #define STRINGIFY(M) STRINGIFY_(M)
  71. #define A(CODE) " " CODE "\n\t"
  72. #define L(CODE) CODE ":\n\t"
  73. // Macros for bit masks
  74. #undef _BV
  75. #define _BV(n) (1<<(n))
  76. #define TEST(n,b) (!!((n)&_BV(b)))
  77. #define SET_BIT_TO(N,B,TF) do{ if (TF) SBI(N,B); else CBI(N,B); }while(0)
  78. #ifndef SBI
  79. #define SBI(A,B) (A |= _BV(B))
  80. #endif
  81. #ifndef CBI
  82. #define CBI(A,B) (A &= ~_BV(B))
  83. #endif
  84. #define TBI(N,B) (N ^= _BV(B))
  85. #define _BV32(b) (1UL << (b))
  86. #define TEST32(n,b) !!((n)&_BV32(b))
  87. #define SBI32(n,b) (n |= _BV32(b))
  88. #define CBI32(n,b) (n &= ~_BV32(b))
  89. #define TBI32(N,B) (N ^= _BV32(B))
  90. #define cu(x) ({__typeof__(x) _x = (x); (_x)*(_x)*(_x);})
  91. #define RADIANS(d) ((d)*float(M_PI)/180.0f)
  92. #define DEGREES(r) ((r)*180.0f/float(M_PI))
  93. #define HYPOT2(x,y) (sq(x)+sq(y))
  94. #define CIRCLE_AREA(R) (float(M_PI) * sq(float(R)))
  95. #define CIRCLE_CIRC(R) (2 * float(M_PI) * float(R))
  96. #define SIGN(a) ({__typeof__(a) _a = (a); (_a>0)-(_a<0);})
  97. #define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))
  98. // Macros to constrain values
  99. #ifdef __cplusplus
  100. // C++11 solution that is standards compliant.
  101. template <class V, class N> static inline constexpr void NOLESS(V& v, const N n) {
  102. if (n > v) v = n;
  103. }
  104. template <class V, class N> static inline constexpr void NOMORE(V& v, const N n) {
  105. if (n < v) v = n;
  106. }
  107. template <class V, class N1, class N2> static inline constexpr void LIMIT(V& v, const N1 n1, const N2 n2) {
  108. if (n1 > v) v = n1;
  109. else if (n2 < v) v = n2;
  110. }
  111. #else
  112. #define NOLESS(v, n) \
  113. do{ \
  114. __typeof__(v) _n = (n); \
  115. if (_n > v) v = _n; \
  116. }while(0)
  117. #define NOMORE(v, n) \
  118. do{ \
  119. __typeof__(v) _n = (n); \
  120. if (_n < v) v = _n; \
  121. }while(0)
  122. #define LIMIT(v, n1, n2) \
  123. do{ \
  124. __typeof__(v) _n1 = (n1); \
  125. __typeof__(v) _n2 = (n2); \
  126. if (_n1 > v) v = _n1; \
  127. else if (_n2 < v) v = _n2; \
  128. }while(0)
  129. #endif
  130. // Macros to chain up to 14 conditions
  131. #define _DO_1(W,C,A) (_##W##_1(A))
  132. #define _DO_2(W,C,A,B) (_##W##_1(A) C _##W##_1(B))
  133. #define _DO_3(W,C,A,V...) (_##W##_1(A) C _DO_2(W,C,V))
  134. #define _DO_4(W,C,A,V...) (_##W##_1(A) C _DO_3(W,C,V))
  135. #define _DO_5(W,C,A,V...) (_##W##_1(A) C _DO_4(W,C,V))
  136. #define _DO_6(W,C,A,V...) (_##W##_1(A) C _DO_5(W,C,V))
  137. #define _DO_7(W,C,A,V...) (_##W##_1(A) C _DO_6(W,C,V))
  138. #define _DO_8(W,C,A,V...) (_##W##_1(A) C _DO_7(W,C,V))
  139. #define _DO_9(W,C,A,V...) (_##W##_1(A) C _DO_8(W,C,V))
  140. #define _DO_10(W,C,A,V...) (_##W##_1(A) C _DO_9(W,C,V))
  141. #define _DO_11(W,C,A,V...) (_##W##_1(A) C _DO_10(W,C,V))
  142. #define _DO_12(W,C,A,V...) (_##W##_1(A) C _DO_11(W,C,V))
  143. #define _DO_13(W,C,A,V...) (_##W##_1(A) C _DO_12(W,C,V))
  144. #define _DO_14(W,C,A,V...) (_##W##_1(A) C _DO_13(W,C,V))
  145. #define _DO_15(W,C,A,V...) (_##W##_1(A) C _DO_14(W,C,V))
  146. #define __DO_N(W,C,N,V...) _DO_##N(W,C,V)
  147. #define _DO_N(W,C,N,V...) __DO_N(W,C,N,V)
  148. #define DO(W,C,V...) (_DO_N(W,C,NUM_ARGS(V),V))
  149. // Macros to support option testing
  150. #define _CAT(a,V...) a##V
  151. #define CAT(a,V...) _CAT(a,V)
  152. #define _ISENA_ ~,1
  153. #define _ISENA_1 ~,1
  154. #define _ISENA_0x1 ~,1
  155. #define _ISENA_true ~,1
  156. #define _ISENA(V...) IS_PROBE(V)
  157. #define _ENA_1(O) _ISENA(CAT(_IS,CAT(ENA_, O)))
  158. #define _DIS_1(O) NOT(_ENA_1(O))
  159. #define ENABLED(V...) DO(ENA,&&,V)
  160. #define DISABLED(V...) DO(DIS,&&,V)
  161. #define COUNT_ENABLED(V...) DO(ENA,+,V)
  162. #define TERN(O,A,B) _TERN(_ENA_1(O),B,A) // OPTION converted to '0' or '1'
  163. #define TERN0(O,A) _TERN(_ENA_1(O),0,A) // OPTION converted to A or '0'
  164. #define TERN1(O,A) _TERN(_ENA_1(O),1,A) // OPTION converted to A or '1'
  165. #define TERN_(O,A) _TERN(_ENA_1(O),,A) // OPTION converted to A or '<nul>'
  166. #define _TERN(E,V...) __TERN(_CAT(T_,E),V) // Prepend 'T_' to get 'T_0' or 'T_1'
  167. #define __TERN(T,V...) ___TERN(_CAT(_NO,T),V) // Prepend '_NO' to get '_NOT_0' or '_NOT_1'
  168. #define ___TERN(P,V...) THIRD(P,V) // If first argument has a comma, A. Else B.
  169. #define IF_ENABLED TERN_
  170. #define IF_DISABLED(O,A) TERN(O,,A)
  171. #define ANY(V...) !DISABLED(V)
  172. #define NONE(V...) DISABLED(V)
  173. #define ALL(V...) ENABLED(V)
  174. #define BOTH(V1,V2) ALL(V1,V2)
  175. #define EITHER(V1,V2) ANY(V1,V2)
  176. #define MANY(V...) (COUNT_ENABLED(V) > 1)
  177. // Macros to support pins/buttons exist testing
  178. #define PIN_EXISTS(PN) (defined(PN##_PIN) && PN##_PIN >= 0)
  179. #define _PINEX_1 PIN_EXISTS
  180. #define PINS_EXIST(V...) DO(PINEX,&&,V)
  181. #define ANY_PIN(V...) DO(PINEX,||,V)
  182. #define BUTTON_EXISTS(BN) (defined(BTN_##BN) && BTN_##BN >= 0)
  183. #define _BTNEX_1 BUTTON_EXISTS
  184. #define BUTTONS_EXIST(V...) DO(BTNEX,&&,V)
  185. #define ANY_BUTTON(V...) DO(BTNEX,||,V)
  186. #define WITHIN(N,L,H) ((N) >= (L) && (N) <= (H))
  187. #define ISEOL(C) ((C) == '\n' || (C) == '\r')
  188. #define NUMERIC(a) WITHIN(a, '0', '9')
  189. #define DECIMAL(a) (NUMERIC(a) || a == '.')
  190. #define HEXCHR(a) (NUMERIC(a) ? (a) - '0' : WITHIN(a, 'a', 'f') ? ((a) - 'a' + 10) : WITHIN(a, 'A', 'F') ? ((a) - 'A' + 10) : -1)
  191. #define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-' || (a) == '+')
  192. #define DECIMAL_SIGNED(a) (DECIMAL(a) || (a) == '-' || (a) == '+')
  193. #define COUNT(a) (sizeof(a)/sizeof(*a))
  194. #define ZERO(a) memset(a,0,sizeof(a))
  195. #define COPY(a,b) do{ \
  196. static_assert(sizeof(a[0]) == sizeof(b[0]), "COPY: '" STRINGIFY(a) "' and '" STRINGIFY(b) "' types (sizes) don't match!"); \
  197. memcpy(&a[0],&b[0],_MIN(sizeof(a),sizeof(b))); \
  198. }while(0)
  199. // Macros for initializing arrays
  200. #define LIST_16(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,...) A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P
  201. #define LIST_15(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,...) A,B,C,D,E,F,G,H,I,J,K,L,M,N,O
  202. #define LIST_14(A,B,C,D,E,F,G,H,I,J,K,L,M,N,...) A,B,C,D,E,F,G,H,I,J,K,L,M,N
  203. #define LIST_13(A,B,C,D,E,F,G,H,I,J,K,L,M,...) A,B,C,D,E,F,G,H,I,J,K,L,M
  204. #define LIST_12(A,B,C,D,E,F,G,H,I,J,K,L,...) A,B,C,D,E,F,G,H,I,J,K,L
  205. #define LIST_11(A,B,C,D,E,F,G,H,I,J,K,...) A,B,C,D,E,F,G,H,I,J,K
  206. #define LIST_10(A,B,C,D,E,F,G,H,I,J,...) A,B,C,D,E,F,G,H,I,J
  207. #define LIST_9( A,B,C,D,E,F,G,H,I,...) A,B,C,D,E,F,G,H,I
  208. #define LIST_8( A,B,C,D,E,F,G,H,...) A,B,C,D,E,F,G,H
  209. #define LIST_7( A,B,C,D,E,F,G,...) A,B,C,D,E,F,G
  210. #define LIST_6( A,B,C,D,E,F,...) A,B,C,D,E,F
  211. #define LIST_5( A,B,C,D,E,...) A,B,C,D,E
  212. #define LIST_4( A,B,C,D,...) A,B,C,D
  213. #define LIST_3( A,B,C,...) A,B,C
  214. #define LIST_2( A,B,...) A,B
  215. #define LIST_1( A,...) A
  216. #define _LIST_N(N,V...) LIST_##N(V)
  217. #define LIST_N(N,V...) _LIST_N(N,V)
  218. #define ARRAY_N(N,V...) { _LIST_N(N,V) }
  219. #define _JOIN_1(O) (O)
  220. #define JOIN_N(N,C,V...) (DO(JOIN,C,LIST_N(N,V)))
  221. #define LOOP_S_LE_N(VAR, S, N) for (uint8_t VAR=(S); VAR<=(N); VAR++)
  222. #define LOOP_S_L_N(VAR, S, N) for (uint8_t VAR=(S); VAR<(N); VAR++)
  223. #define LOOP_LE_N(VAR, N) LOOP_S_LE_N(VAR, 0, N)
  224. #define LOOP_L_N(VAR, N) LOOP_S_L_N(VAR, 0, N)
  225. #define NOOP (void(0))
  226. #define CEILING(x,y) (((x) + (y) - 1) / (y))
  227. #undef ABS
  228. #ifdef __cplusplus
  229. template <class T> static inline constexpr const T ABS(const T v) { return v >= 0 ? v : -v; }
  230. #else
  231. #define ABS(a) ({__typeof__(a) _a = (a); _a >= 0 ? _a : -_a;})
  232. #endif
  233. #define UNEAR_ZERO(x) ((x) < 0.000001f)
  234. #define NEAR_ZERO(x) WITHIN(x, -0.000001f, 0.000001f)
  235. #define NEAR(x,y) NEAR_ZERO((x)-(y))
  236. #define RECIPROCAL(x) (NEAR_ZERO(x) ? 0 : (1 / float(x)))
  237. #define FIXFLOAT(f) ({__typeof__(f) _f = (f); _f + (_f < 0 ? -0.0000005f : 0.0000005f);})
  238. //
  239. // Maths macros that can be overridden by HAL
  240. //
  241. #define ACOS(x) acosf(x)
  242. #define ATAN2(y, x) atan2f(y, x)
  243. #define POW(x, y) powf(x, y)
  244. #define SQRT(x) sqrtf(x)
  245. #define RSQRT(x) (1.0f / sqrtf(x))
  246. #define CEIL(x) ceilf(x)
  247. #define FLOOR(x) floorf(x)
  248. #define TRUNC(x) truncf(x)
  249. #define LROUND(x) lroundf(x)
  250. #define FMOD(x, y) fmodf(x, y)
  251. #define HYPOT(x,y) SQRT(HYPOT2(x,y))
  252. // Use NUM_ARGS(__VA_ARGS__) to get the number of variadic arguments
  253. #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
  254. #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)
  255. #ifdef __cplusplus
  256. #ifndef _MINMAX_H_
  257. #define _MINMAX_H_
  258. extern "C++" {
  259. // C++11 solution that is standards compliant. Return type is deduced automatically
  260. template <class L, class R> static inline constexpr auto _MIN(const L lhs, const R rhs) -> decltype(lhs + rhs) {
  261. return lhs < rhs ? lhs : rhs;
  262. }
  263. template <class L, class R> static inline constexpr auto _MAX(const L lhs, const R rhs) -> decltype(lhs + rhs) {
  264. return lhs > rhs ? lhs : rhs;
  265. }
  266. template<class T, class ... Ts> static inline constexpr const T _MIN(T V, Ts... Vs) { return _MIN(V, _MIN(Vs...)); }
  267. template<class T, class ... Ts> static inline constexpr const T _MAX(T V, Ts... Vs) { return _MAX(V, _MAX(Vs...)); }
  268. }
  269. #endif
  270. // C++11 solution that is standard compliant. <type_traits> is not available on all platform
  271. namespace Private {
  272. template<bool, typename _Tp = void> struct enable_if { };
  273. template<typename _Tp> struct enable_if<true, _Tp> { typedef _Tp type; };
  274. template<typename T, typename U> struct is_same { enum { value = false }; };
  275. template<typename T> struct is_same<T, T> { enum { value = true }; };
  276. template <typename T, typename ... Args> struct first_type_of { typedef T type; };
  277. template <typename T> struct first_type_of<T> { typedef T type; };
  278. }
  279. // C++11 solution using SFINAE to detect the existance of a member in a class at compile time.
  280. // It creates a HasMember<Type> structure containing 'value' set to true if the member exists
  281. #define HAS_MEMBER_IMPL(Member) \
  282. namespace Private { \
  283. template <typename Type, typename Yes=char, typename No=long> struct HasMember_ ## Member { \
  284. template <typename C> static Yes& test( decltype(&C::Member) ) ; \
  285. template <typename C> static No& test(...); \
  286. enum { value = sizeof(test<Type>(0)) == sizeof(Yes) }; }; \
  287. }
  288. // Call the method if it exists, but do nothing if it does not. The method is detected at compile time.
  289. // If the method exists, this is inlined and does not cost anything. Else, an "empty" wrapper is created, returning a default value
  290. #define CALL_IF_EXISTS_IMPL(Return, Method, ...) \
  291. HAS_MEMBER_IMPL(Method) \
  292. namespace Private { \
  293. template <typename T, typename ... Args> FORCE_INLINE typename enable_if<HasMember_ ## Method <T>::value, Return>::type Call_ ## Method(T * t, Args... a) { return static_cast<Return>(t->Method(a...)); } \
  294. _UNUSED static Return Call_ ## Method(...) { return __VA_ARGS__; } \
  295. }
  296. #define CALL_IF_EXISTS(Return, That, Method, ...) \
  297. static_cast<Return>(Private::Call_ ## Method(That, ##__VA_ARGS__))
  298. // Compile-time string manipulation
  299. namespace CompileTimeString {
  300. // Simple compile-time parser to find the position of the end of a string
  301. constexpr const char* findStringEnd(const char *str) {
  302. return *str ? findStringEnd(str + 1) : str;
  303. }
  304. // Check whether a string contains a slash
  305. constexpr bool containsSlash(const char *str) {
  306. return *str == '/' ? true : (*str ? containsSlash(str + 1) : false);
  307. }
  308. // Find the last position of the slash
  309. constexpr const char* findLastSlashPos(const char* str) {
  310. return *str == '/' ? (str + 1) : findLastSlashPos(str - 1);
  311. }
  312. // Compile-time evaluation of the last part of a file path
  313. // Typically used to shorten the path to file in compiled strings
  314. // CompileTimeString::baseName(__FILE__) returns "macros.h" and not /path/to/Marlin/src/core/macros.h
  315. constexpr const char* baseName(const char* str) {
  316. return containsSlash(str) ? findLastSlashPos(findStringEnd(str)) : str;
  317. }
  318. }
  319. #define ONLY_FILENAME CompileTimeString::baseName(__FILE__)
  320. #else
  321. #define MIN_2(a,b) ((a)<(b)?(a):(b))
  322. #define MIN_3(a,V...) MIN_2(a,MIN_2(V))
  323. #define MIN_4(a,V...) MIN_2(a,MIN_3(V))
  324. #define MIN_5(a,V...) MIN_2(a,MIN_4(V))
  325. #define MIN_6(a,V...) MIN_2(a,MIN_5(V))
  326. #define MIN_7(a,V...) MIN_2(a,MIN_6(V))
  327. #define MIN_8(a,V...) MIN_2(a,MIN_7(V))
  328. #define MIN_9(a,V...) MIN_2(a,MIN_8(V))
  329. #define MIN_10(a,V...) MIN_2(a,MIN_9(V))
  330. #define __MIN_N(N,V...) MIN_##N(V)
  331. #define _MIN_N(N,V...) __MIN_N(N,V)
  332. #define _MIN(V...) _MIN_N(NUM_ARGS(V), V)
  333. #define MAX_2(a,b) ((a)>(b)?(a):(b))
  334. #define MAX_3(a,V...) MAX_2(a,MAX_2(V))
  335. #define MAX_4(a,V...) MAX_2(a,MAX_3(V))
  336. #define MAX_5(a,V...) MAX_2(a,MAX_4(V))
  337. #define MAX_6(a,V...) MAX_2(a,MAX_5(V))
  338. #define MAX_7(a,V...) MAX_2(a,MAX_6(V))
  339. #define MAX_8(a,V...) MAX_2(a,MAX_7(V))
  340. #define MAX_9(a,V...) MAX_2(a,MAX_8(V))
  341. #define MAX_10(a,V...) MAX_2(a,MAX_9(V))
  342. #define __MAX_N(N,V...) MAX_##N(V)
  343. #define _MAX_N(N,V...) __MAX_N(N,V)
  344. #define _MAX(V...) _MAX_N(NUM_ARGS(V), V)
  345. #endif
  346. // Macros for adding
  347. #define INC_0 1
  348. #define INC_1 2
  349. #define INC_2 3
  350. #define INC_3 4
  351. #define INC_4 5
  352. #define INC_5 6
  353. #define INC_6 7
  354. #define INC_7 8
  355. #define INC_8 9
  356. #define INC_9 10
  357. #define INC_10 11
  358. #define INC_11 12
  359. #define INC_12 13
  360. #define INC_13 14
  361. #define INC_14 15
  362. #define INC_15 16
  363. #define INCREMENT_(n) INC_##n
  364. #define INCREMENT(n) INCREMENT_(n)
  365. #define ADD0(N) N
  366. #define ADD1(N) INCREMENT_(N)
  367. #define ADD2(N) ADD1(ADD1(N))
  368. #define ADD3(N) ADD1(ADD2(N))
  369. #define ADD4(N) ADD2(ADD2(N))
  370. #define ADD5(N) ADD2(ADD3(N))
  371. #define ADD6(N) ADD3(ADD3(N))
  372. #define ADD7(N) ADD3(ADD4(N))
  373. #define ADD8(N) ADD4(ADD4(N))
  374. #define ADD9(N) ADD4(ADD5(N))
  375. #define ADD10(N) ADD5(ADD5(N))
  376. // Macros for subtracting
  377. #define DEC_0 0
  378. #define DEC_1 0
  379. #define DEC_2 1
  380. #define DEC_3 2
  381. #define DEC_4 3
  382. #define DEC_5 4
  383. #define DEC_6 5
  384. #define DEC_7 6
  385. #define DEC_8 7
  386. #define DEC_9 8
  387. #define DEC_10 9
  388. #define DEC_11 10
  389. #define DEC_12 11
  390. #define DEC_13 12
  391. #define DEC_14 13
  392. #define DEC_15 14
  393. #define DECREMENT_(n) DEC_##n
  394. #define DECREMENT(n) DECREMENT_(n)
  395. #define SUB0(N) N
  396. #define SUB1(N) DECREMENT_(N)
  397. #define SUB2(N) SUB1(SUB1(N))
  398. #define SUB3(N) SUB1(SUB2(N))
  399. #define SUB4(N) SUB2(SUB2(N))
  400. #define SUB5(N) SUB2(SUB3(N))
  401. #define SUB6(N) SUB3(SUB3(N))
  402. #define SUB7(N) SUB3(SUB4(N))
  403. #define SUB8(N) SUB4(SUB4(N))
  404. #define SUB9(N) SUB4(SUB5(N))
  405. #define SUB10(N) SUB5(SUB5(N))
  406. //
  407. // Primitives supporting precompiler REPEAT
  408. //
  409. #define FIRST(a,...) a
  410. #define SECOND(a,b,...) b
  411. #define THIRD(a,b,c,...) c
  412. // Defer expansion
  413. #define EMPTY()
  414. #define DEFER(M) M EMPTY()
  415. #define DEFER2(M) M EMPTY EMPTY()()
  416. #define DEFER3(M) M EMPTY EMPTY EMPTY()()()
  417. #define DEFER4(M) M EMPTY EMPTY EMPTY EMPTY()()()()
  418. // Force define expansion
  419. #define EVAL(V...) EVAL16(V)
  420. #define EVAL1024(V...) EVAL512(EVAL512(V))
  421. #define EVAL512(V...) EVAL256(EVAL256(V))
  422. #define EVAL256(V...) EVAL128(EVAL128(V))
  423. #define EVAL128(V...) EVAL64(EVAL64(V))
  424. #define EVAL64(V...) EVAL32(EVAL32(V))
  425. #define EVAL32(V...) EVAL16(EVAL16(V))
  426. #define EVAL16(V...) EVAL8(EVAL8(V))
  427. #define EVAL8(V...) EVAL4(EVAL4(V))
  428. #define EVAL4(V...) EVAL2(EVAL2(V))
  429. #define EVAL2(V...) EVAL1(EVAL1(V))
  430. #define EVAL1(V...) V
  431. #define IS_PROBE(V...) SECOND(V, 0) // Get the second item passed, or 0
  432. #define PROBE() ~, 1 // Second item will be 1 if this is passed
  433. #define _NOT_0 PROBE()
  434. #define NOT(x) IS_PROBE(_CAT(_NOT_, x)) // NOT('0') gets '1'. Anything else gets '0'.
  435. #define _BOOL(x) NOT(NOT(x)) // NOT('0') gets '0'. Anything else gets '1'.
  436. #define IF_ELSE(TF) _IF_ELSE(_BOOL(TF))
  437. #define _IF_ELSE(TF) _CAT(_IF_, TF)
  438. #define _IF_1(V...) V _IF_1_ELSE
  439. #define _IF_0(...) _IF_0_ELSE
  440. #define _IF_1_ELSE(...)
  441. #define _IF_0_ELSE(V...) V
  442. #define HAS_ARGS(V...) _BOOL(FIRST(_END_OF_ARGUMENTS_ V)())
  443. #define _END_OF_ARGUMENTS_() 0
  444. // Simple Inline IF Macros, friendly to use in other macro definitions
  445. #define IF(O, A, B) ((O) ? (A) : (B))
  446. #define IF_0(O, A) IF(O, A, 0)
  447. #define IF_1(O, A) IF(O, A, 1)
  448. //
  449. // REPEAT core macros. Recurse N times with ascending I.
  450. //
  451. // Call OP(I) N times with ascending counter.
  452. #define _REPEAT(_RPT_I,_RPT_N,_RPT_OP) \
  453. _RPT_OP(_RPT_I) \
  454. IF_ELSE(SUB1(_RPT_N)) \
  455. ( DEFER2(__REPEAT)()(ADD1(_RPT_I),SUB1(_RPT_N),_RPT_OP) ) \
  456. ( /* Do nothing */ )
  457. #define __REPEAT() _REPEAT
  458. // Call OP(I, ...) N times with ascending counter.
  459. #define _REPEAT2(_RPT_I,_RPT_N,_RPT_OP,V...) \
  460. _RPT_OP(_RPT_I,V) \
  461. IF_ELSE(SUB1(_RPT_N)) \
  462. ( DEFER2(__REPEAT2)()(ADD1(_RPT_I),SUB1(_RPT_N),_RPT_OP,V) ) \
  463. ( /* Do nothing */ )
  464. #define __REPEAT2() _REPEAT2
  465. // Repeat a macro passing S...N-1.
  466. #define REPEAT_S(S,N,OP) EVAL(_REPEAT(S,SUB##S(N),OP))
  467. #define REPEAT(N,OP) REPEAT_S(0,N,OP)
  468. // Repeat a macro passing 0...N-1 plus additional arguments.
  469. #define REPEAT2_S(S,N,OP,V...) EVAL(_REPEAT2(S,SUB##S(N),OP,V))
  470. #define REPEAT2(N,OP,V...) REPEAT2_S(0,N,OP,V)
  471. // Use RREPEAT macros with REPEAT macros for nesting
  472. #define _RREPEAT(_RPT_I,_RPT_N,_RPT_OP) \
  473. _RPT_OP(_RPT_I) \
  474. IF_ELSE(SUB1(_RPT_N)) \
  475. ( DEFER2(__RREPEAT)()(ADD1(_RPT_I),SUB1(_RPT_N),_RPT_OP) ) \
  476. ( /* Do nothing */ )
  477. #define __RREPEAT() _RREPEAT
  478. #define _RREPEAT2(_RPT_I,_RPT_N,_RPT_OP,V...) \
  479. _RPT_OP(_RPT_I,V) \
  480. IF_ELSE(SUB1(_RPT_N)) \
  481. ( DEFER2(__RREPEAT2)()(ADD1(_RPT_I),SUB1(_RPT_N),_RPT_OP,V) ) \
  482. ( /* Do nothing */ )
  483. #define __RREPEAT2() _RREPEAT2
  484. #define RREPEAT_S(S,N,OP) EVAL1024(_RREPEAT(S,SUB##S(N),OP))
  485. #define RREPEAT(N,OP) RREPEAT_S(0,N,OP)
  486. #define RREPEAT2_S(S,N,OP,V...) EVAL1024(_RREPEAT2(S,SUB##S(N),OP,V))
  487. #define RREPEAT2(N,OP,V...) RREPEAT2_S(0,N,OP,V)
  488. // See https://github.com/swansontec/map-macro
  489. #define MAP_OUT
  490. #define MAP_END(...)
  491. #define MAP_GET_END() 0, MAP_END
  492. #define MAP_NEXT0(test, next, ...) next MAP_OUT
  493. #define MAP_NEXT1(test, next) MAP_NEXT0 (test, next, 0)
  494. #define MAP_NEXT(test, next) MAP_NEXT1 (MAP_GET_END test, next)
  495. #define MAP0(f, x, peek, ...) f(x) MAP_NEXT (peek, MAP1) (f, peek, __VA_ARGS__)
  496. #define MAP1(f, x, peek, ...) f(x) MAP_NEXT (peek, MAP0) (f, peek, __VA_ARGS__)
  497. #define MAP(f, ...) EVAL512 (MAP1 (f, __VA_ARGS__, (), 0))