Fix bug in PCG implementation in wasi.cpp

This commit is contained in:
刘皓 2025-07-02 09:10:19 -04:00
parent 0d18b9e422
commit 880fdeca43
No known key found for this signature in database
GPG key ID: 7901753DB465B711

View file

@ -1252,7 +1252,7 @@ extern "C" uint32_t w2c_wasi__snapshot__preview1_random_get(wasi_t *wasi, wasm_p
wasi->prng_state = wasi->prng_state * (uint64_t)6364136223846793005U + (uint64_t)1442695040888963407U; // Advance state before computing output to improve instruction-level parallelism wasi->prng_state = wasi->prng_state * (uint64_t)6364136223846793005U + (uint64_t)1442695040888963407U; // Advance state before computing output to improve instruction-level parallelism
uint32_t xsh = (state ^ (state >> 18U)) >> 27U; uint32_t xsh = (state ^ (state >> 18U)) >> 27U;
uint32_t rot = state >> 59U; uint32_t rot = state >> 59U;
uint32_t out = xsh >> rot | xsh << ((uint32_t)32U - rot); uint32_t out = xsh >> rot | xsh << ((uint32_t)31U - rot);
#ifdef MKXPZ_BIG_ENDIAN #ifdef MKXPZ_BIG_ENDIAN
// Byte swap the output on big-endian machines to preserve state state compatibility across machines with different endiannesses // Byte swap the output on big-endian machines to preserve state state compatibility across machines with different endiannesses
std::reverse_copy((uint8_t *)&out, (uint8_t *)&out + 4, wasi->prng_buffer); std::reverse_copy((uint8_t *)&out, (uint8_t *)&out + 4, wasi->prng_buffer);