From 44be918d2a64fdcbe086013e3c39330aa02f6681 Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Wed, 27 Jun 2012 00:43:33 +0200 Subject: [PATCH] Introduce muldivQR(). This is a version of muldiv() with qn and rn precalculated, so it can be avoided to re-calclulate it on every instance. Yet another 116 bytes, unfortunately. --- dda_maths.c | 11 +++++------ dda_maths.h | 20 ++++++++++++++------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/dda_maths.c b/dda_maths.c index e5ea5b2..f2144bb 100644 --- a/dda_maths.c +++ b/dda_maths.c @@ -9,10 +9,11 @@ #include /*! - Integer multiply-divide algorithm. + Integer multiply-divide algorithm. Returns the same as muldiv(multiplicand, multiplier, divisor), but also allowing to use precalculated quotients and remainders. \param multiplicand - \param multiplier + \param qn ( = multiplier / divisor ) + \param rn ( = multiplier % divisor ) \param divisor \return rounded result of multiplicand * multiplier / divisor @@ -25,13 +26,11 @@ Found on http://stackoverflow.com/questions/4144232/ how-to-calculate-a-times-b-divided-by-c-only-using-32-bit-integer-types-even-i */ -const int32_t muldiv(int32_t multiplicand, uint32_t multiplier, - uint32_t divisor) { +const int32_t muldivQR(int32_t multiplicand, uint32_t qn, uint32_t rn, + uint32_t divisor) { uint32_t quotient = 0; uint32_t remainder = 0; uint8_t negative_flag = 0; - uint32_t qn = multiplier / divisor; - uint32_t rn = multiplier % divisor; if (multiplicand < 0) { negative_flag = 1; diff --git a/dda_maths.h b/dda_maths.h index 87aed3f..23d284c 100644 --- a/dda_maths.h +++ b/dda_maths.h @@ -6,13 +6,21 @@ #include "config.h" // return rounded result of multiplicand * multiplier / divisor -const int32_t muldiv(int32_t multiplicand, uint32_t multiplier, - uint32_t divisor); +#define muldiv(multiplicand, multiplier, divisor) \ + muldivQR(multiplicand, multiplier / divisor, multiplier % divisor, divisor) + +// same as before, but with quotient and remainder precalculated elsewhere +const int32_t muldivQR(int32_t multiplicand, uint32_t qn, uint32_t rn, + uint32_t divisor); // convert micrometer distances to motor step distances -#define um_to_steps_x(distance) muldiv(distance, STEPS_PER_M_X, 1000000UL); -#define um_to_steps_y(distance) muldiv(distance, STEPS_PER_M_Y, 1000000UL); -#define um_to_steps_z(distance) muldiv(distance, STEPS_PER_M_Z, 1000000UL); -#define um_to_steps_e(distance) muldiv(distance, STEPS_PER_M_E, 1000000UL); +#define um_to_steps_x(distance) muldivQR(distance, \ + STEPS_PER_M_X / 1000000UL, STEPS_PER_M_X % 1000000UL, 1000000UL); +#define um_to_steps_y(distance) muldivQR(distance, \ + STEPS_PER_M_Y / 1000000UL, STEPS_PER_M_Y % 1000000UL, 1000000UL); +#define um_to_steps_z(distance) muldivQR(distance, \ + STEPS_PER_M_Z / 1000000UL, STEPS_PER_M_Z % 1000000UL, 1000000UL); +#define um_to_steps_e(distance) muldivQR(distance, \ + STEPS_PER_M_E / 1000000UL, STEPS_PER_M_E % 1000000UL, 1000000UL); #endif /* _DDA_MATHS_H */