File size: 18,947 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
 * lit_core.c — LITCORE: the LIT algebraic core, language-agnostic C.
 *
 * The core is brainless and hard-halting.  It broadcasts the graph,
 * computes, reduces by strict-less Hamming, rewires, and repeats; it
 * never interprets what a symbol or a program means.  There is no
 * semantics inside the core.
 *
 * One tick is three steps, in this order:
 *
 *   1. transition: next[i] = V[src[i]] * gain[i]
 *   2. swap:       prev = V (the snapshot), V = next
 *   3. reduction:  M[i] = prev[i] & ~V[i]
 *                  d += weight[i] * popcount(M[i])
 *
 * The machine holds two arrays, V and prev, and swaps their roles; there
 * is no third snapshot array.  The reduction is the strict-less Hamming:
 * it counts only the bits that turned OFF, prev=1 -> V=0.  The
 * integer-lattice iteration only ever goes strictly down (there is no
 * <=, hence no self-connection loop); a self-edge or an increase
 * contributes d=0, which is the natural halt.  The mask M is retained in
 * diff, per slot: d says how much changed, diff says where.  The core
 * never interprets the mask; it is an observation for the caller.
 *
 * The graph (src/gain/weight) lives in the same machine as V.  Writing
 * src/gain is the reconnect; it takes effect on the next tick and does
 * not itself trigger a commit.  The prev snapshot is what keeps
 * "rewire" and "read" from racing inside one tick.
 *
 * A machine owns `clusters` independent copies of the n-slot table.  The
 * same transition is applied to every cluster: that is the broadcast.
 * Clusters are independent; the reduction is exact integer weighted
 * Hamming per cluster.
 *
 * This file has no CPython dependency.  The `lit` dispatcher and the
 * `litc` CLI link this core; `litc --python` is the C ingest for raw
 * Python code units.  There is no Python interface.
 */
#include "lit_core.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef _OPENMP
#include <omp.h>
#endif

static inline uint64_t lit_popcount_u64(uint64_t x)
{
    return (uint64_t)__builtin_popcountll(x);
}

/*
 * VF map composition: the map is arrays, not a machine stack.
 *
 *     F(V)_i = V[s_i] * g_i          (self)
 *     G(V)_i = V[r_i] * h_i          (other)
 *     (G o F)(V)_i = V[s_{r_i}] * (g_{r_i} * h_i)
 *
 * so the composed wiring is src'[i] = s[r_i], gain'[i] = g[r_i] * h[i].
 * The arrays do not grow.
 */
static void compose_flat(const uint32_t *sf, const uint64_t *gf,
                         const uint32_t *sg, const uint64_t *gg,
                         uint32_t *so, uint64_t *go,
                         size_t count, uint32_t n)
{
    for (size_t i = 0; i < count; i++) {
        size_t base = (i / n) * (size_t)n;
        size_t j = base + sf[i];    /* first map's source, same cluster */

        so[i] = sg[j];              /* second map reads it */
        go[i] = gg[j] * gf[i];
    }
}

static void machine_free_arrays(lit_machine *m)
{
    free(m->a);
    free(m->b);
    free(m->src);
    free(m->gain);
    free(m->weight);
    free(m->dist);
    free(m->diff);
    free(m->flat_src);
    free(m->flat_gain);
    free(m->flat_as);
    free(m->flat_bs);
    free(m->flat_ag);
    free(m->flat_bg);
    m->a = NULL;
    m->b = NULL;
    m->V = NULL;
    m->prev = NULL;
    m->src = NULL;
    m->gain = NULL;
    m->weight = NULL;
    m->dist = NULL;
    m->diff = NULL;
    m->flat_src = NULL;
    m->flat_gain = NULL;
    m->flat_as = NULL;
    m->flat_bs = NULL;
    m->flat_ag = NULL;
    m->flat_bg = NULL;
}

void lit_machine_free(lit_machine *m)
{
    if (!m) {
        return;
    }
    machine_free_arrays(m);
    memset(m, 0, sizeof(*m));
}

