Skip to content

Use the "nearly divisionless" algorithm on all targets. #37920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 120 additions & 6 deletions benchmark/single-source/RandomValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,26 @@ public let RandomValues = [
tags: [.api], legacyFactor: 100),
BenchmarkInfo(name: "RandomIntegersLCG", runFunction: run_RandomIntegersLCG,
tags: [.api]),
BenchmarkInfo(name: "RandomInt8Def", runFunction: run_RandomInt8Def,
tags: [.api], legacyFactor: 100),
BenchmarkInfo(name: "RandomInt8LCG", runFunction: run_RandomInt8LCG,
tags: [.api]),
BenchmarkInfo(name: "RandomInt64Def", runFunction: run_RandomInt64Def,
tags: [.api], legacyFactor: 100),
BenchmarkInfo(name: "RandomInt64LCG", runFunction: run_RandomInt64LCG,
tags: [.api]),
BenchmarkInfo(name: "RandomDoubleDef", runFunction: run_RandomDoubleDef,
tags: [.api], legacyFactor: 100),
BenchmarkInfo(name: "RandomDoubleLCG", runFunction: run_RandomDoubleLCG,
tags: [.api], legacyFactor: 2),
tags: [.api]),
BenchmarkInfo(name: "RandomDoubleOpaqueDef", runFunction: run_RandomDoubleOpaqueDef,
tags: [.api], legacyFactor: 100),
BenchmarkInfo(name: "RandomDoubleOpaqueLCG", runFunction: run_RandomDoubleOpaqueLCG,
tags: [.api]),
BenchmarkInfo(name: "RandomDouble01Def", runFunction: run_RandomDouble01Def,
tags: [.api], legacyFactor: 100),
BenchmarkInfo(name: "RandomDouble01LCG", runFunction: run_RandomDouble01LCG,
tags: [.api]),
]

/// A linear congruential PRNG.
Expand All @@ -49,19 +65,70 @@ public func run_RandomIntegersDef(_ N: Int) {
for _ in 0 ..< N {
var x = 0
for _ in 0 ..< 1_000 {
x &+= Int.random(in: 0...10_000)
x &+= .random(in: 0...10_000)
}
blackHole(x)
}
}

@inline(never)
public func run_RandomIntegersLCG(_ N: Int) {
for _ in 0 ..< N {
var x = 0
var generator = LCRNG(seed: 0)
for _ in 0 ..< 100_000 {
x &+= .random(in: 0 ... 10_000, using: &generator)
}
blackHole(x)
}
}

@inline(never)
public func run_RandomInt8Def(_ N: Int) {
for _ in 0 ..< N {
var x: Int8 = 0
for _ in 0 ..< 1_000 {
x &+= .random(in: -65 ... identity(65))
}
blackHole(x)
}
}

@inline(never)
public func run_RandomInt8LCG(_ N: Int) {
for _ in 0 ..< N {
var x: Int8 = 0
var generator = LCRNG(seed: 0)
for _ in 0 ..< 100_000 {
x &+= .random(in: -65 ... identity(65), using: &generator)
}
blackHole(x)
}
}

@inline(never)
public func run_RandomInt64Def(_ N: Int) {
for _ in 0 ..< N {
var x: Int64 = 0
for _ in 0 ..< 1_000 {
x &+= .random(in:
-5_000_000_000_000_000_000 ... identity(5_000_000_000_000_000_000)
)
}
blackHole(x)
}
}

@inline(never)
public func run_RandomInt64LCG(_ N: Int) {
for _ in 0 ..< N {
var x: Int64 = 0
var generator = LCRNG(seed: 0)
for _ in 0 ..< 100_000 {
x &+= Int64.random(in: 0...10_000, using: &generator)
x &+= .random(in:
-5_000_000_000_000_000_000 ... identity(5_000_000_000_000_000_000),
using: &generator
)
}
blackHole(x)
}
Expand All @@ -72,7 +139,7 @@ public func run_RandomDoubleDef(_ N: Int) {
for _ in 0 ..< N {
var x = 0.0
for _ in 0 ..< 1_000 {
x += Double.random(in: -1000...1000)
x += .random(in: -1000 ... 1000)
}
blackHole(x)
}
Expand All @@ -83,9 +150,56 @@ public func run_RandomDoubleLCG(_ N: Int) {
for _ in 0 ..< N {
var x = 0.0
var generator = LCRNG(seed: 0)
for _ in 0 ..< 50_000 {
x += Double.random(in: -1000...1000, using: &generator)
for _ in 0 ..< 100_000 {
x += .random(in: -1000 ... 1000, using: &generator)
}
blackHole(x)
}
}

@inline(never)
public func run_RandomDoubleOpaqueDef(_ N: Int) {
for _ in 0 ..< N {
var x = 0.0
for _ in 0 ..< 1_000 {
x += .random(in: -1000 ... identity(1000))
}
blackHole(x)
}
}

@inline(never)
public func run_RandomDoubleOpaqueLCG(_ N: Int) {
for _ in 0 ..< N {
var x = 0.0
var generator = LCRNG(seed: 0)
for _ in 0 ..< 100_000 {
x += .random(in: -1000 ... identity(1000), using: &generator)
}
blackHole(x)
}
}

@inline(never)
public func run_RandomDouble01Def(_ N: Int) {
for _ in 0 ..< N {
var x = 0.0
for _ in 0 ..< 1_000 {
x += .random(in: 0 ..< 1)
}
blackHole(x)
}
}

@inline(never)
public func run_RandomDouble01LCG(_ N: Int) {
for _ in 0 ..< N {
var x = 0.0
var generator = LCRNG(seed: 0)
for _ in 0 ..< 100_000 {
x += .random(in: 0 ..< 1, using: &generator)
}
blackHole(x)
}
}

12 changes: 0 additions & 12 deletions stdlib/public/core/Random.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,6 @@ extension RandomNumberGenerator {
upperBound: T
) -> T {
_precondition(upperBound != 0, "upperBound cannot be zero.")
#if arch(i386) || arch(arm) || arch(arm64_32) // TODO(FIXME) SR-10912
let tmp = (T.max % upperBound) + 1
let range = tmp == upperBound ? 0 : tmp
var random: T = 0

repeat {
random = next()
} while random < range

return random % upperBound
#else
var random: T = next()
var m = random.multipliedFullWidth(by: upperBound)
if m.low < upperBound {
Expand All @@ -124,7 +113,6 @@ extension RandomNumberGenerator {
}
}
return m.high
#endif
}
}

Expand Down