From 3f3b5c86d88c8f7da86dd5f64d3784febbf92a82 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Thu, 8 Dec 2016 22:24:29 -0800
Subject: [PATCH] Slightly shrink code for jerk-based reduction of safe_speed

---
 Marlin/planner.cpp | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)

diff --git a/Marlin/planner.cpp b/Marlin/planner.cpp
index 1fcaabd884..b405e0d52f 100644
--- a/Marlin/planner.cpp
+++ b/Marlin/planner.cpp
@@ -1204,22 +1204,17 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
   static float previous_safe_speed;
 
   float safe_speed = block->nominal_speed;
-  bool limited = false;
+  uint8_t limited = 0;
   LOOP_XYZE(i) {
-    float jerk = fabs(current_speed[i]);
-    if (jerk > max_jerk[i]) {
-      // The actual jerk is lower if it has been limited by the XY jerk.
+    const float jerk = fabs(current_speed[i]), maxj = max_jerk[i];
+    if (jerk > maxj) {
       if (limited) {
-        // Spare one division by a following gymnastics:
-        // Instead of jerk *= safe_speed / block->nominal_speed,
-        // multiply max_jerk[i] by the divisor.
-        jerk *= safe_speed;
-        float mjerk = max_jerk[i] * block->nominal_speed;
-        if (jerk > mjerk) safe_speed *= mjerk / jerk;
+        const float mjerk = maxj * block->nominal_speed;
+        if (jerk * safe_speed > mjerk) safe_speed = mjerk / jerk;
       }
       else {
-        safe_speed = max_jerk[i];
-        limited = true;
+        ++limited;
+        safe_speed = maxj;
       }
     }
   }
@@ -1236,7 +1231,7 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
     vmax_junction = prev_speed_larger ? block->nominal_speed : previous_nominal_speed;
     // Factor to multiply the previous / current nominal velocities to get componentwise limited velocities.
     float v_factor = 1.f;
-    limited = false;
+    limited = 0;
     // Now limit the jerk in all axes.
     LOOP_XYZE(axis) {
       // Limit an axis. We have to differentiate: coasting, reversal of an axis, full stop.
-- 
GitLab