From 1bcaebbd1f7fc86ccf1c05bc4891fa09f770b634 Mon Sep 17 00:00:00 2001 From: dzaima Date: Sun, 25 May 2025 22:41:09 +0300 Subject: [PATCH] heuristic randomization infrastructure --- src/core/stuff.c | 10 ++++++++++ src/h.h | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/core/stuff.c b/src/core/stuff.c index bfd04cfb..5c7a9039 100644 --- a/src/core/stuff.c +++ b/src/core/stuff.c @@ -627,3 +627,13 @@ DEBUG_FN void g_pst(void) { vm_pstLive(); fflush(stdout); fflush(stderr); } fflush(stderr); } #endif + +#if RANDOMIZE_HEURISTICS + #include "../utils/wyhash.h" + u64 seed; + bool heuristic_rand(bool heuristic, bool true_req, bool false_req) { + assert(heuristic? true_req : false_req); + if (!true_req | !false_req) return heuristic; + return wyrand(&seed) & 1; + } +#endif \ No newline at end of file diff --git a/src/h.h b/src/h.h index 3dadc971..cf016108 100644 --- a/src/h.h +++ b/src/h.h @@ -315,6 +315,7 @@ typedef struct Arr { #define NOGC_CHECK(M) do { if (cbqn_noAlloc && !gc_depth) fatal(M); } while (0) #define NOGC_S cbqn_NOGC_start() #define NOGC_E cbqn_noAlloc=false + #define HEURISTIC_BOUNDED(X, TRUE_REQ, FALSE_REQ) ({ bool hb_ = (X); assert(hb_? (TRUE_REQ) : (FALSE_REQ)); hb_; }) #else #define assert(X) do {if (!(X)) __builtin_unreachable();} while(0) #define debug_assert(X) @@ -324,7 +325,18 @@ typedef struct Arr { #define NOGC_S #define NOGC_E #define NOGC_CHECK(M) + #define HEURISTIC_BOUNDED(X, TRUE_REQ, FALSE_REQ) (X) #endif +#if RANDOMIZE_HEURISTICS + bool heuristic_rand(bool heuristic, bool true_req, bool false_req); + #undef HEURISTIC_BOUNDED + #define HEURISTIC_BOUNDED(X, TRUE_REQ, FALSE_REQ) heuristic_rand(X, TRUE_REQ, FALSE_REQ) +#else + #define RANDOMIZE_HEURISTICS 0 +#endif +#define HEURISTIC(X) HEURISTIC_BOUNDED(X, true, true) +#define MAY_T(X) ({ bool hw_=(X); HEURISTIC_BOUNDED(hw_, true, !hw_); }) // returns X, or, with randomized heuristics enabled, possibly true +#define MAY_F(X) ({ bool hw_=(X); HEURISTIC_BOUNDED(hw_, hw_, true); }) // returns X, or, with randomized heuristics enabled, possibly false #if WARN_SLOW void warn_slow1(char* s, B x); void warn_slow2(char* s, B w, B x);