static int machine_alloc(lit_machine *m, uint64_t *state, int own_state)
{
    size_t count;
    size_t n = (size_t)m->n;
    uint32_t lv = m->levels ? m->levels : 1;

    if (n == 0 || m->k == 0) {
        return -1;
    }
    if ((size_t)m->k > SIZE_MAX / n) {
        return -1;
    }
    count = (size_t)m->k * n;
    if (count > SIZE_MAX / lv) {
        return -1;
    }
    m->owns_state = own_state;
    m->a = own_state ? malloc(count * sizeof(uint64_t)) : NULL;
    m->b = malloc(count * sizeof(uint64_t));
    m->src = malloc(count * sizeof(uint32_t));
    m->gain = malloc(count * sizeof(uint64_t));
    m->weight = malloc(count * sizeof(uint64_t));
    m->dist = malloc((size_t)m->k * sizeof(uint64_t));
    m->diff = malloc(count * sizeof(uint64_t));
    m->flat_src = malloc(count * lv * sizeof(uint32_t));
    m->flat_gain = malloc(count * lv * sizeof(uint64_t));
    m->flat_as = malloc(n * sizeof(uint32_t));
    m->flat_bs = malloc(n * sizeof(uint32_t));
    m->flat_ag = malloc(n * sizeof(uint64_t));
    m->flat_bg = malloc(n * sizeof(uint64_t));
    if ((own_state && !m->a) || !m->b || !m->src || !m->gain ||
        !m->weight || !m->dist || !m->diff || !m->flat_src ||
        !m->flat_gain || !m->flat_as || !m->flat_bs || !m->flat_ag ||
        !m->flat_bg) {
        machine_free_arrays(m);
        return -1;
    }
    m->V = own_state ? m->a : state;
    m->prev = m->b;
    m->flat_dirty = 1;
    memset(m->b, 0, count * sizeof(uint64_t));
    memset(m->diff, 0, count * sizeof(uint64_t));
    return 0;
}

static int machine_setup(lit_machine *m, uint64_t *state,
                         uint32_t n, uint32_t k, uint32_t bits, int own)
{
    size_t count;

    if (!m || !state || n == 0 || k == 0 || bits < 1 || bits > 64) {
        return -1;
    }
    memset(m, 0, sizeof(*m));
    m->n = n;
    m->k = k;
    m->bits = bits;
    m->mask = (bits == 64) ? ~(uint64_t)0 : (((uint64_t)1 << bits) - 1);
    m->levels = 0;
    while (m->levels < 31 && ((uint32_t)1 << m->levels) < n) {
        m->levels++;
    }
    m->halt = n + 1;
    if (machine_alloc(m, state, own) != 0) {
        return -1;
    }
    count = (size_t)k * n;
    if (own) {
        memcpy(m->a, state, count * sizeof(uint64_t));
    }
    for (size_t i = 0; i < count; i++) {
        m->V[i] &= m->mask;
    }
    for (uint32_t c = 0; c < k; c++) {
        for (uint32_t i = 0; i < n; i++) {
            size_t idx = (size_t)c * n + i;

            m->src[idx] = i;
            m->gain[idx] = 1;
            m->weight[idx] = 1;
        }
    }
    memset(m->dist, 0, (size_t)k * sizeof(uint64_t));
    return 0;
}

int lit_machine_init(lit_machine *m, const uint64_t *state,
                     uint32_t n, uint32_t k, uint32_t bits)
{
    return machine_setup(m, (uint64_t *)state, n, k, bits, 1);
}

int lit_machine_init_borrowed(lit_machine *m, uint64_t *state,
                              uint32_t n, uint32_t k, uint32_t bits)
{
    return machine_setup(m, state, n, k, bits, 0);
}

/*
 * The six-item graph sequence: the base VF graph F plus the local
 * recurrence G_{i+1} = G_i o G_i gives F^2, F^4, F^8, F^16, F^32,
 * F^64.  It is built once per wiring change; the step applies the last
 * item, so one tick is F^64 and 2^6 = 64 is the 64/65 edge.
 */
