diff --git a/README.md b/README.md index c40077e..16a8a61 100644 --- a/README.md +++ b/README.md @@ -464,6 +464,8 @@ Integer only, with no libc. Host-tested (no console run). See scale (`SH2Z_VD_FORCE_FRAC`) that carries the engine force with more fraction bits (default 16: unchanged code). `vehicle-dynamics-6` is a build-time stop (`SH2Z_VD_LOCATE_FAR`) for a closest-point search that circles when the body is far off the line, as over a gap (default 0: unchanged code). + `vehicle-dynamics-7` adds two build-time changes (`SH2Z_VD_LOCATE_DAMP`, `SH2Z_VD_LOCATE_FLIP`) for the two other ways that + search runs all its passes, a swing and a flip between two nodes (default 0: unchanged code). - [`sh2zam/body_contact_experimental.h`](saturn/include/sh2zam/body_contact_experimental.h) (`body-contact-1`) resolves contact between vehicles that touch as spheres: shared impact damage, a push while they close, attackers and contact credit. `vehicle-dynamics-3` feeds it into the diff --git a/saturn/VALIDATION.md b/saturn/VALIDATION.md index bf6501b..bbe2d55 100644 --- a/saturn/VALIDATION.md +++ b/saturn/VALIDATION.md @@ -48,6 +48,7 @@ and scope. - [vehicle-dynamics-4](validation/vehicle-dynamics-4.md): 2026-11-22 (engine force carried in Q(16+n), vehicle-dynamics-4) - [vehicle-dynamics-5](validation/vehicle-dynamics-5.md): 2026-11-22 (half-pipe road shape, vehicle-dynamics-5) - [vehicle-dynamics-6](validation/vehicle-dynamics-6.md): 2026-11-22 (search stop for a body far off the line, vehicle-dynamics-6) +- [vehicle-dynamics-7](validation/vehicle-dynamics-7.md): 2026-11-23 (search damping and flip stop, vehicle-dynamics-7) - [vehicle-kernels-1](validation/vehicle-kernels-1.md): 2026-11-17 (vehicle kernels, vehicle-kernels-1) - [view-camera-1](validation/view-camera-1.md): 2026-11-17 (view from a camera, view-camera-1) - [view-merge-2](validation/view-merge-2.md): 2026-11-17 (tier texture-merge planner, view-merge-2) diff --git a/saturn/VEHICLE_DYNAMICS.md b/saturn/VEHICLE_DYNAMICS.md index 502584d..9022b5b 100644 --- a/saturn/VEHICLE_DYNAMICS.md +++ b/saturn/VEHICLE_DYNAMICS.md @@ -114,6 +114,41 @@ units for RUN passes in a row. It keeps the state it has; `sh2z_vd_locate` then (every pass, with the residual). - Every translation unit of one image must use the same values. +## Search damping and flip stop (`vehicle-dynamics-7`, `SH2Z_VD_LOCATE_DAMP`, `SH2Z_VD_LOCATE_FLIP`) + +The search steps the spline parameter by the projection of the residual on the tangent, `t += d.f / |f|^2`, and leaves out +the curvature term of the Newton step, `|f|^2 + (p - x).p''`. A body hundreds of units off the line, such as one over a gap, +runs the whole 99 passes in three ways, two of them under the `vehicle-dynamics-6` line (128 units) and so not stopped by it: + +1. **Swing.** Near the centre of curvature (`-disp.p'' / |f|^2` near 1) the step is about twice the Newton step: the residual + alternates in sign and closes by about 0.94 a pass (36 units to 1 in 54 passes, 883 units off the line). +2. **Join flip.** The closest point is the join of two nodes, with a different tangent length either side; the search flips + between them, back to the same parameter every second pass, with a residual that does not fall (47 units, drifting by + 0.01 a pass). There is no exact repeat, so the cycle cannot be skipped exactly. +3. **Past the centre of curvature.** The curvature term passes -1, the minimum is gone, and the residual falls and rises + again. Not changed by this tag. + +`SH2Z_VD_LOCATE_DAMP` (build time, passes 0 to 99, default 0 = off): after that many passes of the call, the secant step +through the last two passes, `-along * prev_dt / (along - prev_along)`, replaces the pass's step when it is not longer. It has +the step's sign (the slope is negative), so the search is only ever damped, never pushed further than before. A residual that +falls one way takes the longer step. A node change forgets the last pass. A search of DAMP passes or fewer is unchanged. + +`SH2Z_VD_LOCATE_FLIP` (build time, flips 1 or more, default 0 = off): the search ends once it has flipped between two nodes +that many times in a row, each time back to the parameter it had two passes before (within 2^-10 of a node), and on a pass +of the parity the 99th pass has. The state is then the one the 99-pass search would end on, to the drift of the cycle: the +join is on one side or the other of the closest point, and which side is the parity of the pass. + +- Off: the object code of every earlier tag is unchanged (the three vehicle units compared with `cmp`, CI flags). +- Both on (DAMP 8, FLIP 3): +504 bytes of text in `sh2zam_vehicle_dynamics.o`. A secant step costs one division + (`sh2z_divu_div64`) on a pass; the flip test, three compares. +- A search neither change acted on is the earlier search word for word. Where the earlier search converged within 700 units + of the line and a change acted on it, the changed search converged too, within 1 unit of its end state, in 339 of 340 + cases in the host test; the exception ended 12.5 units off it (a flip stop at 40 passes; the earlier search closed in 59 more). + Where the earlier search did not converge there is no right end state: the changed one may end elsewhere. The caller + checks its outcome (the validation record gives the one made on a road with a jump). +- Hooks for host builds, empty by default: `SH2Z_VD_LOCATE_DAMP_HIT(old_dt, new_dt)` and `SH2Z_VD_LOCATE_FLIP_HIT()`. +- Every translation unit of one image must use the same values. + ## Formats | Quantity | Format | diff --git a/saturn/include/sh2zam/vehicle_dynamics_experimental.h b/saturn/include/sh2zam/vehicle_dynamics_experimental.h index 1fe6a39..98ab91d 100644 --- a/saturn/include/sh2zam/vehicle_dynamics_experimental.h +++ b/saturn/include/sh2zam/vehicle_dynamics_experimental.h @@ -82,6 +82,32 @@ #error "SH2Z_VD_LOCATE_FAR must be 0..32767 and SH2Z_VD_LOCATE_FAR_RUN at least 1" #endif +/* Contract tag vehicle-dynamics-7. The other two ways the closest-point search runs all 99 passes of a body hundreds + * of units off the line, both with a residual under the vehicle-dynamics-6 line: + * - the step of a pass ignores the curvature of the line, so where the body sits near the centre of curvature it is + * about twice the Newton step and the residual swings from side to side, closing by 0.94 a pass. + * SH2Z_VD_LOCATE_DAMP (build time, passes 0..99, default 0 = off) replaces the step, after that many passes of + * the call, by the secant step through the last two passes when that step is not longer. It has the step's + * sign. Searches of DAMP passes or fewer are unchanged. + * - the closest point is the join of two nodes, and the search flips between them with a residual that does not + * fall. SH2Z_VD_LOCATE_FLIP (build time, flips >= 1, default 0 = off) ends the search once it has flipped that many + * times in a row, each time returning to the parameter it had two passes before to within 2^-10 of a node, and + * on a pass of the parity the 99th pass has: the state is the one the 99-pass search would end on, to the drift + * of the cycle. + * Over 120,000 randomised searches (a stadium and rough roads, targets up to 4,000 units off the line) a search + * that neither change acted on is the earlier search word for word (107,587 of them); where the earlier search + * converged within 700 units of the line the changed search converged too, within 1 unit of its end state + * (the host test states the bound it measured). Where the earlier search did not converge there is no + * right end state to compare with, and a changed search can end elsewhere. Off, the object code of every + * earlier tag is unchanged. Every translation unit of one image must use the same values. + * SH2Z_VD_LOCATE_DAMP_HIT(old, new) and SH2Z_VD_LOCATE_FLIP_HIT() are empty statements unless a build defines them. */ +#ifndef SH2Z_VD_LOCATE_DAMP +#define SH2Z_VD_LOCATE_DAMP 0 +#endif +#ifndef SH2Z_VD_LOCATE_FLIP +#define SH2Z_VD_LOCATE_FLIP 0 +#endif + #ifdef __cplusplus extern "C" { #endif diff --git a/saturn/src/sh2zam_vehicle_dynamics.c b/saturn/src/sh2zam_vehicle_dynamics.c index aa9fbf8..2e356f3 100644 --- a/saturn/src/sh2zam_vehicle_dynamics.c +++ b/saturn/src/sh2zam_vehicle_dynamics.c @@ -36,6 +36,16 @@ #ifndef SH2Z_VD_LOCATE_PASS #define SH2Z_VD_LOCATE_PASS(d) #endif +/* vehicle-dynamics-7: host-test hooks, empty statements unless a build defines them. DAMP_HIT(old, new) is invoked + * with the step of the pass (Q30 of t) and the secant step that replaces it; FLIP_HIT() when the flip stop ends a search. */ +#ifndef SH2Z_VD_LOCATE_DAMP_HIT +#define SH2Z_VD_LOCATE_DAMP_HIT(old_dt, new_dt) +#endif +#ifndef SH2Z_VD_LOCATE_FLIP_HIT +#define SH2Z_VD_LOCATE_FLIP_HIT() +#endif +/* The parameter distance (Q30) under which a flip-flop returns to the same point (vehicle-dynamics-7). */ +#define VD_FLIP_T (ONE30 >> 10) /* The squared residual (Q32) that SH2Z_VD_LOCATE_FAR units make. */ #define VD_FAR_SQ (((int64_t)SH2Z_VD_LOCATE_FAR * SH2Z_VD_LOCATE_FAR) << 32) @@ -152,6 +162,15 @@ int sh2z_vd_locate(sh2z_vd_body_t *b, const sh2z_vd_track_t *k, const int32_t ta int i; #if SH2Z_VD_LOCATE_FAR int far_run = 0; +#endif +#if SH2Z_VD_LOCATE_DAMP + int pass = 0; + int32_t prev_along = 0, prev_dt = 0; +#endif +#if SH2Z_VD_LOCATE_FLIP + int flips = 0; + int32_t t1 = b->t, t2 = b->t; + uint32_t node1 = b->node, node2 = b->node; #endif for(i = 0; i < 3; i++) d[i] = target[i] - b->ground[i]; while(sumsq(d) > tolerance && --iterations != 0) { @@ -167,17 +186,57 @@ int sh2z_vd_locate(sh2z_vd_body_t *b, const sh2z_vd_track_t *k, const int32_t ta SH2Z_VD_LOCATE_FAR_HIT(); break; } +#endif +#if SH2Z_VD_LOCATE_FLIP + /* vehicle-dynamics-7: the join of two nodes holds the closest point when the search keeps flipping between + * them and returns to the same parameter, to 2^-10 of a node, every second pass. */ + flips = (b->node == node2 && b->node != node1 && b->t - t2 < VD_FLIP_T && t2 - b->t < VD_FLIP_T) ? flips + 1 : 0; + node2 = node1; + node1 = b->node; + t2 = t1; + t1 = b->t; + if(flips >= SH2Z_VD_LOCATE_FLIP && (iterations & 1) == 0) { + SH2Z_VD_LOCATE_FLIP_HIT(); + break; + } #endif const int64_t dot = (int64_t)d[0] * b->fwd[0] + (int64_t)d[1] * b->fwd[1] + (int64_t)d[2] * b->fwd[2]; const int32_t along = vd_div(dot, b->fwd_mag); /* Q16 */ +#if SH2Z_VD_LOCATE_DAMP + int32_t dt = vd_divq(along, b->fwd_mag, 30); + /* vehicle-dynamics-7: after SH2Z_VD_LOCATE_DAMP passes, the secant step through the last two passes + * replaces the step when it is not longer (it then has the step's sign: the slope is negative). A residual that swings from side + * to side (the step is then about twice the Newton step) closes in two or three passes; a residual + * that falls one way keeps the step. */ + if(++pass > SH2Z_VD_LOCATE_DAMP && prev_dt != 0) { + const int32_t da = along - prev_along; + if(da != 0 && (da < 0) != (prev_dt < 0)) { + const int32_t s = vd_div(-(int64_t)along * prev_dt, da); + if((s < 0 ? -(int64_t)s : (int64_t)s) <= (dt < 0 ? -(int64_t)dt : (int64_t)dt)) { + SH2Z_VD_LOCATE_DAMP_HIT(dt, s); + dt = s; + } + } + } + prev_along = along; + prev_dt = dt; + int64_t t = (int64_t)b->t + dt; +#else int64_t t = (int64_t)b->t + vd_divq(along, b->fwd_mag, 30); +#endif int32_t dist, fhat[3], perp[3], n[3]; if(t >= ONE30) { if(t > (int64_t)ONE30 * 19 / 10) return -1; +#if SH2Z_VD_LOCATE_DAMP + prev_dt = 0; +#endif b->node = wrap(k, b->node + 1u); t = (int64_t)mu(vd_sat(t - ONE30), vd_divq(b->fwd_mag, tangent_mag(k, b->node, 0), 30)); } else if(t < 0) { if(t < -(int64_t)ONE30 * 9 / 10) return -1; +#if SH2Z_VD_LOCATE_DAMP + prev_dt = 0; +#endif b->node = wrap(k, b->node + k->count - 1u); t = (int64_t)mu(vd_sat(t), vd_divq(b->fwd_mag, tangent_mag(k, b->node, ONE30), 30)) + ONE30; } diff --git a/saturn/tests/mutate_vehicle_locate7.sh b/saturn/tests/mutate_vehicle_locate7.sh new file mode 100644 index 0000000..d86eb32 --- /dev/null +++ b/saturn/tests/mutate_vehicle_locate7.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# Mutation controls for test_vehicle_locate7.c: each mutated copy of the search (or each extreme configuration) must make the test fail. Usage: mutate_vehicle_locate7.sh [CC] (run from the saturn directory) +set -u +CC=${1:-gcc} +T=$(mktemp -d) +SRC=src/sh2zam_vehicle_dynamics.c +build() { # name VD_SRC extra... + name=$1; src=$2; shift 2 + $CC -std=c11 -O2 -Iinclude tests/test_vehicle_locate7.c $SRC src/sh2zam_unit30.c src/sh2zam_divu.c \ + -DVD_SRC="\"$src\"" "$@" -lm -o "$T/$name" 2>"$T/$name.err" || { echo "mutant $name: build failed"; cat "$T/$name.err" | head -5; return 1; } + if "$T/$name" >"$T/$name.out" 2>&1; then + if [ "$name" = clean ]; then echo "passes $name"; else echo "SURVIVED $name"; fail=1; fi + else + if [ "$name" = clean ]; then echo "FAILED $name"; fail=1; else echo "caught $name: $(tail -1 "$T/$name.out")"; fi + fi +} +mut() { # name sed-expression + sed "$2" $SRC > "$T/$1.c"; cmp -s $SRC "$T/$1.c" && { echo "mutant $1 did not change the source"; fail=1; return; } + build "$1" "$T/$1.c" +} +fail=0 +# the unmutated source must pass +build clean "$(pwd)/$SRC" || fail=1 +build ctl_damp_one "$(pwd)/$SRC" -DB_DAMP=1 +build ctl_flip_one "$(pwd)/$SRC" -DB_FLIP=1 +build ctl_flip_two "$(pwd)/$SRC" -DB_FLIP=2 +mut step_twice_as_long 's/<= (dt < 0 ? -(int64_t)dt : (int64_t)dt)) {/<= 2 * (dt < 0 ? -(int64_t)dt : (int64_t)dt)) {/' +mut secant_sign 's/vd_div(-(int64_t)along \* prev_dt, da)/vd_div((int64_t)along * prev_dt, da)/' +mut damp_from_second_pass 's/if(++pass > SH2Z_VD_LOCATE_DAMP \&\& prev_dt != 0) {/if(++pass > 0 \&\& prev_dt != 0) {/' +mut secant_slope_sign 's/(da < 0) != (prev_dt < 0)/(da < 0) == (prev_dt < 0)/' +mut no_damp_hook 's/SH2Z_VD_LOCATE_DAMP_HIT(dt, s);//' +mut flip_without_parity 's/ \&\& (iterations \& 1) == 0) {/) {/' +mut flip_no_return_test 's/ \&\& b->t - t2 < VD_FLIP_T \&\& t2 - b->t < VD_FLIP_T//' +mut flip_returns_minus_one 's/SH2Z_VD_LOCATE_FLIP_HIT();/SH2Z_VD_LOCATE_FLIP_HIT(); return -1;/' +mut no_flip_hook 's/SH2Z_VD_LOCATE_FLIP_HIT();//' +rm -rf "$T" +exit $fail diff --git a/saturn/tests/test_vehicle_locate7.c b/saturn/tests/test_vehicle_locate7.c new file mode 100644 index 0000000..9743517 --- /dev/null +++ b/saturn/tests/test_vehicle_locate7.c @@ -0,0 +1,278 @@ +/* Host tests for vehicle-dynamics-7: the secant damping (SH2Z_VD_LOCATE_DAMP) and the node flip-flop stop + * (SH2Z_VD_LOCATE_FLIP) of the closest-point search. + * The model is built a second time under private names, as the contact entry points do, with + * SH2Z_VD_LOCATE_DAMP 8 and SH2Z_VD_LOCATE_FLIP 3 (B_DAMP and B_FLIP override them for the controls) and the + * vehicle-dynamics-6 stop off. The linked library (both off, the 99-pass search of every earlier tag) is the oracle. + * Over randomised bodies on a stadium and on rough roads, and targets from level with the line to 2000 units off it: + * - a search in which neither change acted equals the library's, the whole body, word for word; + * - every damped step is not longer than the step it replaces and has its sign (the hook sees both); + * - a damped step is taken only after DAMP passes of the call; + * - a search the flip stop ends ends once, after at least 5 passes, on a pass of the parity the 99th pass has, and + * keeps the target as the body position; + * - where the library's search converged (residual within 1 unit) within 700 units of the line and a change acted + * on it, the copy converged too and ends within B_BOUND units (distance between the spline points) of the + * library's end state; where the library did not converge, the numbers are printed (there is no right end state); + * - the set holds damped searches, flip-ended searches and searches that neither change touched. + * The controls (tests/mutate_vehicle_locate7.sh) plant a step twice as long, a reversed secant, a slope test the wrong + * way, a damping from the second pass, a flip stop without the parity, without the return test, ending a search as + * off the line, and both hooks removed, and build with DAMP 1 and FLIP 1 and 2; each must fail. + * The default build's objects are compared byte for byte against the previous library by the objcmp step of the capture. + * + * Copyright (c) 2026 SH2ZAM Contributors. MIT License. + * Inspired by SH4ZAM by Falco Girgis and SH4ZAM Contributors. + */ +#include "sh2zam/vehicle_dynamics_experimental.h" +#include "sh2zam/unit30_experimental.h" +#include +#include +#include +#include + +#ifndef VD_SRC +#define VD_SRC "../src/sh2zam_vehicle_dynamics.c" +#endif + +#ifndef B_DAMP +#define B_DAMP 8 +#endif +#ifndef B_FLIP +#define B_FLIP 3 +#endif +#ifndef B_BOUND +#define B_BOUND 1.0 +#endif +static unsigned b_passes, b_damp_hits, b_flip_hits, b_damp_bad, b_damp_early; +static void b_note_pass(const int32_t d[3]); +static void b_damp(int32_t old_dt, int32_t new_dt); + +static void vdb_step(sh2z_vd_body_t *, const sh2z_vd_track_t *, const sh2z_vd_vehicle_t *, const sh2z_vd_rules_t *, + const sh2z_vd_input_t *); +static int vdb_locate(sh2z_vd_body_t *, const sh2z_vd_track_t *, const int32_t[3], int, int64_t); +static int32_t vdb_lap_distance(sh2z_vd_body_t *, const sh2z_vd_track_t *); +static __attribute__((unused)) int32_t vdb_body_lateral(const sh2z_vd_body_t *); +static __attribute__((unused)) void vdb_line_view(const sh2z_vd_body_t *, const sh2z_vd_track_t *, sh2z_vd_line_view_t *); +static __attribute__((unused)) void vdb_apply_kick(sh2z_vd_body_t *, int32_t, int32_t); +static void vdb_spline(const sh2z_vd_track_t *, uint32_t, int32_t, int32_t[3], int32_t[3]); +#define sh2z_vd_step vdb_step +#define sh2z_vd_locate vdb_locate +#define sh2z_vd_lap_distance vdb_lap_distance +#define sh2z_vd_body_lateral vdb_body_lateral +#define sh2z_vd_line_view vdb_line_view +#define sh2z_vd_apply_kick vdb_apply_kick +#define sh2z_vd_spline vdb_spline +#undef SH2Z_VD_LOCATE_FAR +#define SH2Z_VD_LOCATE_FAR 0 +#undef SH2Z_VD_LOCATE_DAMP +#define SH2Z_VD_LOCATE_DAMP B_DAMP +#undef SH2Z_VD_LOCATE_FLIP +#define SH2Z_VD_LOCATE_FLIP B_FLIP +#define SH2Z_VD_LOCATE_PASS(d) b_note_pass(d) +#define SH2Z_VD_LOCATE_DAMP_HIT(old_dt, new_dt) b_damp(old_dt, new_dt) +#define SH2Z_VD_LOCATE_FLIP_HIT() (b_flip_hits++) +#if defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wconversion" +#pragma GCC diagnostic ignored "-Wshadow" +#pragma GCC diagnostic ignored "-Wpedantic" +#pragma GCC diagnostic ignored "-Wunused-function" +#endif +#include VD_SRC +#if defined(__GNUC__) +#pragma GCC diagnostic pop +#endif +#undef sh2z_vd_step +#undef sh2z_vd_locate +#undef sh2z_vd_lap_distance +#undef sh2z_vd_body_lateral +#undef sh2z_vd_line_view +#undef sh2z_vd_apply_kick +#undef sh2z_vd_spline + +static unsigned checks; +static void check(bool ok, const char *what) { + ++checks; + if(!ok) { + fprintf(stderr, "vehicle-locate check %u failed: %s\n", checks, what); + exit(EXIT_FAILURE); + } +} + +static int32_t q16(double x) { return (int32_t)lround(x * 65536.0); } +static int32_t q30(double x) { return (int32_t)lround(x * 1073741824.0); } +static int32_t q12(double x) { return (int32_t)lround(x * 4096.0); } +static double f16(int32_t v) { return v / 65536.0; } + +/* The copy's per-pass bookkeeping: passes of the call, and the damped steps against the steps they replace. */ +static void b_note_pass(const int32_t d[3]) { + (void)d; + b_passes++; +} +static void b_damp(int32_t old_dt, int32_t new_dt) { + b_damp_hits++; + if((new_dt < 0) != (old_dt < 0) || llabs((long long)new_dt) > llabs((long long)old_dt)) b_damp_bad++; + if(b_passes <= (unsigned)B_DAMP) b_damp_early++; +} + +/* A stadium: two straights along x joined by half circles; 16 nodes (the vehicle-dynamics tests' track). */ +#define N 16 +static sh2z_vd_node_t nodes[N]; +static sh2z_vd_track_t track; + +static double rough[3]; /* per-node scatter of the rough track (0: the stadium) */ +static uint32_t rnd(void); +static double urand(double lo, double hi); +static void point(int i, double p[3]) { + const double half = 6000.0, r = 3000.0, pi = 3.14159265358979323846; + if(i < 4) { p[0] = -half + i * 3000.0; p[2] = -r; } + else if(i < 8) { double a = -pi / 2 + (i - 3) * pi / 5; p[0] = half + r * cos(a); p[2] = r * sin(a); } + else if(i < 12) { p[0] = half - (i - 8) * 3000.0; p[2] = r; } + else { double a = pi / 2 + (i - 11) * pi / 5; p[0] = -half + r * cos(a); p[2] = r * sin(a); } + p[1] = 0.0; + if(rough[0] != 0.0) { + p[0] += urand(-rough[0], rough[0]); + p[1] += urand(-rough[1], rough[1]); + p[2] += urand(-rough[2], rough[2]); + } +} + +static void build_track(void) { + double total = 0.0; + int i, k; + memset(nodes, 0, sizeof(nodes)); + for(i = 0; i < N; i++) { + double p[3]; + point(i, p); + for(k = 0; k < 3; k++) nodes[i].pos[k] = q16(p[k]); + nodes[i].up[1] = q30(1.0); + nodes[i].tension = q30(0.5); + nodes[i].half_left = nodes[i].half_right = q16(300.0); + nodes[i].shape = SH2Z_VD_SHAPE_WALLED; + nodes[i].zones_by_side = 1; + } + track.nodes = nodes; + track.count = N; + track.laps = 3; + for(i = 0; i < N; i++) { + double len = 0.0, prev[3] = {0, 0, 0}; + int s; + for(s = 0; s <= 256; s++) { + int32_t pos[3]; + sh2z_vd_spline(&track, (uint32_t)i, (int32_t)((int64_t)s * (1 << 30) / 256), pos, NULL); + if(s) len += sqrt(pow(f16(pos[0]) - prev[0], 2) + pow(f16(pos[1]) - prev[1], 2) + pow(f16(pos[2]) - prev[2], 2)); + prev[0] = f16(pos[0]); prev[1] = f16(pos[1]); prev[2] = f16(pos[2]); + } + nodes[i].length = q12(len); + nodes[i].start = q12(total); + total += len; + } + track.length = q12(total); +} + +/* A body on the line at node n, parameter t, with a search state consistent with its spline point. */ +static sh2z_vd_body_t on_line(uint32_t n, int32_t t) { + sh2z_vd_body_t b; + int k; + memset(&b, 0, sizeof(b)); + b.node = n; + b.t = t; + sh2z_vd_spline(&track, n, t, b.spos, b.fwd); + b.fwd_mag = (int32_t)lround(65536.0 * sqrt(pow(f16(b.fwd[0]), 2) + pow(f16(b.fwd[1]), 2) + pow(f16(b.fwd[2]), 2))); + for(k = 0; k < 3; k++) b.pos[k] = b.ground[k] = b.spos[k]; + return b; +} + +static uint64_t rng_state = 0x243F6A8885A308D3ull; +static uint32_t rnd(void) { + rng_state = rng_state * 6364136223846793005ull + 1442695040888963407ull; + return (uint32_t)(rng_state >> 33); +} +static double urand(double lo, double hi) { return lo + (hi - lo) * ((double)rnd() / 2147483648.0); } + +static double dist3(const int32_t a[3], const int32_t b[3]) { + double s = 0.0; + int k; + for(k = 0; k < 3; k++) s += pow(f16(a[k]) - f16(b[k]), 2); + return sqrt(s); +} + +int main(void) { + unsigned i, pass_no, cases = 0, same = 0, damped = 0, flipped = 0, both = 0, lib_off = 0, copy_off = 0; + unsigned conv_n = 0, conv_lost = 0, conv_over = 0, conv_gained = 0, worse = 0, over1 = 0; + double conv_max = 0.0, max_excess = 0.0, max_diff = 0.0; + for(pass_no = 0; pass_no < 6; pass_no++) { + /* pass 0 is the stadium; passes 1 to 5 are rough roads (nodes scattered by up to 1400 units, 450 vertically). */ + rough[0] = pass_no ? 1400.0 : 0.0; rough[1] = 450.0; rough[2] = 1400.0; + build_track(); + for(i = 0; i < 20000; i++) { + const uint32_t n = rnd() % N; + const int32_t t = q30(urand(0.0, 0.99)); + sh2z_vd_body_t a = on_line(n, t), b = a; + int32_t target[3]; + const double dist = (i % 3 == 0) ? urand(0.0, 60.0) : urand(0.0, 2000.0); + double dir[3], len; + int k, ra, rb; + do { + for(k = 0; k < 3; k++) dir[k] = urand(-1.0, 1.0); + len = sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]); + } while(len < 0.2); + for(k = 0; k < 3; k++) target[k] = a.spos[k] + q16(dir[k] / len * dist); + b_passes = 0; + b_damp_hits = b_flip_hits = 0; + ra = sh2z_vd_locate(&a, &track, target, 100, (int64_t)1 << 32); + rb = vdb_locate(&b, &track, target, 100, (int64_t)1 << 32); + cases++; + if(ra != rb) { + if(ra < 0) lib_off++; else copy_off++; + if(b_damp_hits == 0 && b_flip_hits == 0) check(false, "an untouched search has the library's return value"); + continue; + } + check(b_damp_bad == 0, "every damped step is not longer than the step it replaces and has its sign"); + check(b_damp_early == 0, "a damped step is taken only after DAMP passes"); + check(b_flip_hits <= 1, "the flip stop ends a call once"); + if(b_damp_hits == 0 && b_flip_hits == 0) { + check(memcmp(&a, &b, sizeof(a)) == 0, "a search neither change acted on equals the library's, word for word"); + same++; + continue; + } + damped += b_damp_hits > 0; + flipped += b_flip_hits > 0; + both += b_damp_hits > 0 && b_flip_hits > 0; + if(b_flip_hits) { + check(b_passes >= 5 && b_passes <= 99, "a flip-ended search ran 5 to 99 passes"); + check(((99u - (b_passes - 1u)) & 1u) == 0, "a flip-ended search ends on a pass of the parity the 99th pass has"); + } + if(rb == 0) for(k = 0; k < 3; k++) check(b.pos[k] == target[k], "the body position is the target"); + check(b.node < N, "the end state has a valid node"); + { + const double diff = dist3(a.spos, b.spos), lr = dist3(a.ground, target), cr = dist3(b.ground, target); + if(diff > max_diff) max_diff = diff; + if(diff > 1.0) over1++; + if(cr > lr + 1.0 && dist <= 700.0) { + worse++; + if(cr - lr > max_excess) max_excess = cr - lr; + } + if(lr <= 1.0 && dist <= 700.0) { + conv_n++; + if(cr > 1.0) conv_lost++; + if(diff > B_BOUND) conv_over++; + if(diff > conv_max) conv_max = diff; + } else if(cr <= 1.0) { + conv_gained++; + } + } + } + } + printf("vehicle-locate7: %u cases, %u untouched (equal to the library), %u damped, %u flip-ended (%u both)\n", cases, same, damped, flipped, both); + printf("vehicle-locate7: the earlier search converged (residual within 1 unit, within 700 units of the line) in %u of the changed searches; the changed search did not converge in %u, ended more than %.1f unit from its end state in %u, longest %.3f units\n", conv_n, conv_lost, (double)B_BOUND, conv_over, conv_max); + printf("vehicle-locate7: where the earlier search did not converge, the changed one converged in %u; within 700 units its residual was larger by over 1 unit in %u (the largest by %.1f units); end states more than 1 unit apart in %u (the longest %.1f units)\n", conv_gained, worse, max_excess, over1, max_diff); + printf("vehicle-locate7: return value differs in %u cases (%u where only the library reports -1, %u where only the copy does)\n", lib_off + copy_off, lib_off, copy_off); + check(copy_off <= 10, "the copy reports an off-line search where the library does not in at most 10 of the cases"); + check(conv_n >= 100, "the set holds changed searches that the library converged"); + check(conv_lost * 100 <= conv_n && conv_over * 100 <= conv_n, "where the library converged, the changed search converged within the bound of its end state in all but 1 in 100"); + check(conv_max <= 16.0, "and the exceptions end within 16 units of the library's end state"); + check(damped >= 500, "the set holds damped searches"); + check(flipped >= 100, "the set holds flip-ended searches"); + check(same >= 10000, "the set holds searches neither change touched"); + printf("vehicle-locate7: %u checks passed\n", checks); + return EXIT_SUCCESS; +} diff --git a/saturn/validation/vehicle-dynamics-7.md b/saturn/validation/vehicle-dynamics-7.md new file mode 100644 index 0000000..abdf5fa --- /dev/null +++ b/saturn/validation/vehicle-dynamics-7.md @@ -0,0 +1,53 @@ +# Validation record — 2026-11-23 (search damping and flip stop, vehicle-dynamics-7) + +`SH2Z_VD_LOCATE_DAMP` and `SH2Z_VD_LOCATE_FLIP` in `sh2zam/vehicle_dynamics_experimental.h` +([guide](../VEHICLE_DYNAMICS.md#search-damping-and-flip-stop-vehicle-dynamics-7-sh2z_vd_locate_damp-sh2z_vd_locate_flip)) +change the closest-point search of `sh2z_vd_step` and `sh2z_vd_step2` for a body far off the line, in the two ways its +99 passes run under the `vehicle-dynamics-6` line. Off (the default, 0), nothing changes. **No console run was done in this +repository; the caller's numbers below are HOST and EMULATOR (MODELLED timing).** This is an approximation, not an exact change +(the end state of a changed search differs from the 99-pass one within the bounds below). + +- **Default build unchanged (SH-2, `sh2eb-elf-gcc -m2 -mb -O2 -ffreestanding -std=c11 -Wall -Wextra -Werror`):** the objects + of `sh2zam_vehicle_dynamics`, `sh2zam_vehicle_dynamics2` and `sh2zam_vehicle_contact` are identical to main `e83dae5` (`cmp`). + With DAMP 8 and FLIP 3 (and FAR 128) `sh2zam_vehicle_dynamics.o` grows from 24,148 to 24,652 bytes of text, with no new + undefined symbol and no `memcpy`, `memset` or `memmove`. The model suite is unchanged (the objects it links are identical, + so its ELF and `results.json` are too; the CI model job confirms). +- **HOST, library test (`tests/test_vehicle_locate7.c`):** 506,265 checks pass under GCC and Clang with `-Wall -Wextra + -Wconversion -Wshadow -Wpedantic -Werror` and under ASan and UBSan. The model is built a second time under private names with + DAMP 8 and FLIP 3, the linked library (both off) being the oracle, over 120,000 randomised searches (a stadium and five rough + roads, targets up to 2,000 units off the line): + - 112,514 are searches neither change acted on and equal the library word for word; 5,501 were damped, 1,968 ended by the + flip stop (2 both); + - every damped step is not longer than the step it replaces and has its sign; none is taken before DAMP passes; a flip stop + ends a call once, after 5 to 99 passes, on a pass of the parity the 99th pass has, with the body position the target; + - the library converged (residual within 1 unit) within 700 units of the line in 340 of the changed searches: the changed + search converged too in 339 and ended within 0.853 units of the library's end state; the one exception was a flip stop at + 40 passes that ended 12.5 units off the library's end state (the library closed in 59 more). The test allows 1 in 100; + - where the library did not converge there is no right end state: the changed search converged in 5,157 of them; within 700 + units of the line its residual was larger by over 1 unit in 14 (the largest by 21.8 units); end states more than 1 unit + apart in 2,400; + - the return value differs in 19 searches, all of them where only the library reports -1 (off the line); + - the set holds damped, flip-ended and untouched searches (the test requires 500, 100 and 10,000). +- **Controls (`tests/mutate_vehicle_locate7.sh`, 12 of 12 caught, the unmutated source passes):** a step twice as long; a + reversed secant; a slope test the wrong way; damping from the second pass; a flip stop without the parity; without the + return test; a flip stop returning -1; both hooks removed; builds with DAMP 1 and FLIP 1 and FLIP 2. +- **Caller qualification (HOST and EMULATOR, by the port's engine lane; not part of the library's tests).** Fire Field's gap: + the default driver ran 48 passes on its worst tick, a line with kl 0.0044, kh 2.75, target 100 ran 99 passes on five ticks in + a row (86 ticks over 24 passes). The earlier search is the baseline throughout: + - `fzx-phys` over 325 driver settings (kl, kh, target) on that course: finishers 134 to 135 (lost 1, gained 2), the finishes + that both complete within 0.10%, ticks over 12 passes 15,376 to 1,521, passes 2.885 M to 2.414 M, longest 99 to 83. The + noise controls of the same set (the tolerance halved or doubled) lose 3 to 9 and gain 5 to 6; + - the default driver's worst tick 48 to 22 passes, the 0.0044/2.75/100 line's 99 to 44; stick noise on the default driver + (AR(1), 60 seeds each at two amplitudes): runs with a 99-pass search 56 and 54 to 0 and 0, the median of each run's longest + search 99 to 34 and 23, the mean ticks over 24 passes 40.5 and 20.2 to 2.1 and 0.5, finishes 30 and 3 to 30 and 5; + - the 24 courses with the gate's default drivers: 20 have identical checkpoint hashes; Fire Field finishes at 100,490 ms as + before (its checkpoints differ), White Land 1 and Devils Forest 3 keep their finish (checkpoints differ), Big Blue 2 + finishes at 80,037 ms against 80,008 (+0.036%); + - EMULATOR (Ymir, engine on the earlier prototype of the flip rule, which compared nodes only; the final rule is a subset + of its stops, the engine gate on the merged pin repeats it): the 0.0044/2.75/100 line failed the cadence gate with the + earlier search (10 intervals over two fields, worst 4F, critical maximum 57.06 ms) and passed with the change (0 over two, + worst 2F, 22.25 ms), the engine equal to HOST at 26 of 26 checkpoints; the default driver was unchanged (critical maximum + 21.06 to 21.07 ms). +- **Not done:** a console run; a Saturn timing capture of the changed call (the model suite is unchanged, as the default object + is); the third kind of long search (a residual that rises past the centre of curvature), which this tag leaves as it was + (Port Town 1 and Red Canyon 1 keep their 55 and 57 passes).