简体   繁体   English

将uint64_t rdtsc值转换为uint32_t

[英]Converting a uint64_t rdtsc value to a uint32_t

I have a RNG function xorshift128plus that takes a Xorshift128PlusKey : 我有一个RNG功能xorshift128plus,需要一个Xorshift128PlusKey:

/** 
        * \brief Keys for scalar xorshift128. Must be non-zero.
        * These are modified by xorshift128plus.
        */
        struct Xorshift128PlusKey
        {
            uint64_t s1;
            uint64_t s2;
        };

        /** 
        * \brief Return a new 64-bit random number.
        */
        uint64_t xorshift128plus(Xorshift128PlusKey* key);

I would like to seed my RNG using rdtsc (processor time stamp). 我想使用rdtsc (处理器时间戳记)播种我的RNG。 The problem is that the __rdtsc intrinsic under msvc returns a 64 bit unsigned integer and the seed must be a 32 bit unsigned integer . 问题是msvc下的__rdtsc内在函数返回64位无符号整数 ,而种子必须是32位无符号整数 What is the best way to convert the rdtsc to a seed while preserving randomness . 在保留随机性的同时将rdtsc转换为种子的最佳方法是什么。 The conversion must be as fast as possible . 转换必须尽可能快

I can't use the std lib or boost . 我不能使用std libboost (It's for a game engine) (用于游戏引擎)

The 64-bit processor timestamp isn't random at all, so there is no need to preserve randomness when narrowing it to 32 bits. 64位处理器的时间戳完全不是随机的,因此将其范围缩小到32位时,无需保留随机性。 You can simply use the least-significant 32 bits as your seed. 您可以简单地使用最低有效的32位作为种子。 Randomness is the responsibility of the PRNG, not of the seed. 随机性是PRNG的责任,而不是种子的责任。

unsigned __int64 tsc = __rdtsc();
uint32_t seed = static_cast<uint32_t>(tsc & 0xFFFFFFFF);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将uint32_t转换为uint64_t导致不同的值? - casting uint32_t to uint64_t results in different value? 将包含 32 位值的 uint64_t 传递给参数实际上是 uint32_t 的外部 function 是否安全? - Is it safe to pass a uint64_t containing a 32-bit value to an external function whose parameter is actually a uint32_t? 将两个 uint32_t 转换为 uint64_t,然后根据位模式而不是值转换回 double - Turn two uint32_t into a uint64_t, then turn back into a double based on the bit pattern not the value 如何让gcc警告从uint32_t到uint64_t的转换? - How to have gcc warn about conversions from uint32_t to uint64_t? 将 uint32_t 与 uint64_t 连接和拆分的最佳方法 - best way to concatenating and splitting uint32_t to/from uint64_t 如何以类型安全的方式分配uint32_t :: max和uint64_t的最小值? - How to assign min of uint32_t::max and uint64_t in type safe manner? 使用多个uint32_t整数生成uint64_t哈希键 - Generate uint64_t hash key with several uint32_t integers 在 uint32_t 范围内变换 uint64_t 范围 - Transform uint64_t range in uint32_t range 如何将 uint32_t 或 uint64_t 存储在 void 指针中 - How can I store a uint32_t or uint64_t in a void pointer c++中将一长串字符转换为uint32_t或uint64_t - convert a long string of characters to uint32_t or uint64_t in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM