瀏覽代碼

Replace MIN# / MAX# with variadic MIN / MAX (#11960)

AnoNymous 6 年之前
父節點
當前提交
b30ca652ae

+ 5
- 46
Marlin/src/core/macros.h 查看文件

20
  *
20
  *
21
  */
21
  */
22
 
22
 
23
-#ifndef _CORE_MACROS_H_
24
-#define _CORE_MACROS_H_
23
+#pragma once
24
+
25
+#include "minmax.h"
25
 
26
 
26
 #define NUM_AXIS 4
27
 #define NUM_AXIS 4
27
 #define ABCE 4
28
 #define ABCE 4
93
 #define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))
94
 #define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))
94
 
95
 
95
 // Macros to constrain values
96
 // Macros to constrain values
96
-// Avoid double evaluation of arguments to NOMORE/NOLESS/LIMIT
97
-#undef NOMORE
98
-#undef NOLESS
99
-#undef LIMIT
100
 #ifdef __cplusplus
97
 #ifdef __cplusplus
101
 
98
 
102
   // C++11 solution that is standards compliant.
99
   // C++11 solution that is standards compliant.
207
 
204
 
208
 #define CEILING(x,y) (((x) + (y) - 1) / (y))
205
 #define CEILING(x,y) (((x) + (y) - 1) / (y))
209
 
206
 
210
-// Avoid double evaluation of arguments on MIN/MAX/ABS
211
-#undef MIN
212
-#undef MAX
213
 #undef ABS
207
 #undef ABS
214
 #ifdef __cplusplus
208
 #ifdef __cplusplus
215
-
216
-  // C++11 solution that is standards compliant. Return type is deduced automatically
217
-  template <class L, class R> static inline constexpr auto MIN(const L lhs, const R rhs) -> decltype(lhs + rhs) {
218
-    return lhs < rhs ? lhs : rhs;
219
-  }
220
-  template <class L, class R> static inline constexpr auto MAX(const L lhs, const R rhs) -> decltype(lhs + rhs){
221
-    return lhs > rhs ? lhs : rhs;
222
-  }
223
-  template <class T> static inline constexpr const T ABS(const T v) {
224
-    return v >= 0 ? v : -v;
225
-  }
209
+  template <class T> static inline constexpr const T ABS(const T v) { return v >= 0 ? v : -v; }
226
 #else
210
 #else
227
-
228
-  // Using GCC extensions, but Travis GCC version does not like it and gives
229
-  //  "error: statement-expressions are not allowed outside functions nor in template-argument lists"
230
-  #define MIN(a, b) \
231
-    ({__typeof__(a) _a = (a); \
232
-      __typeof__(b) _b = (b); \
233
-      _a < _b ? _a : _b;})
234
-
235
-  #define MAX(a, b) \
236
-    ({__typeof__(a) _a = (a); \
237
-      __typeof__(b) _b = (b); \
238
-      _a > _b ? _a : _b;})
239
-
240
-  #define ABS(a) \
241
-    ({__typeof__(a) _a = (a); \
242
-      _a >= 0 ? _a : -_a;})
243
-
211
+  #define ABS(a) ({__typeof__(a) _a = (a); _a >= 0 ? _a : -_a;})
244
 #endif
212
 #endif
245
 
213
 
246
-#define MIN3(a, b, c)       MIN(MIN(a, b), c)
247
-#define MIN4(a, b, c, d)    MIN(MIN3(a, b, c), d)
248
-#define MIN5(a, b, c, d, e) MIN(MIN4(a, b, c, d), e)
249
-#define MAX3(a, b, c)       MAX(MAX(a, b), c)
250
-#define MAX4(a, b, c, d)    MAX(MAX3(a, b, c), d)
251
-#define MAX5(a, b, c, d, e) MAX(MAX4(a, b, c, d), e)
252
-
253
 #define UNEAR_ZERO(x) ((x) < 0.000001f)
214
 #define UNEAR_ZERO(x) ((x) < 0.000001f)
