okami.mom/strata

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

Niko1221Merge f-own-5 fccdb48a3 (image noise switch, step 1 only) into f-own-int150c2394f

main
3.7 KiB87 linesraw
1// ucrt_study.cpp - TEST-ONLY (OWN-5), Windows / MSVC only: what the Windows noise stream depends on.
2// 1. sd.cpp's PhiloxRNG (its header, compiled by MSVC: unqualified log/sin pick logf/sinf) for seeds 42 and 1234,
3//    with the CRT's FMA3 code paths on (default on an FMA CPU) and off (_set_FMA3_enable(0)).
4// 2. Over every reachable u and v (83,886,081 each): how often the CRT's logf(u) / sinf(v) differ from the correctly
5//    rounded float result (float(log_cr) / float(sin_cr) of strata_noise.hpp), FMA3 on and off.
6//   ucrt_study <dir-with-core/rng_philox.hpp is an include path at build time> [threads]
7#include <math.h>
8
9#include <atomic>
10#include <cinttypes>
11#include <cmath>
12#include <cstdio>
13#include <cstdlib>
14#include <cstring>
15#include <thread>
16#include <vector>
17
18#include "core/rng_philox.hpp"
19#include "strata_noise.hpp"
20
21using namespace strata_noise;
22static const uint64_t N_INT = 1ull << 24, F_LO = 0x4B800000ull, F_HI = 0x4F800000ull;
23static const uint64_t N_DOM = N_INT + (F_HI - F_LO + 1);
24static inline float x_of(uint64_t i) {
25    if (i < N_INT) return (float) (uint32_t) i;
26    uint32_t b = (uint32_t) (F_LO + (i - N_INT));
27    float f;
28    std::memcpy(&f, &b, 4);
29    return f;
30}
31static inline float u_of(float x) { volatile float p = (float) ((double) x * 0x1p-32); return p + 0x1p-33f; }
32static inline float v_of(float y) {
33    const float C = 0x1p-32f * 6.2831855f;
34    volatile float p = (float) ((double) y * (double) C);
35    return p + C / 2;
36}
37static uint64_t fnvb(const std::vector<float>& v) {
38    const unsigned char* p = (const unsigned char*) v.data();
39    uint64_t h = 1469598103934665603ull;
40    for (size_t i = 0; i < v.size() * 4; ++i) h = (h ^ p[i]) * 1099511628211ull;
41    return h;
42}
43
44int main(int argc, char** argv) {
45    const int T = argc > 1 ? std::atoi(argv[1]) : (int) std::thread::hardware_concurrency();
46    std::printf("CRT FMA3 enabled by default: %d\n", _get_FMA3_enable());
47    std::vector<float> crl(N_DOM), crs(N_DOM);     // correctly rounded float log(u), sin(v)
48    {
49        std::atomic<uint64_t> next{0};
50        std::vector<std::thread> th;
51        for (int t = 0; t < T; ++t) th.emplace_back([&] {
52            for (uint64_t c; (c = next.fetch_add(1 << 16)) < N_DOM;)
53                for (uint64_t i = c; i < c + (1 << 16) && i < N_DOM; ++i) {
54                    const float x = x_of(i);
55                    crl[i] = (float) log_cr(u_of(x));
56                    crs[i] = (float) sin_cr(v_of(x));
57                }
58        });
59        for (auto& t : th) t.join();
60    }
61    for (int fma = 1; fma >= 0; --fma) {
62        _set_FMA3_enable(fma);
63        for (uint64_t seed : {42ull, 1234ull}) {
64            PhiloxRNG r;
65            r.manual_seed(seed);
66            std::vector<float> v = r.randn(262144);
67            std::printf("FMA3 %d: sd.cpp MSVC stream seed %" PRIu64 " fnv64 %016" PRIx64 "\n", fma, seed, fnvb(v));
68        }
69        std::atomic<uint64_t> dl{0}, ds{0}, next{0};
70        std::vector<std::thread> th;
71        for (int t = 0; t < T; ++t) th.emplace_back([&] {
72            uint64_t a = 0, b = 0;
73            for (uint64_t c; (c = next.fetch_add(1 << 16)) < N_DOM;)
74                for (uint64_t i = c; i < c + (1 << 16) && i < N_DOM; ++i) {
75                    const float x = x_of(i);
76                    a += fbits(logf(u_of(x))) != fbits(crl[i]);
77                    b += fbits(sinf(v_of(x))) != fbits(crs[i]);
78                }
79            dl += a;
80            ds += b;
81        });
82        for (auto& t : th) t.join();
83        std::printf("FMA3 %d: logf != correctly rounded in %" PRIu64 " of %" PRIu64 " u; sinf != correctly rounded in %"
84                    PRIu64 " of %" PRIu64 " v\n", fma, (uint64_t) dl, N_DOM, (uint64_t) ds, N_DOM);
85    }
86    return 0;
87}