|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Atomics open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +% from SwiftAtomics import * |
| 14 | + |
| 15 | +import Builtin |
| 16 | + |
| 17 | +% for (intType, intStorage, builtinInt) in intTypes: |
| 18 | + |
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | +// ${intType} AtomicValue conformance |
| 21 | +//===----------------------------------------------------------------------===// |
| 22 | + |
| 23 | +// FIXME: Conditionalize the conformance of {U}Int64 on 32 bit platforms that do |
| 24 | +// not support double word atomics. |
| 25 | + |
| 26 | +@available(SwiftStdlib 5.10, *) |
| 27 | +extension ${intType}: AtomicValue { |
| 28 | +% if intType == "Int" or intType == "UInt": |
| 29 | +#if _pointerBitWidth(_64) |
| 30 | + public typealias AtomicRepresentation = AtomicInt64Storage |
| 31 | +#elseif _pointerBitWidth(_32) |
| 32 | + public typealias AtomicRepresentation = AtomicInt32Storage |
| 33 | +#else |
| 34 | +#error("Unsupported platform") |
| 35 | +#endif |
| 36 | +% else: |
| 37 | + public typealias AtomicRepresentation = ${intStorage} |
| 38 | +% end |
| 39 | + |
| 40 | + @available(SwiftStdlib 5.10, *) |
| 41 | + @_alwaysEmitIntoClient |
| 42 | + @_transparent |
| 43 | + public static func encodeAtomicRepresentation( |
| 44 | + _ value: borrowing ${intType} |
| 45 | + ) -> AtomicRepresentation { |
| 46 | + AtomicRepresentation(value._value) |
| 47 | + } |
| 48 | + |
| 49 | + @available(SwiftStdlib 5.10, *) |
| 50 | + @_alwaysEmitIntoClient |
| 51 | + @_transparent |
| 52 | + public static func decodeAtomicRepresentation( |
| 53 | + _ representation: consuming AtomicRepresentation |
| 54 | + ) -> ${intType} { |
| 55 | + ${intType}(representation.storage) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +//===----------------------------------------------------------------------===// |
| 60 | +// ${intType} load then atomic operations |
| 61 | +//===----------------------------------------------------------------------===// |
| 62 | + |
| 63 | +@available(SwiftStdlib 5.10, *) |
| 64 | +extension Atomic where Value == ${intType} { |
| 65 | +% for (_, apiOrder, _, llvmOrder, failureOrder) in updateOrderings: |
| 66 | +% for (name, builtinName, op, label, doc) in integerOperations: |
| 67 | + /// Perform an atomic ${doc} operation and return the original value, applying |
| 68 | + /// the specified memory ordering. |
| 69 | + /// |
| 70 | +% if "Wrapping" in name: |
| 71 | + /// Note: This operation silently wraps around on overflow, like the |
| 72 | + /// `${op}` operator does on `${intType}` values. |
| 73 | + /// |
| 74 | +% end |
| 75 | + /// - Parameter operand: An integer value. |
| 76 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 77 | + /// - Returns: The original value before the operation. |
| 78 | + @available(SwiftStdlib 5.10, *) |
| 79 | + @_alwaysEmitIntoClient |
| 80 | + @_transparent |
| 81 | + public func loadThen${name}( |
| 82 | + ${label} operand: ${intType}${" = 1" if "crement" in name else ""}, |
| 83 | + ordering: Atomic${apiOrder} |
| 84 | + ) -> ${intType} { |
| 85 | +% if intType == "Int" or intType == "UInt": |
| 86 | +#if _pointerBitWidth(_64) |
| 87 | + let result = Builtin.atomicrmw_${atomicOperationName(intType, builtinName)}_${llvmOrder}_Int64( |
| 88 | + rawAddress, |
| 89 | + operand._value |
| 90 | + ) |
| 91 | +#elseif _pointerBitWidth(_32) |
| 92 | + let result = Builtin.atomicrmw_${atomicOperationName(intType, builtinName)}_${llvmOrder}_Int32( |
| 93 | + rawAddress, |
| 94 | + operand._value |
| 95 | + ) |
| 96 | +#else |
| 97 | +#error("Unsupported platform") |
| 98 | +#endif |
| 99 | +% else: |
| 100 | + let result = Builtin.atomicrmw_${atomicOperationName(intType, builtinName)}_${llvmOrder}_${builtinInt}( |
| 101 | + rawAddress, |
| 102 | + operand._value |
| 103 | + ) |
| 104 | +% end |
| 105 | + |
| 106 | + return ${intType}(result) |
| 107 | + } |
| 108 | +% end |
| 109 | + |
| 110 | + /// Perform an atomic wrapping increment operation applying the |
| 111 | + /// specified memory ordering. |
| 112 | + /// |
| 113 | + /// Note: This operation silently wraps around on overflow, like the |
| 114 | + /// `&+=` operator does on `${intType}` values. |
| 115 | + /// |
| 116 | + /// - Parameter operand: The value to add to the current value. |
| 117 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 118 | + @available(SwiftStdlib 5.10, *) |
| 119 | + @_alwaysEmitIntoClient |
| 120 | + @_transparent |
| 121 | + public func wrappingIncrement( |
| 122 | + by operand: ${intType} = 1, |
| 123 | + ordering: Atomic${apiOrder} |
| 124 | + ) { |
| 125 | + _ = loadThenWrappingIncrement(by: operand, ordering: ordering) |
| 126 | + } |
| 127 | + |
| 128 | + /// Perform an atomic wrapping decrement operation applying the |
| 129 | + /// specified memory ordering. |
| 130 | + /// |
| 131 | + /// Note: This operation silently wraps around on overflow, like the |
| 132 | + /// `&-=` operator does on `${intType}` values. |
| 133 | + /// |
| 134 | + /// - Parameter operand: The value to subtract from the current value. |
| 135 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 136 | + @available(SwiftStdlib 5.10, *) |
| 137 | + @_alwaysEmitIntoClient |
| 138 | + @_transparent |
| 139 | + public func wrappingDecrement( |
| 140 | + by operand: ${intType} = 1, |
| 141 | + ordering: Atomic${apiOrder} |
| 142 | + ) { |
| 143 | + _ = loadThenWrappingDecrement(by: operand, ordering: ordering) |
| 144 | + } |
| 145 | +% end |
| 146 | +} |
| 147 | + |
| 148 | +//===----------------------------------------------------------------------===// |
| 149 | +// ${intType} atomic operation then loads |
| 150 | +//===----------------------------------------------------------------------===// |
| 151 | + |
| 152 | +@available(SwiftStdlib 5.10, *) |
| 153 | +extension Atomic where Value == ${intType} { |
| 154 | +% for (name, builtinName, op, label, doc) in integerOperations: |
| 155 | +% for (_, apiOrder, _, llvmOrder, _) in updateOrderings: |
| 156 | + /// Perform an atomic ${doc} operation and return the new value, applying |
| 157 | + /// the specified memory ordering. |
| 158 | + /// |
| 159 | +% if "Wrapping" in name: |
| 160 | + /// Note: This operation silently wraps around on overflow, like the |
| 161 | + /// `${op}` operator does on `${intType}` values. |
| 162 | + /// |
| 163 | +% end |
| 164 | + /// - Parameter operand: An integer value. |
| 165 | + /// - Parameter ordering: The memory ordering to apply on this operation. |
| 166 | + /// - Returns: The new value after the operation. |
| 167 | + @available(SwiftStdlib 5.10, *) |
| 168 | + @_alwaysEmitIntoClient |
| 169 | + @_transparent |
| 170 | + public func ${lowerFirst(name)}ThenLoad( |
| 171 | + ${label} operand: Value${" = 1" if "crement" in name else ""}, |
| 172 | + ordering: Atomic${apiOrder} |
| 173 | + ) -> ${intType} { |
| 174 | +% if intType == "Int" or intType == "UInt": |
| 175 | +#if _pointerBitWidth(_64) |
| 176 | + let original = Builtin.atomicrmw_${atomicOperationName(intType, builtinName)}_${llvmOrder}_Int64( |
| 177 | + rawAddress, |
| 178 | + operand._value |
| 179 | + ) |
| 180 | +#elseif _pointerBitWidth(_32) |
| 181 | + let original = Builtin.atomicrmw_${atomicOperationName(intType, builtinName)}_${llvmOrder}_Int32( |
| 182 | + rawAddress, |
| 183 | + operand._value |
| 184 | + ) |
| 185 | +#else |
| 186 | +#error("Unsupported platform") |
| 187 | +#endif |
| 188 | +% else: |
| 189 | + let original = Builtin.atomicrmw_${atomicOperationName(intType, builtinName)}_${llvmOrder}_${builtinInt}( |
| 190 | + rawAddress, |
| 191 | + operand._value |
| 192 | + ) |
| 193 | +% end |
| 194 | + |
| 195 | +% if name == "Min": |
| 196 | + return Swift.min(${intType}(original), operand) |
| 197 | +% elif name == "Max": |
| 198 | + return Swift.max(${intType}(original), operand) |
| 199 | +% else: |
| 200 | + return ${intType}(original) ${op} operand |
| 201 | +% end |
| 202 | + } |
| 203 | +% end |
| 204 | +% end |
| 205 | +} |
| 206 | + |
| 207 | +% end |
0 commit comments