swiftoperator-keyword

How does the operator "Pointwise Logical Not"(.!) work in Swift?


I learn Swift and use Swift operators (https://developer.apple.com/documentation/swift/operator-declarations) and saw the operator "Pointwise Logical Not"(.!). I started Googling and came across a question on stackoverflow(What is functionality of Pointwise Equal, Pointwise less than, and Pointwise greater in Swift? ), they told me about the SIMD protocol and gave a link to the documentation, but I did not find an operator among the operators of this protocol .! . Could you help me with an example with this operator. Thank you in advance

main.swift:131:9: error: type 'Bool' does not conform to protocol 'SIMDScalar' let z = SIMD3(true, false, true)

main.swift:132:10: error: generic parameter 'Storage' could not be inferred var yy = .!z

main.swift:131:9: error: type 'Bool' does not conform to protocol 'SIMDScalar' let z = SIMD3(true, false, true)

main.swift:132:10: error: generic parameter 'Storage' could not be inferred var yy = .!z


Solution

  • I think your issue comes from trying to make SIMD values filled with booleans. SIMD types only ever store ints/floats/doubles. To store booleans, there's a dedicated type called SIMDMask, which for example, is the result of operators like .==, .<, etc.

    .! negates every boolean member of a SIMD mask, e.g.

    import simd
    
    let a: SIMD2<Int> = [10, 20]
    let b: SIMD2<Int> = [ 0, 30]
    let exampleMask: SIMDMask<SIMD2<Int>> = a .< b
    let invertedMask = .!exampleMask
    
    print( exampleMask) // => [false,  true]
    print(invertedMask) // => [ true, false]
    

    The result of a .< b is the same as .!(a .>= b), in the same way that for single scalar values, !(a < b) is the same as !(a >= b).