254
 #define NEAR_ZERO(x) WITHIN(x, -0.000001f, 0.000001f)
215
 #define NEAR_ZERO(x) WITHIN(x, -0.000001f, 0.000001f)
255
 #define NEAR(x,y) NEAR_ZERO((x)-(y))
216
 #define NEAR(x,y) NEAR_ZERO((x)-(y))
269
 #define LROUND(x)   lroundf(x)
230
 #define LROUND(x)   lroundf(x)
270
 #define FMOD(x, y)  fmodf(x, y)
231
 #define FMOD(x, y)  fmodf(x, y)
271
 #define HYPOT(x,y)  SQRT(HYPOT2(x,y))
232
 #define HYPOT(x,y)  SQRT(HYPOT2(x,y))
272
-
273
-#endif // _CORE_MACROS_H_

+ 68
- 0
Marlin/src/core/minmax.h 查看文件

1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 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
+
23
+#pragma once
24
+
25
+#undef MIN
26
+#undef MAX
27
+
28
+#ifdef __cplusplus
29
+
30
+  extern "C++" {
31
+
32
+    // C++11 solution that is standards compliant. Return type is deduced automatically
33
+    template <class L, class R> static inline constexpr auto MIN(const L lhs, const R rhs) -> decltype(lhs + rhs) {
34
+      return lhs < rhs ? lhs : rhs;
35
+    }
36
+    template <class L, class R> static inline constexpr auto MAX(const L lhs, const R rhs) -> decltype(lhs + rhs) {
37
+      return lhs > rhs ? lhs : rhs;
38
+    }
39
+    template<class T, class ... Ts> static inline constexpr const T MIN(T V, Ts... Vs) { return MIN(V, MIN(Vs...)); }
40
+    template<class T, class ... Ts> static inline constexpr const T MAX(T V, Ts... Vs) { return MAX(V, MAX(Vs...)); }
41
+
42
+  }
43
+
44
+#else
45
+
46
+  // NUM_ARGS(...) evaluates to the number of arguments
47
+  #define _NUM_ARGS(X,X6,X5,X4,X3,X2,X1,N,...) N
48
+  #define NUM_ARGS(...) _NUM_ARGS(0, __VA_ARGS__ ,6,5,4,3,2,1,0)
49
+
50
+  #define MIN_2(a,b)      ({__typeof__(a) _a = (a); __typeof__(b) _b = (b); _a > _b ? _a : _b;})
51
+  #define MIN_3(a,...)    MIN_2(a,MIN_2(__VA_ARGS__))
52
+  #define MIN_4(a,...)    MIN_2(a,MIN_3(__VA_ARGS__))
53
+  #define MIN_5(a,...)    MIN_2(a,MIN_4(__VA_ARGS__))
54
+  #define MIN_6(a,...)    MIN_2(a,MIN_4(__VA_ARGS__))
55
+  #define __MIN_N(N, ...) MIN_ ## N(__VA_ARGS__)
56
+  #define _MIN_N(N, ...)  __MIN_N(N, __VA_ARGS__)
57
+  #define MIN(...)        _MIN_N(NUM_ARGS(__VA_ARGS__), __VA_ARGS__)
58
+
59
+  #define MAX_2(a,b)      ({__typeof__(a) _a = (a); __typeof__(b) _b = (b); _a > _b ? _a : _b;})
60
+  #define MAX_3(a,...)    MAX_2(a,MAX_2(__VA_ARGS__))
61
+  #define MAX_4(a,...)    MAX_2(a,MAX_3(__VA_ARGS__))
62
+  #define MAX_5(a,...)    MAX_2(a,MAX_4(__VA_ARGS__))
63
+  #define MAX_6(a,...)    MAX_2(a,MAX_4(__VA_ARGS__))
64
+  #define __MAX_N(N, ...) MAX_ ## N(__VA_ARGS__)
65
+  #define _MAX_N(N, ...)  __MAX_N(N, __VA_ARGS__)
66
+  #define MAX(...)        _MAX_N(NUM_ARGS(__VA_ARGS__), __VA_ARGS__)
67
+
68
+#endif

