3-phase PV router
Loading...
Searching...
No Matches
test_main.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
16
17#include <Arduino.h>
18#include <unity.h>
19
20#include "../halt_after_tests.h"
21
22#include "mult_asm.h"
23
24// Global volatile to prevent optimization without affecting timing
25volatile uint8_t g_optimizer_defeat = 0;
26
27void setUp(void)
28{
29 // Set up code here (if needed)
30}
31
32void tearDown(void)
33{
34 // Clean up code here (if needed)
35}
36
41{
42 int32_t result;
43
44 // Test positive × positive
45 multS16x16_to32(result, static_cast< int16_t >(2), static_cast< int16_t >(3));
46 TEST_ASSERT_EQUAL(6, result);
47
48 multS16x16_to32(result, static_cast< int16_t >(100), static_cast< int16_t >(100));
49 TEST_ASSERT_EQUAL(10000, result);
50
51 // Test positive × negative
52 multS16x16_to32(result, static_cast< int16_t >(2), static_cast< int16_t >(-3));
53 TEST_ASSERT_EQUAL(-6, result);
54
55 multS16x16_to32(result, static_cast< int16_t >(100), static_cast< int16_t >(-100));
56 TEST_ASSERT_EQUAL(-10000, result);
57
58 // Test negative × negative
59 multS16x16_to32(result, static_cast< int16_t >(-2), static_cast< int16_t >(-3));
60 TEST_ASSERT_EQUAL(6, result);
61
62 // Test edge cases
63 multS16x16_to32(result, static_cast< int16_t >(0), static_cast< int16_t >(1000));
64 TEST_ASSERT_EQUAL(0, result);
65
66 multS16x16_to32(result, static_cast< int16_t >(1000), static_cast< int16_t >(0));
67 TEST_ASSERT_EQUAL(0, result);
68
69 // Test maximum values (be careful of overflow)
70 multS16x16_to32(result, static_cast< int16_t >(32767), static_cast< int16_t >(1));
71 TEST_ASSERT_EQUAL(32767, result);
72
73 multS16x16_to32(result, static_cast< int16_t >(-32768), static_cast< int16_t >(1));
74 TEST_ASSERT_EQUAL(-32768, result);
75
76 // Test larger multiplications
77 multS16x16_to32(result, static_cast< int16_t >(1000), static_cast< int16_t >(1000));
78 TEST_ASSERT_EQUAL(1000000, result);
79
80 multS16x16_to32(result, static_cast< int16_t >(-1000), static_cast< int16_t >(1000));
81 TEST_ASSERT_EQUAL(-1000000, result);
82}
83
88{
89 uint32_t result;
90
91 // Test basic unsigned multiplication
92 multU16x16_to32(result, static_cast< uint16_t >(2), static_cast< uint16_t >(3));
93 TEST_ASSERT_EQUAL_UINT32(6, result);
94
95 multU16x16_to32(result, static_cast< uint16_t >(100), static_cast< uint16_t >(100));
96 TEST_ASSERT_EQUAL_UINT32(10000, result);
97
98 multU16x16_to32(result, static_cast< uint16_t >(1000), static_cast< uint16_t >(1000));
99 TEST_ASSERT_EQUAL_UINT32(1000000, result);
100
101 // Test edge cases
102 multU16x16_to32(result, static_cast< uint16_t >(0), static_cast< uint16_t >(1000));
103 TEST_ASSERT_EQUAL_UINT32(0, result);
104
105 multU16x16_to32(result, static_cast< uint16_t >(1000), static_cast< uint16_t >(0));
106 TEST_ASSERT_EQUAL_UINT32(0, result);
107
108 multU16x16_to32(result, static_cast< uint16_t >(1), static_cast< uint16_t >(65535));
109 TEST_ASSERT_EQUAL_UINT32(65535, result);
110
111 multU16x16_to32(result, static_cast< uint16_t >(65535), static_cast< uint16_t >(1));
112 TEST_ASSERT_EQUAL_UINT32(65535, result);
113
114 // Test maximum values (65535 * 65535)
115 multU16x16_to32(result, static_cast< uint16_t >(65535), static_cast< uint16_t >(65535));
116 TEST_ASSERT_EQUAL_UINT32(4294836225UL, result); // 65535² = 4,294,836,225
117
118 // Test typical ADC values (like voltage sample squared for V²)
119 multU16x16_to32(result, static_cast< uint16_t >(32768), static_cast< uint16_t >(32768));
120 TEST_ASSERT_EQUAL_UINT32(1073741824UL, result); // 32768² = 1,073,741,824
121
122 multU16x16_to32(result, static_cast< uint16_t >(1648), static_cast< uint16_t >(1648));
123 TEST_ASSERT_EQUAL_UINT32(2715904, result); // Typical V sample squared
124
125 // Test various powers of 2
126 multU16x16_to32(result, static_cast< uint16_t >(256), static_cast< uint16_t >(256));
127 TEST_ASSERT_EQUAL_UINT32(65536, result);
128
129 multU16x16_to32(result, static_cast< uint16_t >(512), static_cast< uint16_t >(512));
130 TEST_ASSERT_EQUAL_UINT32(262144, result);
131
132 multU16x16_to32(result, static_cast< uint16_t >(1024), static_cast< uint16_t >(1024));
133 TEST_ASSERT_EQUAL_UINT32(1048576, result);
134}
135
140{
141 uint32_t result;
142
143 // the low byte is dropped
144 multU24x16_to32_hi8(result, 1UL, static_cast< uint16_t >(1));
145 TEST_ASSERT_EQUAL_UINT32(0, result);
146
147 multU24x16_to32_hi8(result, 255UL, static_cast< uint16_t >(1));
148 TEST_ASSERT_EQUAL_UINT32(0, result);
149
150 multU24x16_to32_hi8(result, 256UL, static_cast< uint16_t >(1));
151 TEST_ASSERT_EQUAL_UINT32(1, result);
152
153 multU24x16_to32_hi8(result, 0UL, static_cast< uint16_t >(65535));
154 TEST_ASSERT_EQUAL_UINT32(0, result);
155
156 multU24x16_to32_hi8(result, 1000UL, static_cast< uint16_t >(1000));
157 TEST_ASSERT_EQUAL_UINT32(3906, result); // 1,000,000 >> 8
158
159 // full-scale average power x typical Q20 calibration values
160 multU24x16_to32_hi8(result, 262144UL, static_cast< uint16_t >(52429));
161 TEST_ASSERT_EQUAL_UINT32(53687296UL, result);
162
163 multU24x16_to32_hi8(result, 262144UL, static_cast< uint16_t >(44403));
164 TEST_ASSERT_EQUAL_UINT32(45468672UL, result);
165
166 // every partial product carries
167 multU24x16_to32_hi8(result, 0xFFFFFFUL, static_cast< uint16_t >(0xFFFF));
168 TEST_ASSERT_EQUAL_UINT32(4294901504UL, result);
169
170 multU24x16_to32_hi8(result, 0x123456UL, static_cast< uint16_t >(0xABCD));
171 TEST_ASSERT_EQUAL_UINT32(204966234UL, result);
172
173 multU24x16_to32_hi8(result, 0x800000UL, static_cast< uint16_t >(0x8000));
174 TEST_ASSERT_EQUAL_UINT32(1073741824UL, result);
175
176 multU24x16_to32_hi8(result, 12345UL, static_cast< uint16_t >(54321));
177 TEST_ASSERT_EQUAL_UINT32(2619502UL, result);
178
179 // sweep against the 64-bit reference
180 for (uint32_t a = 1; a < 0x1000000UL; a = a * 3 + 7)
181 {
182 for (uint32_t b = 1; b < 0x10000UL; b = b * 5 + 3)
183 {
184 multU24x16_to32_hi8(result, a, static_cast< uint16_t >(b));
185 TEST_ASSERT_EQUAL_UINT32(static_cast< uint32_t >((static_cast< uint64_t >(a) * b) >> 8), result);
186 }
187 }
188}
189
194{
195 int16_t result;
196
197 // Test Q8 fractional values
198 uint8_t half = float_to_q8(0.5f); // 128
199 uint8_t quarter = float_to_q8(0.25f); // 64
200 uint8_t full = float_to_q8(1.0f); // 255 (close to 1.0)
201 uint8_t three_quarters = float_to_q8(0.75f); // 192
202
203 // Test basic fractions
204 mult16x8_q8(result, static_cast< int16_t >(100), half);
205 TEST_ASSERT_EQUAL(50, result); // 100 * 0.5 = 50
206
207 mult16x8_q8(result, static_cast< int16_t >(100), quarter);
208 TEST_ASSERT_EQUAL(25, result); // 100 * 0.25 = 25
209
210 mult16x8_q8(result, static_cast< int16_t >(100), three_quarters);
211 TEST_ASSERT_EQUAL(75, result); // 100 * 0.75 = 75
212
213 mult16x8_q8(result, static_cast< int16_t >(100), full);
214 TEST_ASSERT_EQUAL(100, result); // 100 * ~1.0 ≈ 100
215
216 // Test negative values
217 mult16x8_q8(result, static_cast< int16_t >(-100), half);
218 TEST_ASSERT_EQUAL(-50, result);
219
220 mult16x8_q8(result, static_cast< int16_t >(-100), quarter);
221 TEST_ASSERT_EQUAL(-25, result);
222
223 // Test edge cases
224 mult16x8_q8(result, static_cast< int16_t >(100), static_cast< uint8_t >(0));
225 TEST_ASSERT_EQUAL(0, result);
226
227 mult16x8_q8(result, static_cast< int16_t >(0), half);
228 TEST_ASSERT_EQUAL(0, result);
229
230 // Test maximum values
231 mult16x8_q8(result, static_cast< int16_t >(32767), half);
232 TEST_ASSERT_EQUAL(16384, result); // 32767 * 0.5 ≈ 16383
233
234 mult16x8_q8(result, static_cast< int16_t >(-32768), half);
235 TEST_ASSERT_EQUAL(-16384, result); // -32768 * 0.5 = -16384
236}
237
242{
243 // Test float_to_q8 conversion
244 TEST_ASSERT_EQUAL(0, float_to_q8(0.0f));
245 TEST_ASSERT_EQUAL(64, float_to_q8(0.25f));
246 TEST_ASSERT_EQUAL(128, float_to_q8(0.5f));
247 TEST_ASSERT_EQUAL(192, float_to_q8(0.75f));
248 TEST_ASSERT_EQUAL(255, float_to_q8(1.0f));
249
250 // Test q8_to_float conversion (approximate due to floating-point precision)
251 TEST_ASSERT_FLOAT_WITHIN(0.01f, 0.0f, q8_to_float(0));
252 TEST_ASSERT_FLOAT_WITHIN(0.01f, 0.25f, q8_to_float(64));
253 TEST_ASSERT_FLOAT_WITHIN(0.01f, 0.5f, q8_to_float(128));
254 TEST_ASSERT_FLOAT_WITHIN(0.01f, 0.75f, q8_to_float(192));
255 TEST_ASSERT_FLOAT_WITHIN(0.01f, 0.996f, q8_to_float(255)); // 255/256 ≈ 0.996
256}
257
262{
263 int16_t result;
264
265 // Test rounding with values that should round up
266 // 100 * (127/256) = 49.609... should round to 50
267 mult16x8_q8(result, static_cast< int16_t >(100), static_cast< uint8_t >(127));
268 TEST_ASSERT_EQUAL(50, result);
269
270 // 100 * (129/256) = 50.390... should round to 50
271 mult16x8_q8(result, static_cast< int16_t >(100), static_cast< uint8_t >(129));
272 TEST_ASSERT_EQUAL(50, result);
273
274 // Test exact values
275 mult16x8_q8(result, static_cast< int16_t >(256), static_cast< uint8_t >(128)); // 256 * 0.5 = 128
276 TEST_ASSERT_EQUAL(128, result);
277
278 mult16x8_q8(result, static_cast< int16_t >(512), static_cast< uint8_t >(64)); // 512 * 0.25 = 128
279 TEST_ASSERT_EQUAL(128, result);
280}
281
286{
287 int32_t result32;
288 int16_t result16;
289
290 // Test multS16x16_to32 with values near 16-bit limits
291 multS16x16_to32(result32, static_cast< int16_t >(32767), static_cast< int16_t >(2));
292 TEST_ASSERT_EQUAL(65534, result32);
293
294 multS16x16_to32(result32, static_cast< int16_t >(-32768), static_cast< int16_t >(2));
295 TEST_ASSERT_EQUAL(-65536, result32);
296
297 multS16x16_to32(result32, static_cast< int16_t >(-32640), static_cast< int16_t >(257));
298 TEST_ASSERT_EQUAL(-8388480, result32);
299
300 // Test maximum positive * maximum positive (largest positive result)
301 multS16x16_to32(result32, static_cast< int16_t >(32767), static_cast< int16_t >(32767));
302 TEST_ASSERT_EQUAL(1073676289L, result32); // 32767² = 1,073,676,289
303
304 // Test maximum negative * maximum negative (largest positive result)
305 multS16x16_to32(result32, static_cast< int16_t >(-32768), static_cast< int16_t >(-32768));
306 TEST_ASSERT_EQUAL(1073741824L, result32); // (-32768)² = 1,073,741,824
307
308 // Test maximum positive * maximum negative (most negative result)
309 multS16x16_to32(result32, static_cast< int16_t >(32767), static_cast< int16_t >(-32768));
310 TEST_ASSERT_EQUAL(-1073709056L, result32); // 32767 * (-32768) = -1,073,709,056
311
312 // Test maximum negative * maximum positive (same as above)
313 multS16x16_to32(result32, static_cast< int16_t >(-32768), static_cast< int16_t >(32767));
314 TEST_ASSERT_EQUAL(-1073709056L, result32);
315
316 // Test one value at limit, other small
317 multS16x16_to32(result32, static_cast< int16_t >(32767), static_cast< int16_t >(-1));
318 TEST_ASSERT_EQUAL(-32767, result32);
319
320 multS16x16_to32(result32, static_cast< int16_t >(-32768), static_cast< int16_t >(-1));
321 TEST_ASSERT_EQUAL(32768, result32);
322
323 // Test typical ADC range values (like in PVRouter ISR)
324 multS16x16_to32(result32, static_cast< int16_t >(1648), static_cast< int16_t >(512)); // Typical voltage * current
325 TEST_ASSERT_EQUAL(843776, result32);
326
327 multS16x16_to32(result32, static_cast< int16_t >(-1648), static_cast< int16_t >(512)); // Negative voltage
328 TEST_ASSERT_EQUAL(-843776, result32);
329
330 multS16x16_to32(result32, static_cast< int16_t >(1648), static_cast< int16_t >(-512)); // Negative current
331 TEST_ASSERT_EQUAL(-843776, result32);
332
333 // Test mult16x8_q8 with extreme values
334 mult16x8_q8(result16, static_cast< int16_t >(32767), static_cast< uint8_t >(255)); // Maximum positive * maximum fraction
335 TEST_ASSERT_EQUAL(32639, result16); // Should be close to 32767
336
337 mult16x8_q8(result16, static_cast< int16_t >(-32768), static_cast< uint8_t >(255)); // Maximum negative * maximum fraction
338 TEST_ASSERT_EQUAL(-32640, result16); // Should be close to -32768
339
340 // Test boundary values with different fractions
341 mult16x8_q8(result16, static_cast< int16_t >(32767), static_cast< uint8_t >(1)); // Maximum positive * minimum fraction
342 TEST_ASSERT_EQUAL(128, result16); // 32767 * (1/256) ≈ 128
343
344 mult16x8_q8(result16, static_cast< int16_t >(-32768), static_cast< uint8_t >(1)); // Maximum negative * minimum fraction
345 TEST_ASSERT_EQUAL(-128, result16); // -32768 * (1/256) = -128
346
347 mult16x8_q8(result16, static_cast< int16_t >(32767), static_cast< uint8_t >(128)); // Maximum positive * 0.5
348 TEST_ASSERT_EQUAL(16384, result16); // 32767 * 0.5 ≈ 16384
349
350 mult16x8_q8(result16, static_cast< int16_t >(-32768), static_cast< uint8_t >(128)); // Maximum negative * 0.5
351 TEST_ASSERT_EQUAL(-16384, result16); // -32768 * 0.5 = -16384
352
353 // Test zero multiplication edge cases
354 mult16x8_q8(result16, static_cast< int16_t >(32767), static_cast< uint8_t >(0)); // Maximum positive * zero
355 TEST_ASSERT_EQUAL(0, result16);
356
357 mult16x8_q8(result16, static_cast< int16_t >(-32768), static_cast< uint8_t >(0)); // Maximum negative * zero
358 TEST_ASSERT_EQUAL(0, result16);
359
360 mult16x8_q8(result16, static_cast< int16_t >(0), static_cast< uint8_t >(255)); // Zero * maximum fraction
361 TEST_ASSERT_EQUAL(0, result16);
362
363 // Test very small fractions with different values
364 mult16x8_q8(result16, static_cast< int16_t >(1000), static_cast< uint8_t >(1)); // 1000 * (1/256) ≈ 4
365 TEST_ASSERT_EQUAL(4, result16);
366
367 mult16x8_q8(result16, static_cast< int16_t >(1000), static_cast< uint8_t >(2)); // 1000 * (2/256) ≈ 8
368 TEST_ASSERT_EQUAL(8, result16);
369
370 mult16x8_q8(result16, static_cast< int16_t >(256), static_cast< uint8_t >(1)); // 256 * (1/256) = 1
371 TEST_ASSERT_EQUAL(1, result16);
372
373 mult16x8_q8(result16, static_cast< int16_t >(128), static_cast< uint8_t >(1)); // 128 * (1/256) = 0.5 → rounds to 1
374 TEST_ASSERT_EQUAL(1, result16);
375
376 mult16x8_q8(result16, static_cast< int16_t >(127), static_cast< uint8_t >(1)); // 127 * (1/256) = 0.496... → rounds to 0
377 TEST_ASSERT_EQUAL(0, result16);
378
379 // Test typical filter values (like in PVRouter)
380 mult16x8_q8(result16, static_cast< int16_t >(1000), float_to_q8(0.004f)); // Typical EWMA factor
381 TEST_ASSERT_EQUAL(4, result16); // 1000 * 0.004 = 4
382
383 mult16x8_q8(result16, static_cast< int16_t >(-500), float_to_q8(0.004f)); // Negative with small factor
384 TEST_ASSERT_EQUAL(-2, result16); // -500 * 0.004 = -2
385
386 // Test near-overflow scenarios (values that could cause intermediate overflow)
387 mult16x8_q8(result16, static_cast< int16_t >(30000), static_cast< uint8_t >(200)); // Large value * large fraction
388 TEST_ASSERT_EQUAL(23438, result16); // 30000 * (200/256) = 23437.5 → 23438 (rounds up)
389
390 mult16x8_q8(result16, static_cast< int16_t >(-30000), static_cast< uint8_t >(200)); // Large negative * large fraction
391 TEST_ASSERT_EQUAL(-23437, result16); // -30000 * (200/256) = -23437.5 → -23438
392}
393
398{
399 int32_t asm_result, std_result;
400 uint32_t asm_result_u, std_result_u;
401 int16_t asm_result16, std_result16;
402
403 // Test values for signed multiplication
404 int16_t test_vals[] = { 100, -200, 1000, -1500, 32767, -32768 };
405 uint8_t test_fracs[] = { 64, 128, 192, 255 }; // 0.25, 0.5, 0.75, ~1.0
406
407 // Compare multS16x16_to32 results
408 for (uint8_t i = 0; i < 6; i++)
409 {
410 for (uint8_t j = 0; j < 6; j++)
411 {
412 multS16x16_to32(asm_result, test_vals[i], test_vals[j]);
413 std_result = (int32_t)test_vals[i] * test_vals[j];
414 TEST_ASSERT_EQUAL(std_result, asm_result);
415 }
416 }
417
418 // Test values for unsigned multiplication
419 uint16_t test_vals_u[] = { 0, 1, 100, 1000, 32767, 32768, 65535 };
420 const uint8_t num_vals_u = sizeof(test_vals_u) / sizeof(test_vals_u[0]);
421
422 // Compare multU16x16_to32 results
423 for (uint8_t i = 0; i < num_vals_u; i++)
424 {
425 for (uint8_t j = 0; j < num_vals_u; j++)
426 {
427 multU16x16_to32(asm_result_u, test_vals_u[i], test_vals_u[j]);
428 std_result_u = (uint32_t)test_vals_u[i] * test_vals_u[j];
429 TEST_ASSERT_EQUAL_UINT32(std_result_u, asm_result_u);
430 }
431 }
432
433 // Compare mult16x8_q8 results
434 for (uint8_t i = 0; i < 6; i++)
435 {
436 for (uint8_t j = 0; j < 4; j++)
437 {
438 mult16x8_q8(asm_result16, test_vals[i], test_fracs[j]);
439 std_result16 = ((int32_t)test_vals[i] * test_fracs[j] + 0x80) >> 8;
440 TEST_ASSERT_EQUAL(std_result16, asm_result16);
441 }
442 }
443}
444
449{
450 const uint16_t iterations = 1000;
451 int32_t result;
452
453 // Test data - use various values to prevent compiler optimizations
454 int16_t test_vals[] = { 100, -200, 1000, -1500, 32767, -32768 };
455 const uint8_t num_vals = sizeof(test_vals) / sizeof(test_vals[0]);
456
457 // Use volatile pointer to force storage without arithmetic overhead
458 volatile int32_t* volatile_result = &result;
459
460 // Warm up
461 for (uint8_t k = 0; k < 10; k++)
462 {
463 multS16x16_to32(result, test_vals[k % num_vals], test_vals[(k + 1) % num_vals]);
464 }
465
466 // Performance test for assembly multiplication
467 unsigned long start_time = micros();
468
469 for (uint16_t i = 0; i < iterations; i++)
470 {
471 for (uint8_t j = 0; j < num_vals; j++)
472 {
473 for (uint8_t k = 0; k < num_vals; k++)
474 {
475 multS16x16_to32(result, test_vals[j], test_vals[k]);
476 *volatile_result = result; // Force storage
477 }
478 }
479 }
480
481 unsigned long asm_time = micros() - start_time;
482
483 // Warm up standard multiplication
484 for (uint8_t k = 0; k < 10; k++)
485 {
486 result = (int32_t)test_vals[k % num_vals] * test_vals[(k + 1) % num_vals];
487 }
488
489 // Performance test for standard multiplication
490 start_time = micros();
491
492 for (uint16_t i = 0; i < iterations; i++)
493 {
494 for (uint8_t j = 0; j < num_vals; j++)
495 {
496 for (uint8_t k = 0; k < num_vals; k++)
497 {
498 result = (int32_t)test_vals[j] * test_vals[k];
499 *volatile_result = result; // Force storage
500 }
501 }
502 }
503
504 unsigned long std_time = micros() - start_time;
505
506 // Calculate total operations
507 uint32_t total_ops = (uint32_t)iterations * num_vals * num_vals;
508
509 // Report results
510 Serial.println(F("--- multS16x16_to32 Performance Results ---"));
511 Serial.print(F("Operations: "));
512 Serial.println(total_ops);
513 Serial.print(F("Assembly time: "));
514 Serial.print(asm_time);
515 Serial.println(F(" µs"));
516 Serial.print(F("Standard time: "));
517 Serial.print(std_time);
518 Serial.println(F(" µs"));
519
520 if (asm_time > 0 && std_time > 0)
521 {
522 Serial.print(F("Assembly ops/µs: "));
523 Serial.println((float)total_ops / asm_time, 2);
524 Serial.print(F("Standard ops/µs: "));
525 Serial.println((float)total_ops / std_time, 2);
526
527 if (asm_time < std_time)
528 {
529 Serial.print(F("✓ Assembly is "));
530 Serial.print((float)std_time / asm_time, 2);
531 Serial.println(F("x faster"));
532 }
533 else
534 {
535 Serial.print(F("⚠ Standard is "));
536 Serial.print((float)asm_time / std_time, 2);
537 Serial.println(F("x faster"));
538 }
539 }
540
541 // The test passes if assembly is reasonably competitive with standard
542 // Function call overhead may make assembly slower in micro-benchmarks
543 // but should be faster in real-world usage (ISR context)
544 TEST_ASSERT_TRUE(asm_time <= std_time * 10); // Allow 10x slower for function overhead
545}
546
551{
552 const uint16_t iterations = 1000;
553 uint32_t result;
554
555 // Test data - unsigned values typical for ADC and V² calculations
556 uint16_t test_vals[] = { 0, 100, 1000, 16384, 32768, 49152, 65535 };
557 const uint8_t num_vals = sizeof(test_vals) / sizeof(test_vals[0]);
558
559 // Use volatile pointer to force storage without arithmetic overhead
560 volatile uint32_t* volatile_result = &result;
561
562 // Warm up
563 for (uint8_t k = 0; k < 10; k++)
564 {
565 multU16x16_to32(result, test_vals[k % num_vals], test_vals[(k + 1) % num_vals]);
566 }
567
568 // Performance test for assembly multiplication
569 unsigned long start_time = micros();
570
571 for (uint16_t i = 0; i < iterations; i++)
572 {
573 for (uint8_t j = 0; j < num_vals; j++)
574 {
575 for (uint8_t k = 0; k < num_vals; k++)
576 {
577 multU16x16_to32(result, test_vals[j], test_vals[k]);
578 *volatile_result = result; // Force storage
579 }
580 }
581 }
582
583 unsigned long asm_time = micros() - start_time;
584
585 // Warm up standard multiplication
586 for (uint8_t k = 0; k < 10; k++)
587 {
588 result = (uint32_t)test_vals[k % num_vals] * test_vals[(k + 1) % num_vals];
589 }
590
591 // Performance test for standard multiplication
592 start_time = micros();
593
594 for (uint16_t i = 0; i < iterations; i++)
595 {
596 for (uint8_t j = 0; j < num_vals; j++)
597 {
598 for (uint8_t k = 0; k < num_vals; k++)
599 {
600 result = (uint32_t)test_vals[j] * test_vals[k];
601 *volatile_result = result; // Force storage
602 }
603 }
604 }
605
606 unsigned long std_time = micros() - start_time;
607
608 // Calculate total operations
609 uint32_t total_ops = (uint32_t)iterations * num_vals * num_vals;
610
611 // Report results
612 Serial.println(F("--- multU16x16_to32 Performance Results ---"));
613 Serial.print(F("Operations: "));
614 Serial.println(total_ops);
615 Serial.print(F("Assembly time: "));
616 Serial.print(asm_time);
617 Serial.println(F(" µs"));
618 Serial.print(F("Standard time: "));
619 Serial.print(std_time);
620 Serial.println(F(" µs"));
621
622 if (asm_time > 0 && std_time > 0)
623 {
624 Serial.print(F("Assembly ops/µs: "));
625 Serial.println((float)total_ops / asm_time, 2);
626 Serial.print(F("Standard ops/µs: "));
627 Serial.println((float)total_ops / std_time, 2);
628
629 if (asm_time < std_time)
630 {
631 Serial.print(F("✓ Assembly is "));
632 Serial.print((float)std_time / asm_time, 2);
633 Serial.println(F("x faster"));
634 }
635 else
636 {
637 Serial.print(F("⚠ Standard is "));
638 Serial.print((float)asm_time / std_time, 2);
639 Serial.println(F("x faster"));
640 }
641 }
642
643 // The test passes if assembly is reasonably competitive with standard
644 TEST_ASSERT_TRUE(asm_time <= std_time * 10); // Allow 10x slower for function overhead
645}
646
651{
652 const uint16_t iterations = 1000;
653 int16_t result;
654
655 // Test data
656 int16_t test_vals[] = { 100, -200, 1000, -1500, 32767, -32768 };
657 uint8_t test_fracs[] = { 32, 64, 96, 128, 160, 192, 224, 255 }; // Various Q8 fractions
658 const uint8_t num_vals = sizeof(test_vals) / sizeof(test_vals[0]);
659 const uint8_t num_fracs = sizeof(test_fracs) / sizeof(test_fracs[0]);
660
661 // Use volatile pointer to force storage without arithmetic overhead
662 volatile int16_t* volatile_result = &result;
663
664 // Warm up
665 for (uint8_t k = 0; k < 10; k++)
666 {
667 mult16x8_q8(result, test_vals[k % num_vals], test_fracs[k % num_fracs]);
668 }
669
670 // Performance test for assembly Q8 multiplication
671 unsigned long start_time = micros();
672
673 for (uint16_t i = 0; i < iterations; i++)
674 {
675 for (uint8_t j = 0; j < num_vals; j++)
676 {
677 for (uint8_t k = 0; k < num_fracs; k++)
678 {
679 mult16x8_q8(result, test_vals[j], test_fracs[k]);
680 *volatile_result = result; // Force storage
681 }
682 }
683 }
684
685 unsigned long asm_time = micros() - start_time;
686
687 // Warm up standard Q8 multiplication
688 for (uint8_t k = 0; k < 10; k++)
689 {
690 result = ((int32_t)test_vals[k % num_vals] * test_fracs[k % num_fracs] + 0x80) >> 8;
691 }
692
693 // Performance test for standard Q8 multiplication
694 start_time = micros();
695
696 for (uint16_t i = 0; i < iterations; i++)
697 {
698 for (uint8_t j = 0; j < num_vals; j++)
699 {
700 for (uint8_t k = 0; k < num_fracs; k++)
701 {
702 result = ((int32_t)test_vals[j] * test_fracs[k] + 0x80) >> 8;
703 *volatile_result = result; // Force storage
704 }
705 }
706 }
707
708 unsigned long std_time = micros() - start_time;
709
710 // Calculate total operations
711 uint32_t total_ops = (uint32_t)iterations * num_vals * num_fracs;
712
713 // Report results
714 Serial.println(F("--- mult16x8_q8 Performance Results ---"));
715 Serial.print(F("Operations: "));
716 Serial.println(total_ops);
717 Serial.print(F("Assembly time: "));
718 Serial.print(asm_time);
719 Serial.println(F(" µs"));
720 Serial.print(F("Standard time: "));
721 Serial.print(std_time);
722 Serial.println(F(" µs"));
723
724 if (asm_time > 0 && std_time > 0)
725 {
726 Serial.print(F("Assembly ops/µs: "));
727 Serial.println((float)total_ops / asm_time, 2);
728 Serial.print(F("Standard ops/µs: "));
729 Serial.println((float)total_ops / std_time, 2);
730
731 if (asm_time < std_time)
732 {
733 Serial.print(F("✓ Assembly is "));
734 Serial.print((float)std_time / asm_time, 2);
735 Serial.println(F("x faster"));
736 }
737 else
738 {
739 Serial.print(F("⚠ Standard is "));
740 Serial.print((float)asm_time / std_time, 2);
741 Serial.println(F("x faster"));
742 }
743 }
744
745 // The test passes if assembly is reasonably competitive with standard
746 // Function call overhead may make assembly slower in micro-benchmarks
747 // but should be faster in real-world usage (ISR context)
748 TEST_ASSERT_TRUE(asm_time <= std_time * 10); // Allow 10x slower for function overhead
749}
750
755{
756 const uint16_t samples = 1000;
757 volatile int32_t power_sum = 0;
758 volatile int32_t voltage_squared_sum = 0;
759 volatile int16_t filtered_current = 0;
760
761 // Simulate typical ISR values
762 int16_t voltage_samples[] = { 1650, 1648, 1645, 1640, 1630, 1615, 1595, 1570 };
763 int16_t current_samples[] = { 512, 510, 505, 498, 485, 468, 445, 415 };
764 int16_t prev_current[] = { 515, 512, 508, 500, 488, 470, 448, 420 };
765 uint8_t filter_factor = float_to_q8(0.004f); // Typical EWMA factor
766
767 const uint8_t num_samples = sizeof(voltage_samples) / sizeof(voltage_samples[0]);
768
769 // Warm up
770 for (uint8_t k = 0; k < 10; k++)
771 {
772 int32_t power, vsquared;
773 int16_t filter_delta;
774 multS16x16_to32(power, voltage_samples[k % num_samples], current_samples[k % num_samples]);
775 multS16x16_to32(vsquared, voltage_samples[k % num_samples], voltage_samples[k % num_samples]);
776 mult16x8_q8(filter_delta, static_cast< int16_t >(prev_current[k % num_samples] - current_samples[k % num_samples]), filter_factor);
777 }
778
779 // Performance test for assembly-optimized ISR simulation
780 unsigned long start_time = micros();
781
782 for (uint16_t i = 0; i < samples; i++)
783 {
784 uint8_t idx = i % num_samples;
785
786 // Typical ISR operations using assembly functions
787 int32_t instant_power, voltage_squared;
788 int16_t filter_delta;
789
790 multS16x16_to32(instant_power, voltage_samples[idx], current_samples[idx]);
791 multS16x16_to32(voltage_squared, voltage_samples[idx], voltage_samples[idx]);
792 mult16x8_q8(filter_delta, static_cast< int16_t >(prev_current[idx] - current_samples[idx]), filter_factor);
793
794 power_sum += instant_power;
795 voltage_squared_sum += voltage_squared;
796 filtered_current += filter_delta;
797 }
798
799 unsigned long asm_time = micros() - start_time;
800
801 // Reset for standard test
802 power_sum = 0;
803 voltage_squared_sum = 0;
804 filtered_current = 0;
805
806 // Warm up standard operations
807 for (uint8_t k = 0; k < 10; k++)
808 {
809 int32_t power = (int32_t)voltage_samples[k % num_samples] * current_samples[k % num_samples];
810 int32_t vsquared = (int32_t)voltage_samples[k % num_samples] * voltage_samples[k % num_samples];
811 int16_t filter_delta = ((int32_t)(prev_current[k % num_samples] - current_samples[k % num_samples]) * filter_factor + 0x80) >> 8;
812 }
813
814 // Performance test for standard C ISR simulation
815 start_time = micros();
816
817 for (uint16_t i = 0; i < samples; i++)
818 {
819 uint8_t idx = i % num_samples;
820
821 // Typical ISR operations using standard C
822 int32_t instant_power = (int32_t)voltage_samples[idx] * current_samples[idx];
823 int32_t voltage_squared = (int32_t)voltage_samples[idx] * voltage_samples[idx];
824 int16_t filter_delta = ((int32_t)(prev_current[idx] - current_samples[idx]) * filter_factor + 0x80) >> 8;
825
826 power_sum += instant_power;
827 voltage_squared_sum += voltage_squared;
828 filtered_current += filter_delta;
829 }
830
831 unsigned long std_time = micros() - start_time;
832
833 // Report results
834 Serial.println(F("--- ISR Simulation Performance Results ---"));
835 Serial.print(F("Samples processed: "));
836 Serial.println(samples);
837 Serial.print(F("Assembly ISR time: "));
838 Serial.print(asm_time);
839 Serial.println(F(" µs"));
840 Serial.print(F("Standard ISR time: "));
841 Serial.print(std_time);
842 Serial.println(F(" µs"));
843
844 if (asm_time > 0 && std_time > 0)
845 {
846 Serial.print(F("Assembly samples/µs: "));
847 Serial.println((float)samples / asm_time, 2);
848 Serial.print(F("Standard samples/µs: "));
849 Serial.println((float)samples / std_time, 2);
850
851 if (asm_time < std_time)
852 {
853 Serial.print(F("✓ Assembly ISR is "));
854 Serial.print((float)std_time / asm_time, 2);
855 Serial.println(F("x faster"));
856
857 // Calculate time savings per ISR call
858 float time_saved_per_sample = (float)(std_time - asm_time) / samples;
859 Serial.print(F("Time saved per ISR: "));
860 Serial.print(time_saved_per_sample, 2);
861 Serial.println(F(" µs"));
862 }
863 else
864 {
865 Serial.print(F("⚠ Standard ISR is "));
866 Serial.print((float)asm_time / std_time, 2);
867 Serial.println(F("x faster"));
868 }
869 }
870
871 // The ISR test is more realistic - functions are inlined in real usage
872 // Allow assembly to be up to 2x slower due to function call overhead in test
873 TEST_ASSERT_TRUE(asm_time <= std_time * 2);
874}
875
876void setup()
877{
878 delay(1000); // Wait for Serial to initialize
879 UNITY_BEGIN(); // Start Unity test framework
880}
881
882uint8_t i = 0;
883uint8_t max_blinks = 1;
884
885void loop()
886{
887 if (i < max_blinks)
888 {
889 // Functional tests
891 delay(100);
893 delay(100);
895 delay(100);
896 RUN_TEST(test_mult16x8_q8_basic);
897 delay(100);
899 delay(100);
901 delay(100);
902 RUN_TEST(test_edge_cases);
903 delay(100);
905 delay(100);
906
907 // Performance tests
908 Serial.println(F(""));
909 Serial.println(F("========================================"));
910 Serial.println(F("Performance Tests"));
911 Serial.println(F("========================================"));
913 delay(200);
915 delay(200);
917 delay(200);
919 delay(200);
920
921 ++i;
922 }
923 else if (i == max_blinks)
924 {
925 UNITY_END(); // End Unity test framework
927 ++i;
928 }
929}
void setup()
void setUp(void)
Definition test_main.cpp:27
void tearDown(void)
Definition test_main.cpp:28
void loop()
void test_edge_cases(void)
Test edge cases and potential overflow scenarios.
void test_performance_mult16x8_q8(void)
Performance comparison for Q8 fractional multiplication.
void test_multU16x16_to32_basic(void)
Test basic functionality of multU16x16_to32 (unsigned)
Definition test_main.cpp:87
void test_performance_multS16x16_to32(void)
Performance comparison between assembly and standard multiplication.
void test_multU24x16_to32_hi8_basic(void)
Test multU24x16_to32_hi8: (a x b) >> 8, a on 24 bits (energy bucket, #120)
void test_assembly_vs_standard(void)
Compare assembly results with standard multiplication.
void test_q8_conversion_helpers(void)
Test Q8 format conversion helpers.
uint8_t max_blinks
void test_mult16x8_q8_basic(void)
Test basic functionality of mult16x8_q8.
void test_mult16x8_q8_rounding(void)
Test rounding behavior of mult16x8_q8.
void test_performance_multU16x16_to32(void)
Performance comparison for unsigned multiplication (multU16x16_to32)
volatile uint8_t g_optimizer_defeat
Definition test_main.cpp:25
void test_multS16x16_to32_basic(void)
Test basic functionality of multS16x16_to32.
Definition test_main.cpp:40
uint8_t i
void test_performance_isr_simulation(void)
ISR-like performance test simulating real-world usage.
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
Stops the MCU once an embedded test suite has run.
void haltAfterTests()
Assembly-optimized multiplication functions for AVR microcontrollers.
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