Files
swift-mirror/stdlib/public/core/SIMDFloatConcreteOperations.swift.gyb
Stephen Canon 592d72bba9 Concrete SIMD.init(repeating:) and SIMD.init(lowHalf:highHalf:) optimizations (#81766)
WIP to add more overloads to optimize SIMD codegen on concrete types.
Here we do:

- init(repeating:)
- init(lowHalf:highHalf:)

These are always inlined, even in debug, since LLVM knows how to lower
them to one or two instructions on the targets that we care about.
2025-05-27 15:15:13 -04:00

121 lines
4.0 KiB
Swift

//===--- SIMDFloatConcreteOperations.swift --------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021-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
//
//===----------------------------------------------------------------------===//
%{
from SwiftIntTypes import all_integer_types
word_bits = int(CMAKE_SIZEOF_VOID_P) * 8
storagescalarCounts = [2,4,8,16,32,64]
vectorscalarCounts = storagescalarCounts + [3]
}%
%for (Scalar, bits) in [('Float16',16), ('Float',32), ('Double',64)]:
% for n in vectorscalarCounts:
% Vector = "SIMD" + str(n) + "<" + Scalar + ">"
% storageN = 4 if n == 3 else n
% Builtin = "Vec" + str(storageN) + "xFPIEEE" + str(bits)
% VecPre = "Vec" + str(storageN) + "x"
% MaskExt = "Builtin.sext_" + VecPre + "Int1_" + VecPre + "Int" + str(bits)
% if bits == 16:
#if !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
@available(SwiftStdlib 5.3, *)
% end
extension SIMD${n} where Scalar == ${Scalar} {
@_alwaysEmitIntoClient @_transparent
internal init(_ _builtin: Builtin.${Builtin}) {
_storage = ${Scalar}.SIMD${storageN}Storage(_builtin)
}
/* Breaks differentiation testing, commented out while we figure out
what to do about that.
@_alwaysEmitIntoClient @_transparent
public init(repeating scalar: ${Scalar}) {
let asVector = Builtin.insertelement_${Builtin}_FPIEEE${bits}_Int32(
Builtin.zeroInitializer(), scalar._value, Builtin.zeroInitializer()
)
let repeated = Builtin.shufflevector_${Builtin}_Vec${storageN}xInt32(
asVector, Builtin.zeroInitializer(), Builtin.zeroInitializer()
)
%if n != 3:
self.init(repeated)
%else:
self.init(Builtin.insertelement_${Builtin}_FPIEEE${bits}_Int32(
repeated, Builtin.zeroInitializer(), Int32(3)._value
))
%end
}
*/
% if n >= 4:
@_alwaysEmitIntoClient @_transparent
public init(
lowHalf: SIMD${n//2}<${Scalar}>,
highHalf: SIMD${n//2}<${Scalar}>
) {
self = unsafe unsafeBitCast((lowHalf, highHalf), to: Self.self)
}
% 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> {
SIMDMask<MaskStorage>(${MaskExt}(
Builtin.fcmp_ole_${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)
))
}
/// 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)
))
}
}
% if bits == 16:
#endif
% end
% end
%end