Prusa-MMU-Private
PrusaMultiMaterialUpgradev3firmwareforMK3SMK4
math.h
Go to the documentation of this file.
1 #pragma once
3 #include "../config/config.h"
4 
5 namespace modules {
6 
8 namespace math {
9 
11 static inline uint16_t mulU8X16toH16(const uint8_t charIn1, const uint16_t intIn2) {
12  uint16_t intRes;
13 #if !defined(__AVR__) || defined(NO_ASM)
14  intRes = ((uint32_t)charIn1 * (uint32_t)intIn2) >> 8;
15 #else
16  asm volatile(
17  "mul %B2, %A1 \n\t"
18  "movw %0, r0 \n\t"
19  "mul %A2, %A1 \n\t"
20  "lsl r0 \n\t" //push MSB to carry for rounding
21  "adc %A0, r1 \n\t" //add with carry (for rounding)
22  "clr r1 \n\t" //make r1 __zero_reg__ again
23  "adc %B0, r1 \n\t" //propagate carry of addition (add 0 with carry)
24  : "=&r"(intRes)
25  : "r"(charIn1), "r"(intIn2)
26  : "r0", "r1" //clobbers: Technically these are either scratch registers or always 0 registers, but I'm making sure the compiler knows just in case.
27  );
28 #endif
29  return intRes;
30 }
31 
33 static inline uint16_t mulU24X24toH16(const uint32_t &longIn1, const uint32_t &longIn2) {
34  uint16_t intRes;
35 #if !defined(__AVR__) || defined(NO_ASM)
36  intRes = ((uint64_t)longIn1 * (uint64_t)longIn2) >> 24;
37 #else
38  asm volatile(
39  "clr r26 \n\t"
40  "mul %A1, %B2 \n\t"
41  "mov r27, r1 \n\t"
42  "mul %B1, %C2 \n\t"
43  "movw %A0, r0 \n\t"
44  "mul %C1, %C2 \n\t"
45  "add %B0, r0 \n\t"
46  "mul %C1, %B2 \n\t"
47  "add %A0, r0 \n\t"
48  "adc %B0, r1 \n\t"
49  "mul %A1, %C2 \n\t"
50  "add r27, r0 \n\t"
51  "adc %A0, r1 \n\t"
52  "adc %B0, r26 \n\t"
53  "mul %B1, %B2 \n\t"
54  "add r27, r0 \n\t"
55  "adc %A0, r1 \n\t"
56  "adc %B0, r26 \n\t"
57  "mul %C1, %A2 \n\t"
58  "add r27, r0 \n\t"
59  "adc %A0, r1 \n\t"
60  "adc %B0, r26 \n\t"
61  "mul %B1, %A2 \n\t"
62  "add r27, r1 \n\t"
63  "adc %A0, r26 \n\t"
64  "adc %B0, r26 \n\t"
65  "lsl r27 \n\t"
66  "adc %A0, r26 \n\t"
67  "adc %B0, r26 \n\t"
68  "clr r1 \n\t"
69  : "=&r"(intRes)
70  : "d"(longIn1), "d"(longIn2)
71  : "r0", "r1", "r26", "r27");
72 #endif
73  return intRes;
74 }
75 
76 } // namespace math
77 } // namespace modules
static uint16_t mulU24X24toH16(const uint32_t &longIn1, const uint32_t &longIn2)
(longIn1 * longIn2) >> 24
Definition: math.h:33
static uint16_t mulU8X16toH16(const uint8_t charIn1, const uint16_t intIn2)
(intIn1 * intIn2) >> 8
Definition: math.h:11
The modules namespace contains models of MMU's components.
Definition: command_base.h:8