From b67201b60f5aad98f6accd24ea454f2d3c27a2ef Mon Sep 17 00:00:00 2001 From: lijincheng Date: Wed, 25 Jun 2025 15:54:36 +0800 Subject: [PATCH] Add fast random 1.change mt to xorshift64 Issue:https://gitee.com/openharmony/arkcompiler_runtime_core/issues/ICHQQB Signed-off-by: lijincheng --- .../ets/runtime/intrinsics/std_math.cpp | 30 +++++++++++++++++-- static_core/runtime/include/thread.h | 8 +++++ static_core/runtime/thread.cpp | 11 ++++++- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/static_core/plugins/ets/runtime/intrinsics/std_math.cpp b/static_core/plugins/ets/runtime/intrinsics/std_math.cpp index b560be1592..92d844a51f 100644 --- a/static_core/plugins/ets/runtime/intrinsics/std_math.cpp +++ b/static_core/plugins/ets/runtime/intrinsics/std_math.cpp @@ -30,6 +30,16 @@ namespace ark::ets::intrinsics { namespace { constexpr int INT_MAX_SIZE = 63; constexpr double ROUND_BIAS = 0.5; +constexpr uint32_t RIGHT_12 = 12; +constexpr uint32_t LEFT_25 = 25; +constexpr uint32_t RIGHT_27 = 27; +constexpr uint64_t RANDOM_MULTIPLY = 0x2545F4914F6CDD1D; +constexpr uint64_t DOUBLE_MASK = 0x3FF0000000000000; + +union Uint64ToDouble { + double to; + uint64_t from; +}; int32_t ToInt32(double x) { @@ -48,6 +58,16 @@ int32_t ToInt32(double x) return static_cast(static_cast(intPart)); } +uint64_t XorShift64(uint64_t *ptr) +{ + uint64_t val = *ptr; + val ^= val >> RIGHT_12; + val ^= val << LEFT_25; + val ^= val >> RIGHT_27; + *ptr = val; + return val * RANDOM_MULTIPLY; +} + uint32_t ToUint32(double x) { return static_cast(ToInt32(x)); @@ -56,8 +76,14 @@ uint32_t ToUint32(double x) extern "C" double StdMathRandom() { - std::uniform_real_distribution urd(0.0, 1.0); - return urd(EtsCoroutine::GetCurrent()->GetPandaVM()->GetRandomEngine()); + uint64_t threadRandom = XorShift64(EtsCoroutine::GetCurrent()->GetThreadRandomState()); + uint64_t random = (threadRandom >> RIGHT_12) | DOUBLE_MASK; + // The use of security functions 'memcpy_s' here will have a greater impact on performance + Uint64ToDouble data {}; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) + data.from = random; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) + return data.to - 1; } extern "C" double StdMathAcos(double val) diff --git a/static_core/runtime/include/thread.h b/static_core/runtime/include/thread.h index 174784d303..bf35de19dc 100644 --- a/static_core/runtime/include/thread.h +++ b/static_core/runtime/include/thread.h @@ -15,6 +15,7 @@ #ifndef PANDA_RUNTIME_THREAD_H_ #define PANDA_RUNTIME_THREAD_H_ +#include #include #include #include @@ -316,8 +317,14 @@ public: return MEMBER_OFFSET(Thread, vm_); } + uint64_t *GetThreadRandomState() + { + return &threadRandomState_; + } + private: void InitCardTableData(mem::GCBarrierSet *barrier); + void InitThreadRandomState(); protected: // NOLINTBEGIN(misc-non-private-member-variables-in-classes) @@ -346,6 +353,7 @@ protected: #ifndef NDEBUG uintptr_t runtimeCallEnabled_ {1}; #endif + uint64_t threadRandomState_ {0}; // NOLINTEND(misc-non-private-member-variables-in-classes) private: diff --git a/static_core/runtime/thread.cpp b/static_core/runtime/thread.cpp index bf404b6556..e3ea24a091 100644 --- a/static_core/runtime/thread.cpp +++ b/static_core/runtime/thread.cpp @@ -28,6 +28,7 @@ #include "runtime/mem/object_helpers.h" #include "tooling/pt_thread_info.h" #include "runtime/mem/runslots_allocator-inl.h" +#include namespace ark { using TaggedValue = coretypes::TaggedValue; @@ -35,6 +36,7 @@ using TaggedType = coretypes::TaggedType; mem::TLAB *ManagedThread::zeroTlab_ = nullptr; static const int MIN_PRIORITY = os::thread::LOWEST_PRIORITY; +static constexpr int MICROSEC_CONVERSION = 1000000; static mem::InternalAllocatorPtr GetInternalAllocator(Thread *thread) { @@ -84,7 +86,7 @@ Thread::Thread(PandaVM *vm, ThreadType threadType) : ThreadProxy(vm->GetMutatorL InitCardTableData(barrierSet_); } InitializeThreadFlag(); - + InitThreadRandomState(); #ifdef PANDA_USE_CUSTOM_SIGNAL_STACK mem::InternalAllocatorPtr allocator = Runtime::GetCurrent()->GetInternalAllocator(); signalStack_.ss_sp = allocator->Alloc(SIGSTKSZ * 8U); @@ -94,6 +96,13 @@ Thread::Thread(PandaVM *vm, ThreadType threadType) : ThreadProxy(vm->GetMutatorL #endif } +void Thread::InitThreadRandomState() +{ + struct timeval tv = {0, 0}; + gettimeofday(&tv, nullptr); + threadRandomState_ = static_cast(tv.tv_sec * MICROSEC_CONVERSION + tv.tv_usec); +} + void Thread::InitCardTableData(mem::GCBarrierSet *barrier) { auto postBarrierType = barrier->GetPostType(); -- Gitee