+ 2
- 2
Marlin/src/feature/leds/tempstat.cpp 查看文件

38
     next_status_led_update_ms += 500; // Update every 0.5s
38
     next_status_led_update_ms += 500; // Update every 0.5s
39
     float max_temp = 0.0;
39
     float max_temp = 0.0;
40
     #if HAS_HEATED_BED
40
     #if HAS_HEATED_BED
41
-      max_temp = MAX3(max_temp, thermalManager.degTargetBed(), thermalManager.degBed());
41
+      max_temp = MAX(max_temp, thermalManager.degTargetBed(), thermalManager.degBed());
42
     #endif
42
     #endif
43
     HOTEND_LOOP()
43
     HOTEND_LOOP()
44
-      max_temp = MAX3(max_temp, thermalManager.degHotend(e), thermalManager.degTargetHotend(e));
44
+      max_temp = MAX(max_temp, thermalManager.degHotend(e), thermalManager.degTargetHotend(e));
45
     const bool new_led = (max_temp > 55.0) ? true : (max_temp < 54.0) ? false : red_led;
45
     const bool new_led = (max_temp > 55.0) ? true : (max_temp < 54.0) ? false : red_led;
46
     if (new_led != red_led) {
46
     if (new_led != red_led) {
47
       red_led = new_led;
47
       red_led = new_led;

+ 1
- 1
Marlin/src/gcode/calibrate/G33.cpp 查看文件

651
       }
651
       }
652
 
652
 
653
       // adjust delta_height and endstops by the max amount
653
       // adjust delta_height and endstops by the max amount
654
-      const float z_temp = MAX3(delta_endstop_adj[A_AXIS], delta_endstop_adj[B_AXIS], delta_endstop_adj[C_AXIS]);
654
+      const float z_temp = MAX(delta_endstop_adj[A_AXIS], delta_endstop_adj[B_AXIS], delta_endstop_adj[C_AXIS]);
655
       delta_height -= z_temp;
655
       delta_height -= z_temp;
656
       LOOP_XYZ(axis) delta_endstop_adj[axis] -= z_temp;
656
       LOOP_XYZ(axis) delta_endstop_adj[axis] -= z_temp;
657
     }
657
     }

+ 1
- 1
Marlin/src/inc/Conditionals_post.h 查看文件

1255
   #define _PROBE_RADIUS (DELTA_PRINTABLE_RADIUS - (MIN_PROBE_EDGE))
1255
   #define _PROBE_RADIUS (DELTA_PRINTABLE_RADIUS - (MIN_PROBE_EDGE))
1256
   #ifndef DELTA_CALIBRATION_RADIUS
1256
   #ifndef DELTA_CALIBRATION_RADIUS
1257
     #ifdef X_PROBE_OFFSET_FROM_EXTRUDER
1257
     #ifdef X_PROBE_OFFSET_FROM_EXTRUDER
1258
-      #define DELTA_CALIBRATION_RADIUS (DELTA_PRINTABLE_RADIUS - MAX3(abs(X_PROBE_OFFSET_FROM_EXTRUDER), abs(Y_PROBE_OFFSET_FROM_EXTRUDER), abs(MIN_PROBE_EDGE)))
1258
+      #define DELTA_CALIBRATION_RADIUS (DELTA_PRINTABLE_RADIUS - MAX(ABS(X_PROBE_OFFSET_FROM_EXTRUDER), ABS(Y_PROBE_OFFSET_FROM_EXTRUDER), ABS(MIN_PROBE_EDGE)))
1259
     #else
1259
     #else
1260
       #define DELTA_CALIBRATION_RADIUS _PROBE_RADIUS
1260
       #define DELTA_CALIBRATION_RADIUS _PROBE_RADIUS
1261
     #endif
1261
     #endif

