Skip to content

Commit 143a870

Browse files
⚡️ Optimize speed lookup for AVR
Co-Authored-By: tombrazier <[email protected]>
1 parent 3365172 commit 143a870

File tree

4 files changed

+171
-176
lines changed

4 files changed

+171
-176
lines changed

Marlin/src/HAL/AVR/math.h

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,32 +84,26 @@ FORCE_INLINE static uint16_t MultiU24X32toH16(uint32_t longIn1, uint32_t longIn2
8484
return intRes;
8585
}
8686

87-
// intRes = intIn1 * intIn2 >> 8
87+
// charRes = charIn1 * charIn2 >> 8
8888
// uses:
89-
// r1, r0 for the result of mul. After the second mul, r0 holds bits 0-7 of the 24 bit result and
90-
// the top bit of r0 is used for rounding.
91-
// [tmp] to store 0.
92-
// [intRes] (A B) is bits 8-15 and is the returned value.
89+
// r1, r0 for the result of mul. After the mul, r0 holds bits 0-7 of the 16 bit result,
90+
// and the top bit of r0 is used for rounding.
91+
// [charRes] is bits 8-15 and is the returned value.
9392
// [charIn1] is an 8 bit parameter.
94-
// [intIn2] (B A) is a 16 bit parameter.
93+
// [charIn2] is an 8 bit parameter.
9594
//
96-
FORCE_INLINE static uint16_t MultiU8X16toH16(uint8_t charIn1, uint16_t intIn2) {
97-
uint8_t tmp;
98-
uint16_t intRes;
95+
FORCE_INLINE static uint8_t MultiU8X8toH8(uint8_t charIn1, uint8_t charIn2) {
96+
uint8_t charRes;
9997
__asm__ __volatile__ (
100-
A("clr %[tmp]")
101-
A("mul %[charIn1], %B[intIn2]")
102-
A("movw %A[intRes], r0")
103-
A("mul %[charIn1], %A[intIn2]")
104-
A("lsl r0")
105-
A("adc %A[intRes], r1")
106-
A("adc %B[intRes], %[tmp]")
98+
A("mul %[charIn1], %[charIn2]")
99+
A("mov %[charRes], r1")
107100
A("clr r1")
108-
: [intRes] "=&r" (intRes),
109-
[tmp] "=&r" (tmp)
101+
A("lsl r0")
102+
A("adc %[charRes], r1")
103+
: [charRes] "=&r" (charRes)
110104
: [charIn1] "d" (charIn1),
111-
[intIn2] "d" (intIn2)
105+
[charIn2] "d" (charIn2)
112106
: "cc"
113107
);
114-
return intRes;
108+
return charRes;
115109
}

Marlin/src/module/stepper.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,22 +2069,21 @@ hal_timer_t Stepper::calc_timer_interval(uint32_t step_rate) {
20692069

20702070
// AVR is able to keep up at 30khz Stepping ISR rate.
20712071
constexpr uint32_t min_step_rate = (F_CPU) / 500000U; // i.e., 32 or 40
2072-
if (step_rate <= min_step_rate) { // lower step rates
2073-
step_rate = 0;
2074-
return uint16_t(pgm_read_word(uintptr_t(speed_lookuptable_slow)));
2072+
if (step_rate >= 0x0800) { // higher step rate
2073+
const uintptr_t table_address = uintptr_t(&speed_lookuptable_fast[uint8_t(step_rate >> 8)]);
2074+
const uint16_t base = uint16_t(pgm_read_word(table_address));
2075+
const uint8_t gain = uint8_t(pgm_read_byte(table_address + 2));
2076+
return base - MultiU8X8toH8(uint8_t(step_rate & 0x00FF), gain);
20752077
}
2076-
else {
2078+
else if (step_rate > min_step_rate) { // lower step rates
20772079
step_rate -= min_step_rate; // Correct for minimal speed
2078-
if (step_rate >= 0x0800) { // higher step rate
2079-
const uintptr_t table_address = uintptr_t(&speed_lookuptable_fast[uint8_t(step_rate >> 8)]);
2080-
const uint16_t gain = uint16_t(pgm_read_word(table_address + 2));
2081-
return uint16_t(pgm_read_word(table_address)) - MultiU8X16toH16(uint8_t(step_rate & 0x00FF), gain);
2082-
}
2083-
else { // lower step rates
2084-
const uintptr_t table_address = uintptr_t(&speed_lookuptable_slow[uint8_t(step_rate >> 3)]);
2085-
return uint16_t(pgm_read_word(table_address))
2086-
- ((uint16_t(pgm_read_word(table_address + 2)) * uint8_t(step_rate & 0x0007)) >> 3);
2087-
}
2080+
const uintptr_t table_address = uintptr_t(&speed_lookuptable_slow[uint8_t(step_rate >> 3)]);
2081+
return uint16_t(pgm_read_word(table_address))
2082+
- ((uint16_t(pgm_read_word(table_address + 2)) * uint8_t(step_rate & 0x0007)) >> 3);
2083+
}
2084+
else {
2085+
step_rate = 0;
2086+
return uint16_t(pgm_read_word(uintptr_t(speed_lookuptable_slow)));
20882087
}
20892088

20902089
#endif // !CPU_32_BIT

0 commit comments

Comments
 (0)