feat(v4): Phase 7 Task 4 -- the loops' words: v3's physics-freeze and inference words, F18 asking the kernel (#189)
capsules/v4/physics.4th, a kernel capsule on Hera (blocks 8200-8201): FREEZE-WORD UNFREEZE-WORD FROZEN? HEAT! HEAT@ DECAY-RATE@ Q.VARIANCE INFER-DECAY-SLOPE INFER-WINDOW-WIDTH INFER-RUN INFER-WINDOW@ INFER-DECAY@ INFER-VARIANCE@ INFER-FIT@ INFER-EARLY-EXIT@ -- each a KERNEL-WORD, requests -21..-34 (v4/mesh.h), served by the kernel on any node: "The compudynamics is stadium/fabric" (Captain Bob, 2026-11-14). A word is named as v3's, the newest visible entry of CONTEXT then FORTH (the image gains CONTEXT's address); the arithmetic is v4/system/inference.c, v3's to the bit. Q48.16 results are v4's doubles (low cell, then high). HEAT! starts the word's decay from now. Test first with v3's POST cases in v4's address units: test_mesh_boot.c 86 checks, 95 on, 83 without opcode physics. docs/reference/v4-dictionary.tsv: the 15 words (392 live). amd64 PASS, word_count=392. Not pushed (held to the end of Phase 7). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
@@ -819,6 +819,107 @@ void v4_mesh_charge(v4_mesh *ms, unsigned place, void (*each)(void *ctx, v4_cell
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- the loops' words (capsules/v4/physics.4th) ---- */
|
||||
|
||||
static unsigned byte_at(const v4_node *n, v4_cell baddr) /* four to a cell, low byte first (D-1) */
|
||||
{
|
||||
v4_cell w = baddr >> 2;
|
||||
if (baddr < 0 || w >= (v4_cell)V4_NODE_WORDS) return 0;
|
||||
return (unsigned)(((v4_ucell)n->mem[w] >> (8u * (unsigned)(baddr & 3))) & 0xFFu);
|
||||
}
|
||||
|
||||
/* the newest visible entry named by (baddr u) -- CONTEXT's vocabulary, then FORTH, as (LOOKUP) -- or 0 */
|
||||
static v4_cell word_named(const v4_node *n, v4_cell baddr, v4_cell u)
|
||||
{
|
||||
v4_cell heads[2], xt;
|
||||
unsigned h, k, len = u > 31 ? 31u : (unsigned)u;
|
||||
if (u <= 0) return 0;
|
||||
heads[0] = n->mem[n->mem[the->im->context]];
|
||||
heads[1] = n->mem[the->im->latest];
|
||||
for (h = 0; h < 2; h++) {
|
||||
for (xt = heads[h], k = 0; xt > 4 && xt < (v4_cell)V4_NODE_WORDS && k < V4_NODE_WORDS; xt = n->mem[xt - 1], k++) {
|
||||
v4_cell nfa = n->mem[xt - 2];
|
||||
unsigned i, ok = 1;
|
||||
if (n->mem[xt - 3] & 2) continue; /* hidden */
|
||||
if (byte_at(n, nfa * 4) != len) continue;
|
||||
for (i = 0; i < len && ok; i++) if (byte_at(n, nfa * 4 + 1 + (v4_cell)i) != byte_at(n, baddr + (v4_cell)i)) ok = 0;
|
||||
if (ok) return xt;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* u cells of the node's memory at addr, as v3's array of 64-bit heats (0 if out of range) */
|
||||
static const uint64_t *cells_at(const v4_node *n, v4_cell addr, v4_cell u)
|
||||
{
|
||||
static uint64_t buf[V4_MESH_WINDOW];
|
||||
v4_cell i;
|
||||
if (u <= 0 || u > (v4_cell)V4_MESH_WINDOW || addr < 0 || addr + u > (v4_cell)V4_NODE_WORDS) return 0;
|
||||
for (i = 0; i < u; i++) buf[i] = (uint64_t)(v4_ucell)n->mem[addr + i];
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* a Q48.16 value as v4's: a double, low cell then high (qmath.f18: Q.1 is 65536 0), at either cell width */
|
||||
static void push_q(v4_node *n, uint64_t q)
|
||||
{
|
||||
#if V4_CELL_BITS >= 64
|
||||
v4_dstack_push(&n->ds, (v4_cell)q);
|
||||
v4_dstack_push(&n->ds, 0);
|
||||
#else
|
||||
v4_dstack_push(&n->ds, (v4_cell)(v4_ucell)(q & 0xFFFFFFFFu));
|
||||
v4_dstack_push(&n->ds, (v4_cell)(v4_ucell)(q >> 32));
|
||||
#endif
|
||||
}
|
||||
|
||||
static int physics_serve(unsigned place, v4_node *n, v4_cell value)
|
||||
{
|
||||
v4_mesh_physics *p = the->physics ? &the->physics[place] : 0;
|
||||
v4_cell u, a, xt;
|
||||
v4_mesh_account *r;
|
||||
const uint64_t *d;
|
||||
if (value > V4_REQ_FREEZE_WORD || value < V4_REQ_INFER_EARLY) return 0;
|
||||
switch (value) {
|
||||
case V4_REQ_FREEZE_WORD: case V4_REQ_UNFREEZE_WORD: case V4_REQ_FROZEN: case V4_REQ_HEAT_FETCH: case V4_REQ_HEAT_STORE:
|
||||
u = v4_dstack_pop(&n->ds); a = v4_dstack_pop(&n->ds);
|
||||
xt = word_named(n, a, u);
|
||||
r = xt ? (v4_mesh_account *)v4_mesh_account_at(the, place, xt) : 0;
|
||||
if (value == V4_REQ_FREEZE_WORD) { if (r) r->state_flags |= V4_MESH_STATE_FROZEN; }
|
||||
else if (value == V4_REQ_UNFREEZE_WORD) { if (r) r->state_flags &= (uint8_t)~V4_MESH_STATE_FROZEN; }
|
||||
else if (value == V4_REQ_FROZEN) v4_dstack_push(&n->ds, r && (r->state_flags & V4_MESH_STATE_FROZEN) ? -1 : 0);
|
||||
else if (value == V4_REQ_HEAT_FETCH) v4_dstack_push(&n->ds, r ? (v4_cell)r->heat : 0);
|
||||
else { /* the heat stored is its heat now: decay counts from here */
|
||||
v4_cell heat = v4_dstack_pop(&n->ds);
|
||||
if (r) { r->heat = (v4_uheat_t)heat; if (p) r->last_decay_tick = p->tick; }
|
||||
}
|
||||
return 1;
|
||||
case V4_REQ_Q_VARIANCE: case V4_REQ_INFER_SLOPE: case V4_REQ_INFER_WIDTH:
|
||||
u = v4_dstack_pop(&n->ds); a = v4_dstack_pop(&n->ds);
|
||||
d = cells_at(n, a, u);
|
||||
if (value == V4_REQ_Q_VARIANCE) push_q(n, d ? v4_inference_variance(d, (uint64_t)u) : 0);
|
||||
else if (value == V4_REQ_INFER_SLOPE) push_q(n, d ? v4_inference_decay_slope(d, (uint64_t)u) : 0);
|
||||
else v4_dstack_push(&n->ds, d ? (v4_cell)v4_inference_window_width(d, (uint64_t)u) : 0);
|
||||
return 1;
|
||||
case V4_REQ_INFER_RUN: /* v3's: the engine run now on the window, outputs kept */
|
||||
if (p) {
|
||||
uint64_t have = p->win.total_executions < V4_MESH_WINDOW ? p->win.total_executions : V4_MESH_WINDOW;
|
||||
uint64_t span = p->win.effective_window_size < have ? p->win.effective_window_size : have, i;
|
||||
for (i = 0; i < span; i++) {
|
||||
uint32_t id = p->win.history[(p->win.window_pos + V4_MESH_WINDOW - span + i) % V4_MESH_WINDOW];
|
||||
trajectory[i] = (id < V4_MESH_WORD_IDS && accounts_of(place)[id].xt) ? (uint64_t)accounts_of(place)[id].heat : 0;
|
||||
}
|
||||
v4_inference_run(trajectory, span, p->l5_window_inference, p->l6_decay_inference, &p->inference);
|
||||
}
|
||||
return 1;
|
||||
default:
|
||||
if (value == V4_REQ_INFER_DECAY) push_q(n, p ? p->inference.adaptive_decay_slope : 0);
|
||||
else if (value == V4_REQ_INFER_VARIANCE) push_q(n, p ? p->inference.window_variance_q48 : 0);
|
||||
else if (value == V4_REQ_INFER_FIT) push_q(n, p ? p->inference.slope_fit_quality_q48 : 0);
|
||||
else if (value == V4_REQ_INFER_WINDOW) v4_dstack_push(&n->ds, p ? (v4_cell)p->inference.adaptive_window_width : 0);
|
||||
else v4_dstack_push(&n->ds, p ? (v4_cell)p->inference.early_exited : 0);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int kernel_take(void *self, v4_cell value) /* a request */
|
||||
{
|
||||
unsigned place = place_of_device(self);
|
||||
@@ -827,6 +928,7 @@ static int kernel_take(void *self, v4_cell value) /* a request */
|
||||
if (v4_blocks_serve(&the->blocks[place], &x->n, value)) return 1;
|
||||
if (value == V4_REQ_WORD_DEFINED) { word_made(place, &x->n, v4_dstack_pop(&x->n.ds)); return 1; }
|
||||
if (value == V4_REQ_WORD_FORGOTTEN) { words_gone(place, &x->n, v4_dstack_pop(&x->n.ds)); return 1; }
|
||||
if (physics_serve(place, &x->n, value)) return 1;
|
||||
if (place == the->hera_place && the->serve && value >= 1 && value <= (v4_cell)the->word_count) {
|
||||
the->serve(&x->n, (unsigned)value);
|
||||
return 1;
|
||||
|
||||
Reference in New Issue
Block a user