1 //===-- Implementation of rand --------------------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "src/stdlib/rand.h" 10 #include "src/__support/common.h" 11 #include "src/stdlib/rand_util.h" 12 13 namespace LIBC_NAMESPACE { 14 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 unsigned long x = rand_next; 19 x ^= x >> 12; 20 x ^= x << 25; 21 x ^= x >> 27; 22 rand_next = x; 23 return static_cast<int>((x * 0x2545F4914F6CDD1Dul) >> 32) & RAND_MAX; 24 } 25 26 } // namespace LIBC_NAMESPACE 27