Please note the inconsistency of single element simd vector being Hashable while the rest is not, eg:
simd_uint1 <-- Is Hashable (via UInt32) while
simd_uint2 <-- Is not Hashable
simd_uint3 <-- Is not Hashable
simd_uint4 <-- Is not Hashable
The following way to make all simd vector types - and not only the single element ones - conform to Hashable seems straight forward IMHO, please let me know if/why the stdlib shouldn't do something similar:
extension simd_uint2 : Hashable {
public var hashValue: Int { return unsafeBitCast(self, to: UInt64.self).hashValue }
}
extension simd_uint3 : Hashable {
public var hashValue: Int { return unsafeBitCast(self, to: DoubleWidth<UInt64>.self).hashValue }
}
extension simd_uint4 : Hashable {
public var hashValue: Int { return unsafeBitCast(self, to: DoubleWidth<UInt64>.self).hashValue }
}
extension simd_float2 : Hashable {
public var hashValue: Int { return unsafeBitCast(self, to: UInt64.self).hashValue }
}
extension simd_float3 : Hashable {
public var hashValue: Int { return unsafeBitCast(self, to: DoubleWidth<UInt64>.self).hashValue }
}
extension simd_float4 : Hashable {
public var hashValue: Int { return unsafeBitCast(self, to: DoubleWidth<UInt64>.self).hashValue }
}
extension simd_double2 : Hashable {
public var hashValue: Int { return unsafeBitCast(self, to: DoubleWidth<UInt64>.self).hashValue }
}
extension simd_double3 : Hashable {
public var hashValue: Int { return unsafeBitCast(self, to: DoubleWidth<DoubleWidth<UInt64>>.self).hashValue }
}
extension simd_double4 : Hashable {
public var hashValue: Int { return unsafeBitCast(self, to: DoubleWidth<DoubleWidth<UInt64>>.self).hashValue }
}
This code compiles only with recent developer snapshots (as eg Xcode 9.2 default toolchain doesn't have DoubleWidth).
EDIT:
I later realized that the above will be incorrect because for Floats, 0.0 == -0.0, so the hashValue for -0.0 and 0.0 need to be the same. Ie for any Float f, f.hashValue == Int(truncatingIfNeeded: f.bitPattern) except for f = -0.0.
Correct, the simd types are not part of the stdlib and thus I was wrong when I wrote "stdlib" above, I meant it as a question towards Apple, and perhaps the Swift community, regarding the design and philosophy of support for the SIMD vector types, as can be seen in eg: https://github.com/apple/swift/blob/master/stdlib/public/SDK/simd/simd.swift.gyb