+ 8
- 8
Marlin/src/lcd/ultralcd.cpp 查看文件

3697
 
3697
 
3698
     void _lcd_configuration_temperature_preheat_settings_menu(const uint8_t material) {
3698
     void _lcd_configuration_temperature_preheat_settings_menu(const uint8_t material) {
3699
       #if HOTENDS > 5
3699
       #if HOTENDS > 5
3700
-        #define MINTEMP_ALL MIN5(HEATER_0_MINTEMP, HEATER_1_MINTEMP, HEATER_2_MINTEMP, HEATER_3_MINTEMP, HEATER_4_MINTEMP, HEATER_5_MINTEMP)
3701
-        #define MAXTEMP_ALL MAX5(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP, HEATER_5_MAXTEMP)
3700
+        #define MINTEMP_ALL MIN(HEATER_0_MINTEMP, HEATER_1_MINTEMP, HEATER_2_MINTEMP, HEATER_3_MINTEMP, HEATER_4_MINTEMP, HEATER_5_MINTEMP)
3701
+        #define MAXTEMP_ALL MAX(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP, HEATER_5_MAXTEMP)
3702
       #elif HOTENDS > 4
3702
       #elif HOTENDS > 4
3703
-        #define MINTEMP_ALL MIN5(HEATER_0_MINTEMP, HEATER_1_MINTEMP, HEATER_2_MINTEMP, HEATER_3_MINTEMP, HEATER_4_MINTEMP)
3704
-        #define MAXTEMP_ALL MAX5(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP)
3703
+        #define MINTEMP_ALL MIN(HEATER_0_MINTEMP, HEATER_1_MINTEMP, HEATER_2_MINTEMP, HEATER_3_MINTEMP, HEATER_4_MINTEMP)
3704
+        #define MAXTEMP_ALL MAX(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP)
3705
       #elif HOTENDS > 3
3705
       #elif HOTENDS > 3
3706
-        #define MINTEMP_ALL MIN4(HEATER_0_MINTEMP, HEATER_1_MINTEMP, HEATER_2_MINTEMP, HEATER_3_MINTEMP)
3707
-        #define MAXTEMP_ALL MAX4(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP)
3706
+        #define MINTEMP_ALL MIN(HEATER_0_MINTEMP, HEATER_1_MINTEMP, HEATER_2_MINTEMP, HEATER_3_MINTEMP)
3707
+        #define MAXTEMP_ALL MAX(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP)
3708
       #elif HOTENDS > 2
3708
       #elif HOTENDS > 2
3709
-        #define MINTEMP_ALL MIN3(HEATER_0_MINTEMP, HEATER_1_MINTEMP, HEATER_2_MINTEMP)
3710
-        #define MAXTEMP_ALL MAX3(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP)
3709
+        #define MINTEMP_ALL MIN(HEATER_0_MINTEMP, HEATER_1_MINTEMP, HEATER_2_MINTEMP)
3710
+        #define MAXTEMP_ALL MAX(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP)
3711
       #elif HOTENDS > 1
3711
       #elif HOTENDS > 1
3712
         #define MINTEMP_ALL MIN(HEATER_0_MINTEMP, HEATER_1_MINTEMP)
3712
         #define MINTEMP_ALL MIN(HEATER_0_MINTEMP, HEATER_1_MINTEMP)
3713
         #define MAXTEMP_ALL MAX(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP)
3713
         #define MAXTEMP_ALL MAX(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP)

+ 1
- 1
Marlin/src/module/motion.cpp 查看文件

1541
           case X_AXIS:
1541
           case X_AXIS:
1542
           case Y_AXIS:
1542
           case Y_AXIS:
1543
             // Get a minimum radius for clamping
1543
             // Get a minimum radius for clamping
1544
-            soft_endstop_radius = MIN3(ABS(MAX(soft_endstop_min[X_AXIS], soft_endstop_min[Y_AXIS])), soft_endstop_max[X_AXIS], soft_endstop_max[Y_AXIS]);
1544
+            soft_endstop_radius = MIN(ABS(MAX(soft_endstop_min[X_AXIS], soft_endstop_min[Y_AXIS])), soft_endstop_max[X_AXIS], soft_endstop_max[Y_AXIS]);
1545
             soft_endstop_radius_2 = sq(soft_endstop_radius);
1545
             soft_endstop_radius_2 = sq(soft_endstop_radius);
1546
             break;
1546
             break;
1547
         #endif
1547
         #endif

+ 4
- 4
Marlin/src/module/planner.cpp 查看文件

1762
   #endif
1762
   #endif
1763
 
1763
 
1764
   block->steps[E_AXIS] = esteps;
1764
   block->steps[E_AXIS] = esteps;
1765
-  block->step_event_count = MAX4(block->steps[A_AXIS], block->steps[B_AXIS], block->steps[C_AXIS], esteps);
1765
+  block->step_event_count = MAX(block->steps[A_AXIS], block->steps[B_AXIS], block->steps[C_AXIS], esteps);
1766
 
1766
 
1767
   // Bail if this is a zero-length block
1767
   // Bail if this is a zero-length block
1768
   if (block->step_event_count < MIN_STEPS_PER_SEGMENT) return false;
1768
   if (block->step_event_count < MIN_STEPS_PER_SEGMENT) return false;
2120
     }
2120
     }
