3-phase PV router
Loading...
Searching...
No Matches
test_main.cpp File Reference

The integer datalog arithmetic on the target: exact results, and cycle counts against the float code it replaces. More...

#include <Arduino.h>
#include <unity.h>
#include <math.h>
#include "../bench_report.h"
#include "../halt_after_tests.h"
#include "datalog_math.h"
Include dependency graph for test_main.cpp:

Go to the source code of this file.

Classes

struct  Cycles
 Cycle statistics of one variant over all inputs. More...
 

Macros

#define MEASURE(stats, overhead, code)
 

Functions

static void bench (const __FlashStringHelper *avgName, const __FlashStringHelper *maxName, const Cycles &stats)
 The average and worst case of an integer function, for the CI history.
 
void loop ()
 
static uint16_t overhead ()
 
static void report (const __FlashStringHelper *name, const Cycles &stats)
 
void setUp (void)
 
void setup ()
 
static void startTimer ()
 
void tearDown (void)
 
void test_isqrt_is_faster_than_float_sqrt (void)
 
void test_isqrt_on_avr_edge_cases (void)
 
void test_isqrt_on_avr_every_boundary (void)
 Every rounding boundary of every root, on the target: the assembly loop is exact.
 
void test_vrms_is_faster_than_the_float_code (void)
 
void test_vrms_on_avr_matches_the_float_code (void)
 sqrtScaled() and vrms() on the target, against the float code, over the whole voltage range.
 
void test_watts_is_faster_than_the_float_code (void)
 

Variables

static const int32_t averagePowers [] { 0L, 1L, -7L, 350L, -2000L, 8380L, -15000L, 60000L, -120000L, 228000L }
 
static volatile int32_t inAveragePower
 
static volatile uint32_t inMeanSquare
 
static volatile uint8_t inPhase
 
static const uint32_t meanSquares [] { 0UL, 1UL, 100UL, 5000UL, 45000UL, 67076UL, 67081UL, 70000UL, 150000UL, 262144UL }
 
static constexpr uint8_t N_INPUTS { 10 }
 
static volatile int16_t out16
 
static volatile uint32_t out32
 
static volatile float powerCal [] { 0.1432025F, 0.1432025F, 0.1432025F }
 
static constexpr float powerCalConst [] { 0.1432025F, 0.1432025F, 0.1432025F }
 
static constexpr auto powerFixed { Datalog::powerCalibration(powerCalConst) }
 
static volatile float voltageCal [] { 0.888065256F, 0.888065256F, 0.888065256F }
 
static constexpr float voltageCalConst [] { 0.888065256F, 0.888065256F, 0.888065256F }
 
static constexpr auto voltageFixed { Datalog::voltageCalibration(100, voltageCalConst) }
 

Detailed Description

The integer datalog arithmetic on the target: exact results, and cycle counts against the float code it replaces.

Version
0.1
Date
2026-11-24

datalog_math.h replaced, in the datalog (#120):

  • power_L = (int16_t)(sumP / n * f_powerCal[phase])
  • Vrms_L_x100 = (uint32_t)(100 * f_voltageCal[phase] * sqrt(sum_Vsquared / n)) The assembly square root is checked at every rounding boundary of the 32-bit range, and the integer version must not be slower. Timer1 runs at the CPU clock (prescaler 1) with interrupts disabled, so the counts are exact CPU cycles, in simavr as on a real board. The overhead of the measurement itself is subtracted.

Definition in file test_main.cpp.

Macro Definition Documentation

◆ MEASURE

#define MEASURE ( stats,
overhead,
code )
Value:
do \
{ \
cli(); \
startTimer(); \
const uint16_t t0{ TCNT1 }; \
code; \
const uint16_t t1{ TCNT1 }; \
sei(); \
(stats).add(static_cast< uint16_t >(t1 - t0 - (overhead))); \
} while (0)
static uint16_t overhead()
Definition test_main.cpp:97

Definition at line 85 of file test_main.cpp.

Function Documentation

◆ bench()

static void bench ( const __FlashStringHelper * avgName,
const __FlashStringHelper * maxName,
const Cycles & stats )
static

The average and worst case of an integer function, for the CI history.

Definition at line 108 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ loop()

void loop ( )

Definition at line 303 of file test_main.cpp.

