3-phase PV router
Loading...
Searching...
No Matches
mult_asm.h
Go to the documentation of this file.
1
20
21#ifndef MULT_ASM_H
22#define MULT_ASM_H
23
24#include <stdint.h>
25
27
46template< typename A, typename B >
47static inline __attribute__((always_inline)) void multS16x16_to32(int32_t& result, A a, B b)
48{
49 static_assert(is_same_v< A, int16_t >, "First argument must be int16_t");
50 static_assert(is_same_v< B, int16_t >, "Second argument must be int16_t");
51
52#ifdef __AVR__
53 asm volatile(
54 "clr r26 \n\t" // Clear temporary register
55 "mul %A1, %A2 \n\t" // a_lo * b_lo
56 "movw %A0, r0 \n\t" // Store low 16 bits
57 "muls %B1, %B2 \n\t" // a_hi * b_hi (signed)
58 "movw %C0, r0 \n\t" // Store high 16 bits
59 "mulsu %B1, %A2 \n\t" // a_hi * b_lo (signed*unsigned)
60 "sbc %D0, r26 \n\t" // Handle sign extension
61 "add %B0, r0 \n\t" // Add partial result
62 "adc %C0, r1 \n\t"
63 "adc %D0, r26 \n\t"
64 "mulsu %B2, %A1 \n\t" // b_hi * a_lo (signed*unsigned)
65 "sbc %D0, r26 \n\t" // Handle sign extension
66 "add %B0, r0 \n\t" // Add partial result
67 "adc %C0, r1 \n\t"
68 "adc %D0, r26 \n\t"
69 "clr r1 \n\t" // Restore r1 to zero
70 : "=&r"(result)
71 : "a"(a), "a"(b)
72 : "r26");
73#else
74 result = static_cast< int32_t >(a) * b;
75#endif
76}
77
96template< typename A, typename B >
97static inline __attribute__((always_inline)) void multU16x16_to32(uint32_t& result, A a, B b)
98{
99 static_assert(is_same_v< A, uint16_t >, "First argument must be uint16_t");
100 static_assert(is_same_v< B, uint16_t >, "Second argument must be uint16_t");
101
102#ifdef __AVR__
103 asm volatile(
104 "clr r26 \n\t" // Clear temporary register
105 "mul %A1, %A2 \n\t" // a_lo * b_lo
106 "movw %A0, r0 \n\t" // Store low 16 bits
107 "mul %B1, %B2 \n\t" // a_hi * b_hi
108 "movw %C0, r0 \n\t" // Store high 16 bits
109 "mul %B2, %A1 \n\t" // b_hi * a_lo
110 "add %B0, r0 \n\t" // Add partial result
111 "adc %C0, r1 \n\t"
112 "adc %D0, r26 \n\t"
113 "mul %B1, %A2 \n\t" // a_hi * b_lo
114 "add %B0, r0 \n\t" // Add partial result
115 "adc %C0, r1 \n\t"
116 "adc %D0, r26 \n\t"
117 "clr r1 \n\t" // Restore r1 to zero
118 : "=&r"(result)
119 : "a"(a), "a"(b)
120 : "r26");
121#else
122 result = static_cast< uint32_t >(a) * b;
123#endif
124}
125
143template< typename A, typename B >
144static inline __attribute__((always_inline)) void multU24x16_to32_hi8(uint32_t& result, A a, B b)
145{
146 static_assert(is_same_v< A, uint32_t >, "First argument must be uint32_t (24 bits used)");
147 static_assert(is_same_v< B, uint16_t >, "Second argument must be uint16_t");
148
149#ifdef __AVR__
150 // Byte k of the result is byte k+1 of the product; product byte 0 is dropped.
151 asm volatile(
152 "clr r26 \n\t" // Clear temporary register
153 "mul %A1, %A2 \n\t" // a0 * b0 -> product bytes 0-1
154 "mov %A0, r1 \n\t" // keep byte 1 only
155 "clr %B0 \n\t"
156 "clr %C0 \n\t"
157 "clr %D0 \n\t"
158 "mul %A1, %B2 \n\t" // a0 * b1 -> product bytes 1-2
159 "add %A0, r0 \n\t"
160 "adc %B0, r1 \n\t"
161 "adc %C0, r26 \n\t"
162 "mul %B1, %A2 \n\t" // a1 * b0 -> product bytes 1-2
163 "add %A0, r0 \n\t"
164 "adc %B0, r1 \n\t"
165 "adc %C0, r26 \n\t"
166 "mul %B1, %B2 \n\t" // a1 * b1 -> product bytes 2-3
167 "add %B0, r0 \n\t"
168 "adc %C0, r1 \n\t"
169 "adc %D0, r26 \n\t"
170 "mul %C1, %A2 \n\t" // a2 * b0 -> product bytes 2-3
171 "add %B0, r0 \n\t"
172 "adc %C0, r1 \n\t"
173 "adc %D0, r26 \n\t"
174 "mul %C1, %B2 \n\t" // a2 * b1 -> product bytes 3-4
175 "add %C0, r0 \n\t"
176 "adc %D0, r1 \n\t" // no carry out: a < 2^24, b < 2^16
177 "clr r1 \n\t" // Restore r1 to zero
178 : "=&r"(result)
179 : "r"(a), "r"(b)
180 : "r26");
181#else
182 result = static_cast< uint32_t >((static_cast< uint64_t >(a) * b) >> 8);
183#endif
184}
185
205template< typename V, typename F >
206static inline __attribute__((always_inline)) void mult16x8_q8(int16_t& result, V value, F fraction)
207{
208 static_assert(is_same_v< V, int16_t >, "Value argument must be int16_t");
209 static_assert(is_same_v< F, uint8_t >, "Fraction argument must be uint8_t");
210
211#ifdef __AVR__
212 asm volatile(
213 "mulsu %B[val], %[frac] \n\t" // value_hi * fraction (signed*unsigned)
214 "movw %A[ret], r0 \n\t" // Store result
215 "mul %A[val], %[frac] \n\t" // value_lo * fraction
216 "lsl r0 \n\t" // Shift for rounding
217 "adc %A[ret], r1 \n\t" // Add with carry (rounding)
218 "clr r1 \n\t" // Restore r1 to zero
219 "adc %B[ret], r1 \n\t" // Propagate carry
220 : [ret] "=&r"(result)
221 : [val] "a"(value), [frac] "a"(fraction));
222#else
223 result = static_cast< int16_t >((static_cast< int32_t >(value) * fraction + 0x80) >> 8);
224#endif
225}
226
239constexpr uint8_t float_to_q8(float frac)
240{
241 return static_cast< uint8_t >(frac * 256.0f + 0.5f);
242}
243
255constexpr float q8_to_float(uint8_t q8_val)
256{
257 return static_cast< float >(q8_val) / 256.0f;
258}
259
260#endif /* MULT_ASM_H */
static void multU16x16_to32(uint32_t &result, A a, B b)
Optimized 16×16→32 unsigned multiplication with assembly.
Definition mult_asm.h:97
static void mult16x8_q8(int16_t &result, V value, F fraction)
Optimized 16×8→16 signed multiplication with Q8 fractional.
Definition mult_asm.h:206
static void multS16x16_to32(int32_t &result, A a, B b)
Optimized 16×16→32 signed multiplication with assembly.
Definition mult_asm.h:47
static void multU24x16_to32_hi8(uint32_t &result, A a, B b)
Optimized 24×16→40 unsigned multiplication, returning bits 8..39.
Definition mult_asm.h:144
constexpr bool is_same_v
Definition is_same.hpp:21
constexpr float q8_to_float(uint8_t q8_val)
Convert Q8 format back to floating-point.
Definition mult_asm.h:255
constexpr uint8_t float_to_q8(float frac)
Convert floating-point fraction to Q8 format.
Definition mult_asm.h:239