okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
150c2394f
main
1// check_domain.cpp - TEST-ONLY (OWN-5): exhaustive check of strata_noise.hpp over every input Box-Muller can see. 2// x = float(counter) for every 32-bit counter: the integers 0 .. 2^24-1 plus every float from 2^24 to 2^32 3// (83,886,081 values). From each x: u (log path) and, with x as y, v (sin path). 4// mode "glibc": compares log_cr / sin_cr / sqrt_cr with glibc's log / sin / sqrt (bit for bit) and the float s and 5// the float r = s * sin as sd.cpp's x86 build makes them; lists every mismatch. 6// mode "hash": only hashes our log and sin doubles over the domain (any platform, e.g. MSVC): equal hashes on two 7// platforms = identical results for every reachable input, hence for every seed. 8// check_domain <glibc|hash> [threads] 9#include <algorithm> 10#include <atomic> 11#include <utility> 12#include <cinttypes> 13#include <cmath> 14#include <cstdio> 15#include <cstdlib> 16#include <cstring> 17#include <mutex> 18#include <string> 19#include <thread> 20#include <vector> 21 22#include "strata_noise.hpp" 23 24using namespace strata_noise ; 25 26static const uint64_t N_INT = 1ull <<24 ; 27static const uint64_t F_LO = 0x4B800000ull ,F_HI = 0x4F800000ull ; 28static const uint64_t N_DOM = N_INT + (F_HI - F_LO + 1 ); 29 30static inline float x_of (uint64_t i ) { 31if (i < N_INT )return (float ) (uint32_t )i ; 32uint32_t b = (uint32_t ) (F_LO + (i - N_INT )); 33float f ; 34 std::memcpy (& f ,& b ,4 ); 35return f ; 36} 37static inline float u_of (float x ) {volatile float p = (float ) ((double )x * 0x1p-32 );return p + 0x1p-33f ; } 38static inline float v_of (float y ) { 39const float C = 0x1p-32f * 6.2831855f ; 40volatile float p = (float ) ((double )y * (double )C ); 41return p + C /2 ; 42} 43static inline uint64_t fnv (uint64_t h ,uint64_t v ) { 44for (int k = 0 ;k < 8 ;++ k ) {h = (h ^ (v & 0xff ))* 1099511628211ull ;v >>=8 ; } 45return h ; 46} 47 48int main (int argc ,char ** argv ) { 49 std::printf ("sin exception table (compiled in): %zu entries\n" , strata_noise::sin_exc ().keys .size ()); 50const std::string mode = argc > 1 ?argv [1 ] :"hash" ; 51const int T = argc > 2 ? std::atoi (argv [2 ]) : (int ) std::thread::hardware_concurrency (); 52const int CH = 256 ;// fixed chunking: the hash does not depend on T 53 std::vector < uint64_t > hlog (CH ),hsin (CH ); 54 std::atomic < int > next {0 }; 55 std::atomic < uint64_t > mis_log {0 },mis_s {0 },mis_sin {0 },mis_sqrt {0 },mis_x86 {0 }; 56 std::mutex mu ; 57 std::vector < std::pair < uint32_t ,uint64_t >> bad_log ,bad_sin ;// input float bits, glibc double bits 58 std::vector < std::thread > th ; 59for (int t = 0 ;t < T ;++ t )th .emplace_back ([& ] { 60for (int c ; (c = next ++ )< CH ;) { 61uint64_t a = N_DOM * c /CH ,b = N_DOM * (c + 1 ) /CH ,h1 = 1469598103934665603ull ,h2 = h1 ; 62for (uint64_t i = a ;i < b ;++ i ) { 63const float x = x_of (i ),u = u_of (x ),v = v_of (x ); 64const double ol = log_cr (u ),os = sin_cr (v ); 65h1 = fnv (h1 ,dbits (ol )); 66h2 = fnv (h2 ,dbits (sin_x86 (v ))); 67if (mode == "glibc" ) { 68const double gl = std::log ((double )u ),gs = std::sin ((double )v ); 69if (dbits (ol )!= dbits (gl )) { 70++ mis_log ; 71float s1 = ol == 0.0 ?-0.0f : (float )sqrt_cr (-2.0 * ol ); 72float s2 = (float ) std::sqrt (-2.0f * gl ); 73if (fbits (s1 )!= fbits (s2 ))++ mis_s ; 74 std::lock_guard < std::mutex > g (mu ); 75bad_log .push_back ({fbits (u ),dbits (gl )}); 76if (mis_log < 20 ) std::printf ("LOG u=%a ours=%a glibc=%a s %s\n" ,u ,ol ,gl , 77fbits (s1 )== fbits (s2 ) ?"same" :"DIFFERS" ); 78 } 79if (gl != 0.0 ) {// our integer sqrt vs IEEE sqrt on glibc's own log 80double q1 = sqrt_cr (-2.0 * gl ),q2 = std::sqrt (-2.0 * gl ); 81if (dbits (q1 )!= dbits (q2 ))++ mis_sqrt ; 82 } 83if (dbits (sin_x86 (v ))!= dbits (gs ))++ mis_x86 ; 84if (dbits (os )!= dbits (gs )) { 85++ mis_sin ; 86 std::lock_guard < std::mutex > g (mu ); 87bad_sin .push_back ({fbits (v ),dbits (gs )}); 88if (mis_sin < 20 ) std::printf ("SIN v=%a ours=%a glibc=%a\n" ,v ,os ,gs ); 89 } 90 } 91 } 92hlog [c ]= h1 ; 93hsin [c ]= h2 ; 94 } 95 }); 96for (auto & t :th )t .join (); 97uint64_t H1 = 1469598103934665603ull ,H2 = H1 ; 98for (int c = 0 ;c < CH ;++ c ) {H1 = fnv (H1 ,hlog [c ]);H2 = fnv (H2 ,hsin [c ]); } 99 std::printf ("domain %" PRIu64 " values; hash(log_cr) %016" PRIx64 " hash(sin_x86) %016" PRIx64 "\n" ,N_DOM ,H1 ,H2 ); 100if (mode == "glibc" ) { 101 std::printf ("log mismatches %" PRIu64 " (of which the float s differs: %" PRIu64 "), sin mismatches %" PRIu64 102", integer sqrt vs IEEE sqrt mismatches %" PRIu64 103", sin_x86 vs glibc mismatches (0 needs the exception table built in) %" PRIu64 "\n" , 104 (uint64_t )mis_log , (uint64_t )mis_s , (uint64_t )mis_sin , (uint64_t )mis_sqrt , (uint64_t )mis_x86 ); 105// the exception lists: every reachable input where glibc differs from ours (float bits, glibc double bits) 106 std::sort (bad_log .begin (),bad_log .end ()); 107 std::sort (bad_sin .begin (),bad_sin .end ()); 108const std::pair < std::vector < std::pair < uint32_t ,uint64_t >>* ,const char *> lists [2 ]= { 109 {& bad_log ,"exc_log.txt" }, {& bad_sin ,"exc_sin.txt" }}; 110for (const auto & l :lists ) { 111FILE * f = std::fopen (l .second ,"w" ); 112for (const auto & e :* l .first ) std::fprintf (f ,"%08x %016" PRIx64 "\n" ,e .first ,e .second ); 113 std::fclose (f ); 114 } 115 std::printf ("wrote exc_log.txt (%zu) and exc_sin.txt (%zu)\n" ,bad_log .size (),bad_sin .size ()); 116 } 117return 0 ; 118}