Browse Source

Macros to eliminate 'f + 0.0' (#21568)

Ramiro Polla 4 years ago
parent
commit
1a2cbe100c
No account linked to committer's email address

+ 11
- 4
Marlin/src/core/macros.h View File

@@ -187,14 +187,21 @@
187 187
 #define DISABLED(V...)      DO(DIS,&&,V)
188 188
 #define COUNT_ENABLED(V...) DO(ENA,+,V)
189 189
 
190
-#define TERN(O,A,B)         _TERN(_ENA_1(O),B,A)    // OPTION converted to '0' or '1'
191
-#define TERN0(O,A)          _TERN(_ENA_1(O),0,A)    // OPTION converted to A or '0'
192
-#define TERN1(O,A)          _TERN(_ENA_1(O),1,A)    // OPTION converted to A or '1'
193
-#define TERN_(O,A)          _TERN(_ENA_1(O),,A)     // OPTION converted to A or '<nul>'
190
+#define TERN(O,A,B)         _TERN(_ENA_1(O),B,A)    // OPTION ? 'A' : 'B'
191
+#define TERN0(O,A)          _TERN(_ENA_1(O),0,A)    // OPTION ? 'A' : '0'
192
+#define TERN1(O,A)          _TERN(_ENA_1(O),1,A)    // OPTION ? 'A' : '1'
193
+#define TERN_(O,A)          _TERN(_ENA_1(O),,A)     // OPTION ? 'A' : '<nul>'
194 194
 #define _TERN(E,V...)       __TERN(_CAT(T_,E),V)    // Prepend 'T_' to get 'T_0' or 'T_1'
195 195
 #define __TERN(T,V...)      ___TERN(_CAT(_NO,T),V)  // Prepend '_NO' to get '_NOT_0' or '_NOT_1'
196 196
 #define ___TERN(P,V...)     THIRD(P,V)              // If first argument has a comma, A. Else B.
197 197
 
198
+// Macros to avoid 'f + 0.0' which is not always optimized away. Minus included for symmetry.
199
+// Compiler flags -fno-signed-zeros -ffinite-math-only also cover 'f * 1.0', 'f - f', etc.
200
+#define PLUS_TERN0(O,A)     _TERN(_ENA_1(O),,+ (A)) // OPTION ? '+ (A)' : '<nul>'
201
+#define MINUS_TERN0(O,A)    _TERN(_ENA_1(O),,- (A)) // OPTION ? '- (A)' : '<nul>'
202
+#define SUM_TERN(O,B,A)     ((B) PLUS_TERN0(O,A))   // ((B) (OPTION ? '+ (A)' : '<nul>'))
203
+#define DIFF_TERN(O,B,A)    ((B) MINUS_TERN0(O,A))  // ((B) (OPTION ? '- (A)' : '<nul>'))
204
+
198 205
 #define IF_ENABLED          TERN_
199 206
 #define IF_DISABLED(O,A)    TERN(O,,A)
200 207
 

+ 1
- 1
Marlin/src/gcode/bedlevel/G35.cpp View File

@@ -102,7 +102,7 @@ void GcodeSuite::G35() {
102 102
     // In BLTOUCH HS mode, the probe travels in a deployed state.
103 103
     // Users of G35 might have a badly misaligned bed, so raise Z by the
104 104
     // length of the deployed pin (BLTOUCH stroke < 7mm)
105
-    do_blocking_move_to_z((Z_CLEARANCE_BETWEEN_PROBES) + TERN0(BLTOUCH_HS_MODE, 7));
105
+    do_blocking_move_to_z(SUM_TERN(BLTOUCH_HS_MODE, Z_CLEARANCE_BETWEEN_PROBES, 7));
106 106
     const float z_probed_height = probe.probe_at_point(screws_tilt_adjust_pos[i], PROBE_PT_RAISE, 0, true);
107 107
 
108 108
     if (isnan(z_probed_height)) {

+ 1
- 1
Marlin/src/lcd/menu/menu_bed_corners.cpp View File

@@ -269,7 +269,7 @@ static inline void _lcd_level_bed_corners_get_next_position() {
269 269
     do {
270 270
       ui.refresh(LCDVIEW_REDRAW_NOW);
271 271
       _lcd_draw_probing();                                             // update screen with # of good points
272
-      do_blocking_move_to_z(current_position.z + LEVEL_CORNERS_Z_HOP + TERN0(BLTOUCH_HS_MODE, 7)); // clearance
272
+      do_blocking_move_to_z(SUM_TERN(BLTOUCH_HS_MODE, current_position.z + LEVEL_CORNERS_Z_HOP, 7)); // clearance
273 273
 
274 274
       _lcd_level_bed_corners_get_next_position();         // Select next corner coordinates
275 275
       current_position -= probe.offset_xy;                // Account for probe offsets

+ 3
- 3
Marlin/src/lcd/menu/menu_motion.cpp View File

@@ -93,7 +93,7 @@ static void _lcd_move_xyz(PGM_P const name, const AxisEnum axis) {
93 93
   ui.encoderPosition = 0;
94 94
   if (ui.should_draw()) {
95 95
     const float pos = NATIVE_TO_LOGICAL(
96
-      ui.manual_move.processing ? destination[axis] : current_position[axis] + TERN0(IS_KINEMATIC, ui.manual_move.offset),
96
+      ui.manual_move.processing ? destination[axis] : SUM_TERN(IS_KINEMATIC, current_position[axis], ui.manual_move.offset),
97 97
       axis
98 98
     );
99 99
     if (parser.using_inch_units()) {
@@ -130,8 +130,8 @@ void lcd_move_z() { _lcd_move_xyz(GET_TEXT(MSG_MOVE_Z), Z_AXIS); }
130 130
       MenuEditItemBase::draw_edit_screen(
131 131
         GET_TEXT(TERN(MULTI_MANUAL, MSG_MOVE_EN, MSG_MOVE_E)),
132 132
         ftostr41sign(current_position.e
133
-          + TERN0(IS_KINEMATIC, ui.manual_move.offset)
134
-          - TERN0(MANUAL_E_MOVES_RELATIVE, manual_move_e_origin)
133
+          PLUS_TERN0(IS_KINEMATIC, ui.manual_move.offset)
134
+          MINUS_TERN0(MANUAL_E_MOVES_RELATIVE, manual_move_e_origin)
135 135
         )
136 136
       );
137 137
     } // should_draw

+ 1
- 1
Marlin/src/lcd/tft/ui_480x320.cpp View File

@@ -610,7 +610,7 @@ static void drawAxisValue(AxisEnum axis) {
610 610
       probe.offset.z :
611 611
     #endif
612 612
     NATIVE_TO_LOGICAL(
613
-      ui.manual_move.processing ? destination[axis] : current_position[axis] + TERN0(IS_KINEMATIC, ui.manual_move.offset),
613
+      ui.manual_move.processing ? destination[axis] : SUM_TERN(IS_KINEMATIC, current_position[axis], ui.manual_move.offset),
614 614
       axis
615 615
     );
616 616
   xy_int_t pos;

+ 1
- 1
Marlin/src/module/delta.cpp View File

@@ -248,7 +248,7 @@ void home_delta() {
248 248
   #endif
249 249
 
250 250
   // Move all carriages together linearly until an endstop is hit.
251
-  current_position.z = (delta_height + 10 - TERN0(HAS_BED_PROBE, probe.offset.z));
251
+  current_position.z = DIFF_TERN(HAS_BED_PROBE, delta_height + 10, probe.offset.z);
252 252
   line_to_current_position(homing_feedrate(Z_AXIS));
253 253
   planner.synchronize();
254 254
 

+ 2
- 2
Marlin/src/module/motion.cpp View File

@@ -583,7 +583,7 @@ void restore_feedrate_and_scaling() {
583 583
     #elif ENABLED(DELTA)
584 584
 
585 585
       soft_endstop.min[axis] = base_min_pos(axis);
586
-      soft_endstop.max[axis] = (axis == Z_AXIS) ? delta_height - TERN0(HAS_BED_PROBE, probe.offset.z) : base_max_pos(axis);
586
+      soft_endstop.max[axis] = (axis == Z_AXIS) ? DIFF_TERN(HAS_BED_PROBE, delta_height, probe.offset.z) : base_max_pos(axis);
587 587
 
588 588
       switch (axis) {
589 589
         case X_AXIS:
@@ -1847,7 +1847,7 @@ void set_axis_is_at_home(const AxisEnum axis) {
1847 1847
   #if EITHER(MORGAN_SCARA, AXEL_TPARA)
1848 1848
     scara_set_axis_is_at_home(axis);
1849 1849
   #elif ENABLED(DELTA)
1850
-    current_position[axis] = (axis == Z_AXIS) ? delta_height - TERN0(HAS_BED_PROBE, probe.offset.z) : base_home_pos(axis);
1850
+    current_position[axis] = (axis == Z_AXIS) ? DIFF_TERN(HAS_BED_PROBE, delta_height, probe.offset.z) : base_home_pos(axis);
1851 1851
   #else
1852 1852
     current_position[axis] = base_home_pos(axis);
1853 1853
   #endif

+ 1
- 1
Marlin/src/module/planner.cpp View File

@@ -2997,7 +2997,7 @@ void Planner::set_e_position_mm(const_float_t e) {
2997 2997
   const uint8_t axis_index = E_AXIS_N(active_extruder);
2998 2998
   TERN_(DISTINCT_E_FACTORS, last_extruder = active_extruder);
2999 2999
 
3000
-  const float e_new = e - TERN0(FWRETRACT, fwretract.current_retract[active_extruder]);
3000
+  const float e_new = DIFF_TERN(FWRETRACT, e, fwretract.current_retract[active_extruder]);
3001 3001
   position.e = LROUND(settings.axis_steps_per_mm[axis_index] * e_new);
3002 3002
   TERN_(HAS_POSITION_FLOAT, position_float.e = e_new);
3003 3003
   TERN_(IS_KINEMATIC, position_cart.e = e);

+ 3
- 3
Marlin/src/module/scara.cpp View File

@@ -51,8 +51,8 @@ float segments_per_second = TERN(AXEL_TPARA, TPARA_SEGMENTS_PER_SECOND, SCARA_SE
51 51
   void forward_kinematics(const_float_t a, const_float_t b) {
52 52
     const float a_sin = sin(RADIANS(a)) * L1,
53 53
                 a_cos = cos(RADIANS(a)) * L1,
54
-                b_sin = sin(RADIANS(b + TERN0(MP_SCARA, a))) * L2,
55
-                b_cos = cos(RADIANS(b + TERN0(MP_SCARA, a))) * L2;
54
+                b_sin = sin(RADIANS(SUM_TERN(MP_SCARA, b, a))) * L2,
55
+                b_cos = cos(RADIANS(SUM_TERN(MP_SCARA, b, a))) * L2;
56 56
 
57 57
     cartes.x = a_cos + b_cos + scara_offset.x;  // theta
58 58
     cartes.y = a_sin + b_sin + scara_offset.y;  // phi
@@ -127,7 +127,7 @@ float segments_per_second = TERN(AXEL_TPARA, TPARA_SEGMENTS_PER_SECOND, SCARA_SE
127 127
     // Angle of Arm2
128 128
     PSI = ATAN2(S2, C2);
129 129
 
130
-    delta.set(DEGREES(THETA), DEGREES(PSI + TERN0(MORGAN_SCARA, THETA)), raw.z);
130
+    delta.set(DEGREES(THETA), DEGREES(SUM_TERN(MORGAN_SCARA, PSI, THETA)), raw.z);
131 131
 
132 132
     /*
133 133
       DEBUG_POS("SCARA IK", raw);

+ 1
- 1
Marlin/src/module/tool_change.cpp View File

@@ -359,7 +359,7 @@ inline void fast_line_to_current(const AxisEnum fr_axis) { _line_to_current(fr_a
359 359
 
360 360
       // STEP 6
361 361
 
362
-      current_position.x = midpos - TERN0(HAS_HOTEND_OFFSET, hotend_offset[new_tool].x);
362
+      current_position.x = DIFF_TERN(HAS_HOTEND_OFFSET, midpos, hotend_offset[new_tool].x);
363 363
 
364 364
       DEBUG_SYNCHRONIZE();
365 365
       DEBUG_POS("(6) Move midway between hotends", current_position);

Loading…
Cancel
Save