okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

waveringanaedge0-strata: add the engine for Edge0 145B and Edge0 120Bfd19246dc

main
3.3 KiB74 linesraw
1// The tile flatten, on its own: every input row covered exactly once, counts within [1, 64], absolute starts, and the
2// same rows as the unflattened groups for random distributions (long tail, cnt 0, cnt 1, cnt = tile, cnt = tile + 1).
3#include "strata/prefill/lat_pseudo_groups.hpp"
4
5#include <cstdio>
6#include <cstdlib>
7#include <random>
8#include <vector>
9
10using strata::prefill::flatten_groups;
11using strata::prefill::LatPseudoGroup;
12
13static int fails = 0;
14#define CHECK(c) do { if (!(c)) { std::printf("FAIL %s:%d %s\n", __FILE__, __LINE__, #c); ++fails; } } while (0)
15
16static void one_case(const char* tag, const std::vector<int32_t>& counts, int32_t base_step, int tile) {
17    const int n = (int) counts.size();
18    std::vector<unsigned long long> blobs((size_t) n);
19    std::vector<int32_t> starts((size_t) n);
20    int32_t at = 0;
21    for (int g = 0; g < n; ++g) { blobs[(size_t) g] = 0x1000ull + (unsigned long long) g; starts[(size_t) g] = at; at += counts[(size_t) g] + base_step; }
22    const auto out = flatten_groups(blobs.data(), starts.data(), counts.data(), n, tile);
23
24    // 1) counts within [1, tile]; 2) each piece lies inside its group and the pieces of a group tile it exactly
25    std::vector<int> seen((size_t) at, 0);
26    size_t q = 0;
27    for (int g = 0; g < n; ++g) {
28        int32_t covered = 0;
29        while (q < out.size() && out[q].blob == blobs[(size_t) g]) {
30            CHECK(out[q].count >= 1 && out[q].count <= tile);
31            CHECK(out[q].start == starts[(size_t) g] + covered);
32            for (int32_t r = 0; r < out[q].count; ++r) {
33                const size_t row = (size_t) (out[q].start + r);
34                CHECK(row < (size_t) at);
35                ++seen[row];
36            }
37            covered += out[q].count;
38            ++q;
39        }
40        CHECK(covered == counts[(size_t) g]);
41    }
42    CHECK(q == out.size());                       // nothing left over
43    // 3) no row covered twice, and only the groups' own rows are touched
44    for (int g = 0, i = 0; g < n; ++g)
45        for (int32_t r = 0; r < counts[(size_t) g]; ++r, ++i) {
46            const size_t row = (size_t) (starts[(size_t) g] + r);
47            CHECK(seen[row] == 1);
48        }
49    std::printf("  %-28s groups %4d  rows %6d  pieces %6zu  ok\n", tag, n, at, out.size());
50}
51
52int main() {
53    std::mt19937 rng(7);
54    const int tile = 64;
55    one_case("uniform 83", std::vector<int32_t>(64, 83), 0, tile);
56    one_case("all one", std::vector<int32_t>(200, 1), 0, tile);
57    one_case("all zero", std::vector<int32_t>(16, 0), 0, tile);
58    one_case("tile boundary", {63, 64, 65, 128, 129, 3098}, 0, tile);
59    {   // the engine's own long tail
60        std::vector<int32_t> c((size_t) 512);
61        std::uniform_int_distribution<int> d(1, 300);
62        for (auto& v : c) v = d(rng);
63        for (int i = 0; i < 6; ++i) c[(size_t) (rng() % 512)] = 3000 + (int) (rng() % 100);
64        one_case("random + long tail", c, 0, tile);
65    }
66    {   // gaps between groups (starts are absolute and need not tile the row space)
67        std::vector<int32_t> c((size_t) 40);
68        std::uniform_int_distribution<int> d(0, 200);
69        for (auto& v : c) v = d(rng);
70        one_case("gaps between groups", c, 7, tile);
71    }
72    std::printf("%s: tile flatten (%d failure(s))\n", fails == 0 ? "PASS" : "FAIL", fails);
73    return fails == 0 ? 0 : 1;
74}