From f2b65ebda241c9f7cc741da21f4658eb1fcb5867 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Mon, 21 Feb 2022 18:24:20 +0100 Subject: [PATCH] Improve mulU8X16toH16 accordingly --- src/modules/math.h | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/modules/math.h b/src/modules/math.h index b0af25e..b339435 100644 --- a/src/modules/math.h +++ b/src/modules/math.h @@ -14,19 +14,17 @@ static inline uint16_t mulU8X16toH16(const uint8_t charIn1, const uint16_t intIn intRes = ((uint32_t)charIn1 * (uint32_t)intIn2) >> 8; #else asm volatile( - "clr r26 \n\t" - "mul %A1, %B2 \n\t" - "movw %A0, r0 \n\t" - "mul %A1, %A2 \n\t" - "add %A0, r1 \n\t" - "adc %B0, r26 \n\t" - "lsr r0 \n\t" - "adc %A0, r26 \n\t" - "adc %B0, r26 \n\t" - "clr r1 \n\t" + "mul %B2, %A1 \n\t" + "movw %0, r0 \n\t" + "mul %A2, %A1 \n\t" + "lsl r0 \n\t" //push MSB to carry for rounding + "adc %A0, r1 \n\t" //add with carry (for rounding) + "clr r1 \n\t" //make r1 __zero_reg__ again + "adc %B0, r1 \n\t" //propagate carry of addition (add 0 with carry) : "=&r"(intRes) - : "d"(charIn1), "d"(intIn2) - : "r26"); + : "r"(charIn1), "r"(intIn2) + : "r0", "r1" //clobbers: Technically these are either scratch registers or always 0 registers, but I'm making sure the compiler knows just in case. + ); #endif return intRes; }