feat(v4): Phase 7 Task 4 -- v3's entropy, TOP-WORDS, WORD-ENTROPY and (- words (#189)
capsules/v4/physics.4th, blocks 8202-8204: ENTROPY@ ENTROPY! RESET-ENTROPY as kernel requests -35..-37 (a word's heat by the address ' leaves, a data word's too; an error for what is not a word, as v3's); TOP-WORDS and WORD-ENTROPY in StarForth, printing in v3's form what the kernel gives them through (HOT#) (HOT) (XT-HEAT), -38..-40 (the i-th hottest in v3's stable order); (- in StarForth, skipping to the matching close paren with nesting, as v3's code does. Tests first: test_mesh_boot.c 101 checks, 110 on, 98 without opcode physics. Listing: 403 live words. amd64 PASS, word_count=403. Not pushed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
+69
-1
@@ -888,13 +888,81 @@ static void push_q(v4_node *n, uint64_t q)
|
||||
#endif
|
||||
}
|
||||
|
||||
/* the record of the word at addr -- its xt, or for a data word the cell after (what ' leaves) -- or 0 */
|
||||
static v4_mesh_account *record_at(unsigned place, const v4_node *n, v4_cell addr)
|
||||
{
|
||||
v4_mesh_account *r = (v4_mesh_account *)v4_mesh_account_at(the, place, addr);
|
||||
if (!r && addr > 5 && (n->mem[addr - 1 - 3] & 4)) r = (v4_mesh_account *)v4_mesh_account_at(the, place, addr - 1);
|
||||
return r;
|
||||
}
|
||||
|
||||
/* the words with heat, newest first, then sorted hottest first as v3's TOP-WORDS (a stable sort: ties keep
|
||||
* dictionary order) */
|
||||
static uint32_t hot_list(unsigned place, const v4_node *n, v4_cell *xts, v4_uheat_t *heats, uint32_t max)
|
||||
{
|
||||
v4_cell xt;
|
||||
uint32_t k = 0, i, j;
|
||||
for (xt = n->mem[the->im->latest], i = 0; xt > 4 && xt < (v4_cell)V4_NODE_WORDS && i < V4_NODE_WORDS && k < max; xt = n->mem[xt - 1], i++) {
|
||||
const v4_mesh_account *a = v4_mesh_account_at(the, place, xt);
|
||||
if (a && a->heat > 0) { xts[k] = xt; heats[k] = a->heat; k++; }
|
||||
}
|
||||
for (i = 1; i < k; i++) { /* insertion: stable, as v3's bubble sort */
|
||||
v4_cell x = xts[i]; v4_uheat_t h = heats[i];
|
||||
for (j = i; j > 0 && heats[j - 1] < h; j--) { xts[j] = xts[j - 1]; heats[j] = heats[j - 1]; }
|
||||
xts[j] = x; heats[j] = h;
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
static int entropy_serve(unsigned place, v4_node *n, v4_cell value)
|
||||
{
|
||||
static v4_cell xts[1000];
|
||||
static v4_uheat_t heats[1000];
|
||||
v4_mesh_account *r;
|
||||
v4_cell a, v;
|
||||
uint32_t k;
|
||||
switch (value) {
|
||||
case V4_REQ_ENTROPY_FETCH: case V4_REQ_ENTROPY_STORE:
|
||||
a = v4_dstack_pop(&n->ds);
|
||||
v = value == V4_REQ_ENTROPY_STORE ? v4_dstack_pop(&n->ds) : 0;
|
||||
r = record_at(place, n, a);
|
||||
if (!r) { v4_node_store(n, n->error_reg, 12); return 1; } /* not a word: v3's is an error too */
|
||||
if (value == V4_REQ_ENTROPY_FETCH) v4_dstack_push(&n->ds, (v4_cell)r->heat);
|
||||
else { r->heat = (v4_uheat_t)v; if (the->physics) r->last_decay_tick = the->physics[place].tick; }
|
||||
return 1;
|
||||
case V4_REQ_RESET_ENTROPY:
|
||||
for (k = 0; k < V4_MESH_WORD_IDS; k++) {
|
||||
v4_mesh_account *x = &accounts_of(place)[k];
|
||||
if (x->xt && x->heat > 0) { x->heat = 0; x->temperature_q8 = 0; }
|
||||
}
|
||||
return 1;
|
||||
case V4_REQ_HOT_COUNT:
|
||||
v4_dstack_push(&n->ds, (v4_cell)hot_list(place, n, xts, heats, 1000));
|
||||
return 1;
|
||||
case V4_REQ_HOT_NTH:
|
||||
a = v4_dstack_pop(&n->ds);
|
||||
k = hot_list(place, n, xts, heats, 1000);
|
||||
if (a >= 0 && (uint32_t)a < k) { v4_dstack_push(&n->ds, xts[a]); v4_dstack_push(&n->ds, (v4_cell)heats[a]); }
|
||||
else { v4_dstack_push(&n->ds, 0); v4_dstack_push(&n->ds, 0); }
|
||||
return 1;
|
||||
default: /* V4_REQ_XT_HEAT */
|
||||
a = v4_dstack_pop(&n->ds);
|
||||
{
|
||||
const v4_mesh_account *x = v4_mesh_account_at(the, place, a);
|
||||
v4_dstack_push(&n->ds, x ? (v4_cell)x->heat : 0);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
if (value > V4_REQ_FREEZE_WORD || value < V4_REQ_XT_HEAT) return 0;
|
||||
if (value <= V4_REQ_ENTROPY_FETCH) return entropy_serve(place, n, value);
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user