/* SPDX-License-Identifier: LicenseRef-Starship-1.0 * Copyright (c) 2023-2026 Robert A. James */ /* test_inference.c -- v4's inference engine (v4/inference.h) against v3's own, to the bit (#189, Phase 7, * Task 3; Captain Bob, 2026-11-13: "Rewrite for v4", v3 the specification). v3's inference_engine.c and its * Q48.16 code are linked in and run on the same trajectories: fixed ones, and many made at random. */ #include "v4/inference.h" #include #include #include typedef uint64_t q48_16_t; q48_16_t compute_variance_q48(const uint64_t *heat_data, uint64_t length); uint32_t find_variance_inflection(const uint64_t *heat_data, uint64_t trajectory_length, q48_16_t full_variance); uint64_t infer_decay_slope_q48(const uint64_t *heat_data, uint64_t length); q48_16_t q48_log_approx(uint64_t x); q48_16_t q48_mul(q48_16_t a, q48_16_t b); /* what v3's engine reaches only through extract_heat_trajectory, which this test does not run */ void *vm_dictionary_lookup_by_word_id(void *vm, uint32_t id); void *vm_dictionary_lookup_by_word_id(void *vm, uint32_t id) { (void)vm; (void)id; return 0; } uint64_t rolling_window_export_execution_history(const void *w, uint32_t *out, uint64_t max); uint64_t rolling_window_export_execution_history(const void *w, uint32_t *out, uint64_t max) { (void)w; (void)out; (void)max; return 0; } void sf_mutex_lock(void *m); void sf_mutex_lock(void *m) { (void)m; } void sf_mutex_unlock(void *m); void sf_mutex_unlock(void *m) { (void)m; } static int failures = 0, checks = 0; #define CHECK(c,...) do{checks++; if(!(c)){failures++; printf("FAIL %s:%d: ",__FILE__,__LINE__); printf(__VA_ARGS__); printf("\n");}}while(0) static uint32_t rng = 0x2545F491u; static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 17; rng ^= rng << 5; return rng; } static uint64_t h[4096]; static int same(uint64_t n) { return v4_inference_variance(h, n) == compute_variance_q48(h, n) && v4_inference_window_width(h, n) == find_variance_inflection(h, n, 0) && v4_inference_decay_slope(h, n) == infer_decay_slope_q48(h, n); } int main(void) { unsigned k, bad = 0; uint64_t x; printf("v4 inference engine against v3's\n"); /* POST's own: 100 200 300, and 300 200 100 */ h[0] = 100; h[1] = 200; h[2] = 300; CHECK(v4_inference_variance(h, 3) == compute_variance_q48(h, 3), "the variance of 100 200 300: %llu, v3 %llu", (unsigned long long)v4_inference_variance(h, 3), (unsigned long long)compute_variance_q48(h, 3)); CHECK(v4_inference_window_width(h, 3) == find_variance_inflection(h, 3, 0), "its window width: %u, v3 %u", v4_inference_window_width(h, 3), find_variance_inflection(h, 3, 0)); h[0] = 300; h[1] = 200; h[2] = 100; CHECK(v4_inference_decay_slope(h, 3) == infer_decay_slope_q48(h, 3), "the decay slope of 300 200 100: %llu, v3 %llu", (unsigned long long)v4_inference_decay_slope(h, 3), (unsigned long long)infer_decay_slope_q48(h, 3)); /* the arithmetic underneath */ for (k = 0; k < 20000; k++) { uint64_t a = ((uint64_t)rnd() << 32 | rnd()) >> (rnd() % 40), b = ((uint64_t)rnd() << 32 | rnd()) >> (rnd() % 40); if (v4_q48_mul(a, b) != q48_mul(a, b) || v4_q48_log(a) != q48_log_approx(a)) bad++; } CHECK(bad == 0, "Q48.16 multiply and log agree on 20000 random pairs (%u differ)", bad); for (x = 0; x < 300000; x += 7) if (v4_q48_log(x) != q48_log_approx(x)) { bad++; break; } CHECK(bad == 0, "and the log on every 7th value to 300000"); /* trajectories: short and long, flat, ramps, noise, bursts, zeros */ bad = 0; for (k = 0; k < 400; k++) { uint64_t n = 2 + rnd() % 4095, i, shape = k % 5, top = 1 + rnd() % (k % 3 == 0 ? 60u : 100000u); for (i = 0; i < n; i++) h[i] = shape == 0 ? top : shape == 1 ? (n - i) * top / n : shape == 2 ? rnd() % (top + 1) : shape == 3 ? ((i / 64) % 2 ? top : rnd() % 3) : (rnd() % 4 ? 0 : rnd() % (top + 1)); if (!same(n)) { bad++; if (bad < 4) printf(" differ: shape %u, n %lu\n", (unsigned)shape, (unsigned long)n); } } CHECK(bad == 0, "variance, window width and decay slope agree on 400 trajectories (%u differ)", bad); printf(" %d checks, %d failures\n", checks, failures); return failures ? 1 : 0; }