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

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