static void machine_rebuild_flat(lit_machine *m)
{
    const uint32_t k = m->k;
    const uint32_t n = m->n;
    uint32_t *as = m->flat_as;
    uint32_t *bs = m->flat_bs;
    uint64_t *ag = m->flat_ag;
    uint64_t *bg = m->flat_bg;

    for (uint32_t cc = 0; cc < k; cc++) {
        memcpy(as, m->src + (size_t)cc * n, (size_t)n * sizeof(uint32_t));
        memcpy(ag, m->gain + (size_t)cc * n, (size_t)n * sizeof(uint64_t));
        for (uint32_t c = 0; c < m->levels; c++) {
            compose_flat(as, ag, as, ag, bs, bg, n, n);
            { uint32_t *p = as; as = bs; bs = p; }
            { uint64_t *p = ag; ag = bg; bg = p; }
            memcpy(m->flat_src + ((size_t)cc * m->levels + c) * n, as,
                   (size_t)n * sizeof(uint32_t));
            memcpy(m->flat_gain + ((size_t)cc * m->levels + c) * n, ag,
                   (size_t)n * sizeof(uint64_t));
        }
    }
    m->flat_dirty = 0;
}

/*
 * VF_TRANSFER: next = F(V), then swap: V becomes the committed state and
 * prev keeps the snapshot.  Two arrays total (V and prev), not three.
 * Each cluster only touches its own slice, so the cluster loop is the
 * broadcast parallel loop.
 */
static void machine_transfer(lit_machine *m)
{
    const uint32_t k = m->k;
    const uint32_t n = m->n;

    if (m->flat_dirty) {
        machine_rebuild_flat(m);
    }
#ifdef _OPENMP
#pragma omp parallel for schedule(static) if (k > 1)
#endif
    for (long cc = 0; cc < (long)k; cc++) {
        size_t base = (size_t)cc * n;
        const uint64_t *V = m->V + base;
        uint64_t *next = m->prev + base;
        const uint32_t *src;
        const uint64_t *gain;

        if (m->levels == 0) {
            src = m->src + base;
            gain = m->gain + base;
        } else {
            src = m->flat_src +
                ((size_t)cc * m->levels + (m->levels - 1)) * n;
            gain = m->flat_gain +
                ((size_t)cc * m->levels + (m->levels - 1)) * n;
        }
        for (uint32_t i = 0; i < n; i++) {
            next[i] = (V[src[i]] * gain[i]) & m->mask;
        }
    }
    {
        uint64_t *tmp = m->V;

        m->V = m->prev;
        m->prev = tmp;
    }
    m->pending = 1;
}

/*
 * HAMMING_REDUCE: strict-less weighted Hamming, exact: the bits of the
 * snapshot that turned off in the committed state (prev & ~V), never the
 * bits that turned on.  The per-slot mask is kept in diff; the weighted
 * sum is dist.  Without a pending transfer every distance and mask is 0.
 */
static void machine_reduce(lit_machine *m)
{
    const uint32_t k = m->k;
    const uint32_t n = m->n;

    if (!m->pending) {
        memset(m->dist, 0, (size_t)k * sizeof(uint64_t));
        memset(m->diff, 0, (size_t)k * n * sizeof(uint64_t));
        return;
    }
#ifdef _OPENMP
#pragma omp parallel for schedule(static) if (k > 1)
#endif
    for (long cc = 0; cc < (long)k; cc++) {
        size_t base = (size_t)cc * n;
        const uint64_t *V = m->V + base;
        const uint64_t *prev = m->prev + base;
        const uint64_t *weight = m->weight + base;
        uint64_t *diff = m->diff + base;
        uint64_t d = 0;

        for (uint32_t i = 0; i < n; i++) {
            uint64_t mask = (prev[i] & ~V[i]) & m->mask;

            diff[i] = mask;
            d += weight[i] * lit_popcount_u64(mask);
        }
        m->dist[cc] = d;
    }
    m->pending = 0;
}

void lit_machine_step(lit_machine *m)
{
    machine_transfer(m);
    machine_reduce(m);
}

void lit_machine_run(lit_machine *m, uint64_t max_cycles,
                     uint64_t *cycles_out, int *converged_out)
{
    uint64_t cycles = 0;
    int converged = 0;

    if (max_cycles == 0) {
        max_cycles = 1;          /* one tick is F^64: one step, early halt */
    }
    if (max_cycles > m->halt) {
        max_cycles = m->halt;    /* the hard halt is n + 1 (65 at n=64) */
    }
    for (;;) {
        lit_machine_step(m);
        cycles++;
        converged = 1;
        for (uint32_t c = 0; c < m->k; c++) {
            if (m->dist[c] != 0) {
                converged = 0;
                break;
            }
        }
        if (converged || cycles >= max_cycles) {
            break;
        }
    }
    if (cycles_out) {
        *cycles_out = cycles;
    }
    if (converged_out) {
        *converged_out = converged;
    }
}

