Skip to content

Simd float concrete comparisons #81892

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
119 changes: 78 additions & 41 deletions stdlib/public/core/SIMDFloatConcreteOperations.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ extension SIMD${n} where Scalar == ${Scalar} {
_storage = ${Scalar}.SIMD${storageN}Storage(_builtin)
}

/// A vector with the specified scalar in all lanes.
///
/// Equivalent to:
/// ```
/// var result = SIMD${n}<${Scalar}>()
/// for i in result.indices {
/// result[i] = scalar
/// }
/// ```
@_alwaysEmitIntoClient @_transparent
public init(repeating scalar: ${Scalar}) {
let asVector = Builtin.insertelement_${Builtin}_FPIEEE${bits}_Int32(
Expand All @@ -52,6 +61,16 @@ extension SIMD${n} where Scalar == ${Scalar} {
}

% if n >= 4:
/// A vector formed by concatenating lowHalf and highHalf.
///
/// Equivalent to:
/// ```
/// var result = SIMD${n}<${Scalar}>()
/// for i in 0..<${n//2} {
/// result[i] = lowHalf[i]
/// result[${n//2}+i] = highHalf[i]
/// }
/// ```
@_alwaysEmitIntoClient @_transparent
public init(
lowHalf: SIMD${n//2}<${Scalar}>,
Expand All @@ -61,53 +80,71 @@ extension SIMD${n} where Scalar == ${Scalar} {
}

% end
/// A vector mask with the result of a pointwise equality comparison.
@_alwaysEmitIntoClient
public static func .==(a: Self, b: Self) -> SIMDMask<MaskStorage> {
SIMDMask<MaskStorage>(${MaskExt}(
Builtin.fcmp_oeq_${Builtin}(a._storage._value, b._storage._value)
))
}

/// A vector mask with the result of a pointwise inequality comparison.
@_alwaysEmitIntoClient
public static func .!=(a: Self, b: Self) -> SIMDMask<MaskStorage> {
SIMDMask<MaskStorage>(${MaskExt}(
Builtin.fcmp_une_${Builtin}(a._storage._value, b._storage._value)
))
}

/// A vector mask with the result of a pointwise less-than comparison.
@_alwaysEmitIntoClient
public static func .<(a: Self, b: Self) -> SIMDMask<MaskStorage> {
SIMDMask<MaskStorage>(${MaskExt}(
Builtin.fcmp_olt_${Builtin}(a._storage._value, b._storage._value)
))
}

/// A vector mask with the result of a pointwise less-than-or-equal-to comparison.
@_alwaysEmitIntoClient
public static func .<=(a: Self, b: Self) -> SIMDMask<MaskStorage> {
%{
compares = [
("==", "oeq", "equal to"),
("!=", "une", "not equal to"),
("<", "olt", "less than"),
("<=", "ole", "less than or equal to"),
(">=", "oge", "greater than or equal to"),
(">", "ogt", "greater than")
]
}%
% for (op, bi, description) in compares:
/// Pointwise compare ${description}.
///
/// Each lane of the result is true if that lane of a is ${description} the
/// corresponding lane of b, and false otherwise.
///
/// Equivalent to:
/// ```
/// var result = SIMDMask<MaskStorage>()
/// for i in 0..<${n} {
/// result[i] = (a[i] ${op} b[i])
/// }
/// ```
@_alwaysEmitIntoClient @_transparent
public static func .${op}(a: Self, b: Self) -> SIMDMask<MaskStorage> {
SIMDMask<MaskStorage>(${MaskExt}(
Builtin.fcmp_ole_${Builtin}(a._storage._value, b._storage._value)
Builtin.fcmp_${bi}_${Builtin}(a._storage._value, b._storage._value)
))
}

/// A vector mask with the result of a pointwise greater-than comparison.
@_alwaysEmitIntoClient
public static func .>(a: Self, b: Self) -> SIMDMask<MaskStorage> {
SIMDMask<MaskStorage>(${MaskExt}(
Builtin.fcmp_ogt_${Builtin}(a._storage._value, b._storage._value)
))
/// Pointwise compare ${description}.
///
/// Each lane of the result is true if that lane of a is ${description} b,
/// and false otherwise.
///
/// Equivalent to:
/// ```
/// var result = SIMDMask<MaskStorage>()
/// for i in 0..<${n} {
/// result[i] = (a[i] ${op} b)
/// }
/// ```
@_alwaysEmitIntoClient @_transparent
public static func .${op}(a: Self, b: Scalar) -> SIMDMask<MaskStorage> {
a .${op} Self(repeating: b)
}

/// A vector mask with the result of a pointwise greater-than-or-equal-to comparison.
@_alwaysEmitIntoClient
public static func .>=(a: Self, b: Self) -> SIMDMask<MaskStorage> {
SIMDMask<MaskStorage>(${MaskExt}(
Builtin.fcmp_oge_${Builtin}(a._storage._value, b._storage._value)
))

/// Pointwise compare ${description}.
///
/// Each lane of the result is true if a is ${description} the corresponding
/// lane of b, and false otherwise.
///
/// Equivalent to:
/// ```
/// var result = SIMDMask<MaskStorage>()
/// for i in 0..<${n} {
/// result[i] = (a ${op} b[i])
/// }
/// ```
@_alwaysEmitIntoClient @_transparent
public static func .${op}(a: Scalar, b: Self) -> SIMDMask<MaskStorage> {
Self(repeating: a) .${op} b
}

% end
}
% if bits == 16:
#endif
Expand Down
19 changes: 19 additions & 0 deletions stdlib/public/core/SIMDIntegerConcreteOperations.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ extension SIMD${n} where Scalar == ${Scalar} {
_storage = ${Scalar}.SIMD${storageN}Storage(_builtin)
}

/// A vector with the specified scalar in all lanes.
///
/// Equivalent to:
/// ```
/// var result = SIMD${n}<${Scalar}>()
/// for i in result.indices {
/// result[i] = scalar
/// }
/// ```
@_alwaysEmitIntoClient @_transparent
public init(repeating scalar: ${Scalar}) {
let asVector = Builtin.insertelement_${Builtin}_Int${int.bits}_Int32(
Expand All @@ -49,6 +58,16 @@ extension SIMD${n} where Scalar == ${Scalar} {
}

% if n >= 4:
/// A vector formed by concatenating lowHalf and highHalf.
///
/// Equivalent to:
/// ```
/// var result = SIMD${n}<${Scalar}>()
/// for i in 0..<${n//2} {
/// result[i] = lowHalf[i]
/// result[${n//2}+i] = highHalf[i]
/// }
/// ```
@_alwaysEmitIntoClient @_transparent
public init(
lowHalf: SIMD${n//2}<${Scalar}>,
Expand Down
13 changes: 0 additions & 13 deletions stdlib/public/core/SIMDMaskConcreteOperations.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ extension SIMDMask where Storage == ${Vector} {
_storage = ${Vector}(repeating: scalar ? -1 : 0)
}

% if n >= 4:
@_alwaysEmitIntoClient @_transparent
public init(
lowHalf: SIMDMask<SIMD${n//2}<${Scalar}>>,
highHalf: SIMDMask<SIMD${n//2}<${Scalar}>>
) {
_storage = ${Vector}(
lowHalf: lowHalf._storage,
highHalf: highHalf._storage
)
}

% end
@_alwaysEmitIntoClient
internal static var allTrue: Self {
let zero = ${Vector}()
Expand Down
10 changes: 9 additions & 1 deletion stdlib/public/core/SIMDVector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ extension SIMD {
return 0 ..< scalarCount
}

/// A vector with the specified value in all lanes.
/// A vector with the specified scalar in all lanes.
///
/// Equivalent to:
/// ```
/// var result = Self()
/// for i in result.indices {
/// result[i] = scalar
/// }
/// ```
@_transparent
public init(repeating value: Scalar) {
self.init()
Expand Down
11 changes: 10 additions & 1 deletion stdlib/public/core/SIMDVectorTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,16 @@ public struct SIMD${n}<Scalar>: SIMD where Scalar: SIMDScalar {
% end
% end
% if n >= 4:
/// Creates a new vector from two half-length vectors.
/// A vector formed by concatenating lowHalf and highHalf.
///
/// Equivalent to:
/// ```
/// var result = SIMD${n}<Scalar>()
/// for i in 0..<${n//2} {
/// result[i] = lowHalf[i]
/// result[${n//2}+i] = highHalf[i]
/// }
/// ```
@_transparent
public init(lowHalf: SIMD${n//2}<Scalar>, highHalf: SIMD${n//2}<Scalar>) {
self.init()
Expand Down
161 changes: 161 additions & 0 deletions test/stdlib/SIMDFloatComparisons.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
//===--- SIMDFloatComparisons.swift.gyb -----------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %empty-directory(%t)
// RUN: %gyb %s -o %t/SIMDFloatComparisons.swift
// RUN: %target-swift-frontend -primary-file %t/SIMDFloatComparisons.swift -S | %FileCheck %t/SIMDFloatComparisons.swift --check-prefix=CHECK --check-prefix=CHECK-%target-cpu --check-prefix=CHECKOnone-%target-cpu
// RUN: %target-swift-frontend -primary-file %t/SIMDFloatComparisons.swift -S -O | %FileCheck %t/SIMDFloatComparisons.swift --check-prefix=CHECK --check-prefix=CHECK-%target-cpu --check-prefix=CHECKO-%target-cpu

import Swift

%for bits in [16,32,64]:
% scalar = {16:'Float16',32:'Float',64:'Double'}[bits]
% for totalBits in [64,128]:
% n = totalBits // bits
% if n != 1:
% neonSuffix = str(n) + {8:'b',16:'h',32:'s',64:'d'}[bits]
% if bits == 16:
#if arch(arm64)
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
% end
func compare_eq${n}x${bits}(
_ a: SIMD${n}<${scalar}>, _ b: SIMD${n}<${scalar}>
) -> SIMDMask<SIMD${n}<Int${bits}>> {
a .== b
}
% if bits == 16:
#endif
// CHECK-arm64: compare_eq${n}x${bits}{{[[:alnum:]_]+}}:
% else:
// CHECK: compare_eq${n}x${bits}{{[[:alnum:]_]+}}:
// CHECK-x86_64: cmpeqp${'s' if bits == 32 else 'd'}
// CHECK-x86_64: ret
% end
// CHECKO-arm64-NEXT: fcmeq.${neonSuffix} v0, v0, v1
// CHECKO-arm64-NEXT: ret
// CHECKOnone-arm64: fcmeq.${neonSuffix}
// CHECKOnone-arm64: ret

% if bits == 16:
#if arch(arm64)
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
% end
func compare_ne${n}x${bits}(
_ a: SIMD${n}<${scalar}>, _ b: SIMD${n}<${scalar}>
) -> SIMDMask<SIMD${n}<Int${bits}>> {
a .!= b
}
% if bits == 16:
#endif
// CHECK-arm64: compare_ne${n}x${bits}{{[[:alnum:]_]+}}:
% else:
// CHECK: compare_ne${n}x${bits}{{[[:alnum:]_]+}}:
// CHECK-x86_64: cmpneqp${'s' if bits == 32 else 'd'}
// CHECK-x86_64: ret
% end
// CHECKO-arm64-NEXT: fcmeq.${neonSuffix} [[TMP:v[0-9]+]], v0, v1
// CHECKO-arm64-NEXT: mvn.${totalBits//8}b v0, [[TMP]]
// CHECKO-arm64-NEXT: ret
// CHECKOnone-arm64: fcmeq.${neonSuffix}
// CHECKOnone-arm64: mvn.${totalBits//8}b
// CHECKOnone-arm64: ret

% if bits == 16:
#if arch(arm64)
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
% end
func compare_lt${n}x${bits}(
_ a: SIMD${n}<${scalar}>, _ b: SIMD${n}<${scalar}>
) -> SIMDMask<SIMD${n}<Int${bits}>> {
a .< b
}
% if bits == 16:
#endif
// CHECK-arm64: compare_lt${n}x${bits}{{[[:alnum:]_]+}}:
% else:
// CHECK: compare_lt${n}x${bits}{{[[:alnum:]_]+}}:
// CHECK-x86_64: cmpltp${'s' if bits == 32 else 'd'}
// CHECK-x86_64: ret
% end
// CHECKO-arm64-NEXT: fcmgt.${neonSuffix} v0, v1, v0
// CHECKO-arm64-NEXT: ret
// CHECKOnone-arm64: fcmgt.${neonSuffix}
// CHECKOnone-arm64: ret

% if bits == 16:
#if arch(arm64)
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
% end
func compare_le${n}x${bits}(
_ a: SIMD${n}<${scalar}>, _ b: SIMD${n}<${scalar}>
) -> SIMDMask<SIMD${n}<Int${bits}>> {
a .<= b
}
% if bits == 16:
#endif
// CHECK-arm64: compare_le${n}x${bits}{{[[:alnum:]_]+}}:
% else:
// CHECK: compare_le${n}x${bits}{{[[:alnum:]_]+}}:
// CHECK-x86_64: cmplep${'s' if bits == 32 else 'd'}
// CHECK-x86_64: ret
% end
// CHECKO-arm64-NEXT: fcmge.${neonSuffix} v0, v1, v0
// CHECKO-arm64-NEXT: ret
// CHECKOnone-arm64: fcmge.${neonSuffix}
// CHECKOnone-arm64: ret

% if bits == 16:
#if arch(arm64)
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
% end
func compare_ge${n}x${bits}(
_ a: SIMD${n}<${scalar}>, _ b: SIMD${n}<${scalar}>
) -> SIMDMask<SIMD${n}<Int${bits}>> {
a .>= b
}
% if bits == 16:
#endif
// CHECK-arm64: compare_ge${n}x${bits}{{[[:alnum:]_]+}}:
% else:
// CHECK: compare_ge${n}x${bits}{{[[:alnum:]_]+}}:
// CHECK-x86_64: cmplep${'s' if bits == 32 else 'd'}
// CHECK-x86_64: ret
% end
// CHECKO-arm64-NEXT: fcmge.${neonSuffix} v0, v0, v1
// CHECKO-arm64-NEXT: ret
// CHECKOnone-arm64: fcmge.${neonSuffix}
// CHECKOnone-arm64: ret

% if bits == 16:
#if arch(arm64)
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
% end
func compare_gt${n}x${bits}(
_ a: SIMD${n}<${scalar}>, _ b: SIMD${n}<${scalar}>
) -> SIMDMask<SIMD${n}<Int${bits}>> {
a .> b
}
% if bits == 16:
#endif
// CHECK-arm64: compare_gt${n}x${bits}{{[[:alnum:]_]+}}:
% else:
// CHECK: compare_gt${n}x${bits}{{[[:alnum:]_]+}}:
// CHECK-x86_64: cmpltp${'s' if bits == 32 else 'd'}
// CHECK-x86_64: ret
% end
// CHECKO-arm64-NEXT: fcmgt.${neonSuffix} v0, v0, v1
// CHECKO-arm64-NEXT: ret
// CHECKOnone-arm64: fcmgt.${neonSuffix}
// CHECKOnone-arm64: ret

% end
% end
%end
Loading