File size: 4,828 Bytes
1ddfac5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * lit_core.h — LITCORE: the LIT algebraic core, language-agnostic C.
 *
 * LITCORE is brainless: broadcast the graph, multiply, reduce,
 * rewire, repeat.  It never interprets what a symbol means; there is
 * no semantics inside the core.
 *
 * Six is the optimal operation; DO NOT TOUCH IT.
 */
#ifndef LIT_CORE_H
#define LIT_CORE_H

#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/* LIT bytecode: LOAD slot:u32 value:u64; WIRE slot:u32 src:u32
 * gain:u64 weight:u64; everything else is one byte.  Little-endian. */
#define LIT_BC_LOAD            0x01
#define LIT_BC_WIRE            0x02
#define LIT_BC_BROADCAST       0x03
#define LIT_BC_VF_TRANSFER     0x04
#define LIT_BC_HAMMING_REDUCE  0x05
#define LIT_BC_HALT            0x06

/* lit flat: 2^LIT_FLAT = 64, the 64/65 halt edge. */
#define LIT_FLAT 6

/* The hard halt bound: 65 ticks at L=64.  A run is one tick by
 * default (one tick applies F^64, so the step is already the whole
 * job) and never exceeds LIT_HALT ticks even if asked. */
#define LIT_HALT 65

typedef struct lit_machine {
    uint32_t n;          /* slots per cluster */
    uint32_t k;          /* clusters */
    uint64_t *a;         /* owned state when owns_state */
    uint64_t *b;         /* transition buffer */
    uint64_t *V;         /* current state (a or a borrowed buffer) */
    uint64_t *prev;      /* state before the last transfer (b) */
    uint32_t *src;       /* k*n wiring */
    uint64_t *gain;      /* k*n */
    uint64_t *weight;    /* k*n */
    uint64_t *dist;      /* k, last per-cluster distance */
    uint64_t *diff;      /* k*n, last strict-less difference mask */
    uint32_t *flat_src;  /* k*LIT_FLAT*n */
    uint64_t *flat_gain; /* k*LIT_FLAT*n */
    uint32_t *flat_as;   /* n-word recurrence scratch */
    uint32_t *flat_bs;
    uint64_t *flat_ag;
    uint64_t *flat_bg;
    int flat_dirty;      /* the base graph changed; rebuild on the tick */
    int pending;         /* a VF_TRANSFER awaits HAMMING_REDUCE */
    int owns_state;      /* a was allocated by the core */
    uint32_t bits;       /* L: symbol width, 1..64 */
    uint64_t mask;       /* (1<<L)-1, or ~0 for L=64 */
    uint32_t levels;     /* flat exponent: ceil(log2(n)); 6 for n=64 */
    uint32_t halt;       /* hard halt: n + 1; 65 for n=64 */
} lit_machine;

/* Copy `state` (n*k words) into the machine.  Returns 0, or -1 on OOM. */
int lit_machine_init(lit_machine *m, const uint64_t *state,
                     uint32_t n, uint32_t k, uint32_t bits);

/* Borrow caller-owned `state` (n*k words); the core never frees it. */
int lit_machine_init_borrowed(lit_machine *m, uint64_t *state,
                              uint32_t n, uint32_t k, uint32_t bits);

/* Release every array the core owns; the borrowed state is untouched. */
void lit_machine_free(lit_machine *m);

/* One complete tick: transfer then strict-less Hamming reduce. */
void lit_machine_step(lit_machine *m);

/* Run one tick (F^64) with early halt; max_cycles 0 means one tick,
 * any larger request is clamped to LIT_HALT.  Writes the ticks taken
 * and whether every cluster's d reached 0. */
void lit_machine_run(lit_machine *m, uint64_t max_cycles,
                     uint64_t *cycles, int *converged);

/* Rewire one slot (weight NULL keeps the existing weight).  0 or -1/-2. */
int lit_machine_connect(lit_machine *m, uint32_t slot, uint32_t src,
                        uint64_t gain, const uint64_t *weight, int cluster);

/* Rewrite a cluster's or every cluster's wiring.  0 or -1/-2. */
int lit_machine_graph(lit_machine *m, const uint32_t *src,
                      const uint64_t *gain, const uint64_t *weight,
                      int cluster);

/* Load one n-word window into every cluster; no tick, no commit. */
void lit_machine_load(lit_machine *m, const uint64_t *words);

/* Wire every cluster as a ring: slot i reads slot i-1, gain/weight 1. */
void lit_machine_ring(lit_machine *m);

/* Copy cluster 0's committed V into out (n words). */
void lit_machine_copy_v(const lit_machine *m, uint64_t *out);

/*
 * Execute LIT bytecode.  On success returns 0 and writes a malloc'ed
 * history (reductions*k words), the reduction count, ticks, halted and
 * broadcast flags.  The caller frees *history.  On failure returns -1
 * and writes a message into err (if err is non-NULL).
 */
int lit_machine_run_bytecode(lit_machine *m, const uint8_t *code, size_t len,
                             uint64_t **history, size_t *reductions,
                             uint64_t *ticks, int *halted, int *broadcast,
                             char *err, size_t errlen);

/* One in-place forward-ring tick over a writable uint64 buffer. */
uint64_t lit_ring_tick(uint64_t *v, size_t n, uint32_t bits, int wrap);

#ifdef __cplusplus
}
#endif

#endif /* LIT_CORE_H */