31static volatile float powerCal[]{ 0.1432025F, 0.1432025F, 0.1432025F };
32static volatile float voltageCal[]{ 0.888065256F, 0.888065256F, 0.888065256F };
34static constexpr float powerCalConst[]{ 0.1432025F, 0.1432025F, 0.1432025F };
35static constexpr float voltageCalConst[]{ 0.888065256F, 0.888065256F, 0.888065256F };
40static const uint32_t
meanSquares[]{ 0UL, 1UL, 100UL, 5000UL, 45000UL, 67076UL, 67081UL, 70000UL, 150000UL, 262144UL };
41static const int32_t
averagePowers[]{ 0L, 1L, -7L, 350L, -2000L, 8380L, -15000L, 60000L, -120000L, 228000L };
53 uint16_t
min{ UINT16_MAX };
57 void add(uint16_t cycles)
85#define MEASURE(stats, overhead, code) \
90 const uint16_t t0{ TCNT1 }; \
92 const uint16_t t1{ TCNT1 }; \
94 (stats).add(static_cast< uint16_t >(t1 - t0 - (overhead))); \
108static void bench(
const __FlashStringHelper* avgName,
const __FlashStringHelper* maxName,
const Cycles& stats)
114static void report(
const __FlashStringHelper* name,
const Cycles& stats)
117 Serial.print(F(
": min "));
118 Serial.print(stats.
min);
119 Serial.print(F(
", avg "));
121 Serial.print(F(
", max "));
122 Serial.print(stats.
max);
123 Serial.println(F(
" cycles"));
141 uint32_t errors{ 0 };
142 uint32_t firstError{ 0 };
143 for (uint32_t root = 0; root <= UINT16_MAX; ++root)
145 const uint32_t square{ root * root };
146 const auto r{
static_cast< uint16_t
>(root) };
147 const uint16_t expected[]{ r, r, r,
static_cast< uint16_t
>(root < UINT16_MAX ? root + 1 : UINT16_MAX) };
148 const uint32_t
values[]{ root > 1 ? square - 1 : square, square, square + root, square + root + 1 };
149 const uint8_t count{
static_cast< uint8_t
>(root < UINT16_MAX ? 4 : 3) };
150 for (uint8_t
i = 0;
i < count; ++
i)
160 Serial.print(F(
"first wrong value: "));
161 Serial.println(firstError);
163 TEST_ASSERT_EQUAL_UINT32(0, errors);
177 TEST_ASSERT_EQUAL_UINT16(UINT16_MAX,
Datalog::isqrt(65535UL * 65535UL));
185 for (uint32_t meanSquare = 1; meanSquare <= 512UL * 512UL; meanSquare = meanSquare * 9 / 8 + 1)
189 const float exact{ sqrtf(
static_cast< float >(meanSquare)) *
static_cast< float >(1UL << bits) };
190 TEST_ASSERT_FLOAT_WITHIN(0.5F + exact * 1.2e-7F, exact, root);
206 Cycles floatSqrt, intSqrt, scaledSqrt;
220 Serial.println(F(
"--- square root ---"));
221 report(F(
"float sqrt() "), floatSqrt);
222 report(F(
"Datalog::isqrt() "), intSqrt);
223 report(F(
"Datalog::sqrtScaled "), scaledSqrt);
224 bench(F(
"datalog: isqrt avg"), F(
"datalog: isqrt max"), intSqrt);
225 bench(F(
"datalog: sqrtScaled avg"), F(
"datalog: sqrtScaled max"), scaledSqrt);
227 TEST_ASSERT_LESS_OR_EQUAL_UINT16(floatSqrt.
max, scaledSqrt.
max);
228 TEST_ASSERT_LESS_OR_EQUAL_UINT32(floatSqrt.
sum, scaledSqrt.
sum);
238 Cycles floatVrms, intVrms;
251 Serial.println(F(
"--- Vrms_L_x100 ---"));
252 report(F(
"float (as before) "), floatVrms);
253 report(F(
"Datalog::vrms() "), intVrms);
254 bench(F(
"datalog: vrms avg"), F(
"datalog: vrms max"), intVrms);
256 TEST_ASSERT_LESS_OR_EQUAL_UINT16(floatVrms.
max, intVrms.
max);
257 TEST_ASSERT_LESS_OR_EQUAL_UINT32(floatVrms.
sum, intVrms.
sum);
263 Cycles floatWatts, intWatts;
277 Serial.println(F(
"--- power_L ---"));
278 report(F(
"float (as before) "), floatWatts);
279 report(F(
"Datalog::watts() "), intWatts);
280 bench(F(
"datalog: watts avg"), F(
"datalog: watts max"), intWatts);
282 TEST_ASSERT_LESS_OR_EQUAL_UINT16(floatWatts.
max, intWatts.
max);
283 TEST_ASSERT_LESS_OR_EQUAL_UINT32(floatWatts.
sum, intWatts.
sum);
Machine-readable lines per benchmark result, for the CI performance report.
void benchCycles(const __FlashStringHelper *name, const uint32_t cycles, const uint32_t ops=1)
A result in CPU cycles, reported twice: in microseconds and in cycles.
Integer arithmetic of the datalog: power and RMS voltage per phase.
static constexpr auto powerFixed
static constexpr auto voltageFixed
static volatile uint32_t inMeanSquare
void test_isqrt_is_faster_than_float_sqrt(void)
static constexpr float powerCalConst[]
static volatile uint32_t out32
#define MEASURE(stats, overhead, code)
static const uint32_t meanSquares[]
static const int32_t averagePowers[]
void test_watts_is_faster_than_the_float_code(void)
static volatile float powerCal[]
static volatile int32_t inAveragePower
void test_isqrt_on_avr_edge_cases(void)
static uint16_t overhead()
static constexpr uint8_t N_INPUTS
static volatile int16_t out16
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.
static volatile float voltageCal[]
static void report(const __FlashStringHelper *name, const Cycles &stats)
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.
static constexpr float voltageCalConst[]
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)
static volatile uint8_t inPhase
Stops the MCU once an embedded test suite has run.
constexpr FixedCalibration< N > voltageCalibration(const uint16_t factor, const float(&cal)[N])
The voltage calibration in fixed point, for vrms(), built at compile time.
int16_t watts(const int32_t averagePower, const uint16_t calFixed, const uint8_t shift)
The average power of a phase, in watts.
uint16_t isqrt(const uint32_t value)
Integer square root, rounded to the nearest integer.
constexpr FixedCalibration< N > powerCalibration(const float(&cal)[N])
The power calibration in fixed point, for watts(), built at compile time.
uint16_t vrms(const uint32_t meanSquare, const uint16_t calFixed, const uint8_t shift)
The RMS voltage of a phase.
uint16_t sqrtScaled(const uint32_t meanSquare, uint8_t &fractionBits)
The square root of a mean square, with as many fractional bits as fit.
static double reference(int32_t averagePower, float cal)
The float arithmetic being replaced, in bucket units.
Cycle statistics of one variant over all inputs.
void add(uint16_t cycles)