Skip to content

Commit 86b372e

Browse files
committed
[Embedded] Enable SIMD types in embedded Swift
1 parent eda8f15 commit 86b372e

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

stdlib/public/core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ split_embedded_sources(
252252
if(SWIFT_STDLIB_ENABLE_VECTOR_TYPES)
253253
list(APPEND SWIFTLIB_SOURCES SIMDVector.swift)
254254
list(APPEND SWIFTLIB_GYB_SOURCES SIMDConcreteOperations.swift.gyb SIMDVectorTypes.swift.gyb)
255+
256+
list(APPEND SWIFTLIB_EMBEDDED_SOURCES SIMDVector.swift)
257+
list(APPEND SWIFTLIB_EMBEDDED_GYB_SOURCES SIMDConcreteOperations.swift.gyb SIMDVectorTypes.swift.gyb)
255258
endif()
256259

257260
list(APPEND SWIFTLIB_EMBEDDED_SOURCES

stdlib/public/core/SIMDVector.swift

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ prefix operator .!
3333
/// conform to `SIMD`.
3434
public protocol SIMDStorage {
3535
/// The type of scalars in the vector space.
36+
#if $Embedded
37+
associatedtype Scalar: Hashable
38+
#else
3639
associatedtype Scalar: Codable, Hashable
37-
40+
#endif
41+
3842
/// The number of scalars, or elements, in the vector.
3943
var scalarCount: Int { get }
4044

@@ -71,6 +75,20 @@ public protocol SIMDScalar {
7175
associatedtype SIMD64Storage: SIMDStorage where SIMD64Storage.Scalar == Self
7276
}
7377

78+
#if $Embedded
79+
/// A SIMD vector of a fixed number of elements.
80+
public protocol SIMD<Scalar>:
81+
SIMDStorage,
82+
Hashable,
83+
ExpressibleByArrayLiteral
84+
{
85+
/// The mask type resulting from pointwise comparisons of this vector type.
86+
associatedtype MaskStorage: SIMD
87+
where MaskStorage.Scalar: FixedWidthInteger & SignedInteger
88+
}
89+
90+
#else
91+
7492
/// A SIMD vector of a fixed number of elements.
7593
public protocol SIMD<Scalar>:
7694
SIMDStorage,
@@ -84,6 +102,8 @@ public protocol SIMD<Scalar>:
84102
where MaskStorage.Scalar: FixedWidthInteger & SignedInteger
85103
}
86104

105+
#endif
106+
87107
extension SIMD {
88108
/// The valid indices for subscripting the vector.
89109
@_transparent
@@ -111,6 +131,8 @@ extension SIMD {
111131
public func hash(into hasher: inout Hasher) {
112132
for i in indices { hasher.combine(self[i]) }
113133
}
134+
135+
#if !$Embedded
114136

115137
/// Encodes the scalars of this vector into the given encoder in an unkeyed
116138
/// container.
@@ -154,7 +176,9 @@ extension SIMD {
154176
return "\(Self.self)(" + indices.map({"\(self[$0])"}).joined(separator: ", ") + ")"
155177
}
156178
}
157-
179+
180+
#endif
181+
158182
/// A vector mask with the result of a pointwise equality comparison.
159183
///
160184
/// Equivalent to:
@@ -539,6 +563,8 @@ extension SIMD where Scalar: FixedWidthInteger {
539563
return Self(repeating: 1)
540564
}
541565

566+
#if !$Embedded
567+
542568
/// Returns a vector with random values from within the specified range in
543569
/// all lanes, using the given generator as a source for randomness.
544570
@inlinable
@@ -560,7 +586,7 @@ extension SIMD where Scalar: FixedWidthInteger {
560586
var g = SystemRandomNumberGenerator()
561587
return Self.random(in: range, using: &g)
562588
}
563-
589+
564590
/// Returns a vector with random values from within the specified range in
565591
/// all lanes, using the given generator as a source for randomness.
566592
@inlinable
@@ -582,6 +608,9 @@ extension SIMD where Scalar: FixedWidthInteger {
582608
var g = SystemRandomNumberGenerator()
583609
return Self.random(in: range, using: &g)
584610
}
611+
612+
#endif
613+
585614
}
586615

587616
extension SIMD where Scalar: FloatingPoint {
@@ -608,6 +637,8 @@ extension SIMD where Scalar: FloatingPoint {
608637
}
609638
}
610639

640+
#if !$Embedded
641+
611642
extension SIMD
612643
where Scalar: BinaryFloatingPoint, Scalar.RawSignificand: FixedWidthInteger {
613644
/// Returns a vector with random values from within the specified range in
@@ -655,6 +686,8 @@ where Scalar: BinaryFloatingPoint, Scalar.RawSignificand: FixedWidthInteger {
655686
}
656687
}
657688

689+
#endif
690+
658691
@frozen
659692
public struct SIMDMask<Storage>: SIMD
660693
where Storage: SIMD,
@@ -695,6 +728,8 @@ public struct SIMDMask<Storage>: SIMD
695728
}
696729
}
697730

731+
#if !$Embedded
732+
698733
extension SIMDMask {
699734
/// Returns a vector mask with `true` or `false` randomly assigned in each
700735
/// lane, using the given generator as a source for randomness.
@@ -714,6 +749,8 @@ extension SIMDMask {
714749
}
715750
}
716751

752+
#endif
753+
717754
// Implementations of integer operations. These should eventually all
718755
// be replaced with @_semantics to lower directly to vector IR nodes.
719756
extension SIMD where Scalar: FixedWidthInteger {

stdlib/public/core/SIMDVectorTypes.swift.gyb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ extension SIMD${n} where Scalar: FixedWidthInteger {
155155
}
156156
}
157157

158+
#if !$Embedded
159+
158160
extension SIMD${n}: CustomDebugStringConvertible {
159161
public var debugDescription: String {
160162
return "SIMD${n}<\(Scalar.self)>(${', '.join(map(lambda c:
@@ -163,6 +165,8 @@ extension SIMD${n}: CustomDebugStringConvertible {
163165
}
164166
}
165167

168+
#endif
169+
166170
extension SIMD${n} where Scalar: BinaryFloatingPoint {
167171
/// Creates a new vector from the given vector of integers.
168172
///

0 commit comments

Comments
 (0)