Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions variants/host/HostArduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <stdlib.h>
#include <string.h>

#include <type_traits>

// Seeded rather than entropic: a run has to be reproducible from its seed, and
// the identity a node generates comes through here.
//
Expand Down Expand Up @@ -59,16 +61,22 @@ inline long hostRandom() {
// standard library breaks <limits>, whose numeric_limits<T>::min() then looks
// like a macro call with one argument. That produced 368 errors inside libstdc++
// and none in any MeshCore file, which is a confusing place to start debugging.
//
// Each returns a value, never a reference: when both arguments have one type,
// `decltype(a < b ? a : b)` is a reference to the function's own by-value
// parameter, gone once it returns. std::decay_t keeps the type a macro's
// expression would have, without the reference.
template <class T, class U>
constexpr auto min(T a, U b) -> decltype(a < b ? a : b) {
constexpr auto min(T a, U b) -> std::decay_t<decltype(a < b ? a : b)> {
return a < b ? a : b;
}
template <class T, class U>
constexpr auto max(T a, U b) -> decltype(a > b ? a : b) {
constexpr auto max(T a, U b) -> std::decay_t<decltype(a > b ? a : b)> {
return a > b ? a : b;
}
template <class T, class L, class H>
constexpr auto constrain(T v, L lo, H hi) -> decltype(v < lo ? lo : (v > hi ? hi : v)) {
constexpr auto constrain(T v, L lo, H hi)
-> std::decay_t<decltype(v < lo ? lo : (v > hi ? hi : v))> {
return v < lo ? lo : (v > hi ? hi : v);
}

Expand Down
47 changes: 47 additions & 0 deletions variants/host/arduino_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// min, max and constrain must return a value, as Arduino's macros give one.
//
// They are templates here, not macros (see HostArduino.h). Declared with
// `-> decltype(a < b ? a : b)`, a call whose two arguments have one type
// returned a reference to the function's own by-value parameter, which is gone
// once it returns. Optimised, `min(frame[2], (uint8_t)64)` in the companion's
// CMD_SET_AUTOADD_CONFIG read whatever was left on the stack and stored 0 as
// the hop limit.
//
// c++ -std=c++17 -O2 -I variants/host variants/host/arduino_test.cpp -o /tmp/arduino_test
// /tmp/arduino_test

#include "HostArduino.h"

#include <cstdio>
#include <type_traits>

static int failures = 0;

static void expect(bool ok, const char* what) {
if (!ok) {
printf("FAIL %s\n", what);
failures++;
}
}

// Inputs the optimiser can't fold, as a frame's bytes are.
volatile uint8_t frame[3] = {0x3a, 0x0e, 0x07};
volatile int lo = 2, hi = 9, v = 12;

int main() {
uint8_t a = frame[2], b = 64;
expect(!std::is_reference<decltype(min(a, b))>::value, "min of one type returns a value");
expect(!std::is_reference<decltype(max(a, b))>::value, "max of one type returns a value");
expect(!std::is_reference<decltype(constrain(a, a, b))>::value, "constrain of one type returns a value");

uint8_t hops = min(frame[2], (uint8_t)64); // as the companion's CMD_SET_AUTOADD_CONFIG calls it
expect(hops == 7, "min(uint8_t, uint8_t) gives the smaller");
expect(max(frame[1], frame[2]) == 0x0e, "max(uint8_t, uint8_t) gives the larger");
int vv = v, l = lo, h = hi;
expect(constrain(vv, l, h) == 9, "constrain(int, int, int) clamps");
expect(min(frame[2], 64) == 7, "min of mixed types still works");

if (failures) return 1;
printf("ok\n");
return 0;
}
Loading