okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
150c2394f
main
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 ) { 25if (i < N_INT )return (float ) (uint32_t )i ; 26uint32_t b = (uint32_t ) (F_LO + (i - N_INT )); 27float f ; 28 std::memcpy (& f ,& b ,4 ); 29return 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 ) { 33const float C = 0x1p-32f * 6.2831855f ; 34volatile float p = (float ) ((double )y * (double )C ); 35return p + C /2 ; 36} 37static uint64_t fnvb (const std::vector < float >& v ) { 38const unsigned char * p = (const unsigned char * )v .data (); 39uint64_t h = 1469598103934665603ull ; 40for (size_t i = 0 ;i < v .size ()* 4 ;++ i )h = (h ^p [i ])* 1099511628211ull ; 41return h ; 42} 43 44int main (int argc ,char ** argv ) { 45const 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 ; 51for (int t = 0 ;t < T ;++ t )th .emplace_back ([& ] { 52for (uint64_t c ; (c = next .fetch_add (1 <<16 ))< N_DOM ;) 53for (uint64_t i = c ;i < c + (1 <<16 )&& i < N_DOM ;++ i ) { 54const float x = x_of (i ); 55crl [i ]= (float )log_cr (u_of (x )); 56crs [i ]= (float )sin_cr (v_of (x )); 57 } 58 }); 59for (auto & t :th )t .join (); 60 } 61for (int fma = 1 ;fma >=0 ;-- fma ) { 62_set_FMA3_enable (fma ); 63for (uint64_t seed : {42ull ,1234ull }) { 64PhiloxRNG r ; 65r .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 ; 71for (int t = 0 ;t < T ;++ t )th .emplace_back ([& ] { 72uint64_t a = 0 ,b = 0 ; 73for (uint64_t c ; (c = next .fetch_add (1 <<16 ))< N_DOM ;) 74for (uint64_t i = c ;i < c + (1 <<16 )&& i < N_DOM ;++ i ) { 75const float x = x_of (i ); 76a += fbits (logf (u_of (x )))!= fbits (crl [i ]); 77b += fbits (sinf (v_of (x )))!= fbits (crs [i ]); 78 } 79dl += a ; 80ds += b ; 81 }); 82for (auto & t :th )t .join (); 83 std::printf ("FMA3 %d: logf != correctly rounded in %" PRIu64 " of %" PRIu64 " u; sinf != correctly rounded in %" 84PRIu64 " of %" PRIu64 " v\n" ,fma , (uint64_t )dl ,N_DOM , (uint64_t )ds ,N_DOM ); 85 } 86return 0 ; 87}