diff --git a/benchmark/single-source/RandomValues.swift b/benchmark/single-source/RandomValues.swift index 6a5a5b4be2075..cea327d02f24a 100644 --- a/benchmark/single-source/RandomValues.swift +++ b/benchmark/single-source/RandomValues.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift.org open source project // -// Copyright (c) 2018 Apple Inc. and the Swift project authors +// Copyright (c) 2018 - 2020 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information @@ -19,14 +19,35 @@ import TestsUtils // public let RandomValues = [ - BenchmarkInfo(name: "RandomIntegersDef", runFunction: run_RandomIntegersDef, - tags: [.api], legacyFactor: 100), - BenchmarkInfo(name: "RandomIntegersLCG", runFunction: run_RandomIntegersLCG, - tags: [.api]), - BenchmarkInfo(name: "RandomDoubleDef", runFunction: run_RandomDoubleDef, - tags: [.api], legacyFactor: 100), - BenchmarkInfo(name: "RandomDoubleLCG", runFunction: run_RandomDoubleLCG, - tags: [.api], legacyFactor: 2), + BenchmarkInfo(name: "RandomIntegersDef", + runFunction: run_RandomIntegersDef, tags: [.api], legacyFactor: 100), + + BenchmarkInfo(name: "RandomIntegersLCG", + runFunction: run_RandomIntegersLCG, tags: [.api]), + + BenchmarkInfo(name: "RandomDoubleDef", + runFunction: run_RandomDoubleDef, tags: [.api], legacyFactor: 100), + + BenchmarkInfo(name: "RandomDoubleLCG", + runFunction: run_RandomDoubleLCG, tags: [.api], legacyFactor: 2), + + BenchmarkInfo(name: "RandomDoubleUnitDef", + runFunction: run_RandomDoubleUnitDef, tags: [.api]), + + BenchmarkInfo(name: "RandomDoubleUnitLCG", + runFunction: run_RandomDoubleUnitLCG, tags: [.api]), + + BenchmarkInfo(name: "RandomDoubleBinadeDef", + runFunction: run_RandomDoubleBinadeDef, tags: [.api]), + + BenchmarkInfo(name: "RandomDoubleBinadeLCG", + runFunction: run_RandomDoubleBinadeLCG, tags: [.api]), + + BenchmarkInfo(name: "RandomDoubleAsymDef", + runFunction: run_RandomDoubleAsymDef, tags: [.api]), + + BenchmarkInfo(name: "RandomDoubleAsymLCG", + runFunction: run_RandomDoubleAsymLCG, tags: [.api]), ] /// A linear congruential PRNG. @@ -89,3 +110,62 @@ public func run_RandomDoubleLCG(_ N: Int) { blackHole(x) } } + +@inline(never) +public func run_RandomDoubleUnitDef(_ N: Int) { + randomDoubleHelperDef(N, 0..<1) +} + +@inline(never) +public func run_RandomDoubleUnitLCG(_ N: Int) { + randomDoubleHelperLCG(N, 0..<1) +} + +@inline(never) +public func run_RandomDoubleBinadeDef(_ N: Int) { + randomDoubleHelperDef(N, 1..<2) +} + +@inline(never) +public func run_RandomDoubleBinadeLCG(_ N: Int) { + randomDoubleHelperLCG(N, 1..<2) +} + +@inline(never) +public func run_RandomDoubleAsymDef(_ N: Int) { + let range = -(1.0.nextDown) ..< 0.126 + randomDoubleHelperDef(N, range) +} + +@inline(never) +public func run_RandomDoubleAsymLCG(_ N: Int) { + let range = -(1.0.nextDown) ..< 0.126 + randomDoubleHelperLCG(N, range) +} + +@inline(__always) +private func randomDoubleHelperDef( + _ N: Int, _ range: Range, reps: Int = 1_000 +) { + for _ in 0 ..< N { + var x = 0.0 + for _ in 0 ..< reps { + x += Double.random(in: range) + } + blackHole(x) + } +} + +@inline(__always) +private func randomDoubleHelperLCG( + _ N: Int, _ range: Range, reps: Int = 50_000 +) { + for _ in 0 ..< N { + var x = 0.0 + var generator = LCRNG(seed: 0) + for _ in 0 ..< reps { + x += Double.random(in: range, using: &generator) + } + blackHole(x) + } +}