My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

macros.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #define ABCE 4
  24. #define XYZE 4
  25. #define ABC 3
  26. #define XYZ 3
  27. #define XY 2
  28. #define _AXIS(A) (A##_AXIS)
  29. #define _XMIN_ 100
  30. #define _YMIN_ 200
  31. #define _ZMIN_ 300
  32. #define _XMAX_ 101
  33. #define _YMAX_ 201
  34. #define _ZMAX_ 301
  35. #define _XDIAG_ 102
  36. #define _YDIAG_ 202
  37. #define _ZDIAG_ 302
  38. #define _E0DIAG_ 400
  39. #define _E1DIAG_ 401
  40. #define _E2DIAG_ 402
  41. #define _E3DIAG_ 403
  42. #define _E4DIAG_ 404
  43. #define _E5DIAG_ 405
  44. #define _E6DIAG_ 406
  45. #define _E7DIAG_ 407
  46. #define _FORCE_INLINE_ __attribute__((__always_inline__)) __inline__
  47. #define FORCE_INLINE __attribute__((always_inline)) inline
  48. #define _UNUSED __attribute__((unused))
  49. #define _O0 __attribute__((optimize("O0")))
  50. #define _Os __attribute__((optimize("Os")))
  51. #define _O1 __attribute__((optimize("O1")))
  52. #define _O2 __attribute__((optimize("O2")))
  53. #define _O3 __attribute__((optimize("O3")))
  54. #ifndef UNUSED
  55. #if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)
  56. #define UNUSED(X) (void)X
  57. #else
  58. #define UNUSED(x) ((void)(x))
  59. #endif
  60. #endif
  61. // Clock speed factors
  62. #if !defined(CYCLES_PER_MICROSECOND) && !defined(__STM32F1__)
  63. #define CYCLES_PER_MICROSECOND (F_CPU / 1000000UL) // 16 or 20 on AVR
  64. #endif
  65. // Nanoseconds per cycle
  66. #define NANOSECONDS_PER_CYCLE (1000000000.0 / F_CPU)
  67. // Macros to make sprintf_P read from PROGMEM (AVR extension)
  68. #ifdef __AVR__
  69. #define S_FMT "%S"
  70. #else
  71. #define S_FMT "%s"
  72. #endif
  73. // Macros to make a string from a macro
  74. #define STRINGIFY_(M) #M
  75. #define STRINGIFY(M) STRINGIFY_(M)
  76. #define A(CODE) " " CODE "\n\t"
  77. #define L(CODE) CODE ":\n\t"
  78. // Macros for bit masks
  79. #undef _BV
  80. #define _BV(n) (1<<(n))
  81. #define TEST(n,b) (!!((n)&_BV(b)))
  82. #define SET_BIT_TO(N,B,TF) do{ if (TF) SBI(N,B); else CBI(N,B); }while(0)
  83. #ifndef SBI
  84. #define SBI(A,B) (A |= (1 << (B)))
  85. #endif
  86. #ifndef CBI
  87. #define CBI(A,B) (A &= ~(1 << (B)))
  88. #endif
  89. #define _BV32(b) (1UL << (b))
  90. #define TEST32(n,b) !!((n)&_BV32(b))
  91. #define SBI32(n,b) (n |= _BV32(b))
  92. #define CBI32(n,b) (n &= ~_BV32(b))
  93. #define cu(x) ((x)*(x)*(x))
  94. #define RADIANS(d) ((d)*float(M_PI)/180.0f)
  95. #define DEGREES(r) ((r)*180.0f/float(M_PI))
  96. #define HYPOT2(x,y) (sq(x)+sq(y))
  97. #define CIRCLE_AREA(R) (float(M_PI) * sq(float(R)))
  98. #define CIRCLE_CIRC(R) (2 * float(M_PI) * float(R))
  99. #define SIGN(a) ((a>0)-(a<0))
  100. #define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))
  101. // Macros to constrain values
  102. #ifdef __cplusplus
  103. // C++11 solution that is standards compliant.
  104. template <class V, class N> static inline constexpr void NOLESS(V& v, const N n) {
  105. if (n > v) v = n;
  106. }
  107. template <class V, class N> static inline constexpr void NOMORE(V& v, const N n) {
  108. if (n < v) v = n;
  109. }
  110. template <class V, class N1, class N2> static inline constexpr void LIMIT(V& v, const N1 n1, const N2 n2) {
  111. if (n1 > v) v = n1;
  112. else if (n2 < v) v = n2;
  113. }
  114. #else
  115. // Using GCC extensions, but Travis GCC version does not like it and gives
  116. // "error: statement-expressions are not allowed outside functions nor in template-argument lists"
  117. #define NOLESS(v, n) \
  118. do{ \
  119. __typeof__(n) _n = (n); \
  120. if (_n > v) v = _n; \
  121. }while(0)
  122. #define NOMORE(v, n) \
  123. do{ \
  124. __typeof__(n) _n = (n); \
  125. if (_n < v) v = _n; \
  126. }while(0)
  127. #define LIMIT(v, n1, n2) \
  128. do{ \
  129. __typeof__(n1) _n1 = (n1); \
  130. __typeof__(n2) _n2 = (n2); \
  131. if (_n1 > v) v = _n1; \
  132. else if (_n2 < v) v = _n2; \
  133. }while(0)
  134. #endif
  135. // Macros to chain up to 12 conditions
  136. #define _DO_1(W,C,A) (_##W##_1(A))
  137. #define _DO_2(W,C,A,B) (_##W##_1(A) C _##W##_1(B))
  138. #define _DO_3(W,C,A,V...) (_##W##_1(A) C _DO_2(W,C,V))
  139. #define _DO_4(W,C,A,V...) (_##W##_1(A) C _DO_3(W,C,V))
  140. #define _DO_5(W,C,A,V...) (_##W##_1(A) C _DO_4(W,C,V))
  141. #define _DO_6(W,C,A,V...) (_##W##_1(A) C _DO_5(W,C,V))
  142. #define _DO_7(W,C,A,V...) (_##W##_1(A) C _DO_6(W,C,V))
  143. #define _DO_8(W,C,A,V...) (_##W##_1(A) C _DO_7(W,C,V))
  144. #define _DO_9(W,C,A,V...) (_##W##_1(A) C _DO_8(W,C,V))
  145. #define _DO_10(W,C,A,V...) (_##W##_1(A) C _DO_9(W,C,V))
  146. #define _DO_11(W,C,A,V...) (_##W##_1(A) C _DO_10(W,C,V))
  147. #define _DO_12(W,C,A,V...) (_##W##_1(A) C _DO_11(W,C,V))
  148. #define __DO_N(W,C,N,V...) _DO_##N(W,C,V)
  149. #define _DO_N(W,C,N,V...) __DO_N(W,C,N,V)
  150. #define DO(W,C,V...) _DO_N(W,C,NUM_ARGS(V),V)
  151. // Macros to support option testing
  152. #define _CAT(a,V...) a##V
  153. #define CAT(a,V...) _CAT(a,V)
  154. #define _ISENA_ ~,1
  155. #define _ISENA_1 ~,1
  156. #define _ISENA_0x1 ~,1
  157. #define _ISENA_true ~,1
  158. #define _ISENA(V...) IS_PROBE(V)
  159. #define _ENA_1(O) _ISENA(CAT(_IS,CAT(ENA_, O)))
  160. #define _DIS_1(O) NOT(_ENA_1(O))
  161. #define ENABLED(V...) DO(ENA,&&,V)
  162. #define DISABLED(V...) DO(DIS,&&,V)
  163. #define TERN(O,A,B) _TERN(_ENA_1(O),B,A) // OPTION converted to '0' or '1'
  164. #define _TERN(E,V...) __TERN(_CAT(T_,E),V) // Prepend 'T_' to get 'T_0' or 'T_1'
  165. #define __TERN(T,V...) ___TERN(_CAT(_NO,T),V) // Prepend '_NO' to get '_NOT_0' or '_NOT_1'
  166. #define ___TERN(P,V...) THIRD(P,V) // If first argument has a comma, A. Else B.
  167. #define ANY(V...) !DISABLED(V)
  168. #define NONE(V...) DISABLED(V)
  169. #define ALL(V...) ENABLED(V)
  170. #define BOTH(V1,V2) ALL(V1,V2)
  171. #define EITHER(V1,V2) ANY(V1,V2)
  172. // Macros to support pins/buttons exist testing
  173. #define PIN_EXISTS(PN) (defined(PN##_PIN) && PN##_PIN >= 0)
  174. #define _PINEX_1 PIN_EXISTS
  175. #define PINS_EXIST(V...) DO(PINEX,&&,V)
  176. #define ANY_PIN(V...) DO(PINEX,||,V)
  177. #define BUTTON_EXISTS(BN) (defined(BTN_##BN) && BTN_##BN >= 0)
  178. #define _BTNEX_1 BUTTON_EXISTS
  179. #define BUTTONS_EXIST(V...) DO(BTNEX,&&,V)
  180. #define ANY_BUTTON(V...) DO(BTNEX,||,V)
  181. #define WITHIN(N,L,H) ((N) >= (L) && (N) <= (H))
  182. #define NUMERIC(a) WITHIN(a, '0', '9')
  183. #define DECIMAL(a) (NUMERIC(a) || a == '.')
  184. #define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-' || (a) == '+')
  185. #define DECIMAL_SIGNED(a) (DECIMAL(a) || (a) == '-' || (a) == '+')
  186. #define COUNT(a) (sizeof(a)/sizeof(*a))
  187. #define ZERO(a) memset(a,0,sizeof(a))
  188. #define COPY(a,b) do{ \
  189. static_assert(sizeof(a[0]) == sizeof(b[0]), "COPY: '" STRINGIFY(a) "' and '" STRINGIFY(b) "' types (sizes) don't match!"); \
  190. memcpy(&a[0],&b[0],_MIN(sizeof(a),sizeof(b))); \
  191. }while(0)
  192. // Macros for initializing arrays
  193. #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
  194. #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
  195. #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
  196. #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
  197. #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
  198. #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
  199. #define LIST_10(A,B,C,D,E,F,G,H,I,J,...) A,B,C,D,E,F,G,H,I,J
  200. #define LIST_9( A,B,C,D,E,F,G,H,I,...) A,B,C,D,E,F,G,H,I
  201. #define LIST_8( A,B,C,D,E,F,G,H,...) A,B,C,D,E,F,G,H
  202. #define LIST_7( A,B,C,D,E,F,G,...) A,B,C,D,E,F,G
  203. #define LIST_6( A,B,C,D,E,F,...) A,B,C,D,E,F
  204. #define LIST_5( A,B,C,D,E,...) A,B,C,D,E
  205. #define LIST_4( A,B,C,D,...) A,B,C,D
  206. #define LIST_3( A,B,C,...) A,B,C
  207. #define LIST_2( A,B,...) A,B
  208. #define LIST_1( A,...) A
  209. #define _LIST_N(N,V...) LIST_##N(V)
  210. #define LIST_N(N,V...) _LIST_N(N,V)
  211. #define ARRAY_N(N,V...) { _LIST_N(N,V) }
  212. #define _JOIN_1(O) (O)
  213. #define JOIN_N(N,C,V...) (DO(JOIN,C,LIST_N(N,V)))
  214. #define LOOP_S_LE_N(VAR, S, N) for (uint8_t VAR=(S); VAR<=(N); VAR++)
  215. #define LOOP_S_L_N(VAR, S, N) for (uint8_t VAR=(S); VAR<(N); VAR++)
  216. #define LOOP_LE_N(VAR, N) LOOP_S_LE_N(VAR, 0, N)
  217. #define LOOP_L_N(VAR, N) LOOP_S_L_N(VAR, 0, N)
  218. #define NOOP (void(0))
  219. #define CEILING(x,y) (((x) + (y) - 1) / (y))
  220. #undef ABS
  221. #ifdef __cplusplus
  222. template <class T> static inline constexpr const T ABS(const T v) { return v >= 0 ? v : -v; }
  223. #else
  224. #define ABS(a) ({__typeof__(a) _a = (a); _a >= 0 ? _a : -_a;})
  225. #endif
  226. #define UNEAR_ZERO(x) ((x) < 0.000001f)
  227. #define NEAR_ZERO(x) WITHIN(x, -0.000001f, 0.000001f)
  228. #define NEAR(x,y) NEAR_ZERO((x)-(y))
  229. #define RECIPROCAL(x) (NEAR_ZERO(x) ? 0 : (1 / float(x)))
  230. #define FIXFLOAT(f) (f + (f < 0 ? -0.00005f : 0.00005f))
  231. //
  232. // Maths macros that can be overridden by HAL
  233. //
  234. #define ACOS(x) acosf(x)
  235. #define ATAN2(y, x) atan2f(y, x)
  236. #define POW(x, y) powf(x, y)
  237. #define SQRT(x) sqrtf(x)
  238. #define RSQRT(x) (1.0f / sqrtf(x))
  239. #define CEIL(x) ceilf(x)
  240. #define FLOOR(x) floorf(x)
  241. #define LROUND(x) lroundf(x)
  242. #define FMOD(x, y) fmodf(x, y)
  243. #define HYPOT(x,y) SQRT(HYPOT2(x,y))
  244. #ifdef TARGET_LPC1768
  245. #define I2C_ADDRESS(A) ((A) << 1)
  246. #else
  247. #define I2C_ADDRESS(A) A
  248. #endif
  249. // Use NUM_ARGS(__VA_ARGS__) to get the number of variadic arguments
  250. #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
  251. #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)
  252. #ifdef __cplusplus
  253. #ifndef _MINMAX_H_
  254. #define _MINMAX_H_
  255. extern "C++" {
  256. // C++11 solution that is standards compliant. Return type is deduced automatically
  257. template <class L, class R> static inline constexpr auto _MIN(const L lhs, const R rhs) -> decltype(lhs + rhs) {
  258. return lhs < rhs ? lhs : rhs;
  259. }
  260. template <class L, class R> static inline constexpr auto _MAX(const L lhs, const R rhs) -> decltype(lhs + rhs) {
  261. return lhs > rhs ? lhs : rhs;
  262. }
  263. template<class T, class ... Ts> static inline constexpr const T _MIN(T V, Ts... Vs) { return _MIN(V, _MIN(Vs...)); }
  264. template<class T, class ... Ts> static inline constexpr const T _MAX(T V, Ts... Vs) { return _MAX(V, _MAX(Vs...)); }
  265. }
  266. #endif
  267. #else
  268. #define MIN_2(a,b) ((a)<(b)?(a):(b))
  269. #define MIN_3(a,V...) MIN_2(a,MIN_2(V))
  270. #define MIN_4(a,V...) MIN_2(a,MIN_3(V))
  271. #define MIN_5(a,V...) MIN_2(a,MIN_4(V))
  272. #define MIN_6(a,V...) MIN_2(a,MIN_5(V))
  273. #define MIN_7(a,V...) MIN_2(a,MIN_6(V))
  274. #define MIN_8(a,V...) MIN_2(a,MIN_7(V))
  275. #define MIN_9(a,V...) MIN_2(a,MIN_8(V))
  276. #define MIN_10(a,V...) MIN_2(a,MIN_9(V))
  277. #define __MIN_N(N,V...) MIN_##N(V)
  278. #define _MIN_N(N,V...) __MIN_N(N,V)
  279. #define _MIN(V...) _MIN_N(NUM_ARGS(V), V)
  280. #define MAX_2(a,b) ((a)>(b)?(a):(b))
  281. #define MAX_3(a,V...) MAX_2(a,MAX_2(V))
  282. #define MAX_4(a,V...) MAX_2(a,MAX_3(V))
  283. #define MAX_5(a,V...) MAX_2(a,MAX_4(V))
  284. #define MAX_6(a,V...) MAX_2(a,MAX_5(V))
  285. #define MAX_7(a,V...) MAX_2(a,MAX_6(V))
  286. #define MAX_8(a,V...) MAX_2(a,MAX_7(V))
  287. #define MAX_9(a,V...) MAX_2(a,MAX_8(V))
  288. #define MAX_10(a,V...) MAX_2(a,MAX_9(V))
  289. #define __MAX_N(N,V...) MAX_##N(V)
  290. #define _MAX_N(N,V...) __MAX_N(N,V)
  291. #define _MAX(V...) _MAX_N(NUM_ARGS(V), V)
  292. #endif
  293. // Macros for adding
  294. #define INC_0 1
  295. #define INC_1 2
  296. #define INC_2 3
  297. #define INC_3 4
  298. #define INC_4 5
  299. #define INC_5 6
  300. #define INC_6 7
  301. #define INC_7 8
  302. #define INC_8 9
  303. #define INCREMENT_(n) INC_##n
  304. #define INCREMENT(n) INCREMENT_(n)
  305. #define ADD0(N) N
  306. #define ADD1(N) INCREMENT_(N)
  307. #define ADD2(N) ADD1(ADD1(N))
  308. #define ADD3(N) ADD1(ADD2(N))
  309. #define ADD4(N) ADD2(ADD2(N))
  310. #define ADD5(N) ADD2(ADD3(N))
  311. #define ADD6(N) ADD3(ADD3(N))
  312. #define ADD7(N) ADD3(ADD4(N))
  313. #define ADD8(N) ADD4(ADD4(N))
  314. #define ADD9(N) ADD4(ADD5(N))
  315. #define ADD10(N) ADD5(ADD5(N))
  316. // Macros for subtracting
  317. #define DEC_0 0
  318. #define DEC_1 0
  319. #define DEC_2 1
  320. #define DEC_3 2
  321. #define DEC_4 3
  322. #define DEC_5 4
  323. #define DEC_6 5
  324. #define DEC_7 6
  325. #define DEC_8 7
  326. #define DEC_9 8
  327. #define DECREMENT_(n) DEC_##n
  328. #define DECREMENT(n) DECREMENT_(n)
  329. #define SUB0(N) N
  330. #define SUB1(N) DECREMENT_(N)
  331. #define SUB2(N) SUB1(SUB1(N))
  332. #define SUB3(N) SUB1(SUB2(N))
  333. #define SUB4(N) SUB2(SUB2(N))
  334. #define SUB5(N) SUB2(SUB3(N))
  335. #define SUB6(N) SUB3(SUB3(N))
  336. #define SUB7(N) SUB3(SUB4(N))
  337. #define SUB8(N) SUB4(SUB4(N))
  338. #define SUB9(N) SUB4(SUB5(N))
  339. #define SUB10(N) SUB5(SUB5(N))
  340. //
  341. // Primitives supporting precompiler REPEAT
  342. //
  343. #define FIRST(a,...) a
  344. #define SECOND(a,b,...) b
  345. #define THIRD(a,b,c,...) c
  346. // Defer expansion
  347. #define EMPTY()
  348. #define DEFER(M) M EMPTY()
  349. #define DEFER2(M) M EMPTY EMPTY()()
  350. #define DEFER3(M) M EMPTY EMPTY EMPTY()()()
  351. #define DEFER4(M) M EMPTY EMPTY EMPTY EMPTY()()()()
  352. // Force define expansion
  353. #define EVAL(V...) EVAL16(V)
  354. #define EVAL1024(V...) EVAL512(EVAL512(V))
  355. #define EVAL512(V...) EVAL256(EVAL256(V))
  356. #define EVAL256(V...) EVAL128(EVAL128(V))
  357. #define EVAL128(V...) EVAL64(EVAL64(V))
  358. #define EVAL64(V...) EVAL32(EVAL32(V))
  359. #define EVAL32(V...) EVAL16(EVAL16(V))
  360. #define EVAL16(V...) EVAL8(EVAL8(V))
  361. #define EVAL8(V...) EVAL4(EVAL4(V))
  362. #define EVAL4(V...) EVAL2(EVAL2(V))
  363. #define EVAL2(V...) EVAL1(EVAL1(V))
  364. #define EVAL1(V...) V
  365. #define IS_PROBE(V...) SECOND(V, 0) // Get the second item passed, or 0
  366. #define PROBE() ~, 1 // Second item will be 1 if this is passed
  367. #define _NOT_0 PROBE()
  368. #define NOT(x) IS_PROBE(_CAT(_NOT_, x)) // NOT('0') gets '1'. Anything else gets '0'.
  369. #define _BOOL(x) NOT(NOT(x)) // NOT('0') gets '0'. Anything else gets '1'.
  370. #define IF_ELSE(TF) _IF_ELSE(_BOOL(TF))
  371. #define _IF_ELSE(TF) _CAT(_IF_, TF)
  372. #define _IF_1(V...) V _IF_1_ELSE
  373. #define _IF_0(...) _IF_0_ELSE
  374. #define _IF_1_ELSE(...)
  375. #define _IF_0_ELSE(V...) V
  376. #define HAS_ARGS(V...) _BOOL(FIRST(_END_OF_ARGUMENTS_ V)())
  377. #define _END_OF_ARGUMENTS_() 0
  378. //
  379. // REPEAT core macros. Recurse N times with ascending I.
  380. //
  381. // Call OP(I) N times with ascending counter.
  382. #define _REPEAT(_RPT_I,_RPT_N,_RPT_OP) \
  383. _RPT_OP(_RPT_I) \
  384. IF_ELSE(SUB1(_RPT_N)) \
  385. ( DEFER2(__REPEAT)()(ADD1(_RPT_I),SUB1(_RPT_N),_RPT_OP) ) \
  386. ( /* Do nothing */ )
  387. #define __REPEAT() _REPEAT
  388. // Call OP(I, ...) N times with ascending counter.
  389. #define _REPEAT2(_RPT_I,_RPT_N,_RPT_OP,V...) \
  390. _RPT_OP(_RPT_I,V) \
  391. IF_ELSE(SUB1(_RPT_N)) \
  392. ( DEFER2(__REPEAT2)()(ADD1(_RPT_I),SUB1(_RPT_N),_RPT_OP,V) ) \
  393. ( /* Do nothing */ )
  394. #define __REPEAT2() _REPEAT2
  395. // Repeat a macro passing S...N-1.
  396. #define REPEAT_S(S,N,OP) EVAL(_REPEAT(S,SUB##S(N),OP))
  397. #define REPEAT(N,OP) REPEAT_S(0,N,OP)
  398. // Repeat a macro passing 0...N-1 plus additional arguments.
  399. #define REPEAT2_S(S,N,OP,V...) EVAL(_REPEAT2(S,SUB##S(N),OP,V))
  400. #define REPEAT2(N,OP,V...) REPEAT2_S(0,N,OP,V)
  401. // Use RREPEAT macros with REPEAT macros for nesting
  402. #define _RREPEAT(_RPT_I,_RPT_N,_RPT_OP) \
  403. _RPT_OP(_RPT_I) \
  404. IF_ELSE(SUB1(_RPT_N)) \
  405. ( DEFER2(__RREPEAT)()(ADD1(_RPT_I),SUB1(_RPT_N),_RPT_OP) ) \
  406. ( /* Do nothing */ )
  407. #define __RREPEAT() _RREPEAT
  408. #define _RREPEAT2(_RPT_I,_RPT_N,_RPT_OP,V...) \
  409. _RPT_OP(_RPT_I,V) \
  410. IF_ELSE(SUB1(_RPT_N)) \
  411. ( DEFER2(__RREPEAT2)()(ADD1(_RPT_I),SUB1(_RPT_N),_RPT_OP,V) ) \
  412. ( /* Do nothing */ )
  413. #define __RREPEAT2() _RREPEAT2
  414. #define RREPEAT_S(S,N,OP) EVAL1024(_RREPEAT(S,SUB##S(N),OP))
  415. #define RREPEAT(N,OP) RREPEAT_S(0,N,OP)
  416. #define RREPEAT2_S(S,N,OP,V...) EVAL1024(_RREPEAT2(S,SUB##S(N),OP,V))
  417. #define RREPEAT2(N,OP,V...) RREPEAT2_S(0,N,OP,V)