Files
swift-mirror/stdlib/private/SwiftPrivate/PRNG.swift
Anton Blanchard b1827d8a8f Add powerpc64le Linux support
This patch adds powerpc64le Linux support. While the patch also adds
the matching powerpc64 bits, there are endian issues that need to be
sorted out.

The PowerPC LLVM changes for the swift ABI (eg returning three element
non-homogeneous aggregates) are still in the works, but a simple LLVM
fix to allow those aggregates results in swift passing all but 8
test cases.
2016-01-15 06:48:31 +00:00

65 lines
1.9 KiB
Swift

//===--- PRNG.swift -------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SwiftShims
@inline(never) @_semantics("stdlib_binary_only") // Hide the libbsd dependency
public func rand32() -> UInt32 {
return _swift_stdlib_arc4random()
}
@inline(never) @_semantics("stdlib_binary_only") // Hide the libbsd dependency
public func rand32(exclusiveUpperBound limit: UInt32) -> UInt32 {
return _swift_stdlib_arc4random_uniform(limit)
}
@inline(never) @_semantics("stdlib_binary_only") // Hide the libbsd dependency
public func rand64() -> UInt64 {
return
(UInt64(_swift_stdlib_arc4random()) << 32) |
UInt64(_swift_stdlib_arc4random())
}
public func randInt() -> Int {
#if arch(i386) || arch(arm)
return Int(Int32(bitPattern: rand32()))
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le)
return Int(Int64(bitPattern: rand64()))
#else
fatalError("unimplemented")
#endif
}
public func randArray64(count: Int) -> [UInt64] {
var result = [UInt64](count: count, repeatedValue: 0)
for i in result.indices {
result[i] = rand64()
}
return result
}
public func randArray(count: Int) -> [Int] {
var result = [Int](count: count, repeatedValue: 0)
for i in result.indices {
result[i] = randInt()
}
return result
}
public func pickRandom<
C : CollectionType where C.Index : RandomAccessIndexType
>(c: C) -> C.Generator.Element {
let i = Int(rand32(exclusiveUpperBound: numericCast(c.count)))
return c[c.startIndex.advancedBy(numericCast(i))]
}