int lit_machine_connect(lit_machine *m, uint32_t slot, uint32_t src,
                        uint64_t gain, const uint64_t *weight, int cluster)
{
    uint32_t c0, c1;

    if (!m) {
        return -1;
    }
    if (slot >= m->n || src >= m->n) {
        return -1;
    }
    if (cluster < -1 || cluster >= (int)m->k) {
        return -2;
    }
    c0 = (cluster < 0) ? 0 : (uint32_t)cluster;
    c1 = (cluster < 0) ? m->k : (uint32_t)cluster + 1;
    for (uint32_t c = c0; c < c1; c++) {
        size_t idx = (size_t)c * m->n + slot;

        m->src[idx] = src;
        m->gain[idx] = gain;
        if (weight) {
            m->weight[idx] = *weight;
        }
    }
    m->flat_dirty = 1;
    return 0;
}

int lit_machine_graph(lit_machine *m, const uint32_t *src,
                      const uint64_t *gain, const uint64_t *weight,
                      int cluster)
{
    uint32_t c0, c1;

    if (!m || !src || !gain || !weight) {
        return -1;
    }
    if (cluster < -1 || cluster >= (int)m->k) {
        return -2;
    }
    for (uint32_t i = 0; i < m->n; i++) {
        if (src[i] >= m->n) {
            return -1;
        }
    }
    c0 = (cluster < 0) ? 0 : (uint32_t)cluster;
    c1 = (cluster < 0) ? m->k : (uint32_t)cluster + 1;
    for (uint32_t c = c0; c < c1; c++) {
        size_t base = (size_t)c * m->n;

        memcpy(m->src + base, src, (size_t)m->n * sizeof(uint32_t));
        memcpy(m->gain + base, gain, (size_t)m->n * sizeof(uint64_t));
        memcpy(m->weight + base, weight, (size_t)m->n * sizeof(uint64_t));
    }
    m->flat_dirty = 1;
    return 0;
}

void lit_machine_load(lit_machine *m, const uint64_t *words)
{
    if (!m || !words) {
        return;
    }
    for (uint32_t c = 0; c < m->k; c++) {
        memcpy(m->V + (size_t)c * m->n, words,
               (size_t)m->n * sizeof(uint64_t));
    }
    m->pending = 0;
}

void lit_machine_ring(lit_machine *m)
{
    const uint32_t n = m->n;

    if (!m) {
        return;
    }
    for (uint32_t c = 0; c < m->k; c++) {
        size_t base = (size_t)c * n;

        for (uint32_t i = 0; i < n; i++) {
            m->src[base + i] = (i == 0) ? n - 1 : i - 1;
            m->gain[base + i] = 1;
            m->weight[base + i] = 1;
        }
    }
    m->flat_dirty = 1;
}

void lit_machine_copy_v(const lit_machine *m, uint64_t *out)
{
    if (!m || !out) {
        return;
    }
    memcpy(out, m->V, (size_t)m->n * sizeof(uint64_t));   /* cluster 0 */
}

static inline uint32_t bc_u32(const uint8_t *p)
{
    return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
           ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
}

static inline uint64_t bc_u64(const uint8_t *p)
{
    uint64_t v = 0;

    for (int i = 7; i >= 0; i--) {
        v = (v << 8) | p[i];
    }
    return v;
}

/*
 * Validate the stream and count reductions before running anything.
 * Returns 1 if HALT was seen, 0 if the stream ended, -1 truncated,
 * -2 unknown opcode, -3 slot/src out of range.
 */
static int bc_scan(const uint8_t *code, size_t len, uint32_t n,
                   size_t *reductions)
{
    size_t pc = 0, red = 0;

    while (pc < len) {
        uint8_t op = code[pc++];

        switch (op) {
        case LIT_BC_LOAD:
            if (len - pc < 12) {
                return -1;
            }
            if (bc_u32(code + pc) >= n) {
                return -3;
            }
            pc += 12;
            break;
        case LIT_BC_WIRE:
            if (len - pc < 24) {
                return -1;
            }
            if (bc_u32(code + pc) >= n || bc_u32(code + pc + 4) >= n) {
                return -3;
            }
            pc += 24;
            break;
        case LIT_BC_BROADCAST:
        case LIT_BC_VF_TRANSFER:
            break;
        case LIT_BC_HAMMING_REDUCE:
            red++;
            break;
        case LIT_BC_HALT:
            *reductions = red;
            return 1;
        default:
            return -2;
        }
    }
    *reductions = red;
    return 0;
}

