[libc-commits] [libc] [libc] Change rand implementation so all tests pass in both 32- and 64-bit systems (PR #98692)

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Wed Jul 17 07:43:07 PDT 2024


jhuber6 wrote:

> > I still think these should just be separate functions, the constants aren't arbitrary and can't be mixed, making it a template is just confusing.
> 
> I can move the code to rand()'s body, wdyt?
> 
> ```
> LLVM_LIBC_FUNCTION(int, rand, (void)) {
>   unsigned long orig = rand_next.load(cpp::MemoryOrder::RELAXED);
> 
>   if constexpr (sizeof(void *) == sizeof(uint64_t)) {
>     for (;;) {
>       unsigned long x = orig;
>       x ^= x >> 12;
>       x ^= x << 25;
>       x ^= x >> 27;
>       if (rand_next.compare_exchange_strong(orig, x, cpp::MemoryOrder::ACQUIRE,
>                                             cpp::MemoryOrder::RELAXED))
>         return static_cast<int>((x * 0x2545F4914F6CDD1Dul) >> 32) & RAND_MAX;
>       sleep_briefly();
>     }
>   } else {
>     for (;;) {
>       unsigned long x = orig;
>       x ^= x >> 13;
>       x ^= x << 27;
>       x ^= x >> 5;
>       if (rand_next.compare_exchange_strong(orig, x, cpp::MemoryOrder::ACQUIRE,
>                                             cpp::MemoryOrder::RELAXED))
>         return static_cast<int>(x * 1597334677ul) & RAND_MAX;
>       sleep_briefly();
>     }
>   }
>   __builtin_unreachable();
> }
> ```

Works for me, though having the function name helps people know what it's supposed to be. If it's an internal function it'll just be trvially dropped if uncalled.

https://github.com/llvm/llvm-project/pull/98692


More information about the libc-commits mailing list