First code for StarForth v4 (JUSTIFICATION.md section 10, step 1): one node of the 32-instruction core as a C99 model, with cell width as a build parameter. - Node: P, A, B, F18 circular stacks (10 and 9 deep, D-2), word-addressed memory (D-1), 5% guard bands on every bounded list. - Instruction word: six 5-bit slots in 32 bits at every cell width. - Executor: all 32 opcodes of DECOMPOSITION.md 1.3. Cell arithmetic wraps explicitly; no signed overflow or implementation-defined shift. - Heat: per-opcode and per-call-target counters and the anti-clock, driven by instruction retirement (1.4, D-6 interim). - Slot packer and runner for tests, and a reference unsigned multiply in plain C99 with no 128-bit type. Tests run at 32- and 64-bit cells, and under ASan and UBSan. They cover every opcode and execute the first section 4 definitions (NIP SWAP OR NEGATE ROT 0< 0= 2DUP - U<) against the C operation each stands for. UM* as written in section 4 is exact only while u1 <= 2^(n-2). Two known failing cases are pinned in test_foundation.c until it is rewritten. DECOMPOSITION.md: record D-9, the instruction word is 32 bits at every cell width (ruled 2026-10-02). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
32 lines
1.5 KiB
C
32 lines
1.5 KiB
C
/* umul.h -- unsigned multiply fixing UM* under D-3 constraints.
|
|
*
|
|
* DECOMPOSITION.md line 205-207: "NEEDS REVISION: written for the carry-keeping
|
|
* +* that D-3 rejected. With plain F18 +* the carry out of T is lost, so this
|
|
* is only correct when the partial sums never overflow T (operands below 2^31).
|
|
* A full-range UM* needs a correction step; to be rewritten and proven on the
|
|
* golden model."
|
|
*
|
|
* D-3 states: "Plain F18 semantics. The carry out of T is not kept."
|
|
* The +* defined in the F18 shifts T:A right; the add is T <- T+S when bit 0
|
|
* of A was set, but any carry out of that addition is discarded (not shifted
|
|
* into the high part). That means UM* as written only works for inputs whose
|
|
* partial products don't generate a carry beyond T's width during the loop.
|
|
*
|
|
* We need a correct full-range unsigned multiply (u1*u2 -> ulo, uhi) that is
|
|
* correct for all cell-width values of u1,u2, expressed in terms of the v4
|
|
* primitives or at least specified precisely for the golden model. Since
|
|
* we're building the golden model in C first, the canonical implementation is
|
|
* the width-correct C multiplication split into high/low halves at V4_CELL_BITS.
|
|
* This header provides both: a reference C implementation (for verification)
|
|
* and a documented form that matches D-3's +* constraint.
|
|
*/
|
|
#ifndef V4_UMUL_H
|
|
#define V4_UMUL_H
|
|
|
|
#include "v4/cell.h"
|
|
|
|
/* Unsigned multiply: (u1, u2) -> (ulo, uhi). Full range, width-correct. */
|
|
void v4_umul(v4_ucell u1, v4_ucell u2, v4_ucell *ulo, v4_ucell *uhi);
|
|
|
|
#endif /* V4_UMUL_H */
|