◆ overhead()

static uint16_t overhead ( )
static

Definition at line 97 of file test_main.cpp.

Here is the caller graph for this function:

◆ report()

static void report ( const __FlashStringHelper * name,
const Cycles & stats )
static

Definition at line 114 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setUp()

void setUp ( void )

Definition at line 27 of file test_main.cpp.

◆ setup()

void setup ( )

Definition at line 286 of file test_main.cpp.

Here is the call graph for this function:

◆ startTimer()

static void startTimer ( )
static

Definition at line 76 of file test_main.cpp.

◆ tearDown()

void tearDown ( void )

Definition at line 28 of file test_main.cpp.

◆ test_isqrt_is_faster_than_float_sqrt()

void test_isqrt_is_faster_than_float_sqrt ( void )

Definition at line 203 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_isqrt_on_avr_edge_cases()

void test_isqrt_on_avr_edge_cases ( void )

Definition at line 166 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_isqrt_on_avr_every_boundary()

void test_isqrt_on_avr_every_boundary ( void )

Every rounding boundary of every root, on the target: the assembly loop is exact.

round(sqrt(x)) changes from r to r + 1 between r^2 + r and r^2 + r + 1, since (r + 0.5)^2 = r^2 + r + 0.25. Checking r^2 - 1 (from r = 2), r^2, r^2 + r and r^2 + r + 1 for all 65536 roots covers every boundary of the 32-bit range (up to 65535^2 + 65535 = UINT32_MAX), and with them the largest remainders (worst cases of the 3-byte remainder) and the carries of each byte.

Definition at line 139 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_vrms_is_faster_than_the_float_code()

void test_vrms_is_faster_than_the_float_code ( void )

Definition at line 235 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_vrms_on_avr_matches_the_float_code()

void test_vrms_on_avr_matches_the_float_code ( void )

sqrtScaled() and vrms() on the target, against the float code, over the whole voltage range.

Definition at line 182 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_watts_is_faster_than_the_float_code()

void test_watts_is_faster_than_the_float_code ( void )

Definition at line 260 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ averagePowers

const int32_t averagePowers[] { 0L, 1L, -7L, 350L, -2000L, 8380L, -15000L, 60000L, -120000L, 228000L }
static

Definition at line 41 of file test_main.cpp.

◆ inAveragePower

volatile int32_t inAveragePower
static

Definition at line 45 of file test_main.cpp.

◆ inMeanSquare

volatile uint32_t inMeanSquare
static

Definition at line 44 of file test_main.cpp.

◆ inPhase

volatile uint8_t inPhase
static

Definition at line 46 of file test_main.cpp.

◆ meanSquares

const uint32_t meanSquares[] { 0UL, 1UL, 100UL, 5000UL, 45000UL, 67076UL, 67081UL, 70000UL, 150000UL, 262144UL }
static

Definition at line 40 of file test_main.cpp.

◆ N_INPUTS

uint8_t N_INPUTS { 10 }
staticconstexpr

Definition at line 42 of file test_main.cpp.

◆ out16

volatile int16_t out16
static

Definition at line 48 of file test_main.cpp.

◆ out32

volatile uint32_t out32
static

Definition at line 47 of file test_main.cpp.

◆ powerCal

volatile float powerCal[] { 0.1432025F, 0.1432025F, 0.1432025F }
static

Definition at line 31 of file test_main.cpp.

◆ powerCalConst

float powerCalConst[] { 0.1432025F, 0.1432025F, 0.1432025F }
staticconstexpr

Definition at line 34 of file test_main.cpp.

◆ powerFixed

auto powerFixed { Datalog::powerCalibration(powerCalConst) }
staticconstexpr

Definition at line 36 of file test_main.cpp.

◆ voltageCal

volatile float voltageCal[] { 0.888065256F, 0.888065256F, 0.888065256F }
static

Definition at line 32 of file test_main.cpp.

◆ voltageCalConst

float voltageCalConst[] { 0.888065256F, 0.888065256F, 0.888065256F }
staticconstexpr

Definition at line 35 of file test_main.cpp.

◆ voltageFixed

auto voltageFixed { Datalog::voltageCalibration(100, voltageCalConst) }
staticconstexpr

Definition at line 37 of file test_main.cpp.