AVR: Fix 64-bit signed fixed-point multiply
Corrects a bug in AVR's 64-bit signed fixed-point multiplication that mishandled negative numbers.
The fixed-point multiply routine for 64-bit signed integers on AVR microcontrollers was incorrect for negative numbers. It failed to restore the original value of the B register after the multiplication when B was negative. This patch ensures that B is properly restored by negating it twice if it was negative, preserving the expected register state for the caller.
In Details
In libgcc/config/avr/lib1funcs-fixed.S, the __mulQ64_work function performs 64-bit signed fixed-point multiplication. The issue lies in how the register B (mapped to R10-R17) is handled when its absolute value is taken for the multiplication. If B is negative, it's negated once to get its absolute value, but this negated value is then used in the multiplication. The original value of B is not restored correctly after the operation, violating the callee-saved register assumption for B. The fix involves storing the sign of B and double-negating it if it was negative, effectively r…
- AVR
- A family of microcontrollers developed by Atmel (now Microchip Technology). GCC has specific backends for generating code for AVR targets.
- fixed-point multiply
- Multiplication of numbers represented in a fixed-point format, where the position of the decimal (or binary) point is fixed. This is often used when floating-point hardware is unavailable or for performance reasons.
- callee-saved regs
- Processor registers that a called function (callee) must preserve the values of, so that they are the same when the caller resumes execution as they were before the call. The caller can rely on these registers holding their original values after a function returns.