While reading the Swift Programming Language book by Apple, I came across Pointwise equal, Pointwise less than and Pointwise greater than operators. Reference: https://developer.apple.com/documentation/swift/swift_standard_library/operator_declarations
.== Pointwise equal
.!= Pointwise not equal
I couldn't find any explanation and example on when to use these. What is the functionality of these operators?
The operators are part of protocol SIMD
, the protocol to which the “SIMD vector types” conform.
They were introduced in Swift 5 with SE-0229. From that proposal:
Let's discuss Masks. SIMDs are Equatable, so they have the == and != operators, but they also provide the "pointwise comparison"
.==
and.!=
operators, which compare the lanes of two vectors, and produce a Mask, which is a vector of boolean values. Each lane of the mask is eithertrue
orfalse
, depending on the result of comparing the values in the corresponding lanes. An example:(swift) let x = SIMD4<Int>(1,2,3,4) // x : SIMD4<Int> = SIMD4<Int>(1, 2, 3, 4) (swift) let y = SIMD4<Int>(3,2,1,0) // y : SIMD4<Int> = SIMD4<Int>(3, 2, 1, 0) (swift) x .== y // r0 : SIMDMask<SIMD4<Int.SIMDMaskScalar>> = SIMDMask<SIMD4<Int>>(false, true, false, false)
here, the second lane is
true
, because2 == 2
, while all other lanes are false because the elements of x and y in those lanes are not equal.
Extract from protocol SIMD
:
A SIMD vector of a fixed number of elements.
static func .== (Self, Self) -> SIMDMask<Self.MaskStorage> // Returns a vector mask with the result of a pointwise equality comparison. static func .> (Self, Self) -> SIMDMask<Self.MaskStorage> // Returns a vector mask with the result of a pointwise greater than comparison.
Another example:
import simd
let x = SIMD3<Float>(1.0, 2.0, 3.0)
let y = SIMD3<Float>(3.0, 2.0, 1.0)
print(x .== y) // SIMDMask<SIMD3<Int32>>(false, true, false)
print(x .!= y) // SIMDMask<SIMD3<Int32>>(true, false, true)
print(x .< y) // SIMDMask<SIMD3<Int32>>(true, false, false)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With