Prusa MINI Firmware overview
math.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4  *
5  * Based on Sprinter and grbl.
6  * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 #pragma once
23 
24 /**
25  * Optimized math functions for AVR
26  */
27 
28 // intRes = longIn1 * longIn2 >> 24
29 // uses:
30 // A[tmp] to store 0
31 // B[tmp] to store bits 16-23 of the 48bit result. The top bit is used to round the two byte result.
32 // note that the lower two bytes and the upper byte of the 48bit result are not calculated.
33 // this can cause the result to be out by one as the lower bytes may cause carries into the upper ones.
34 // B A are bits 24-39 and are the returned value
35 // C B A is longIn1
36 // D C B A is longIn2
37 //
38 static FORCE_INLINE uint16_t MultiU24X32toH16(uint32_t longIn1, uint32_t longIn2) {
39  uint8_t tmp1;
40  uint8_t tmp2;
41  uint16_t intRes;
42  __asm__ __volatile__(
43  A("clr %[tmp1]")
44  A("mul %A[longIn1], %B[longIn2]")
45  A("mov %[tmp2], r1")
46  A("mul %B[longIn1], %C[longIn2]")
47  A("movw %A[intRes], r0")
48  A("mul %C[longIn1], %C[longIn2]")
49  A("add %B[intRes], r0")
50  A("mul %C[longIn1], %B[longIn2]")
51  A("add %A[intRes], r0")
52  A("adc %B[intRes], r1")
53  A("mul %A[longIn1], %C[longIn2]")
54  A("add %[tmp2], r0")
55  A("adc %A[intRes], r1")
56  A("adc %B[intRes], %[tmp1]")
57  A("mul %B[longIn1], %B[longIn2]")
58  A("add %[tmp2], r0")
59  A("adc %A[intRes], r1")
60  A("adc %B[intRes], %[tmp1]")
61  A("mul %C[longIn1], %A[longIn2]")
62  A("add %[tmp2], r0")
63  A("adc %A[intRes], r1")
64  A("adc %B[intRes], %[tmp1]")
65  A("mul %B[longIn1], %A[longIn2]")
66  A("add %[tmp2], r1")
67  A("adc %A[intRes], %[tmp1]")
68  A("adc %B[intRes], %[tmp1]")
69  A("lsr %[tmp2]")
70  A("adc %A[intRes], %[tmp1]")
71  A("adc %B[intRes], %[tmp1]")
72  A("mul %D[longIn2], %A[longIn1]")
73  A("add %A[intRes], r0")
74  A("adc %B[intRes], r1")
75  A("mul %D[longIn2], %B[longIn1]")
76  A("add %B[intRes], r0")
77  A("clr r1")
78  : [intRes] "=&r" (intRes),
79  [tmp1] "=&r" (tmp1),
80  [tmp2] "=&r" (tmp2)
81  : [longIn1] "d" (longIn1),
82  [longIn2] "d" (longIn2)
83  : "cc"
84  );
85  return intRes;
86 }
87 
88 // intRes = intIn1 * intIn2 >> 16
89 // uses:
90 // r26 to store 0
91 // r27 to store the byte 1 of the 24 bit result
92 static FORCE_INLINE uint16_t MultiU16X8toH16(uint8_t charIn1, uint16_t intIn2) {
93  uint8_t tmp;
94  uint16_t intRes;
95  __asm__ __volatile__ (
96  A("clr %[tmp]")
97  A("mul %[charIn1], %B[intIn2]")
98  A("movw %A[intRes], r0")
99  A("mul %[charIn1], %A[intIn2]")
100  A("add %A[intRes], r1")
101  A("adc %B[intRes], %[tmp]")
102  A("lsr r0")
103  A("adc %A[intRes], %[tmp]")
104  A("adc %B[intRes], %[tmp]")
105  A("clr r1")
106  : [intRes] "=&r" (intRes),
107  [tmp] "=&r" (tmp)
108  : [charIn1] "d" (charIn1),
109  [intIn2] "d" (intIn2)
110  : "cc"
111  );
112  return intRes;
113 }
A
#define A(CODE)
Definition: macros.h:75
FORCE_INLINE
#define FORCE_INLINE
Definition: macros.h:40
MultiU16X8toH16
static FORCE_INLINE uint16_t MultiU16X8toH16(uint8_t charIn1, uint16_t intIn2)
Definition: math.h:92
uint8_t
const uint8_t[]
Definition: 404_html.c:3
MultiU24X32toH16
static FORCE_INLINE uint16_t MultiU24X32toH16(uint32_t longIn1, uint32_t longIn2)
Definition: math.h:38