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
 #define DISABLED(V...)      DO(DIS,&&,V)
187
 #define DISABLED(V...)      DO(DIS,&&,V)
188
 #define COUNT_ENABLED(V...) DO(ENA,+,V)
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
 #define _TERN(E,V...)       __TERN(_CAT(T_,E),V)    // Prepend 'T_' to get 'T_0' or 'T_1'
194
 #define _TERN(E,V...)       __TERN(_CAT(T_,E),V)    // Prepend 'T_' to get 'T_0' or 'T_1'
195
 #define __TERN(T,V...)      ___TERN(_CAT(_NO,T),V)  // Prepend '_NO' to get '_NOT_0' or '_NOT_1'
195
 #define __TERN(T,V...)      ___TERN(_CAT(_NO,T),V)  // Prepend '_NO' to get '_NOT_0' or '_NOT_1'
196
 #define ___TERN(P,V...)     THIRD(P,V)              // If first argument has a comma, A. Else B.
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
 #define IF_ENABLED          TERN_
205
 #define IF_ENABLED          TERN_
199
 #define IF_DISABLED(O,A)    TERN(O,,A)
206
 #define IF_DISABLED(O,A)    TERN(O,,A)
200
 
207
 

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

102
     // In BLTOUCH HS mode, the probe travels in a deployed state.
102
     // In BLTOUCH HS mode, the probe travels in a deployed state.
103
     // Users of G35 might have a badly misaligned bed, so raise Z by the
103
     // Users of G35 might have a badly misaligned bed, so raise Z by the
104
     // length of the deployed pin (BLTOUCH stroke < 7mm)
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
     const float z_probed_height = probe.probe_at_point(screws_tilt_adjust_pos[i], PROBE_PT_RAISE, 0, true);
106
     const float z_probed_height = probe.probe_at_point(screws_tilt_adjust_pos[i], PROBE_PT_RAISE, 0, true);
107
 
107
 
108
     if (isnan(z_probed_height)) {
108
     if (isnan(z_probed_height)) {

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

269
     do {
269
     do {
270
       ui.refresh(LCDVIEW_REDRAW_NOW);
270
       ui.refresh(LCDVIEW_REDRAW_NOW);
271
       _lcd_draw_probing();                                             // update screen with # of good points
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
       _lcd_level_bed_corners_get_next_position();         // Select next corner coordinates
274
       _lcd_level_bed_corners_get_next_position();         // Select next corner coordinates
275
       current_position -= probe.offset_xy;                // Account for probe offsets
275
       current_position -= probe.offset_xy;                // Account for probe offsets

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

93
   ui.encoderPosition = 0;
93
   ui.encoderPosition = 0;
94
   if (ui.should_draw()) {
94
   if (ui.should_draw()) {
95
     const float pos = NATIVE_TO_LOGICAL(
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
       axis
97
       axis
98
     );
98
     );
99
     if (parser.using_inch_units()) {
99
     if (parser.using_inch_units()) {
130
       MenuEditItemBase::draw_edit_screen(
130
       MenuEditItemBase::draw_edit_screen(
131
         GET_TEXT(TERN(MULTI_MANUAL, MSG_MOVE_EN, MSG_MOVE_E)),
131
         GET_TEXT(TERN(MULTI_MANUAL, MSG_MOVE_EN, MSG_MOVE_E)),
132
         ftostr41sign(current_position.e
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
     } // should_draw
137
     } // should_draw

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

610
       probe.offset.z :
610
       probe.offset.z :
611
     #endif
611
     #endif
612
     NATIVE_TO_LOGICAL(
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
       axis
614
       axis
615
     );
615
     );
616
   xy_int_t pos;
616
   xy_int_t pos;

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

248
   #endif
248
   #endif
249
 
249
 
250
   // Move all carriages together linearly until an endstop is hit.
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
   line_to_current_position(homing_feedrate(Z_AXIS));
252
   line_to_current_position(homing_feedrate(Z_AXIS));
253
   planner.synchronize();
253
   planner.synchronize();
254
 
254
 

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

583
     #elif ENABLED(DELTA)
583
     #elif ENABLED(DELTA)
584
 
584
 
585
       soft_endstop.min[axis] = base_min_pos(axis);
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
       switch (axis) {
588
       switch (axis) {
589
         case X_AXIS:
589
         case X_AXIS:
1847
   #if EITHER(MORGAN_SCARA, AXEL_TPARA)
1847
   #if EITHER(MORGAN_SCARA, AXEL_TPARA)
1848
     scara_set_axis_is_at_home(axis);
1848
     scara_set_axis_is_at_home(axis);
1849
   #elif ENABLED(DELTA)
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
   #else
1851
   #else
1852
     current_position[axis] = base_home_pos(axis);
1852
     current_position[axis] = base_home_pos(axis);
1853
   #endif
1853
   #endif

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

2997
   const uint8_t axis_index = E_AXIS_N(active_extruder);
2997
   const uint8_t axis_index = E_AXIS_N(active_extruder);
2998
   TERN_(DISTINCT_E_FACTORS, last_extruder = active_extruder);
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
   position.e = LROUND(settings.axis_steps_per_mm[axis_index] * e_new);
3001
   position.e = LROUND(settings.axis_steps_per_mm[axis_index] * e_new);
3002
   TERN_(HAS_POSITION_FLOAT, position_float.e = e_new);
3002
   TERN_(HAS_POSITION_FLOAT, position_float.e = e_new);
3003
   TERN_(IS_KINEMATIC, position_cart.e = e);
3003
   TERN_(IS_KINEMATIC, position_cart.e = e);

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

51
   void forward_kinematics(const_float_t a, const_float_t b) {
51
   void forward_kinematics(const_float_t a, const_float_t b) {
52
     const float a_sin = sin(RADIANS(a)) * L1,
52
     const float a_sin = sin(RADIANS(a)) * L1,
53
                 a_cos = cos(RADIANS(a)) * L1,
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
     cartes.x = a_cos + b_cos + scara_offset.x;  // theta
57
     cartes.x = a_cos + b_cos + scara_offset.x;  // theta
58
     cartes.y = a_sin + b_sin + scara_offset.y;  // phi
58
     cartes.y = a_sin + b_sin + scara_offset.y;  // phi
127
     // Angle of Arm2
127
     // Angle of Arm2
128
     PSI = ATAN2(S2, C2);
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
       DEBUG_POS("SCARA IK", raw);
133
       DEBUG_POS("SCARA IK", raw);

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

359
 
359
 
360
       // STEP 6
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
       DEBUG_SYNCHRONIZE();
364
       DEBUG_SYNCHRONIZE();
365
       DEBUG_POS("(6) Move midway between hotends", current_position);
365
       DEBUG_POS("(6) Move midway between hotends", current_position);

Loading…
Cancel
Save