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 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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 _IMIN_ 500
  36. #define _JMIN_ 600
  37. #define _KMIN_ 700
  38. #define _XMAX_ 101
  39. #define _YMAX_ 201
  40. #define _ZMAX_ 301
  41. #define _IMAX_ 501
  42. #define _JMAX_ 601
  43. #define _KMAX_ 701
  44. #define _XDIAG_ 102
  45. #define _YDIAG_ 202
  46. #define _ZDIAG_ 302
  47. #define _IDIAG_ 502
  48. #define _JDIAG_ 602
  49. #define _KDIAG_ 702
  50. #define _E0DIAG_ 400
  51. #define _E1DIAG_ 401
  52. #define _E2DIAG_ 402
  53. #define _E3DIAG_ 403
  54. #define _E4DIAG_ 404
  55. #define _E5DIAG_ 405
  56. #define _E6DIAG_ 406
  57. #define _E7DIAG_ 407
  58. #define _FORCE_INLINE_ __attribute__((__always_inline__)) __inline__
  59. #define FORCE_INLINE __attribute__((always_inline)) inline
  60. #define NO_INLINE __attribute__((noinline))
  61. #define _UNUSED __attribute__((unused))
  62. #define _O0 __attribute__((optimize("O0")))
  63. #define _Os __attribute__((optimize("Os")))
  64. #define _O1 __attribute__((optimize("O1")))
  65. #define _O2 __attribute__((optimize("O2")))
  66. #define _O3 __attribute__((optimize("O3")))
  67. #define IS_CONSTEXPR(...) __builtin_constant_p(__VA_ARGS__) // Only valid solution with C++14. Should use std::is_constant_evaluated() in C++20 instead
  68. #ifndef UNUSED
  69. #define UNUSED(x) ((void)(x))
  70. #endif
  71. // Clock speed factors
  72. #if !defined(CYCLES_PER_MICROSECOND) && !defined(__STM32F1__)
  73. #define CYCLES_PER_MICROSECOND (F_CPU / 1000000UL) // 16 or 20 on AVR
  74. #endif
  75. // Nanoseconds per cycle
  76. #define NANOSECONDS_PER_CYCLE (1000000000.0 / F_CPU)
  77. // Macros to make a string from a macro
  78. #define STRINGIFY_(M) #M
  79. #define STRINGIFY(M) STRINGIFY_(M)
  80. #define A(CODE) " " CODE "\n\t"
  81. #define L(CODE) CODE ":\n\t"
  82. // Macros for bit masks
  83. #undef _BV
  84. #define _BV(n) (1<<(n))
  85. #define TEST(n,b) (!!((n)&_BV(b)))
  86. #define SET_BIT_TO(N,B,TF) do{ if (TF) SBI(N,B); else CBI(N,B); }while(0)
  87. #ifndef SBI
  88. #define SBI(A,B) (A |= _BV(B))
  89. #endif
  90. #ifndef CBI
  91. #define CBI(A,B) (A &= ~_BV(B))
  92. #endif
  93. #define TBI(N,B) (N ^= _BV(B))
  94. #define _BV32(b) (1UL << (b))
  95. #define TEST32(n,b) !!((n)&_BV32(b))
  96. #define SBI32(n,b) (n |= _BV32(b))
  97. #define CBI32(n,b) (n &= ~_BV32(b))
  98. #define TBI32(N,B) (N ^= _BV32(B))
  99. #define cu(x) ({__typeof__(x) _x = (x); (_x)*(_x)*(_x);})
  100. #define RADIANS(d) ((d)*float(M_PI)/180.0f)
  101. #define DEGREES(r) ((r)*180.0f/float(M_PI))
  102. #define HYPOT2(x,y) (sq(x)+sq(y))
  103. #define NORMSQ(x,y,z) (sq(x)+sq(y)+sq(z))
  104. #define CIRCLE_AREA(R) (float(M_PI) * sq(float(R)))
  105. #define CIRCLE_CIRC(R) (2 * float(M_PI) * float(R))
  106. #define SIGN(a) ({__typeof__(a) _a = (a); (_a>0)-(_a<0);})
  107. #define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))
  108. // Macros to constrain values
  109. #ifdef __cplusplus
  110. // C++11 solution that is standards compliant.
  111. template <class V, class N> static inline constexpr void NOLESS(V& v, const N n) {
  112. if (n > v) v = n;
  113. }
  114. template <class V, class N> static inline constexpr void NOMORE(V& v, const N n) {
  115. if (n < v) v = n;
  116. }
  117. template <class V, class N1, class N2> static inline constexpr void LIMIT(V& v, const N1 n1, const N2 n2) {
  118. if (n1 > v) v = n1;
  119. else if (n2 < v) v = n2;
  120. }
  121. #else
  122. #define NOLESS(v, n) \
  123. do{ \
  124. __typeof__(v) _n = (n); \
  125. if (_n > v) v = _n; \
  126. }while(0)
  127. #define NOMORE(v, n) \
  128. do{ \
  129. __typeof__(v) _n = (n); \
  130. if (_n < v) v = _n; \
  131. }while(0)
  132. #define LIMIT(v, n1, n2) \
  133. do{ \
  134. __typeof__(v) _n1 = (n1); \
  135. __typeof__(v) _n2 = (n2); \
  136. if (_n1 > v) v = _n1; \
  137. else if (_n2 < v) v = _n2; \
  138. }while(0)
  139. #endif
  140. // Macros to chain up to 14 conditions
  141. #define _DO_1(W,C,A) (_##W##_1(A))
  142. #define _DO_2(W,C,A,B) (_##W##_1(A) C _##W##_1(B))
  143. #define _DO_3(W,C,A,V...) (_##W##_1(A) C _DO_2(W,C,V))
  144. #define _DO_4(W,C,A,V...) (_##W##_1(A) C _DO_3(W,C,V))
  145. #define _DO_5(W,C,A,V...) (_##W##_1(A) C _DO_4(W,C,V))
  146. #define _DO_6(W,C,A,V...) (_##W##_1(A) C _DO_5(W,C,V))
  147. #define _DO_7(W,C,A,V...) (_##W##_1(A) C _DO_6(W,C,V))
  148. #define _DO_8(W,C,A,V...) (_##W##_1(A) C _DO_7(W,C,V))
  149. #define _DO_9(W,C,A,V...) (_##W##_1(A) C _DO_8(W,C,V))
  150. #define _DO_10(W,C,A,V...) (_##W##_1(A) C _DO_9(W,C,V))
  151. #define _DO_11(W,C,A,V...) (_##W##_1(A) C _DO_10(W,C,V))
  152. #define _DO_12(W,C,A,V...) (_##W##_1(A) C _DO_11(W,C,V))
  153. #define _DO_13(W,C,A,V...) (_##W##_1(A) C _DO_12(W,C,V))
  154. #define _DO_14(W,C,A,V...) (_##W##_1(A) C _DO_13(W,C,V))
  155. #define _DO_15(W,C,A,V...) (_##W##_1(A) C _DO_14(W,C,V))
  156. #define __DO_N(W,C,N,V...) _DO_##N(W,C,V)
  157. #define _DO_N(W,C,N,V...) __DO_N(W,C,N,V)
  158. #define DO(W,C,V...) (_DO_N(W,C,NUM_ARGS(V),V))
  159. // Macros to support option testing
  160. #define _CAT(a,V...) a##V
  161. #define CAT(a,V...) _CAT(a,V)
  162. #define _ISENA_ ~,1
  163. #define _ISENA_1 ~,1
  164. #define _ISENA_0x1 ~,1
  165. #define _ISENA_true ~,1
  166. #define _ISENA(V...) IS_PROBE(V)
  167. #define _ENA_1(O) _ISENA(CAT(_IS,CAT(ENA_, O)))
  168. #define _DIS_1(O) NOT(_ENA_1(O))
  169. #define ENABLED(V...) DO(ENA,&&,V)
  170. #define DISABLED(V...) DO(DIS,&&,V)
  171. #define COUNT_ENABLED(V...) DO(ENA,+,V)
  172. #define TERN(O,A,B) _TERN(_ENA_1(O),B,A) // OPTION ? 'A' : 'B'
  173. #define TERN0(O,A) _TERN(_ENA_1(O),0,A) // OPTION ? 'A' : '0'
  174. #define TERN1(O,A) _TERN(_ENA_1(O),1,A) // OPTION ? 'A' : '1'
  175. #define TERN_(O,A) _TERN(_ENA_1(O),,A) // OPTION ? 'A' : '<nul>'
  176. #define _TERN(E,V...) __TERN(_CAT(T_,E),V) // Prepend 'T_' to get 'T_0' or 'T_1'
  177. #define __TERN(T,V...) ___TERN(_CAT(_NO,T),V) // Prepend '_NO' to get '_NOT_0' or '_NOT_1'
  178. #define ___TERN(P,V...) THIRD(P,V) // If first argument has a comma, A. Else B.
  179. #define _OPTARG(A...) , A
  180. #define OPTARG(O,A...) TERN_(O,DEFER4(_OPTARG)(A))
  181. #define _OPTCODE(A) A;
  182. #define OPTCODE(O,A) TERN_(O,DEFER4(_OPTCODE)(A))
  183. // Macros to avoid 'f + 0.0' which is not always optimized away. Minus included for symmetry.
  184. // Compiler flags -fno-signed-zeros -ffinite-math-only also cover 'f * 1.0', 'f - f', etc.
  185. #define PLUS_TERN0(O,A) _TERN(_ENA_1(O),,+ (A)) // OPTION ? '+ (A)' : '<nul>'
  186. #define MINUS_TERN0(O,A) _TERN(_ENA_1(O),,- (A)) // OPTION ? '- (A)' : '<nul>'
  187. #define SUM_TERN(O,B,A) ((B) PLUS_TERN0(O,A)) // ((B) (OPTION ? '+ (A)' : '<nul>'))
  188. #define DIFF_TERN(O,B,A) ((B) MINUS_TERN0(O,A)) // ((B) (OPTION ? '- (A)' : '<nul>'))
  189. #define IF_ENABLED TERN_
  190. #define IF_DISABLED(O,A) TERN(O,,A)
  191. #define ANY(V...) !DISABLED(V)
  192. #define NONE(V...) DISABLED(V)
  193. #define ALL(V...) ENABLED(V)
  194. #define BOTH(V1,V2) ALL(V1,V2)
  195. #define EITHER(V1,V2) ANY(V1,V2)
  196. #define MANY(V...) (COUNT_ENABLED(V) > 1)
  197. // Macros to support pins/buttons exist testing
  198. #define PIN_EXISTS(PN) (defined(PN##_PIN) && PN##_PIN >= 0)
  199. #define _PINEX_1 PIN_EXISTS
  200. #define PINS_EXIST(V...) DO(PINEX,&&,V)
  201. #define ANY_PIN(V...) DO(PINEX,||,V)
  202. #define BUTTON_EXISTS(BN) (defined(BTN_##BN) && BTN_##BN >= 0)
  203. #define _BTNEX_1 BUTTON_EXISTS
  204. #define BUTTONS_EXIST(V...) DO(BTNEX,&&,V)
  205. #define ANY_BUTTON(V...) DO(BTNEX,||,V)
  206. #define WITHIN(N,L,H) ((N) >= (L) && (N) <= (H))
  207. #define ISEOL(C) ((C) == '\n' || (C) == '\r')
  208. #define NUMERIC(a) WITHIN(a, '0', '9')
  209. #define DECIMAL(a) (NUMERIC(a) || a == '.')
  210. #define HEXCHR(a) (NUMERIC(a) ? (a) - '0' : WITHIN(a, 'a', 'f') ? ((a) - 'a' + 10) : WITHIN(a, 'A', 'F') ? ((a) - 'A' + 10) : -1)
  211. #define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-' || (a) == '+')
  212. #define DECIMAL_SIGNED(a) (DECIMAL(a) || (a) == '-' || (a) == '+')
  213. #define COUNT(a) (sizeof(a)/sizeof(*a))
  214. #define ZERO(a) memset(a,0,sizeof(a))
  215. #define COPY(a,b) do{ \
  216. static_assert(sizeof(a[0]) == sizeof(b[0]), "COPY: '" STRINGIFY(a) "' and '" STRINGIFY(b) "' types (sizes) don't match!"); \
  217. memcpy(&a[0],&b[0],_MIN(sizeof(a),sizeof(b))); \
  218. }while(0)
  219. #define CODE_9( A,B,C,D,E,F,G,H,I,...) A; B; C; D; E; F; G; H; I
  220. #define CODE_8( A,B,C,D,E,F,G,H,...) A; B; C; D; E; F; G; H
  221. #define CODE_7( A,B,C,D,E,F,G,...) A; B; C; D; E; F; G
  222. #define CODE_6( A,B,C,D,E,F,...) A; B; C; D; E; F
  223. #define CODE_5( A,B,C,D,E,...) A; B; C; D; E
  224. #define CODE_4( A,B,C,D,...) A; B; C; D
  225. #define CODE_3( A,B,C,...) A; B; C
  226. #define CODE_2( A,B,...) A; B
  227. #define CODE_1( A,...) A
  228. #define _CODE_N(N,V...) CODE_##N(V)
  229. #define CODE_N(N,V...) _CODE_N(N,V)
  230. #define GANG_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
  231. #define GANG_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
  232. #define GANG_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
  233. #define GANG_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
  234. #define GANG_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
  235. #define GANG_11(A,B,C,D,E,F,G,H,I,J,K,...) A B C D E F G H I J K
  236. #define GANG_10(A,B,C,D,E,F,G,H,I,J,...) A B C D E F G H I J
  237. #define GANG_9( A,B,C,D,E,F,G,H,I,...) A B C D E F G H I
  238. #define GANG_8( A,B,C,D,E,F,G,H,...) A B C D E F G H
  239. #define GANG_7( A,B,C,D,E,F,G,...) A B C D E F G
  240. #define GANG_6( A,B,C,D,E,F,...) A B C D E F
  241. #define GANG_5( A,B,C,D,E,...) A B C D E
  242. #define GANG_4( A,B,C,D,...) A B C D
  243. #define GANG_3( A,B,C,...) A B C
  244. #define GANG_2( A,B,...) A B
  245. #define GANG_1( A,...) A
  246. #define _GANG_N(N,V...) GANG_##N(V)
  247. #define GANG_N(N,V...) _GANG_N(N,V)
  248. #define GANG_N_1(N,K) _GANG_N(N,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K)
  249. // Macros for initializing arrays
  250. #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
  251. #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
  252. #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
  253. #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
  254. #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
  255. #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
  256. #define LIST_10(A,B,C,D,E,F,G,H,I,J,...) A,B,C,D,E,F,G,H,I,J
  257. #define LIST_9( A,B,C,D,E,F,G,H,I,...) A,B,C,D,E,F,G,H,I
  258. #define LIST_8( A,B,C,D,E,F,G,H,...) A,B,C,D,E,F,G,H
  259. #define LIST_7( A,B,C,D,E,F,G,...) A,B,C,D,E,F,G
  260. #define LIST_6( A,B,C,D,E,F,...) A,B,C,D,E,F
  261. #define LIST_5( A,B,C,D,E,...) A,B,C,D,E
  262. #define LIST_4( A,B,C,D,...) A,B,C,D
  263. #define LIST_3( A,B,C,...) A,B,C
  264. #define LIST_2( A,B,...) A,B
  265. #define LIST_1( A,...) A
  266. #define LIST_0(...)
  267. #define _LIST_N(N,V...) LIST_##N(V)
  268. #define LIST_N(N,V...) _LIST_N(N,V)
  269. #define LIST_N_1(N,K) _LIST_N(N,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K)
  270. #define ARRAY_N(N,V...) { _LIST_N(N,V) }
  271. #define ARRAY_N_1(N,K) { LIST_N_1(N,K) }
  272. #define _JOIN_1(O) (O)
  273. #define JOIN_N(N,C,V...) (DO(JOIN,C,LIST_N(N,V)))
  274. #define LOOP_S_LE_N(VAR, S, N) for (uint8_t VAR=(S); VAR<=(N); VAR++)
  275. #define LOOP_S_L_N(VAR, S, N) for (uint8_t VAR=(S); VAR<(N); VAR++)
  276. #define LOOP_LE_N(VAR, N) LOOP_S_LE_N(VAR, 0, N)
  277. #define LOOP_L_N(VAR, N) LOOP_S_L_N(VAR, 0, N)
  278. #define NOOP (void(0))
  279. #define CEILING(x,y) (((x) + (y) - 1) / (y))
  280. #undef ABS
  281. #ifdef __cplusplus
  282. template <class T> static inline constexpr const T ABS(const T v) { return v >= 0 ? v : -v; }
  283. #else
  284. #define ABS(a) ({__typeof__(a) _a = (a); _a >= 0 ? _a : -_a;})
  285. #endif
  286. #define UNEAR_ZERO(x) ((x) < 0.000001f)
  287. #define NEAR_ZERO(x) WITHIN(x, -0.000001f, 0.000001f)
  288. #define NEAR(x,y) NEAR_ZERO((x)-(y))
  289. #define RECIPROCAL(x) (NEAR_ZERO(x) ? 0 : (1 / float(x)))
  290. #define FIXFLOAT(f) ({__typeof__(f) _f = (f); _f + (_f < 0 ? -0.0000005f : 0.0000005f);})
  291. //
  292. // Maths macros that can be overridden by HAL
  293. //
  294. #define ACOS(x) acosf(x)
  295. #define ATAN2(y, x) atan2f(y, x)
  296. #define POW(x, y) powf(x, y)
  297. #define SQRT(x) sqrtf(x)
  298. #define RSQRT(x) (1.0f / sqrtf(x))
  299. #define CEIL(x) ceilf(x)
  300. #define FLOOR(x) floorf(x)
  301. #define TRUNC(x) truncf(x)
  302. #define LROUND(x) lroundf(x)
  303. #define FMOD(x, y) fmodf(x, y)
  304. #define HYPOT(x,y) SQRT(HYPOT2(x,y))
  305. // Use NUM_ARGS(__VA_ARGS__) to get the number of variadic arguments
  306. #define _NUM_ARGS(_,n,m,l,k,j,i,h,g,f,e,d,c,b,a,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
  307. #define NUM_ARGS(V...) _NUM_ARGS(0,V,40,39,38,37,36,35,34,33,32,31,30,29,28,27,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)
  308. // Use TWO_ARGS(__VA_ARGS__) to get whether there are 1, 2, or >2 arguments
  309. #define _TWO_ARGS(_,n,m,l,k,j,i,h,g,f,e,d,c,b,a,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
  310. #define TWO_ARGS(V...) _TWO_ARGS(0,V,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,1,0)
  311. #ifdef __cplusplus
  312. #ifndef _MINMAX_H_
  313. #define _MINMAX_H_
  314. extern "C++" {
  315. // C++11 solution that is standards compliant. Return type is deduced automatically
  316. template <class L, class R> static inline constexpr auto _MIN(const L lhs, const R rhs) -> decltype(lhs + rhs) {
  317. return lhs < rhs ? lhs : rhs;
  318. }
  319. template <class L, class R> static inline constexpr auto _MAX(const L lhs, const R rhs) -> decltype(lhs + rhs) {
  320. return lhs > rhs ? lhs : rhs;
  321. }
  322. template<class T, class ... Ts> static inline constexpr const T _MIN(T V, Ts... Vs) { return _MIN(V, _MIN(Vs...)); }
  323. template<class T, class ... Ts> static inline constexpr const T _MAX(T V, Ts... Vs) { return _MAX(V, _MAX(Vs...)); }
  324. }
  325. #endif
  326. // Allow manipulating enumeration value like flags without ugly cast everywhere
  327. #define ENUM_FLAGS(T) \
  328. FORCE_INLINE constexpr T operator&(T x, T y) { return static_cast<T>(static_cast<int>(x) & static_cast<int>(y)); } \
  329. FORCE_INLINE constexpr T operator|(T x, T y) { return static_cast<T>(static_cast<int>(x) | static_cast<int>(y)); } \
  330. FORCE_INLINE constexpr T operator^(T x, T y) { return static_cast<T>(static_cast<int>(x) ^ static_cast<int>(y)); } \
  331. FORCE_INLINE constexpr T operator~(T x) { return static_cast<T>(~static_cast<int>(x)); } \
  332. FORCE_INLINE T & operator&=(T &x, T y) { return x &= y; } \
  333. FORCE_INLINE T & operator|=(T &x, T y) { return x |= y; } \
  334. FORCE_INLINE T & operator^=(T &x, T y) { return x ^= y; }
  335. // C++11 solution that is standard compliant. <type_traits> is not available on all platform
  336. namespace Private {
  337. template<bool, typename _Tp = void> struct enable_if { };
  338. template<typename _Tp> struct enable_if<true, _Tp> { typedef _Tp type; };
  339. template<typename T, typename U> struct is_same { enum { value = false }; };
  340. template<typename T> struct is_same<T, T> { enum { value = true }; };
  341. template <typename T, typename ... Args> struct first_type_of { typedef T type; };
  342. template <typename T> struct first_type_of<T> { typedef T type; };
  343. }
  344. // C++11 solution using SFINAE to detect the existence of a member in a class at compile time.
  345. // It creates a HasMember<Type> structure containing 'value' set to true if the member exists
  346. #define HAS_MEMBER_IMPL(Member) \
  347. namespace Private { \
  348. template <typename Type, typename Yes=char, typename No=long> struct HasMember_ ## Member { \
  349. template <typename C> static Yes& test( decltype(&C::Member) ) ; \
  350. template <typename C> static No& test(...); \
  351. enum { value = sizeof(test<Type>(0)) == sizeof(Yes) }; }; \
  352. }
  353. // Call the method if it exists, but do nothing if it does not. The method is detected at compile time.
  354. // If the method exists, this is inlined and does not cost anything. Else, an "empty" wrapper is created, returning a default value
  355. #define CALL_IF_EXISTS_IMPL(Return, Method, ...) \
  356. HAS_MEMBER_IMPL(Method) \
  357. namespace Private { \
  358. 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...)); } \
  359. _UNUSED static Return Call_ ## Method(...) { return __VA_ARGS__; } \
  360. }
  361. #define CALL_IF_EXISTS(Return, That, Method, ...) \
  362. static_cast<Return>(Private::Call_ ## Method(That, ##__VA_ARGS__))
  363. // Compile-time string manipulation
  364. namespace CompileTimeString {
  365. // Simple compile-time parser to find the position of the end of a string
  366. constexpr const char* findStringEnd(const char *str) {
  367. return *str ? findStringEnd(str + 1) : str;
  368. }
  369. // Check whether a string contains a specific character
  370. constexpr bool contains(const char *str, const char ch) {
  371. return *str == ch ? true : (*str ? contains(str + 1, ch) : false);
  372. }
  373. // Find the last position of the specific character (should be called with findStringEnd)
  374. constexpr const char* findLastPos(const char *str, const char ch) {
  375. return *str == ch ? (str + 1) : findLastPos(str - 1, ch);
  376. }
  377. // Compile-time evaluation of the last part of a file path
  378. // Typically used to shorten the path to file in compiled strings
  379. // CompileTimeString::baseName(__FILE__) returns "macros.h" and not /path/to/Marlin/src/core/macros.h
  380. constexpr const char* baseName(const char *str) {
  381. return contains(str, '/') ? findLastPos(findStringEnd(str), '/') : str;
  382. }
  383. // Find the first occurrence of a character in a string (or return the last position in the string)
  384. constexpr const char* findFirst(const char *str, const char ch) {
  385. return *str == ch || *str == 0 ? (str + 1) : findFirst(str + 1, ch);
  386. }
  387. // Compute the string length at compile time
  388. constexpr unsigned stringLen(const char *str) {
  389. return *str == 0 ? 0 : 1 + stringLen(str + 1);
  390. }
  391. }
  392. #define ONLY_FILENAME CompileTimeString::baseName(__FILE__)
  393. /** Get the templated type name. This does not depends on RTTI, but on the preprocessor, so it should be quite safe to use even on old compilers.
  394. WARNING: DO NOT RENAME THIS FUNCTION (or change the text inside the function to match what the preprocessor will generate)
  395. The name is chosen very short since the binary will store "const char* gtn(T*) [with T = YourTypeHere]" so avoid long function name here */
  396. template <typename T>
  397. inline const char* gtn(T*) {
  398. // It works on GCC by instantiating __PRETTY_FUNCTION__ and parsing the result. So the syntax here is very limited to GCC output
  399. constexpr unsigned verboseChatLen = sizeof("const char* gtn(T*) [with T = ") - 1;
  400. static char templateType[sizeof(__PRETTY_FUNCTION__) - verboseChatLen] = {};
  401. __builtin_memcpy(templateType, __PRETTY_FUNCTION__ + verboseChatLen, sizeof(__PRETTY_FUNCTION__) - verboseChatLen - 2);
  402. return templateType;
  403. }
  404. #else
  405. #define __MIN_N(N,V...) MIN_##N(V)
  406. #define _MIN_N(N,V...) __MIN_N(N,V)
  407. #define _MIN_N_REF() _MIN_N
  408. #define _MIN(V...) EVAL(_MIN_N(TWO_ARGS(V),V))
  409. #define MIN_2(a,b) ((a)<(b)?(a):(b))
  410. #define MIN_3(a,V...) MIN_2(a,DEFER2(_MIN_N_REF)()(TWO_ARGS(V),V))
  411. #define __MAX_N(N,V...) MAX_##N(V)
  412. #define _MAX_N(N,V...) __MAX_N(N,V)
  413. #define _MAX_N_REF() _MAX_N
  414. #define _MAX(V...) EVAL(_MAX_N(TWO_ARGS(V),V))
  415. #define MAX_2(a,b) ((a)>(b)?(a):(b))
  416. #define MAX_3(a,V...) MAX_2(a,DEFER2(_MAX_N_REF)()(TWO_ARGS(V),V))
  417. #endif
  418. // Macros for adding
  419. #define INC_0 1
  420. #define INC_1 2
  421. #define INC_2 3
  422. #define INC_3 4
  423. #define INC_4 5
  424. #define INC_5 6
  425. #define INC_6 7
  426. #define INC_7 8
  427. #define INC_8 9
  428. #define INC_9 10
  429. #define INC_10 11
  430. #define INC_11 12
  431. #define INC_12 13
  432. #define INC_13 14
  433. #define INC_14 15
  434. #define INC_15 16
  435. #define INCREMENT_(n) INC_##n
  436. #define INCREMENT(n) INCREMENT_(n)
  437. #define ADD0(N) N
  438. #define ADD1(N) INCREMENT_(N)
  439. #define ADD2(N) ADD1(ADD1(N))
  440. #define ADD3(N) ADD1(ADD2(N))
  441. #define ADD4(N) ADD2(ADD2(N))
  442. #define ADD5(N) ADD2(ADD3(N))
  443. #define ADD6(N) ADD3(ADD3(N))
  444. #define ADD7(N) ADD3(ADD4(N))
  445. #define ADD8(N) ADD4(ADD4(N))
  446. #define ADD9(N) ADD4(ADD5(N))
  447. #define ADD10(N) ADD5(ADD5(N))
  448. #define SUM(A,B) _CAT(ADD,A)(B)
  449. #define DOUBLE_(n) ADD##n(n)
  450. #define DOUBLE(n) DOUBLE_(n)
  451. // Macros for subtracting
  452. #define DEC_0 0
  453. #define DEC_1 0
  454. #define DEC_2 1
  455. #define DEC_3 2
  456. #define DEC_4 3
  457. #define DEC_5 4
  458. #define DEC_6 5
  459. #define DEC_7 6
  460. #define DEC_8 7
  461. #define DEC_9 8
  462. #define DEC_10 9
  463. #define DEC_11 10
  464. #define DEC_12 11
  465. #define DEC_13 12
  466. #define DEC_14 13
  467. #define DEC_15 14
  468. #define DECREMENT_(n) DEC_##n
  469. #define DECREMENT(n) DECREMENT_(n)
  470. #define SUB0(N) N
  471. #define SUB1(N) DECREMENT_(N)
  472. #define SUB2(N) SUB1(SUB1(N))
  473. #define SUB3(N) SUB1(SUB2(N))
  474. #define SUB4(N) SUB2(SUB2(N))
  475. #define SUB5(N) SUB2(SUB3(N))
  476. #define SUB6(N) SUB3(SUB3(N))
  477. #define SUB7(N) SUB3(SUB4(N))
  478. #define SUB8(N) SUB4(SUB4(N))
  479. #define SUB9(N) SUB4(SUB5(N))
  480. #define SUB10(N) SUB5(SUB5(N))
  481. //
  482. // Primitives supporting precompiler REPEAT
  483. //
  484. #define FIRST(a,...) a
  485. #define SECOND(a,b,...) b
  486. #define THIRD(a,b,c,...) c
  487. // Defer expansion
  488. #define EMPTY()
  489. #define DEFER(M) M EMPTY()
  490. #define DEFER2(M) M EMPTY EMPTY()()
  491. #define DEFER3(M) M EMPTY EMPTY EMPTY()()()
  492. #define DEFER4(M) M EMPTY EMPTY EMPTY EMPTY()()()()
  493. // Force define expansion
  494. #define EVAL(V...) EVAL16(V)
  495. #define EVAL1024(V...) EVAL512(EVAL512(V))
  496. #define EVAL512(V...) EVAL256(EVAL256(V))
  497. #define EVAL256(V...) EVAL128(EVAL128(V))
  498. #define EVAL128(V...) EVAL64(EVAL64(V))
  499. #define EVAL64(V...) EVAL32(EVAL32(V))
  500. #define EVAL32(V...) EVAL16(EVAL16(V))
  501. #define EVAL16(V...) EVAL8(EVAL8(V))
  502. #define EVAL8(V...) EVAL4(EVAL4(V))
  503. #define EVAL4(V...) EVAL2(EVAL2(V))
  504. #define EVAL2(V...) EVAL1(EVAL1(V))
  505. #define EVAL1(V...) V
  506. #define IS_PROBE(V...) SECOND(V, 0) // Get the second item passed, or 0
  507. #define PROBE() ~, 1 // Second item will be 1 if this is passed
  508. #define _NOT_0 PROBE()
  509. #define NOT(x) IS_PROBE(_CAT(_NOT_, x)) // NOT('0') gets '1'. Anything else gets '0'.
  510. #define _BOOL(x) NOT(NOT(x)) // NOT('0') gets '0'. Anything else gets '1'.
  511. #define IF_ELSE(TF) _IF_ELSE(_BOOL(TF))
  512. #define _IF_ELSE(TF) _CAT(_IF_, TF)
  513. #define _IF_1(V...) V _IF_1_ELSE
  514. #define _IF_0(...) _IF_0_ELSE
  515. #define _IF_1_ELSE(...)
  516. #define _IF_0_ELSE(V...) V
  517. #define HAS_ARGS(V...) _BOOL(FIRST(_END_OF_ARGUMENTS_ V)())
  518. #define _END_OF_ARGUMENTS_() 0
  519. // Simple Inline IF Macros, friendly to use in other macro definitions
  520. #define IF(O, A, B) ((O) ? (A) : (B))
  521. #define IF_0(O, A) IF(O, A, 0)
  522. #define IF_1(O, A) IF(O, A, 1)
  523. //
  524. // REPEAT core macros. Recurse N times with ascending I.
  525. //
  526. // Call OP(I) N times with ascending counter.
  527. #define _REPEAT(_RPT_I,_RPT_N,_RPT_OP) \
  528. _RPT_OP(_RPT_I) \
  529. IF_ELSE(SUB1(_RPT_N)) \
  530. ( DEFER2(__REPEAT)()(ADD1(_RPT_I),SUB1(_RPT_N),_RPT_OP) ) \
  531. ( /* Do nothing */ )
  532. #define __REPEAT() _REPEAT
  533. // Call OP(I, ...) N times with ascending counter.
  534. #define _REPEAT2(_RPT_I,_RPT_N,_RPT_OP,V...) \
  535. _RPT_OP(_RPT_I,V) \
  536. IF_ELSE(SUB1(_RPT_N)) \
  537. ( DEFER2(__REPEAT2)()(ADD1(_RPT_I),SUB1(_RPT_N),_RPT_OP,V) ) \
  538. ( /* Do nothing */ )
  539. #define __REPEAT2() _REPEAT2
  540. // Repeat a macro passing S...N-1.
  541. #define REPEAT_S(S,N,OP) EVAL(_REPEAT(S,SUB##S(N),OP))
  542. #define REPEAT(N,OP) REPEAT_S(0,N,OP)
  543. #define REPEAT_1(N,OP) REPEAT_S(1,INCREMENT(N),OP)
  544. // Repeat a macro passing 0...N-1 plus additional arguments.
  545. #define REPEAT2_S(S,N,OP,V...) EVAL(_REPEAT2(S,SUB##S(N),OP,V))
  546. #define REPEAT2(N,OP,V...) REPEAT2_S(0,N,OP,V)
  547. // Use RREPEAT macros with REPEAT macros for nesting
  548. #define _RREPEAT(_RPT_I,_RPT_N,_RPT_OP) \
  549. _RPT_OP(_RPT_I) \
  550. IF_ELSE(SUB1(_RPT_N)) \
  551. ( DEFER2(__RREPEAT)()(ADD1(_RPT_I),SUB1(_RPT_N),_RPT_OP) ) \
  552. ( /* Do nothing */ )
  553. #define __RREPEAT() _RREPEAT
  554. #define _RREPEAT2(_RPT_I,_RPT_N,_RPT_OP,V...) \
  555. _RPT_OP(_RPT_I,V) \
  556. IF_ELSE(SUB1(_RPT_N)) \
  557. ( DEFER2(__RREPEAT2)()(ADD1(_RPT_I),SUB1(_RPT_N),_RPT_OP,V) ) \
  558. ( /* Do nothing */ )
  559. #define __RREPEAT2() _RREPEAT2
  560. #define RREPEAT_S(S,N,OP) EVAL1024(_RREPEAT(S,SUB##S(N),OP))
  561. #define RREPEAT(N,OP) RREPEAT_S(0,N,OP)
  562. #define RREPEAT2_S(S,N,OP,V...) EVAL1024(_RREPEAT2(S,SUB##S(N),OP,V))
  563. #define RREPEAT2(N,OP,V...) RREPEAT2_S(0,N,OP,V)
  564. // See https://github.com/swansontec/map-macro
  565. #define MAP_OUT
  566. #define MAP_END(...)
  567. #define MAP_GET_END() 0, MAP_END
  568. #define MAP_NEXT0(test, next, ...) next MAP_OUT
  569. #define MAP_NEXT1(test, next) MAP_NEXT0 (test, next, 0)
  570. #define MAP_NEXT(test, next) MAP_NEXT1 (MAP_GET_END test, next)
  571. #define MAP0(f, x, peek, ...) f(x) MAP_NEXT (peek, MAP1) (f, peek, __VA_ARGS__)
  572. #define MAP1(f, x, peek, ...) f(x) MAP_NEXT (peek, MAP0) (f, peek, __VA_ARGS__)
  573. #define MAP(f, ...) EVAL512 (MAP1 (f, __VA_ARGS__, (), 0))