Skip to content

Commit bd5913a

Browse files
committed
[embedded] Make RNG APIs available on embedded Swift
1 parent c80fbae commit bd5913a

File tree

10 files changed

+94
-14
lines changed

10 files changed

+94
-14
lines changed

stdlib/public/core/Bool.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ public struct Bool: Sendable {
133133
/// - Returns: Either `true` or `false`, randomly chosen with equal
134134
/// probability.
135135
@inlinable
136-
@_unavailableInEmbedded
137136
public static func random() -> Bool {
138137
var g = SystemRandomNumberGenerator()
139138
return Bool.random(using: &g)

stdlib/public/core/Collection.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,6 @@ extension Collection {
951951
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length
952952
/// of the collection.
953953
@inlinable
954-
@_unavailableInEmbedded
955954
public func randomElement() -> Element? {
956955
var g = SystemRandomNumberGenerator()
957956
return randomElement(using: &g)

stdlib/public/core/CollectionAlgorithms.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@ extension Sequence {
571571
///
572572
/// - Complexity: O(*n*), where *n* is the length of the sequence.
573573
@inlinable
574-
@_unavailableInEmbedded
575574
public func shuffled() -> [Element] {
576575
var g = SystemRandomNumberGenerator()
577576
return shuffled(using: &g)
@@ -630,7 +629,6 @@ extension MutableCollection where Self: RandomAccessCollection {
630629
///
631630
/// - Complexity: O(*n*), where *n* is the length of the collection.
632631
@inlinable
633-
@_unavailableInEmbedded
634632
public mutating func shuffle() {
635633
var g = SystemRandomNumberGenerator()
636634
shuffle(using: &g)

stdlib/public/core/FloatingPointRandom.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ extension BinaryFloatingPoint where Self.RawSignificand: FixedWidthInteger {
108108
/// `range` must be finite and non-empty.
109109
/// - Returns: A random value within the bounds of `range`.
110110
@inlinable
111-
@_unavailableInEmbedded
112111
public static func random(in range: Range<Self>) -> Self {
113112
var g = SystemRandomNumberGenerator()
114113
return Self.random(in: range, using: &g)
@@ -209,7 +208,6 @@ extension BinaryFloatingPoint where Self.RawSignificand: FixedWidthInteger {
209208
/// - Parameter range: The range in which to create a random value. Must be finite.
210209
/// - Returns: A random value within the bounds of `range`.
211210
@inlinable
212-
@_unavailableInEmbedded
213211
public static func random(in range: ClosedRange<Self>) -> Self {
214212
var g = SystemRandomNumberGenerator()
215213
return Self.random(in: range, using: &g)

stdlib/public/core/Integers.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2694,7 +2694,6 @@ extension FixedWidthInteger {
26942694
/// `range` must not be empty.
26952695
/// - Returns: A random value within the bounds of `range`.
26962696
@inlinable
2697-
@_unavailableInEmbedded
26982697
public static func random(in range: Range<Self>) -> Self {
26992698
var g = SystemRandomNumberGenerator()
27002699
return Self.random(in: range, using: &g)
@@ -2767,7 +2766,6 @@ extension FixedWidthInteger {
27672766
/// - Parameter range: The range in which to create a random value.
27682767
/// - Returns: A random value within the bounds of `range`.
27692768
@inlinable
2770-
@_unavailableInEmbedded
27712769
public static func random(in range: ClosedRange<Self>) -> Self {
27722770
var g = SystemRandomNumberGenerator()
27732771
return Self.random(in: range, using: &g)

stdlib/public/core/Random.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ extension RandomNumberGenerator {
147147
/// from `/dev/urandom`.
148148
/// - Windows uses `BCryptGenRandom`.
149149
@frozen
150-
@_unavailableInEmbedded
151150
public struct SystemRandomNumberGenerator: RandomNumberGenerator, Sendable {
152151
/// Creates a new instance of the system's default random number generator.
153152
@inlinable
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -parse-as-library -enable-experimental-feature Embedded %s -c -o %t/a.o
3+
4+
// RUN: grep DEP\: %s | sed 's#// DEP\: ##' | sort > %t/allowed-dependencies.txt
5+
6+
// Linux/ELF doesn't use the "_" prefix in symbol mangling.
7+
// RUN: if [ %target-os == "linux-gnu" ]; then sed -E -i -e 's/^_(.*)$/\1/' %t/allowed-dependencies.txt; fi
8+
9+
// RUN: %llvm-nm --undefined-only --format=just-symbols %t/a.o | sort | tee %t/actual-dependencies.txt
10+
11+
// Fail if there is any entry in actual-dependencies.txt that's not in allowed-dependencies.txt
12+
// RUN: test -z "`comm -13 %t/allowed-dependencies.txt %t/actual-dependencies.txt`"
13+
14+
// DEP: ___stack_chk_fail
15+
// DEP: ___stack_chk_guard
16+
// DEP: _arc4random_buf
17+
// DEP: _free
18+
// DEP: _memset
19+
// DEP: _putchar
20+
// DEP: _posix_memalign
21+
22+
// RUN: %target-clang -x c -c %S/Inputs/print.c -o %t/print.o
23+
// RUN: %target-clang %t/a.o %t/print.o -o %t/a.out
24+
// RUN: %target-run %t/a.out | %FileCheck %s
25+
26+
// REQUIRES: swift_in_compiler
27+
// REQUIRES: executable_test
28+
// REQUIRES: optimized_stdlib
29+
// REQUIRES: OS=macosx || OS=linux-gnu
30+
// UNSUPPORTED: OS=linux-gnu && CPU=aarch64
31+
32+
@_silgen_name("putchar")
33+
func putchar(_: UInt8)
34+
35+
public func print(_ s: StaticString, terminator: StaticString = "\n") {
36+
var p = s.utf8Start
37+
while p.pointee != 0 {
38+
putchar(p.pointee)
39+
p += 1
40+
}
41+
p = terminator.utf8Start
42+
while p.pointee != 0 {
43+
putchar(p.pointee)
44+
p += 1
45+
}
46+
}
47+
48+
class MyClass {
49+
func foo() { print("MyClass.foo") }
50+
}
51+
52+
class MySubClass: MyClass {
53+
override func foo() { print("MySubClass.foo") }
54+
}
55+
56+
@main
57+
struct Main {
58+
static var objects: [MyClass] = []
59+
static func main() {
60+
print("Hello Embedded Swift!")
61+
// CHECK: Hello Embedded Swift!
62+
objects.append(MyClass())
63+
objects.append(MySubClass())
64+
for o in objects {
65+
o.foo()
66+
}
67+
// CHECK: MyClass.foo
68+
// CHECK: MySubClass.foo
69+
print(Bool.random() ? "you won" : "you lost")
70+
// CHECK: you {{won|lost}}
71+
}
72+
}

test/embedded/stdlib-array.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public func test() {
2121
array.max()
2222
array.partition(by: { $0 > 0 })
2323
array.reduce(0, +)
24-
// array.shuffle()
25-
// array = array.shuffled()
26-
// array.randomElement()
24+
array.shuffle()
25+
array = array.shuffled()
26+
array.randomElement()
2727
}
2828

2929
test()

test/embedded/stdlib-random.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-swift-frontend -target armv7-apple-none-macho -Xcc -D__MACH__ -emit-ir %s -enable-experimental-feature Embedded | %FileCheck %s
2+
// RUN: %target-swift-frontend -target arm64-apple-none-macho -Xcc -D__MACH__ -Xcc -D__arm64__ -Xcc -D__APPLE__ -emit-ir %s -enable-experimental-feature Embedded | %FileCheck %s
3+
4+
// REQUIRES: swift_in_compiler
5+
// REQUIRES: optimized_stdlib
6+
7+
public func test() {
8+
Bool.random()
9+
Int.random(in: 0 ..< 100)
10+
Double.random(in: 0.0 ..< 1.0)
11+
[1, 2, 3].shuffled()
12+
[1, 2, 3].randomElement()
13+
}
14+
15+
test()
16+
17+
// CHECK: define {{.*}}i32 @main(i32 %0, ptr %1)

test/embedded/stdlib-set.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public func test() {
1717
s.min()
1818
s.max()
1919
s.reduce(0, +)
20-
// s.shuffled()
21-
// s.randomElement()
20+
s.shuffled()
21+
s.randomElement()
2222
}
2323

2424
test()

0 commit comments

Comments
 (0)