Optimise calculations to use hypot() where possible

flash: -122
RAM: 0

It is defined: hypot(x,y) = sqrtf(x*x + y*y)
This commit is contained in:
Guðni Már Gilbert 2022-02-06 09:30:29 +00:00 committed by Alex Voinea
parent 7d72f0ee2e
commit 37c9dcbe53

View File

@ -418,9 +418,9 @@ BedSkewOffsetDetectionResultType calculate_machine_skew_and_offset_LS(
for (uint8_t i = 0; i < npts; ++i) { for (uint8_t i = 0; i < npts; ++i) {
float x = vec_x[0] * measured_pts[i * 2] + vec_y[0] * measured_pts[i * 2 + 1] + cntr[0]; float x = vec_x[0] * measured_pts[i * 2] + vec_y[0] * measured_pts[i * 2 + 1] + cntr[0];
float y = vec_x[1] * measured_pts[i * 2] + vec_y[1] * measured_pts[i * 2 + 1] + cntr[1]; float y = vec_x[1] * measured_pts[i * 2] + vec_y[1] * measured_pts[i * 2 + 1] + cntr[1];
float errX = sqr(pgm_read_float(true_pts + i * 2) - x); float errX = pgm_read_float(true_pts + i * 2) - x;
float errY = sqr(pgm_read_float(true_pts + i * 2 + 1) - y); float errY = pgm_read_float(true_pts + i * 2 + 1) - y;
float err = sqrt(errX + errY); float err = hypot(errX, errY);
#ifdef SUPPORT_VERBOSITY #ifdef SUPPORT_VERBOSITY
if (verbosity_level >= 10) { if (verbosity_level >= 10) {
SERIAL_ECHOPGM("point #"); SERIAL_ECHOPGM("point #");
@ -434,15 +434,15 @@ BedSkewOffsetDetectionResultType calculate_machine_skew_and_offset_LS(
if(verbosity_level >= 20) SERIAL_ECHOPGM("Point on first row"); if(verbosity_level >= 20) SERIAL_ECHOPGM("Point on first row");
#endif // SUPPORT_VERBOSITY #endif // SUPPORT_VERBOSITY
float w = point_weight_y(i, measured_pts[2 * i + 1]); float w = point_weight_y(i, measured_pts[2 * i + 1]);
if (sqrt(errX) > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_X || if (errX > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_X ||
(w != 0.f && sqrt(errY) > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_Y)) { (w != 0.f && errY > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_Y)) {
result = BED_SKEW_OFFSET_DETECTION_FITTING_FAILED; result = BED_SKEW_OFFSET_DETECTION_FITTING_FAILED;
#ifdef SUPPORT_VERBOSITY #ifdef SUPPORT_VERBOSITY
if (verbosity_level >= 20) { if (verbosity_level >= 20) {
SERIAL_ECHOPGM(", weigth Y: "); SERIAL_ECHOPGM(", weigth Y: ");
MYSERIAL.print(w); MYSERIAL.print(w);
if (sqrt(errX) > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_X) SERIAL_ECHOPGM(", error X > max. error X"); if (errX > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_X) SERIAL_ECHOPGM(", error X > max. error X");
if (w != 0.f && sqrt(errY) > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_Y) SERIAL_ECHOPGM(", error Y > max. error Y"); if (w != 0.f && errY > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_Y) SERIAL_ECHOPGM(", error Y > max. error Y");
} }
#endif // SUPPORT_VERBOSITY #endif // SUPPORT_VERBOSITY
} }
@ -477,9 +477,9 @@ BedSkewOffsetDetectionResultType calculate_machine_skew_and_offset_LS(
SERIAL_ECHOPGM("error: "); SERIAL_ECHOPGM("error: ");
MYSERIAL.print(err); MYSERIAL.print(err);
SERIAL_ECHOPGM(", error X: "); SERIAL_ECHOPGM(", error X: ");
MYSERIAL.print(sqrt(errX)); MYSERIAL.print(errX);
SERIAL_ECHOPGM(", error Y: "); SERIAL_ECHOPGM(", error Y: ");
MYSERIAL.print(sqrt(errY)); MYSERIAL.print(errY);
SERIAL_ECHOLNPGM(""); SERIAL_ECHOLNPGM("");
SERIAL_ECHOLNPGM(""); SERIAL_ECHOLNPGM("");
} }
@ -645,7 +645,7 @@ BedSkewOffsetDetectionResultType calculate_machine_skew_and_offset_LS(
SERIAL_ECHOPGM(", "); SERIAL_ECHOPGM(", ");
MYSERIAL.print(pgm_read_float(true_pts + i * 2 + 1), 5); MYSERIAL.print(pgm_read_float(true_pts + i * 2 + 1), 5);
SERIAL_ECHOPGM("), error: "); SERIAL_ECHOPGM("), error: ");
MYSERIAL.print(sqrt(sqr(measured_pts[i * 2] - x) + sqr(measured_pts[i * 2 + 1] - y))); MYSERIAL.print( hypot(measured_pts[i * 2] - x, measured_pts[i * 2 + 1] - y) );
SERIAL_ECHOLNPGM(""); SERIAL_ECHOLNPGM("");
} }
if (verbosity_level >= 20) { if (verbosity_level >= 20) {
@ -810,7 +810,7 @@ void world2machine_read_valid(float vec_x[2], float vec_y[2], float cntr[2])
else else
{ {
// Length of the vec_x shall be close to unity. // Length of the vec_x shall be close to unity.
float l = sqrt(vec_x[0] * vec_x[0] + vec_x[1] * vec_x[1]); float l = hypot(vec_x[0], vec_x[1]);
if (l < 0.9 || l > 1.1) if (l < 0.9 || l > 1.1)
{ {
#if 0 #if 0
@ -821,7 +821,7 @@ void world2machine_read_valid(float vec_x[2], float vec_y[2], float cntr[2])
reset = true; reset = true;
} }
// Length of the vec_y shall be close to unity. // Length of the vec_y shall be close to unity.
l = sqrt(vec_y[0] * vec_y[0] + vec_y[1] * vec_y[1]); l = hypot(vec_y[0], vec_y[1]);
if (l < 0.9 || l > 1.1) if (l < 0.9 || l > 1.1)
{ {
#if 0 #if 0
@ -832,7 +832,7 @@ void world2machine_read_valid(float vec_x[2], float vec_y[2], float cntr[2])
reset = true; reset = true;
} }
// Correction of the zero point shall be reasonably small. // Correction of the zero point shall be reasonably small.
l = sqrt(cntr[0] * cntr[0] + cntr[1] * cntr[1]); l = hypot(cntr[0], cntr[1]);
if (l > 15.f) if (l > 15.f)
{ {
#if 0 #if 0
@ -1579,7 +1579,7 @@ inline bool improve_bed_induction_sensor_point()
// Trim the vector from center_old_[x,y] to destination[x,y] by the bed dimensions. // Trim the vector from center_old_[x,y] to destination[x,y] by the bed dimensions.
float vx = destination[X_AXIS] - center_old_x; float vx = destination[X_AXIS] - center_old_x;
float vy = destination[Y_AXIS] - center_old_y; float vy = destination[Y_AXIS] - center_old_y;
float l = sqrt(vx*vx+vy*vy); float l = hypot(vx, vy);
float t; float t;
if (destination[X_AXIS] < X_MIN_POS) { if (destination[X_AXIS] < X_MIN_POS) {
// Exiting the bed at xmin. // Exiting the bed at xmin.
@ -2441,16 +2441,16 @@ BedSkewOffsetDetectionResultType find_bed_offset_and_skew(int8_t verbosity_level
#ifdef SUPPORT_VERBOSITY #ifdef SUPPORT_VERBOSITY
if (verbosity_level >= 10) { if (verbosity_level >= 10) {
// Length of the vec_x // Length of the vec_x
float l = sqrt(vec_x[0] * vec_x[0] + vec_x[1] * vec_x[1]); float l = hypot(vec_x[0], vec_x[1]);
SERIAL_ECHOLNPGM("X vector length:"); SERIAL_ECHOLNPGM("X vector length:");
MYSERIAL.println(l); MYSERIAL.println(l);
// Length of the vec_y // Length of the vec_y
l = sqrt(vec_y[0] * vec_y[0] + vec_y[1] * vec_y[1]); l = hypot(vec_y[0], vec_y[1]);
SERIAL_ECHOLNPGM("Y vector length:"); SERIAL_ECHOLNPGM("Y vector length:");
MYSERIAL.println(l); MYSERIAL.println(l);
// Zero point correction // Zero point correction
l = sqrt(cntr[0] * cntr[0] + cntr[1] * cntr[1]); l = hypot(cntr[0], cntr[1]);
SERIAL_ECHOLNPGM("Zero point correction:"); SERIAL_ECHOLNPGM("Zero point correction:");
MYSERIAL.println(l); MYSERIAL.println(l);