int lit_machine_run_bytecode(lit_machine *m, const uint8_t *code, size_t len,
                             uint64_t **history, size_t *reductions,
                             uint64_t *ticks_out, int *halted,
                             int *broadcast, char *err, size_t errlen)
{
    size_t nred = 0, red_i = 0, hist_count;
    int scan;
    uint64_t ticks = 0, *hist = NULL;
    size_t pc = 0;
    int was_broadcast = 0;

    if (!m || !code) {
        if (err && errlen) {
            snprintf(err, errlen, "null machine or code");
        }
        return -1;
    }
    scan = bc_scan(code, len, m->n, &nred);
    if (scan < 0) {
        if (err && errlen) {
            if (scan == -1) {
                snprintf(err, errlen, "truncated LIT bytecode");
            } else if (scan == -3) {
                snprintf(err, errlen, "LIT bytecode slot/src out of range");
            } else {
                snprintf(err, errlen, "unknown LIT opcode");
            }
        }
        return -1;
    }
    hist_count = nred * (size_t)m->k;
    if (hist_count) {
        hist = malloc(hist_count * sizeof(uint64_t));
        if (!hist) {
            if (err && errlen) {
                snprintf(err, errlen, "out of memory");
            }
            return -1;
        }
    }

    while (pc < len) {
        uint8_t op = code[pc++];

        switch (op) {
        case LIT_BC_LOAD: {
            uint32_t slot = bc_u32(code + pc);
            uint64_t value = bc_u64(code + pc + 4);

            pc += 12;
            for (uint32_t c = 0; c < m->k; c++) {
                m->V[(size_t)c * m->n + slot] = value & m->mask;
            }
            break;
        }
        case LIT_BC_WIRE: {
            uint32_t slot = bc_u32(code + pc);
            uint32_t src = bc_u32(code + pc + 4);
            uint64_t gain = bc_u64(code + pc + 8);
            uint64_t weight = bc_u64(code + pc + 16);

            pc += 24;
            for (uint32_t c = 0; c < m->k; c++) {
                size_t idx = (size_t)c * m->n + slot;

                m->src[idx] = src;
                m->gain[idx] = gain;
                m->weight[idx] = weight;
            }
            m->flat_dirty = 1;
            break;
        }
        case LIT_BC_BROADCAST:
            was_broadcast = 1;
            break;
        case LIT_BC_VF_TRANSFER:
            machine_transfer(m);
            ticks++;
            break;
        case LIT_BC_HAMMING_REDUCE:
            machine_reduce(m);
            if (hist) {
                for (uint32_t c = 0; c < m->k; c++) {
                    hist[red_i * m->k + c] = m->dist[c];
                }
            }
            red_i++;
            break;
        case LIT_BC_HALT:
            pc = len;
            break;
        }
    }

    *history = hist;
    if (reductions) {
        *reductions = nred;
    }
    if (ticks_out) {
        *ticks_out = ticks;
    }
    if (halted) {
        *halted = (scan == 1);
    }
    if (broadcast) {
        *broadcast = was_broadcast;
    }
    return 0;
}

uint64_t lit_ring_tick(uint64_t *v, size_t n, uint32_t bits, int wrap)
{
    uint64_t mask, last, d = 0;

    if (!v || n == 0) {
        return 0;
    }
    if (bits < 1) {
        bits = 1;
    }
    if (bits > 64) {
        bits = 64;
    }
    mask = (bits == 64) ? ~(uint64_t)0 : (((uint64_t)1 << bits) - 1);
    last = v[n - 1];
    for (size_t i = n - 1; i > 0; i--) {
        uint64_t old = v[i];
        uint64_t nx = v[i - 1];          /* the source is still old */

        d += lit_popcount_u64(old & ~nx & mask);
        v[i] = nx;
    }
    {
        uint64_t old = v[0];
        uint64_t nx = wrap ? last : old;

        d += lit_popcount_u64(old & ~nx & mask);
        v[0] = nx;
    }
    return d;
}