From e141f3a03f1999a3478897e92ea626b437d40466 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Sat, 4 Mar 2017 19:18:11 -0600
Subject: [PATCH] Optimize coordinate transformation

Pre-compute the combined position shift and home offset to save a
single float fetch-and-add per conversion. Great for delta/scara and
bed leveling.
---
 Marlin/Marlin.h        | 9 +++++----
 Marlin/Marlin_main.cpp | 7 +++++--
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/Marlin/Marlin.h b/Marlin/Marlin.h
index 7d759553cc3..cb6654eae8f 100644
--- a/Marlin/Marlin.h
+++ b/Marlin/Marlin.h
@@ -278,10 +278,11 @@ extern float current_position[NUM_AXIS];
 
 // Workspace offsets
 #if DISABLED(NO_WORKSPACE_OFFSETS)
-  extern float position_shift[XYZ];
-  extern float home_offset[XYZ];
-  #define LOGICAL_POSITION(POS, AXIS) ((POS) + home_offset[AXIS] + position_shift[AXIS])
-  #define RAW_POSITION(POS, AXIS)     ((POS) - home_offset[AXIS] - position_shift[AXIS])
+  extern float position_shift[XYZ],
+               home_offset[XYZ],
+               workspace_offset[XYZ];
+  #define LOGICAL_POSITION(POS, AXIS) ((POS) + workspace_offset[AXIS])
+  #define RAW_POSITION(POS, AXIS)     ((POS) - workspace_offset[AXIS])
 #else
   #define LOGICAL_POSITION(POS, AXIS) (POS)
   #define RAW_POSITION(POS, AXIS)     (POS)
diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index 3c6ef8bd5e5..5923447f447 100755
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -405,6 +405,9 @@ float filament_size[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(DEFAULT_NOMINAL_FILAMENT_DI
   // Set by M206, M428, or menu item. Saved to EEPROM.
   float home_offset[XYZ] = { 0 };
 
+  // The above two are combined to save on computes
+  float workspace_offset[XYZ] = { 0 };
+
 #endif
 
 // Software Endstops are based on the configured limits.
@@ -1349,7 +1352,7 @@ bool get_target_extruder_from_command(int code) {
    * at the same positions relative to the machine.
    */
   void update_software_endstops(const AxisEnum axis) {
-    const float offs = LOGICAL_POSITION(0, axis);
+    const float offs = workspace_offset[axis] = LOGICAL_POSITION(0, axis);
 
     #if ENABLED(DUAL_X_CARRIAGE)
       if (axis == X_AXIS) {
@@ -1408,7 +1411,7 @@ bool get_target_extruder_from_command(int code) {
    * Since this changes the current_position, code should
    * call sync_plan_position soon after this.
    */
-  static void set_home_offset(AxisEnum axis, float v) {
+  static void set_home_offset(const AxisEnum axis, const float v) {
     current_position[axis] += v - home_offset[axis];
     home_offset[axis] = v;
     update_software_endstops(axis);