2121
     ys0 = axis_segment_time_us[Y_AXIS][0] = ys0 + segment_time_us;
2121
     ys0 = axis_segment_time_us[Y_AXIS][0] = ys0 + segment_time_us;
2122
 
2122
 
2123
-    const uint32_t max_x_segment_time = MAX3(xs0, xs1, xs2),
2124
-                   max_y_segment_time = MAX3(ys0, ys1, ys2),
2123
+    const uint32_t max_x_segment_time = MAX(xs0, xs1, xs2),
2124
+                   max_y_segment_time = MAX(ys0, ys1, ys2),
2125
                    min_xy_segment_time = MIN(max_x_segment_time, max_y_segment_time);
2125
                    min_xy_segment_time = MIN(max_x_segment_time, max_y_segment_time);
2126
     if (min_xy_segment_time < MAX_FREQ_TIME_US) {
2126
     if (min_xy_segment_time < MAX_FREQ_TIME_US) {
2127
       const float low_sf = speed_factor * min_xy_segment_time / (MAX_FREQ_TIME_US);
2127
       const float low_sf = speed_factor * min_xy_segment_time / (MAX_FREQ_TIME_US);
2354
       }
2354
       }
2355
 
2355
 
2356
       // Get the lowest speed
2356
       // Get the lowest speed
2357
-      vmax_junction_sqr = MIN3(vmax_junction_sqr, block->nominal_speed_sqr, previous_nominal_speed_sqr);
2357
+      vmax_junction_sqr = MIN(vmax_junction_sqr, block->nominal_speed_sqr, previous_nominal_speed_sqr);
2358
     }
2358
     }
2359
     else // Init entry speed to zero. Assume it starts from rest. Planner will correct this later.
2359
     else // Init entry speed to zero. Assume it starts from rest. Planner will correct this later.
2360
       vmax_junction_sqr = 0;
2360
       vmax_junction_sqr = 0;

+ 1
- 6
frameworks/CMSIS/LPC1768/include/lpc_types.h 查看文件

145
 /* External data/function define */
145
 /* External data/function define */
146
 #define EXTERN extern
146
 #define EXTERN extern
147
 
147
 
148
-#ifndef MAX
149
-  #define MAX(a, b) (((a) > (b)) ? (a) : (b))
150
-#endif
151
-#ifndef MIN
152
-  #define MIN(a, b) (((a) < (b)) ? (a) : (b))
153
-#endif
148
+#include "../../../../src/core/minmax.h"
154
 
149
 
155
 /**
150
 /**
156
  * @}
151
  * @}

Loading…
取消
儲存