1 //===-- Implementation of rand --------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "src/stdlib/rand.h"
10 #include "src/__support/common.h"
11 #include "src/stdlib/rand_util.h"
13 namespace LIBC_NAMESPACE
{
15 // An implementation of the xorshift64star pseudo random number generator. This
16 // is a good general purpose generator for most non-cryptographics applications.
17 LLVM_LIBC_FUNCTION(int, rand
, (void)) {
18 rand_next
^= rand_next
>> 12;
19 rand_next
^= rand_next
<< 25;
20 rand_next
^= rand_next
>> 27;
21 return static_cast<int>((rand_next
* 0x2545F4914F6CDD1Dul
) >> 32) & RAND_MAX
;
24 } // namespace LIBC_NAMESPACE