|
| 1 | +// Implementations of integer operations. These should eventually all |
| 2 | +// be replaced with @_semantics to lower directly to vector IR nodes. |
| 3 | +public extension SIMD where Scalar : FixedWidthInteger { |
| 4 | + @_transparent |
| 5 | + var leadingZeroBitCount: Self { |
| 6 | + var result = Self() |
| 7 | + for i in indices { result[i] = Scalar(self[i].leadingZeroBitCount) } |
| 8 | + return result |
| 9 | + } |
| 10 | + |
| 11 | + @_transparent |
| 12 | + var trailingZeroBitCount: Self { |
| 13 | + var result = Self() |
| 14 | + for i in indices { result[i] = Scalar(self[i].trailingZeroBitCount) } |
| 15 | + return result |
| 16 | + } |
| 17 | + |
| 18 | + @_transparent |
| 19 | + var nonzeroBitCount: Self { |
| 20 | + var result = Self() |
| 21 | + for i in indices { result[i] = Scalar(self[i].nonzeroBitCount) } |
| 22 | + return result |
| 23 | + } |
| 24 | + |
| 25 | + @_transparent |
| 26 | + static prefix func ~(rhs: Self) -> Self { |
| 27 | + var result = Self() |
| 28 | + for i in result.indices { result[i] = ~rhs[i] } |
| 29 | + return result |
| 30 | + } |
| 31 | + |
| 32 | + @_transparent |
| 33 | + static func &(lhs: Self, rhs: Self) -> Self { |
| 34 | + var result = Self() |
| 35 | + for i in result.indices { result[i] = lhs[i] & rhs[i] } |
| 36 | + return result |
| 37 | + } |
| 38 | + |
| 39 | + @_transparent |
| 40 | + static func ^(lhs: Self, rhs: Self) -> Self { |
| 41 | + var result = Self() |
| 42 | + for i in result.indices { result[i] = lhs[i] ^ rhs[i] } |
| 43 | + return result |
| 44 | + } |
| 45 | + |
| 46 | + @_transparent |
| 47 | + static func |(lhs: Self, rhs: Self) -> Self { |
| 48 | + var result = Self() |
| 49 | + for i in result.indices { result[i] = lhs[i] | rhs[i] } |
| 50 | + return result |
| 51 | + } |
| 52 | + |
| 53 | + @_transparent |
| 54 | + static func &<<(lhs: Self, rhs: Self) -> Self { |
| 55 | + var result = Self() |
| 56 | + for i in result.indices { result[i] = lhs[i] &<< rhs[i] } |
| 57 | + return result |
| 58 | + } |
| 59 | + |
| 60 | + @_transparent |
| 61 | + static func &>>(lhs: Self, rhs: Self) -> Self { |
| 62 | + var result = Self() |
| 63 | + for i in result.indices { result[i] = lhs[i] &>> rhs[i] } |
| 64 | + return result |
| 65 | + } |
| 66 | + |
| 67 | + @_transparent |
| 68 | + static func &+(lhs: Self, rhs: Self) -> Self { |
| 69 | + var result = Self() |
| 70 | + for i in result.indices { result[i] = lhs[i] &+ rhs[i] } |
| 71 | + return result |
| 72 | + } |
| 73 | + |
| 74 | + @_transparent |
| 75 | + static func &-(lhs: Self, rhs: Self) -> Self { |
| 76 | + var result = Self() |
| 77 | + for i in result.indices { result[i] = lhs[i] &- rhs[i] } |
| 78 | + return result |
| 79 | + } |
| 80 | + |
| 81 | + @_transparent |
| 82 | + static func &*(lhs: Self, rhs: Self) -> Self { |
| 83 | + var result = Self() |
| 84 | + for i in result.indices { result[i] = lhs[i] &* rhs[i] } |
| 85 | + return result |
| 86 | + } |
| 87 | + |
| 88 | + @_transparent |
| 89 | + static func /(lhs: Self, rhs: Self) -> Self { |
| 90 | + var result = Self() |
| 91 | + for i in result.indices { result[i] = lhs[i] / rhs[i] } |
| 92 | + return result |
| 93 | + } |
| 94 | + |
| 95 | + @_transparent |
| 96 | + static func %(lhs: Self, rhs: Self) -> Self { |
| 97 | + var result = Self() |
| 98 | + for i in result.indices { result[i] = lhs[i] % rhs[i] } |
| 99 | + return result |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Implementations of floating-point operations. These should eventually all |
| 104 | +// be replaced with @_semantics to lower directly to vector IR nodes. |
| 105 | +public extension SIMD where Scalar : FloatingPoint { |
| 106 | + @_transparent |
| 107 | + static func +(lhs: Self, rhs: Self) -> Self { |
| 108 | + var result = Self() |
| 109 | + for i in result.indices { result[i] = lhs[i] + rhs[i] } |
| 110 | + return result |
| 111 | + } |
| 112 | + |
| 113 | + @_transparent |
| 114 | + static func -(lhs: Self, rhs: Self) -> Self { |
| 115 | + var result = Self() |
| 116 | + for i in result.indices { result[i] = lhs[i] - rhs[i] } |
| 117 | + return result |
| 118 | + } |
| 119 | + |
| 120 | + @_transparent |
| 121 | + static func *(lhs: Self, rhs: Self) -> Self { |
| 122 | + var result = Self() |
| 123 | + for i in result.indices { result[i] = lhs[i] * rhs[i] } |
| 124 | + return result |
| 125 | + } |
| 126 | + |
| 127 | + @_transparent |
| 128 | + static func /(lhs: Self, rhs: Self) -> Self { |
| 129 | + var result = Self() |
| 130 | + for i in result.indices { result[i] = lhs[i] / rhs[i] } |
| 131 | + return result |
| 132 | + } |
| 133 | + |
| 134 | + @_transparent |
| 135 | + func addingProduct(_ lhs: Self, _ rhs: Self) -> Self { |
| 136 | + var result = Self() |
| 137 | + for i in result.indices { result[i] = self[i].addingProduct(lhs[i], rhs[i]) } |
| 138 | + return result |
| 139 | + } |
| 140 | + |
| 141 | + @_transparent |
| 142 | + func squareRoot( ) -> Self { |
| 143 | + var result = Self() |
| 144 | + for i in result.indices { result[i] = self[i].squareRoot() } |
| 145 | + return result |
| 146 | + } |
| 147 | + |
| 148 | + @_transparent |
| 149 | + func rounded(_ rule: FloatingPointRoundingRule) -> Self { |
| 150 | + var result = Self() |
| 151 | + for i in result.indices { result[i] = self[i].rounded(rule) } |
| 152 | + return result |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +public extension SIMDMask { |
| 157 | + @_transparent |
| 158 | + static prefix func .!(rhs: SIMDMask) -> SIMDMask { |
| 159 | + return SIMDMask(~rhs._storage) |
| 160 | + } |
| 161 | + |
| 162 | + @_transparent |
| 163 | + static func .&(lhs: SIMDMask, rhs: SIMDMask) -> SIMDMask { |
| 164 | + return SIMDMask(lhs._storage & rhs._storage) |
| 165 | + } |
| 166 | + |
| 167 | + @_transparent |
| 168 | + static func .^(lhs: SIMDMask, rhs: SIMDMask) -> SIMDMask { |
| 169 | + return SIMDMask(lhs._storage ^ rhs._storage) |
| 170 | + } |
| 171 | + |
| 172 | + @_transparent |
| 173 | + static func .|(lhs: SIMDMask, rhs: SIMDMask) -> SIMDMask { |
| 174 | + return SIMDMask(lhs._storage | rhs._storage) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +// These operations should never need @_semantics; they should be trivial |
| 179 | +// wrappers around the core operations defined above. |
| 180 | +public extension SIMD where Scalar : FixedWidthInteger { |
| 181 | + @_transparent static func &(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) & rhs } |
| 182 | + @_transparent static func ^(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) ^ rhs } |
| 183 | + @_transparent static func |(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) | rhs } |
| 184 | + @_transparent static func &<<(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) &<< rhs } |
| 185 | + @_transparent static func &>>(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) &>> rhs } |
| 186 | + @_transparent static func &+(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) &+ rhs } |
| 187 | + @_transparent static func &-(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) &- rhs } |
| 188 | + @_transparent static func &*(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) &* rhs } |
| 189 | + @_transparent static func /(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) / rhs } |
| 190 | + @_transparent static func %(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) % rhs } |
| 191 | + |
| 192 | + @_transparent static func &(lhs: Self, rhs: Scalar) -> Self { return lhs & Self(repeating: rhs) } |
| 193 | + @_transparent static func ^(lhs: Self, rhs: Scalar) -> Self { return lhs ^ Self(repeating: rhs) } |
| 194 | + @_transparent static func |(lhs: Self, rhs: Scalar) -> Self { return lhs | Self(repeating: rhs) } |
| 195 | + @_transparent static func &<<(lhs: Self, rhs: Scalar) -> Self { return lhs &<< Self(repeating: rhs) } |
| 196 | + @_transparent static func &>>(lhs: Self, rhs: Scalar) -> Self { return lhs &>> Self(repeating: rhs) } |
| 197 | + @_transparent static func &+(lhs: Self, rhs: Scalar) -> Self { return lhs &+ Self(repeating: rhs) } |
| 198 | + @_transparent static func &-(lhs: Self, rhs: Scalar) -> Self { return lhs &- Self(repeating: rhs) } |
| 199 | + @_transparent static func &*(lhs: Self, rhs: Scalar) -> Self { return lhs &* Self(repeating: rhs) } |
| 200 | + @_transparent static func /(lhs: Self, rhs: Scalar) -> Self { return lhs / Self(repeating: rhs) } |
| 201 | + @_transparent static func %(lhs: Self, rhs: Scalar) -> Self { return lhs % Self(repeating: rhs) } |
| 202 | + |
| 203 | + @_transparent static func &=(lhs: inout Self, rhs: Self) { lhs = lhs & rhs } |
| 204 | + @_transparent static func ^=(lhs: inout Self, rhs: Self) { lhs = lhs ^ rhs } |
| 205 | + @_transparent static func |=(lhs: inout Self, rhs: Self) { lhs = lhs | rhs } |
| 206 | + @_transparent static func &<<=(lhs: inout Self, rhs: Self) { lhs = lhs &<< rhs } |
| 207 | + @_transparent static func &>>=(lhs: inout Self, rhs: Self) { lhs = lhs &>> rhs } |
| 208 | + @_transparent static func &+=(lhs: inout Self, rhs: Self) { lhs = lhs &+ rhs } |
| 209 | + @_transparent static func &-=(lhs: inout Self, rhs: Self) { lhs = lhs &- rhs } |
| 210 | + @_transparent static func &*=(lhs: inout Self, rhs: Self) { lhs = lhs &* rhs } |
| 211 | + @_transparent static func /=(lhs: inout Self, rhs: Self) { lhs = lhs / rhs } |
| 212 | + @_transparent static func %=(lhs: inout Self, rhs: Self) { lhs = lhs % rhs } |
| 213 | + |
| 214 | + @_transparent static func &=(lhs: inout Self, rhs: Scalar) { lhs = lhs & rhs } |
| 215 | + @_transparent static func ^=(lhs: inout Self, rhs: Scalar) { lhs = lhs ^ rhs } |
| 216 | + @_transparent static func |=(lhs: inout Self, rhs: Scalar) { lhs = lhs | rhs } |
| 217 | + @_transparent static func &<<=(lhs: inout Self, rhs: Scalar) { lhs = lhs &<< rhs } |
| 218 | + @_transparent static func &>>=(lhs: inout Self, rhs: Scalar) { lhs = lhs &>> rhs } |
| 219 | + @_transparent static func &+=(lhs: inout Self, rhs: Scalar) { lhs = lhs &+ rhs } |
| 220 | + @_transparent static func &-=(lhs: inout Self, rhs: Scalar) { lhs = lhs &- rhs } |
| 221 | + @_transparent static func &*=(lhs: inout Self, rhs: Scalar) { lhs = lhs &* rhs } |
| 222 | + @_transparent static func /=(lhs: inout Self, rhs: Scalar) { lhs = lhs / rhs } |
| 223 | + @_transparent static func %=(lhs: inout Self, rhs: Scalar) { lhs = lhs % rhs } |
| 224 | + |
| 225 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&+' instead") |
| 226 | + static func +(lhs: Self, rhs: Self) -> Self { fatalError() } |
| 227 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&-' instead") |
| 228 | + static func -(lhs: Self, rhs: Self) -> Self { fatalError() } |
| 229 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&*' instead") |
| 230 | + static func *(lhs: Self, rhs: Self) -> Self { fatalError() } |
| 231 | + |
| 232 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&+' instead") |
| 233 | + static func +(lhs: Self, rhs: Scalar) -> Self { fatalError() } |
| 234 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&-' instead") |
| 235 | + static func -(lhs: Self, rhs: Scalar) -> Self { fatalError() } |
| 236 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&*' instead") |
| 237 | + static func *(lhs: Self, rhs: Scalar) -> Self { fatalError() } |
| 238 | + |
| 239 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&+' instead") |
| 240 | + static func +(lhs: Scalar, rhs: Self) -> Self { fatalError() } |
| 241 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&-' instead") |
| 242 | + static func -(lhs: Scalar, rhs: Self) -> Self { fatalError() } |
| 243 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&*' instead") |
| 244 | + static func *(lhs: Scalar, rhs: Self) -> Self { fatalError() } |
| 245 | + |
| 246 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&+=' instead") |
| 247 | + static func +=(lhs: inout Self, rhs: Self) { fatalError() } |
| 248 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&-=' instead") |
| 249 | + static func -=(lhs: inout Self, rhs: Self) { fatalError() } |
| 250 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&*=' instead") |
| 251 | + static func *=(lhs: inout Self, rhs: Self) { fatalError() } |
| 252 | + |
| 253 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&+=' instead") |
| 254 | + static func +=(lhs: inout Self, rhs: Scalar) { fatalError() } |
| 255 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&-=' instead") |
| 256 | + static func -=(lhs: inout Self, rhs: Scalar) { fatalError() } |
| 257 | + @available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&*=' instead") |
| 258 | + static func *=(lhs: inout Self, rhs: Scalar) { fatalError() } |
| 259 | +} |
| 260 | + |
| 261 | +public extension SIMD where Scalar : FloatingPoint { |
| 262 | + @_transparent static prefix func -(rhs: Self) -> Self { return 0 - rhs } |
| 263 | + |
| 264 | + @_transparent static func +(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) + rhs } |
| 265 | + @_transparent static func -(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) - rhs } |
| 266 | + @_transparent static func *(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) * rhs } |
| 267 | + @_transparent static func /(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) / rhs } |
| 268 | + |
| 269 | + @_transparent static func +(lhs: Self, rhs: Scalar) -> Self { return lhs + Self(repeating: rhs) } |
| 270 | + @_transparent static func -(lhs: Self, rhs: Scalar) -> Self { return lhs - Self(repeating: rhs) } |
| 271 | + @_transparent static func *(lhs: Self, rhs: Scalar) -> Self { return lhs * Self(repeating: rhs) } |
| 272 | + @_transparent static func /(lhs: Self, rhs: Scalar) -> Self { return lhs / Self(repeating: rhs) } |
| 273 | + |
| 274 | + @_transparent static func +=(lhs: inout Self, rhs: Self) { lhs = lhs + rhs } |
| 275 | + @_transparent static func -=(lhs: inout Self, rhs: Self) { lhs = lhs - rhs } |
| 276 | + @_transparent static func *=(lhs: inout Self, rhs: Self) { lhs = lhs * rhs } |
| 277 | + @_transparent static func /=(lhs: inout Self, rhs: Self) { lhs = lhs / rhs } |
| 278 | + |
| 279 | + @_transparent static func +=(lhs: inout Self, rhs: Scalar) { lhs = lhs + rhs } |
| 280 | + @_transparent static func -=(lhs: inout Self, rhs: Scalar) { lhs = lhs - rhs } |
| 281 | + @_transparent static func *=(lhs: inout Self, rhs: Scalar) { lhs = lhs * rhs } |
| 282 | + @_transparent static func /=(lhs: inout Self, rhs: Scalar) { lhs = lhs / rhs } |
| 283 | + |
| 284 | + @_transparent func addingProduct(_ lhs: Scalar, _ rhs: Self) -> Self { |
| 285 | + return self.addingProduct(Self(repeating: lhs), rhs) |
| 286 | + } |
| 287 | + @_transparent func addingProduct(_ lhs: Self, _ rhs: Scalar) -> Self { |
| 288 | + return self.addingProduct(lhs, Self(repeating: rhs)) |
| 289 | + } |
| 290 | + @_transparent mutating func addProduct(_ lhs: Self, _ rhs: Self) { |
| 291 | + self = self.addingProduct(lhs, rhs) |
| 292 | + } |
| 293 | + @_transparent mutating func addProduct(_ lhs: Scalar, _ rhs: Self) { |
| 294 | + self = self.addingProduct(lhs, rhs) |
| 295 | + } |
| 296 | + @_transparent mutating func addProduct(_ lhs: Self, _ rhs: Scalar) { |
| 297 | + self = self.addingProduct(lhs, rhs) |
| 298 | + } |
| 299 | + |
| 300 | + @_transparent mutating func formSquareRoot( ) { |
| 301 | + self = self.squareRoot() |
| 302 | + } |
| 303 | + |
| 304 | + @_transparent mutating func round(_ rule: FloatingPointRoundingRule) { |
| 305 | + self = self.rounded(rule) |
| 306 | + } |
| 307 | +} |
| 308 | + |
| 309 | +public extension SIMDMask { |
| 310 | + @_transparent static func .&(lhs: Bool, rhs: SIMDMask) -> SIMDMask { return SIMDMask(repeating: lhs) .& rhs } |
| 311 | + @_transparent static func .^(lhs: Bool, rhs: SIMDMask) -> SIMDMask { return SIMDMask(repeating: lhs) .^ rhs } |
| 312 | + @_transparent static func .|(lhs: Bool, rhs: SIMDMask) -> SIMDMask { return SIMDMask(repeating: lhs) .| rhs } |
| 313 | + |
| 314 | + @_transparent static func .&(lhs: SIMDMask, rhs: Bool) -> SIMDMask { return lhs .& SIMDMask(repeating: rhs) } |
| 315 | + @_transparent static func .^(lhs: SIMDMask, rhs: Bool) -> SIMDMask { return lhs .^ SIMDMask(repeating: rhs) } |
| 316 | + @_transparent static func .|(lhs: SIMDMask, rhs: Bool) -> SIMDMask { return lhs .| SIMDMask(repeating: rhs) } |
| 317 | + |
| 318 | + @_transparent static func .&=(lhs: inout SIMDMask, rhs: SIMDMask) { lhs = lhs .& rhs } |
| 319 | + @_transparent static func .^=(lhs: inout SIMDMask, rhs: SIMDMask) { lhs = lhs .^ rhs } |
| 320 | + @_transparent static func .|=(lhs: inout SIMDMask, rhs: SIMDMask) { lhs = lhs .| rhs } |
| 321 | + |
| 322 | + @_transparent static func .&=(lhs: inout SIMDMask, rhs: Bool) { lhs = lhs .& rhs } |
| 323 | + @_transparent static func .^=(lhs: inout SIMDMask, rhs: Bool) { lhs = lhs .^ rhs } |
| 324 | + @_transparent static func .|=(lhs: inout SIMDMask, rhs: Bool) { lhs = lhs .| rhs } |
| 325 | +} |
0 commit comments